Tcp.h
资源名称:PPP0208.rar [点击查看]
上传用户:karykuang
上传日期:2010-02-26
资源大小:103k
文件大小:6k
源码类别:
TCP/IP协议栈
开发平台:
Visual C++
- // Tcp.h: interface for the CTcp class.
- //
- //////////////////////////////////////////////////////////////////////
- #if !defined(AFX_TCP_H__109B7123_703F_11D5_9C82_0010B54D784D__INCLUDED_)
- #define AFX_TCP_H__109B7123_703F_11D5_9C82_0010B54D784D__INCLUDED_
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
- /* Canonically-sized data */
- typedef unsigned long longword; /* 32 bits */
- typedef unsigned short word; /* 16 bits */
- typedef unsigned char byte; /* 8 bits */
- typedef byte octet; /* 8 bits, for TCP */
- #define NIL 0 /* The distinguished empty pointer */
- #define in_GetVersion(ip) (((ip)->vht >> 12) & 0xf)//4位版本号
- #define in_GetHdrlen(ip) (((ip)->vht >> 8) & 0xf)//4位首部长度
- #define in_GetHdrlenBytes(ip) (((ip)->vht >> 6) & 0x3c)//?
- #define in_GetTos(ip) ((ip)->vht & 0xff) //8位服务类型
- #define in_GetTTL(ip) ((ip)->ttlProtocol >> 8)//8位生存期
- #define in_GetProtocol(ip) ((ip)->ttlProtocol & 0xff)//8位协议
- //定义TCP首部中6位标志比特
- #define tcp_FlagFIN 0x0001 //发端完成发送任务
- #define tcp_FlagSYN 0x0002 //同步序号用来发起一个连接
- #define tcp_FlagRST 0x0004 //重建连接
- #define tcp_FlagPUSH 0x0008 //接收方应该尽快将这个报文段交给应用层
- #define tcp_FlagACK 0x0010 //确认序号有效
- #define tcp_FlagURG 0x0020 //紧急指针有效
- #define tcp_FlagDO 0xF000 //?
- #define tcp_GetDataOffset(tp) ((tp)->flags >> 12)//4位首部长度
- /*
- * TCP states, from tcp manual.
- * Note: close-wait state is bypassed by automatically closing a connection
- * when a FIN is received. This is easy to undo.
- */
- #define tcp_StateLISTEN 0 /* listening for connection */
- #define tcp_StateSYNSENT 1 /* syn sent, active open */
- #define tcp_StateSYNREC 2 /* syn received, synack+syn sent. */
- #define tcp_StateESTAB 3 /* established */
- #define tcp_StateFINWT1 4 /* sent FIN */
- #define tcp_StateFINWT2 5 /* sent FIN, received FINACK */
- /*#define tcp_StateCLOSEWT 6 /* received FIN waiting for close */
- #define tcp_StateCLOSING 6 /* sent FIN, received FIN (waiting for FINACK) */
- #define tcp_StateLASTACK 7 /* fin received, finack+fin sent */
- #define tcp_StateTIMEWT 8 /* dally(延误) after sending final FINACK */
- #define tcp_StateCLOSED 9 /* finack received */
- #define tcp_MaxData 32 /* maximum bytes to buffer on output */
- /* Timer definitions */
- #define tcp_RETRANSMITTIME 1000 /* interval at which retransmitter is called */
- #define tcp_LONGTIMEOUT 31000 /* timeout for opens */
- #define tcp_TIMEOUT 10000 /* timeout during a connection */
- class CTcp
- {
- public:
- CTcp();
- virtual ~CTcp();
- /* Useful type definitions */
- typedef int (*procref)();
- typedef short BOOL; /* boolean type */
- /* protocol address definitions */
- typedef longword in_HwAddress;
- //typedef word eth_HwAddress[3];
- /* The Ethernet header */
- /*typedef struct {
- eth_HwAddress destination;
- eth_HwAddress source;
- word type;
- } eth_Header;*/
- /* The Internet Header: */
- typedef struct {
- word vht; /* version, hdrlen, tos */
- word length;
- word identification;
- word frag;
- word ttlProtocol;
- word checksum;
- in_HwAddress source;
- in_HwAddress destination;
- } in_Header;
- typedef struct {
- word srcPort;
- word dstPort;
- longword seqnum;
- longword acknum;
- word flags;
- word window;
- word checksum;
- word urgentPointer;
- } tcp_Header;
- /* The TCP/UDP Pseudo Header */
- typedef struct {
- in_HwAddress src;
- in_HwAddress dst;
- octet mbz;
- octet protocol;
- word length;
- word checksum;
- } tcp_PseudoHeader;
- // TCP Socket definition
- typedef struct _tcp_socket {
- struct _tcp_socket *next;
- short state; /* connection state */
- procref dataHandler; /* called with incoming data */
- //eth_HwAddress hisethaddr; /* ethernet address of peer */
- in_HwAddress hisaddr; /* internet address of peer */
- word myport, hisport;/* tcp ports for this connection */
- longword acknum, seqnum; /* data ack'd and sequence num */
- int timeout; /* timeout, in milliseconds */
- BOOL unhappy; /* flag, indicates retransmitting segt's */
- word flags; /* tcp flags word for last packet sent */
- short dataSize; /* number of bytes of data to send */
- byte data[tcp_MaxData]; /* data to send */
- } tcp_Socket;
- //extern eth_HwAddress sed_lclEthAddr;
- //extern eth_HwAddress sed_ethBcastAddr;
- //extern in_HwAddress sin_lclINAddr;
- #ifdef DEBUG
- // Primitive logging facility
- #define tcp_LOGPACKETS 1 /* log packet headers */
- word tcp_logState;
- #endif
- //Local IP address
- in_HwAddress sin_lclINAddr;
- // IP identification numbers
- int tcp_id;
- tcp_Socket *tcp_allsocs;
- void tcp_Init();
- void tcp_Open(tcp_Socket *s,word lport,in_HwAddress ina,word port,procref datahandler);
- void tcp_Listen(tcp_Socket *s,word port,procref datahandler,longword timeout);
- void tcp_Close(tcp_Socket *s);
- void tcp_Abort(tcp_Socket *s);
- void tcp_Retransmitter();
- void tcp_Unthread(tcp_Socket *ds);
- void tcpapp(procref application);
- int tcp_Write(tcp_Socket *s,byte *dp,int len);
- void tcp_Flush(tcp_Socket *s);
- void tcp_Handler(in_Header *ip);
- void tcp_ProcessData(tcp_Socket *s,tcp_Header *tp,int len);
- void tcp_Send(tcp_Socket *s);
- longword checksum(word *dp,int length);
- void tcp_DumpHeader(in_Header *ip,tcp_Header *tp,char* mesg);
- void Move( byte *src, byte *dest,int numbytes );
- void writelog(CString temp);
- };
- #endif // !defined(AFX_TCP_H__109B7123_703F_11D5_9C82_0010B54D784D__INCLUDED_)