#ifdef __cplusplus
extern "C" {
#endif

#define TCPIP_MAX_RETRY		5
#define TCPIP_RESET_TIME	3

#define HW_ALEN	    6		// MAC address on ethernet length is 6 
#define IP_ALEN     4		// IP address length is 4 

#define TCP_DATA			20	/* data begin, minimum TCP header length */

#define UDP_HEAD_LEN		45		// EP_DATA+IP_DATA+UDP_DATA+3
#define UDP_DATA			8

#define TCPIP_RESERVED_PORT	1024

#define PPPOE_INIT_INFO		6
#define PPPOE_INFO			8

/* APR packet length */
#define ARP_LENGTH      (8 + HW_ALEN + IP_ALEN + HW_ALEN + IP_ALEN)

/* ARP packet type is 0x0806 */
#define EPT_ARP			0x0806

/* IP packet type is 0x0800 */
#define EPT_IP			0x0800

/* 802.1q VLAN packet type is 0x8100 */
#define EPT_VLAN		0x8100

/* 802.1q header fields */
#define VLAN_TPID			0
#define VLAN_TCI			2
#define VLAN_HEADER_SIZE	4
#define VLAN_ID_MASK		0x0fff

/* PPPoE packet type */
#define EPT_PPPOE_INIT	0x8863
#define EPT_PPPOE		0x8864

/* PPP Protocol */
#define PPP_PROT_LCP		0xc021
#define PPP_PROT_PAP		0xc023
#define PPP_PROT_CHAP		0xc223
#define PPP_PROT_IPCP		0x8021
#define PPP_PROT_IP			0x0021

/* Internet Protocol (IP) constants */

/* Some assigned protocol numbers */
#define IPT_ICMP    1   /* protocol type for ICMP packets */
#define IPT_IGMP    2   /* protocol type for IGMP packets */
#define IPT_TCP     6   /* protocol type for TCP packets */
#define IPT_EGP     8   /* protocol type for EGP packets */
#define IPT_UDP     17  /* protocol type for UDP packets */
#define IPT_RSVP    46  /* protocol type for RSVP packets */
#define IPT_OSPF    89  /* protocol type for OSPF packets */

/* Field offset in an ethernet packet */
#define EP_DST  0
#define EP_SRC  HW_ALEN
#define EP_TYPE (HW_ALEN + HW_ALEN)
#define EP_DATA (EP_TYPE + 2)     

/* Field offeset in an IP packet */
#define IP_VERLEN   0   /* IP version & header length (in longs) */
#define IP_TOS      1   /* type of service */
#define IP_LEN      2   /* total packet length (in octets) */	
#define IP_ID       4   /* datagram id */
#define IP_FRAGOFF  6   /* fragment offset (in 8-octet's) */
#define IP_TTL      8   /* time to live, in gateway hops */
#define IP_PROTO    9   /* IP protocol */
#define IP_CKSUM    10  /* header checksum */
#define IP_SRC      12  /* IP address of source */
#define IP_DST      16  /* IP address of destination */
#define IP_DATA     20  /* variable length data, minimum IP header length */

/* IP precedence values */
#define IPP_NETCTL  0xe0    /* network control */
#define IPP_INCTL   0xc0    /* internet control */
#define IPP_CRIT    0xa0    /* critical */
#define IPP_FLASHO  0x80    /* flash over-ride */
#define IPP_FLASH   0x60    /* flash */
#define IPP_IMMED   0x40    /* immediate */
#define IPP_PRIO    0x20    /* priority */
#define IPP_NORMAL  0x00    /* normal */

#define IP_FIRST_BYTE   0x45
#define IP_VERSION      4   /* current version value */
#define IP_MINHLEN      5   /* minimum header length (in longs) */
#define IP_INIT_TTL     128 /* initial time-to-live value */

// The max length of PPPOE data packet, except enter in IP mode
#define PPPOE_SEND_LEN	64

// Hdlc head format
#define HDLC_ADDR		0
#define HDLC_CTRL		1
#define HDLC_PROT		2
#define HDLC_INFO		4
#define HDLC_FCS_LEN	2

#define FLAG_VALUE		0x7e
#define ADDR_VALUE		0xff
#define CTRL_VALUE		0x03

#define Adapter_pReceivePacket		(SRAM_CONST_START + NE2000_RECV_CACHE)
#define Adapter_pReceiveUdpData		(Adapter_pReceivePacket + UDP_DATA)
#define Adapter_pReceiveTcpData		(Adapter_pReceivePacket + TCP_DATA)

#define Adapter_pSendPacket			(SRAM_CONST_START + NE2000_SEND_CACHE)
#define	Adapter_pSendUdpData		(Adapter_pSendPacket + UDP_DATA)
#define	Adapter_pSendTcpData		(Adapter_pSendPacket + TCP_DATA)

extern		PXCHAR	Adapter_pReceiveMacAddress;
extern		PXCHAR	Adapter_pReceiveIPAddress;
extern		DSHORT	Adapter_iReceiveUdpPort;                  
extern		DSHORT	Adapter_iReceiveLen;		// Total length of a received packet
extern	 BOOLEAN Adapter_bReceiveBroadcast;

extern		BOOLEAN	Tcp_bDataEvent;
extern		BOOLEAN	Tcp_bCloseEvent;
extern		BOOLEAN	Tcp_bConnectEvent;
extern		BOOLEAN	Tcp_bAcceptEvent;
extern		BOOLEAN	Tcp_bResetEvent;

extern	 BOOLEAN PPPoE_bInUse;

extern		XCHAR Dns_pResolvedIP[IP_ALEN];

extern	 BOOLEAN	Discover_bFinished;

extern	 XCHAR	IP_pReceiveHeader[IP_DATA];
extern	 XCHAR	Mac_pReceiveHeader[4+EP_DATA];
extern	 XCHAR PPPoE_pHead[PPPOE_INFO];

                     
	void	TcpipInitialize();													// Tcpip.c
	void	TcpipHandleTimer();

	void	UdpOpen(PXCHAR pDstIP, USHORT iDstPort, USHORT iSrcPort, UCHAR iSdramLo);
	void	UdpOpenBroadcast(USHORT iDstPort, USHORT iSrcPort, UCHAR iSdramLo);
	void	UdpSetTos(UCHAR iTos, UCHAR iSdramLo);
	void	UdpSend(USHORT iLength, UCHAR iSdramLo);
	void	UdpRun();

	void	UdpDebugUpdate();
	void	UdpDebugString(char * p);
	void	UdpDebugRemoteStop();

	void	TcpOpen(PXCHAR pDstIP, USHORT iDstPort, UCHAR iSdramLo);
	void	TcpClose(UCHAR iSdramLo);
	void	TcpSend(USHORT iLength);
	void	TcpRun();
	void	TcpSendClose(UCHAR iSdramLo);

	void	TcbInit(USHORT iSrcPort, UCHAR iTTL, UCHAR iSdramLo);
	BOOLEAN	TcbRun(UCHAR iSdramLo);
	BOOLEAN	TcbHandleTimer(UCHAR iSdramLo);
	BOOLEAN	TcbClosed(UCHAR iSdramLo);	
	BOOLEAN TcbAcked();
	BOOLEAN TcbConnected(UCHAR iSdramLo);
	USHORT	TcbGetRemotePort();	
	PXCHAR	TcbGetRemoteIP();

	void	PPPoEDisconnect();													// pppoe.c
	void	PPPoE_Run();
	void	PPPoE_InitRun();
	BOOLEAN PPPoE_IsIP();

//	void	Ping(PXCHAR pDstIP);												// icmp.c
	void	IcmpRun();

	void	ArpRun();

 void DnsSend(PXCHAR pDomain, BOOLEAN bUseSrv);								// dns.c

 BOOLEAN DhcpRenewing();														// dhcp.c

 void	PPP_Initialize();													// PPP.c
 void	LcpRun();
 void	PapRun();
 void	ChapRun();
 void	IpcpRun();
 void	OtherProtRun();														// PPP.c
 void	LcpConfigReq();

	void	DebugString(PXCHAR p);												// debug.c
	void	DebugCString(PCCHAR p);
	void	DebugNewLine();
	void	DebugChar(UCHAR ch);

	void	OutputString(USHORT iID);
	void	OutputLine(USHORT iID);		// no "\r\n" in the end												
	void	OutputVal(USHORT iVal, USHORT iPrefixID);
	void	OutputPXCHAR(PXCHAR pLine, USHORT iLength);	// for debug >= 2 only
	void	OutputVals(PXCHAR pVals, USHORT iLength);	// for debug >= 2 only
	void	OutputValsAsString(PXCHAR pVals);
	void	OutputIP(PXCHAR pIP, USHORT iPrefixID);
	void	OutputIPPort(PXCHAR pIP, USHORT iPort, USHORT iPrefixID);

#ifdef __cplusplus
}
#endif

