#ifndef _SOCKET3_H_
#define _SOCKET3_H_

#include <Symbol.h>
#include <Memory.h>
//#include <Socket4.h>

#define INADDR_ANY 0

#define PACKET_DISPLAY_ETHERNET 0
#define PACKET_DISPLAY_ARP      1
#define PACKET_DISPLAY_RARP     2
#define PACKET_DISPLAY_IP       3
#define PACKET_DISPLAY_ICMP     4
#define PACKET_DISPLAY_IGMP     5
#define PACKET_DISPLAY_UDP      6
#define PACKET_DISPLAY_TCP      7

//SOCKET编程必用的宏
#define TCP_WINDOW_SIZE   2048
#define PACKET_IP_VERSION 4
#define PACKET_INTNUMBER_ASM 60H
#define PACKET_INTNUMBER_C   0x60

#define MAX_LISTENING_COUNT 5
#define NULL_SOCKET     (SOCKET)(0)
#define INVALID_SOCKET  (SOCKET)(~0)
#define SOCKET_ERROR    (int)(~0)

//#define h_errno         WSAGetLastError()
typedef unsigned char   u_char;
typedef unsigned short  u_short;
typedef unsigned int    u_int;
typedef unsigned long   u_long;
typedef unsigned char 	byte;
typedef int  word;
typedef	long lword;
typedef	unsigned unshort;
typedef char *caddr_t;

typedef long            in_name;
typedef long            seq_t;
typedef u_int           SOCKET;

#include <Socket5.h>

//定义ICMP消息
#define	PGNOSND		0	//Couldn't send pkt
#define	PGTMO		1	//超时
#define	PGBADDATA	2	//rcved bad data back
#define	PGWAITING	3	//waiting for rcpt of packet
#define	PGSUCCESS	4	//成功

//定义
#define	DSTNET		0
#define	DSTHOST		1
#define	DSTPROT		2
#define	DSTPORT		3
#define	DSTFRAG		4
#define	DSTSRC		5

//定义IP报文的传输协议
#define			IPPROTO_IP               0  
#define			IPPROTO_ICMP             1  //ICMP协议
#define			IPPROTO_IGMP             2  //IGMP协议
#define			IPPROTO_GGP              3  //GGP网关协议
#define			IPPROTO_ST               5  /* Stream                           */
#define			IPPROTO_TCP              6  //TCP协议
#define			IPPROTO_UCL              7  /* UCL                              */
#define			IPPROTO_EGP              8  /* Exterior Gateway Protocol        */
#define			IPPROTO_IGP              9  /* any private interior gateway     */
#define			IPPROTO_BBN_RCC_MON     10  /* BBN RCC Monitoring               */
#define			IPPROTO_NVP_II          11  /* Network Voice Protocol           */
#define			IPPROTO_PUP             12  /* PUP                              */
#define			IPPROTO_ARGUS           13  /* ARGUS                            */
#define			IPPROTO_EMCON           14  /* EMCON                            */
#define			IPPROTO_XNET            15  /* Cross Net Debugger               */
#define			IPPROTO_CHAOS           16  /* Chaos                            */
#define			IPPROTO_UDP             17  //UDP协议
#define			IPPROTO_MUX             18  /* Multiplexing                     */
#define			IPPROTO_DCN_MEAS        19  /* DCN Measurement Subsystems       */
#define			IPPROTO_HMP             20  /* Host Monitoring                  */
#define			IPPROTO_PRM             21  /* Packet Radio Measurement         */
#define			IPPROTO_XNS_IDP         22  /* XEROX NS IDP                     */
#define			IPPROTO_TRUNK_1         23  /* Trunk-1                          */
#define			IPPROTO_TRUNK_2         24  /* Trunk-2                          */
#define			IPPROTO_LEAF_1          25  /* Leaf-1                           */
#define			IPPROTO_LEAF_2          26  /* Leaf-2                           */
#define			IPPROTO_RDP             27  /* Reliable Data Protocol           */
#define			IPPROTO_IRTP            28  /* Internet Reliable Transaction    */
#define			IPPROTO_ISO_TP4         29  /* ISO Transport Protocol Class 4   */
#define			IPPROTO_NETBLT          30  /* Bulk Data Transfer Protocol      */
#define			IPPROTO_INTERNAL        61  /* any host internal protocol       */
#define			IPPROTO_CFTP            62  /* CFTP                             */
#define			IPPROTO_LOCAL_NET       63  /* any local network                */
#define			IPPROTO_SAT_EXPAK       64  /* SATNET and Backroom EXPAK        */
#define			IPPROTO_MIT_SUBNET      65  /* MIT Subnet Support               */
#define			IPPROTO_RVD             66  /* MIT Remote Virtual Disk Protocol */
#define			IPPROTO_IPPC            67  /* Internet Pluribus Packet Core    */
#define			IPPROTO_DIST_FS         68  /* any distributed file system      */
#define			IPPROTO_SAT_MON         69  /* SATNET Monitoring                */
#define			IPPROTO_IPCV            71  /* Internet Packet Core Utility     */
#define			IPPROTO_BR_SAT_MON      76  /* Backroom SATNET Monitoring       */
#define			IPPROTO_WB_MON          78  /* WIDEBAND Monitoring              */
#define			IPPROTO_WB_EXPAK        79  /* WIDEBAND EXPAK                   */
#define			IPPROTO_OSPF            89  //OSPF最先开放短路径协议
#define			IPPROTO_RAW             255

//#define SOCK_STREAM   IPPROTO_TCP
//#define SOCK_DGRAM    IPPROTO_UDP
//#define SOCK_RAW      IPPROTO_RAW

//使用的TCP端号
#define IPPORT_ECHO             7
#define IPPORT_DISCARD          9
#define IPPORT_SYSTAT           11
#define IPPORT_DAYTIME          13
#define IPPORT_NETSTAT          15
#define IPPORT_FTP              21
#define IPPORT_TELNET           23
#define IPPORT_SMTP             25
#define IPPORT_TIMESERVER       37
#define IPPORT_NAMESERVER       42
#define IPPORT_WHOIS            43
#define IPPORT_MTP              57
#define IPPORT_TFTP             69
#define IPPORT_RJE              77
#define IPPORT_FINGER           79
#define IPPORT_TTYLINK          87
#define IPPORT_SUPDUP           95
#define IPPORT_EXECSERVER       512
#define IPPORT_LOGINSERVER      513
#define IPPORT_CMDSERVER        514
#define IPPORT_EFSSERVER        520
#define IPPORT_BIFFUDP          512
#define IPPORT_WHOSERVER        513
#define IPPORT_ROUTESERVER      520

//定义Ethernet的类型
#define	ETHERTYPE_IP		0x0800	//IP
#define	ETHERTYPE_ARP		0x0806	//ARP
#define	ETHERTYPE_RARP		0x8035  //RARP

//Ethernet数据包
#define MAX_IPADDR_LENGTH        4
#define MAX_ETHERNETADDR_LENGTH  6

#define MAX_ETHERNETPAGE_SIZE  (6 + 6 + 2 + 1500)

class CFifo
{

private:
        int m_nMaxFrame;
        int m_nHead;
        int m_nTail;

        void** m_psBuffer;
public:
       BOOL IsFull();
       BOOL IsEmpty(){return((m_nHead == m_nTail)?TRUE:FALSE);};
	   void Clear(){m_nHead = m_nTail;}

       void SetMaxFrame(int nCount){m_nMaxFrame = nCount;}
       int  GetMaxFrame(){return(m_nMaxFrame);}

       void SetHead(int nHead){m_nHead = nHead;}
       int  GetHead(){return(m_nHead);}

       void SetTail(int nTail){m_nTail = nTail;}
       int  GetTail(){return(m_nTail);}

       void  SetBuffer(void** pBuffer){m_psBuffer = pBuffer;}
       void* SetFrame(int nLocation, void* pFrame);
       void* GetFrame(int nLocation);

       BOOL  AddFrameToTail(void* pFrame);
       void* GetFrameFromHead(BOOL bMove = TRUE);

public:
       CFifo();
};

#define ETHERFIFO_UDPTCP_SIZE  10

#define ETHERFIFO_START_SIZE       4
#define ETHERFIFO_SEND_SIZE       32
#define ETHERFIFO_ARP_SIZE         2
#define ETHERFIFO_RARP_SIZE        2
#define ETHERFIFO_IP_SIZE         12
#define ETHERFIFO_ICMP_SIZE       12
#define ETHERFIFO_UDP_SIZE       12
#define ETHERFIFO_TCP_SIZE       12
#define ETHERFIFO_FREE_SIZE        4

#define ETHERFIFO_START       1
#define ETHERFIFO_SEND        2
#define ETHERFIFO_ARP         3
#define ETHERFIFO_RARP        4
#define ETHERFIFO_IP          5
#define ETHERFIFO_ICMP        6
#define ETHERFIFO_UDP         7
#define ETHERFIFO_TCP         8
#define ETHERFIFO_FREE        9

class CEthernetFifo:public CFifo
{
public:
	CFifo  m_SendFifo;
	CFifo  m_RecvArpFifo;
	CFifo  m_RecvRarpFifo;
    CFifo  m_RecvIpFifo;
    CFifo  m_RecvIcmpFifo;
//	CFifo  m_RecvUdpFifo;
// 	CFifo  m_RecvTcpFifo;

public:
	CXmsMemory m_XmsMemory_Send;
    CXmsMemory m_XmsMemory_Recv;
public:
	CEthernetFifo();
};

typedef BYTE IP_ADDRESS[MAX_IPADDR_LENGTH];
typedef BYTE ETHERNET_ADDRESS[MAX_ETHERNETADDR_LENGTH];

typedef struct ETHERNET_HEADER
{
	ETHERNET_ADDRESS	mEthernetRecvAddr;//目的地址
	ETHERNET_ADDRESS	mEthernetSendAddr;//源地址
	WORD				wType;//帧类型
	//BYTE				ip_data[];//数据
	//DWORD				Check;//crc校验
} ETHERNET_HEADER;


//Arp数据包
typedef struct tagARP_HEADER
{
	WORD             wHardwareStyle;//硬件类型
	WORD             wProtoentStyle;//协议类型
	BYTE             byHardwareLen;//硬件长度
	BYTE             byProtoentLen;//协议长度
	WORD             wOperationCode;//1=申请；2=应答
	ETHERNET_ADDRESS mEthernetSendAddr;//发送机硬件地址
	IP_ADDRESS       mIPSendAddr;//发送机IP地址
	ETHERNET_ADDRESS mEthernetRecvAddr;//接收机硬件地址
	IP_ADDRESS       mIPRecvAddr;//接收机IP地址
}ARP_HEADER;

//Rarp数据包
typedef struct tagRARP_HEADER
{
	WORD             wHardwareStyle;//硬件类型
	WORD             wProtoentStyle;//协议类型
	BYTE             byHardwareLen;//硬件长度
	BYTE             byProtoentLen;//协议长度
	WORD             wOperationCode;//1=申请；2=应答
	ETHERNET_ADDRESS mEthernetSendAddr;//发送机硬件地址
	IP_ADDRESS       mIPSendAddr;//发送机IP地址
	ETHERNET_ADDRESS mEthernetRecvAddr;//接收机硬件地址
	IP_ADDRESS       mIPRecvAddr;//接收机IP地址
}RARP_HEADER;

#pragma
//IP包定义
typedef struct tagIP_HEADER
{
	BYTE			byIHL: 4;		//网际头标长度,以4字节为单位
	BYTE 			byVersion : 4;	//版本

	byte			byServeType;	//服务类型，现行TCP/IP不使用该值
	unshort			wLength;		//总长度，以字节为单位
	unshort			wID;			//标识，每一“完整的”数据报是唯一的
	unshort			wFlags : 3;		//标志，x1x不能分段;10x:表有分段;00x:没分段
	unshort			wOffset : 13;	//片偏移，在分段时，其原始的8字节为单位的偏移
	byte			byTTL;			//生存期,值为0时，应丢弃
	byte			byProtoent;		//协议
	unshort			wChecksum;		//头校验
	IP_ADDRESS		mIPSendAddr;	//源IP地址
	IP_ADDRESS		mIPRecvAddr;	//目的IP地址
	//unsigned long dwOption:24;	//选项
	//unsigned long dwFill:8;		//填充
	//byte			bysData[];		//数据
}IP_HEADER;

//ICMP包。3=住宿不可达,4=终止源,11=超时
struct ICMP_Dest
{
	BYTE	  byType;//类型
	BYTE	  byCode;//代码
	unshort   wChecksum;//校验和
	DWORD     dwReserve;//保留1
	IP_HEADER mIPHead;//IP头
	BYTE	  bysData[8];//64位
};

//ICMP包。5=重定向
struct ICMP_Redirect
{
	BYTE	  byType;//类型
	BYTE	  byCode;//代码
	unshort   wChecksum;//校验和
	long      gwIPAddress;//Gatewat IP地址
	IP_HEADER mIPHead;//IP头
	BYTE	  bysData[8];//64位

};

//ICMP包。8=响应申请,0=响应应答
struct ICMP_Response
{
	BYTE	  byType;//类型
	BYTE	  byCode;//代码
	unshort   wChecksum;//校验和
	unsigned  id;//标识
	unsigned  seq;//序列号
	IP_HEADER mIPHeader;//IP头
	BYTE	  bysData[8];//64位
    BYTE      bysData2[4];
};


//ICMP包。12=参数失灵
struct ICMP_Para
{
	BYTE	  byType;//类型
	BYTE	  byCode;//代码
	unshort   wChecksum;//校验和
	BYTE      byPtr;//Ptr
	BYTE      byReserve[3];//保留
	IP_HEADER mIPHead;//IP头
	BYTE	  bysData[8];//64位
};


//ICMP包。13=时间标志请求
struct ICMP_StampReq
{
	BYTE	  byType;//类型
	BYTE	  byCode;//代码
	unshort   wChecksum;//校验和
	unsigned  id;//标识
	unsigned  seq;//序列号
	long	  stamp[1];//源时标
};

//ICMP包。14=时间标志应答
struct ICMP_StampAck
{
	BYTE	  byType;//类型
	BYTE	  byCode;//代码
	unshort   wChecksum;//校验和
	unsigned  id;//标识
	unsigned  seq;//序列号
	long	  stamp[3];//源时标+接收时标+传输时标
};

//ICMP包。15=信息请求,16＝信息印答,17=地址掩码请求
struct ICMP_INFORMATION
{
	BYTE	  byType;//类型
	BYTE	  byCode;//代码
	unshort   wChecksum;//校验和
	unsigned  id;//标识
	unsigned  seq;//序列号
};

//ICMP包。16 =地址掩码请求 17=地址掩码应答
struct ICMP_ADDRESSMASK
{
	BYTE	  byType;//类型
	BYTE	  byCode;//代码
	unshort   wChecksum;//校验和
	unsigned  id;//标识
	unsigned  seq;//序列号
    DWORD 	  dwMask;
};

typedef union tagICMP_HEADER
{
	struct ICMP_Dest     mIcmp3_4_11;
	struct ICMP_Redirect mIcmp5;
	struct ICMP_Response mIcmp0_8;
	struct ICMP_Para     mIcmp12;
	struct ICMP_StampReq mIcmp13;
	struct ICMP_StampAck mIcmp14;
    struct ICMP_ADDRESSMASK mIcmp16_17;
}ICMP_HEADER;

//PING数据包
struct PING_HEAD //使用ICMP的响应申请(=8)和响应应答(=0)
{				
	char	  byType;//类型
	char	  byCode;//代码
	unshort   wChecksum;//校验和
	unsigned  id;//标识
	unsigned  seq;//序列号
};

//伪TCP头
typedef struct tagTCPx_HEADER
{
	IP_ADDRESS mIPSendAddr;
	IP_ADDRESS mIPRecvAddr;
    BYTE       byReserve;
    BYTE       byProtoent;
    WORD       wLength;

}TCPx_HEADER;

//TCP包定义
typedef struct tagTCP_HEADER {
    WORD			wSrcPort;		//源端口
    WORD			wDesPort;		//目的端口
    DWORD			dwSeqNO;		//序列号，指明当前段在全部信息中的位置序号
	DWORD			dwAckNO;        //确认号，指明将要到达的下一个段的序列号
    WORD			wUU1:4;
    WORD			wTHL:4;
    WORD			wFIN:1;
    WORD			wSYN:1;
    WORD			wRST:1;
    WORD			wPSH:1;
    WORD			wACK:1;
    WORD			wURG:1;
    WORD			wUU2:2;
    /*
    unsigned        wTHL  : 4;		//数据偏移
	unsigned        wUU1  : 6;		//保留1
    unsigned        wURG : 1;		//URG紧急标志。指明紧急指针有效
    unsigned        wACK : 1;		//ACK确认标志
    unsigned        wPSH  : 1;		//PSH压栈标志
    unsigned        wRST  : 1;		//RST重置标志
    unsigned        wSYN  : 1;		//SYN同步标志
    unsigned        wFIN  : 1;		//FIN结束标志
    */
    unshort			wWin;			//窗口，表接收机能够接收的数据块数
    unshort			wChecksum;      //校验码
    unshort			wUrgPoint;      //紧急指针
	//unsigned long option:24;		//选项
	//unsigned long fill:8;			//填充
	//byte			data[];			//数据

}TCP_HEADER;

//伪UDP头
typedef struct tagUDPx_HEADER
{
	IP_ADDRESS mIPSendAddr;
	IP_ADDRESS mIPRecvAddr;
    BYTE       byReserve;
    BYTE       byProtoent;
    WORD       wLength;

}UDPx_HEADER;
//UDP包定义
typedef struct tagUDP_HEADER 
{
	unsigned		wSrcPort;	//源端口
	unsigned		wDesPort;	//目的端口
	unsigned		wLength;		//UDP包长度
	unsigned		wChecksum;	//UDP校验值
}UDP_HEADER;

//定义协议家族
#define AF_UNSPEC       0        //没有指定
#define AF_UNIX         1        //local to host (pipes, portals)
#define AF_INET         2        //internetwork: UDP, TCP, etc.
#define AF_IMPLINK      3        //arpanet imp addresses
#define AF_PUP          4        //pup protocols: e.g. BSP
#define AF_CHAOS        5        //mit CHAOS protocols
#define AF_IPX          6        //IPX and SPX
#define AF_NS           6        //XEROX NS protocols
#define AF_ISO          7        //ISO protocols
#define AF_OSI          AF_ISO   //OSI is ISO
#define AF_ECMA         8        //european computer manufacturers
#define AF_DATAKIT      9        //datakit protocols
#define AF_CCITT        10       //CCITT protocols, X.25 etc
#define AF_SNA          11       //IBM SNA
#define AF_DECnet       12       //DECnet
#define AF_DLI          13       //Direct data link interface
#define AF_LAT          14       //LAT
#define AF_HYLINK       15       //NSC Hyperchannel
#define AF_APPLETALK    16       //AppleTalk
#define AF_NETBIOS      17       //NetBios-style addresses
#define AF_VOICEVIEW    18       //VoiceView
#define AF_FIREFOX      19       //FireFox
#define AF_UNKNOWN1     20       //Somebody is using this!
#define AF_BAN          21       //Banyan
#define AF_MAX          22

//定义套接字类型
#define SOCK_STREAM     1 //流套接字
#define SOCK_DGRAM      2 //数据报套接字
#define SOCK_RAW        3 //原始协议套接字
#define SOCK_RDM        4 //reliably-delivered message
#define SOCK_SEQPACKET  5 //sequenced packet stream



struct  hostent
{
        char    FAR * h_name;           /* official name of host */
        char    FAR * FAR * h_aliases;  /* alias list */
        short   h_addrtype;             /* host address type */
        short   h_length;               /* length of address */
        char    FAR * FAR * h_addr_list; /* list of addresses */
#define h_addr  h_addr_list[0]          /* address, for backward compat */
};
typedef struct hostent HOSTENT;
typedef struct hostent *PHOSTENT;
typedef struct hostent FAR *LPHOSTENT;

struct  servent
{
        char    FAR * s_name;           /* official service name */
        char    FAR * FAR * s_aliases;  /* alias list */
        short   s_port;                 /* port # */
        char    FAR * s_proto;          /* protocol to use */
};
typedef struct servent SERVENT;
typedef struct servent *PSERVENT;
typedef struct servent FAR *LPSERVENT;

struct  protoent
{
        char    FAR * p_name;           //official protocol name
        char    FAR * FAR * p_aliases;  //alias list
        short   p_proto;                //protocol #
};
typedef struct protoent PROTOENT;
typedef struct protoent *PPROTOENT;
typedef struct protoent FAR *LPPROTOENT;

struct  linger {
        u_short l_onoff;	//option on/off
        u_short l_linger;	//linger time
};
typedef struct linger LINGER;
typedef struct linger *PLINGER;
typedef struct linger FAR *LPLINGER;





#define IOCPARM_MASK    0x7f            /* parameters must be < 128 bytes */
#define IOC_VOID        0x20000000      /* no parameters */
#define IOC_OUT         0x40000000      /* copy out parameters */
#define IOC_IN          0x80000000      /* copy in parameters */
#define IOC_INOUT       (IOC_IN|IOC_OUT)                                      
#define _IO(x,y)        (IOC_VOID|((x)<<8)|(y))
#define _IOR(x,y,t)     (IOC_OUT|(((long)sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|(y))
#define _IOW(x,y,t)     (IOC_IN|(((long)sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|(y))

#define FIONREAD    _IOR('f', 127, u_long) /* get # bytes to read */
#define FIONBIO     _IOW('f', 126, u_long) /* set/clear non-blocking i/o */
#define FIOASYNC    _IOW('f', 125, u_long) /* set/clear async i/o */

/* Socket I/O Controls */
#define SIOCSHIWAT  _IOW('s',  0, u_long)  /* set high watermark */
#define SIOCGHIWAT  _IOR('s',  1, u_long)  /* get high watermark */
#define SIOCSLOWAT  _IOW('s',  2, u_long)  /* set low watermark */
#define SIOCGLOWAT  _IOR('s',  3, u_long)  /* get low watermark */
#define SIOCATMARK  _IOR('s',  7, u_long)  /* at oob mark? */




//TCP连接表
typedef struct tagTCPCACHE
{
	WORD wState;
	IP_ADDRESS mIPLocalAddr;
	WORD wLocalPort;
	IP_ADDRESS mIPRemoteAddr;
	WORD wRemotePort;
}TCPCACHE;

//UDP连接表
typedef struct tagUDPCACHE
{
	WORD wState;
	IP_ADDRESS mIPLocalAddr;
	WORD wLocalPort;
	IP_ADDRESS mIPRemoteAddr;
	WORD wRemotePort;
}UDPCACHE;



struct sockaddr 
{
    unsigned short sa_family;
    char           sa_data[14];
};

struct in_addr 
{
	union 
	{
		struct 
		{ 
			u_char s_b1;
			u_char s_b2;
			u_char s_b3;
			u_char s_b4; 
		} S_un_b;
		struct 
		{ 
			u_short s_w1;
			u_short s_w2; 
		} S_un_w;
		u_long S_addr;
	} S_un;
};

struct sockaddr_in
{
	short            sin_family;
	unsigned short   sin_port;
	struct   in_addr sin_addr;
	char             sin_zero[8];
};


struct PACKETPARAM 
{
	unsigned char	major_rev;	/* Revision of Packet Driver spec */
	unsigned char	minor_rev;	/*  this driver conforms to. */
	unsigned char	length;		/* Length of structure in bytes */
	unsigned char	addr_len;	/* Length of a MAC-layer address */
	unsigned short	mtu;		/* MTU, including MAC headers */
	unsigned short	multicast_aval;	/* Buffer size for multicast addr */
	unsigned short	rcv_bufs;	/* (# of back-to-back MTU rcvs) - 1 */
	unsigned short	xmt_bufs;	/* (# of successive xmits) - 1 */
	unsigned short	int_num;	/* Interrupt # to hook for post-EOI
					   processing, 0 == none */
};

struct PACKETSTATISTICS
{
    unsigned long	packets_in;	/* Totals across all handles */
	unsigned long	packets_out;
	unsigned long	bytes_in;	/* Including MAC headers */
	unsigned long	bytes_out;
	unsigned long	errors_in;	/* Totals across all error types */
	unsigned long	errors_out;
	unsigned long	packets_lost;	/* No buffer from receiver(), card */
					/*  out of resources, etc. */
};

WORD	GetEthernetChecksum(WORD* pBuffer, int nSize, BOOL bDisplay = FALSE);

void FAR PacketReceived();




//==========================================================
//==========================================================
//==========================================================
int Initial_PktDrv();
int Driver_Info(int hPacket, WORD* wVersion, WORD* wClass,
				  WORD* wType, WORD* wNumber,
				  WORD* wFunctionality, char* pName, int nLen);
int Access_Type(WORD wClass, WORD wType, WORD wNumber,
			   char* pName, int nLen, void FAR (*receive)(),
                           HANDLE* hPacket);
int Release_Type(int nHandle);
int Send_Packet(BYTE* pBuffer, int nLength);
int Terminate_Device(int hPacket);
int Terminate_Device(int hPacket);
int Get_Address(int hPacket, BYTE* pBuffer, int nLength);
int Reset_Interface(int nPacket);
int Get_Parameters(PACKETPARAM* pPacketParam);
int As_Send_Packet1(BYTE* pBuffer, int nLen, int (FAR *upcall)());
int AsynchronousSendPacket2();
int DropPacketFromYueve();
int Set_Receive_Mode(int hPacket, int nMode);
int Get_Rev_Mode(int hPacket, int* pnMode);
int Set_Multicase_List(char* pAddrList, int nLength);
int Get_Multicase_List(char* pAddrList, int* pnLength);
int Get_Statistic(PACKETSTATISTICS* pStatistics);
int Set_Address(char* pAddr, int nLength);
int Send_Raw_Bytes();
int FlushRawBytesReceived();
int FetchRawBytesReceived();
int PktGeterr();




BOOL AfxGetLocalNICAddress(ETHERNET_ADDRESS mEthernetAddress);
int  InitPacketInterface(int* phIp_Packet, int* phArp_Packet, void* pEthernetAddr);


BOOL GlobalInitSocket();
BOOL GlobalCloseSocket();
BOOL EthernetEntry(BOOL bSingle, BOOL bSend, BOOL bIcmp);
void WaitPacketReceiveFinish();
void GetNetcardPacketAttrib(DWORD* pdwC1, DWORD* pdwC2, DWORD* pdwC3);
BOOL DoWithSelfIPAddress();

long GetEthereFifoLocation(int nType, int nLoc);

BYTE* AfxGetIpSelfAddr();
BYTE* AfxGetIpPeerAddr();
BYTE* AfxGetIpMaskAddr();

BOOL WSAGetLastError();
void CleanupNetcardPage(BOOL bRecv);



//基本函数========================================
//建立套接字，指定五元中的“协议”
SOCKET socket(int af,int type,int protocol);
//绑定套接定，指定五元中的“本地主机地址和端口号”
int bind(SOCKET s, const struct sockaddr FAR * name, int namelen);
//侦听套接字，通知TCP协议，服务器准备好接收连接，并指明请求连接队列的长度
int listen(SOCKET s,int backlog);
//等待来自客户端的连接，用在服务器端
SOCKET accept(SOCKET s, struct sockaddr FAR * addr, int FAR * addrlen);
//向服务器发出连接请求，用在客户端
int connect(SOCKET s, const struct sockaddr FAR * name, int namelen);
//接收数据-TCP
int recv(SOCKET s,char FAR * buf,int len,int flags);
//发送数据-TCP
int send(SOCKET s,const char FAR * buf,int len,int flags);
//接收数据-UDP
int recvfrom(SOCKET s,char FAR * buf,int len,int flags,struct sockaddr FAR * from,int FAR * fromlen);
//发送数据-UDP
int sendto(SOCKET s,const char FAR * buf,int len,int flags,const struct sockaddr FAR * to,int tolen);
//关闭套接字
int closesocket(SOCKET s);
//关闭套接字
int shutdown( SOCKET s,int how);

//转换函数========================================
//长整型主机字节转网络字节
u_long htonl(u_long hostlong);
//长整型网络字节转主机字节 
u_long ntohl(u_long netlong);
//短整型主机字节转网络字节
u_short htons(u_short hostshort);
//短整型网络字节转主机字节
u_short ntohs( u_short netshort);
//internet地址转网络字符串串地址
char FAR *inet_ntoa(struct in_addr in );
//网络字符串地址转internet地址
unsigned long inet_addr(const char FAR * cp);
//将NIC地址转成字符串
char FAR *iphy_fromnic(ETHERNET_ADDRESS in);



//检测套接字的状态
int select(int nfds,fd_set FAR * readfds, fd_set FAR * writefds, 
	fd_set FAR *exceptfds,const struct timeval FAR * timeout);
//为套接字提供控制(1)设置阻塞模式(2)可读的数据量(3)检测带外数据是否读完
int ioctlsocket(SOCKET s,long cmd,u_long FAR * argp);
//查询网络包的状态
BOOL QueryNetcardPage(SOCKET hSocket, BOOL bRecv = TRUE, DWORD dwMilliTime = 0,
     DWORD* pdwCount = NULL, IP_HEADER* pIPHeader = NULL, ETHERNET_HEADER* pEthernetHeader = NULL);



//设置套接字相关选项属性
int setsockopt(SOCKET s,int level,int optname,const char FAR * optval,int optlen);
//检索套接字的相关选项属性
int getsockopt(SOCKET s,int level,int optname,char FAR * optval,int FAR * optlen);
//获得与指定套接字自己的“名字”
int getsockname(SOCKET s, struct sockaddr FAR * name,int FAR * namelen);
//获得与指定套接字连接对方的“名字”
int getpeername(SOCKET s,struct sockaddr FAR * name,int FAR * namelen);




//通过intenet地址获得主机信息
struct hostent  FAR *gethostbyaddr(const char FAR * addr, int len, int type);
//通过主机名字获得主机信息
struct hostent  FAR *gethostbyname(const char FAR * name);
//获取本地址主机的名字
int gethostname(char FAR * name,int namelen);
//通过协议名称获得协议信息
struct protoent FAR *getprotobyname(const char FAR * name);
//通过协议号获得协议信息
struct protoent FAR *getprotobynumber(int number);
//通过服务名称和协议名称获取协议信息
struct servent  FAR *getservbyname(const char FAR * name,const char FAR * proto);
//通过服务名称和协议名称获取协议信息
struct servent  FAR *getservbyport(int port,const char FAR * proto);


void FAR TFTP_Dialog();

BOOL TFTP_SendFile (SOCKET hSocket,const struct sockaddr_in FAR * to,
	const char* pLocalFileName, const char* pRemoteFileName);

BOOL TFTP_ReadFile (SOCKET hSocket,const struct sockaddr_in FAR * to,
	const char* pLocalFileName, const char* pRemoteFileName);


#include <Socket4.h>
#endif