/*
    Copyright 2000 (c) by Boris Lutz,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.
 
    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.
 
    For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
    This software is provided under the terms of the GNU General Public 
    Licence. A full copy of the GNU GPL is provided in the file COPYING 
    found in the development root of Topsy.
*/
/*
	
    File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/Net/NetBootP.h,v $
    Affiliation:           ETH Zuerich, TIK
    Version:               $Revision: 2922 $
    Last Date of Change:   $Date: 2007-03-19 17:48:52 +0100 (Mon, 19 Mär 2007) $  by: $Author: baderma $
 
*/


#ifndef _NET_BOOTP_H_
#define _NET_BOOTP_H_


#include "NetHAL.h"
#include "Topsy.h"

// BootP types
#define NETBOOTP_OPCODE_REQUEST	1
#define NETBOOTP_OPCODE_REPLY	2
#define NETBOOTP_HTYPE_ENET		1
#define NETBOOTP_HLEN_ENET		6

// State machine
#define STATE_STARTED					0
#define STATE_FIRST_REQUEST_SENT		1
#define STATE_FIRST_ANSWER_RECEIVED		2
#define STATE_VERIFY_COMPLETE			3
#define STATE_SECOND_REQUEST_SENT		4
#define STATE_SECOND_ANSWER_RECEIVED	5

// Return values
#define RETVAL_OK				0
#define RETVAL_FAILED			1

// Loops & timeouts
#define MAX_LOOP				2
#ifdef ia32
#define SELECT_TIMEOUT			1000
#else /* this is for the slow simulator */
#define SELECT_TIMEOUT			2
#endif

// Select call return values
#define NETSELECT_READY			0
#define NETSELECT_TIMEOUT		1
#define NETSELECT_ERROR			2

// Port numbers
#define BOOTP_SERVER_PORT		67
#define BOOTP_CLIENT_PORT		68

// Maximum Size of a DHCP / BootP Packet (RFC 2131)
#define BOOTP_MAX_SIZE			576

typedef struct NetBootPHdr_t
{
  u8		op;
  u8		htype;
  u8		hlen;
  u8		hops;
  u32		xid;
  u16		secs;
  u16		flags;		// Just for DHCP, not used in BootP
  u8		ciaddr[4];
  u8		yiaddr[4];
  u8		siaddr[4];
  u8		giaddr[4];
  u8		chaddr[16];
  char	sname[64];
  char	file[128];
}
NetBootPHdr;

// function prototypes
void netBootP (ThreadId arg);
int SendBootPRequest (ThreadId dev, long sessionID, u8 *myHWAddr);
int ReadBootPReply (ThreadId dev, NetBootPHdr* myHdr);
int selectDevice (ThreadId dev, long timeout);
int VerifyBootPReply (NetBootPHdr* myHdr, long sessionID, u8 *myHWAddr);
int CompareBootReply (NetBootPHdr* myHdrA, NetBootPHdr* myHdrB);


#endif
