www.pudn.com > TcpIpOn8051.rar > TCP.H
/* ***** TCP (Transmission Control Protocol) header ***** */
typedef struct
{
WORD sport, // Source port
dport; // Destination port
DWORD seq, // Sequence number
ack; // Ack number
BYTE hlen, // TCP header len (num of bytes << 2)
flags; // Option flags
WORD window, // Flow control credit (num of bytes)
check, // Checksum
urgent; // Urgent data pointer
}tcpheader;
#define TFIN 0x01 // Option flags: no more data
#define TSYN 0x02 // sync sequence nums
#define TRST 0x04 // reset connection
#define TPUSH 0x08 // push buffered data
#define TACK 0x10 // acknowledgement
#define TURGE 0x20 // urgent
/* ***** TCP packet ('segment') ***** */
typedef struct
{
ipheader i; // IP header
tcpheader t; // TCP header
BYTE tcpdata[MAXDATA]; // Data area (oversized)
}tcppacket;
// TCP definitions for 'Creating TCP/IP from scratch'
// Well-known TCP port numbers
#define echoport 7 // Echo
#define dayport 13 // Daytime
//#define charport 19 // Character generator
#define telport 23 // Telnet remote login
#define httpport 80 // HTTP
#define mineport 1024 // Start of ephemeral (temporary) port numbers
#define maxeport 5000 // Max ephemeral port number
// Dummy values for the segment data length
#define DLEN_NODATA -1 // Segment received, but no data
#define CBUFFLEN 0x400
typedef struct
{
WORD len; // Length of data (must be first)
DWORD in; // Incoming data
DWORD out; // Outgoing data
DWORD trial; // Outgoing data 'on trial'
//send data buffer para
BYTE yesornot; //flag of send data or not,tx and rx
BYTE flags; //tcp flags,only use in tx
DWORD seqnum; //seq number,tx
DWORD acknum; //acknumber,tx
WORD spoint; //the start addr of send data
WORD dlen; //length of data to send,tx and rx
//data
BYTE tdata[CBUFFLEN]; // Buffer
} CBUFF;
typedef enum {
// Passive open & close
TCP_CLOSED, // Closed
TCP_SYNR, // SYN recvd: send SYN ACK
TCP_EST, // Established: connection OK
TCP_CLWAIT, // Close wait: FIN recvd, send data then FIN ACK
TCP_LASTACK, // Last ACK: waiting for ACK of my FIN
TCP_RSTR, // Reset received
// Active open and close
TCP_AOPEN,
TCP_SYNS, // SYN sent: awaiting SYN ACK
// Established: same as for passive open
TCP_ACLOSE, // Active close: send FIN
TCP_FINWT1, // FIN wait 1: FIN sent, waiting for ACK
TCP_FINWT2, // FIN wait 2: receiving data, waiting for FIN
TCP_CLING, // Closing: awaiting final ACK
TCP_TWAIT // Timed wait
} TCP_STATE;
// Simplified connection states for applications
typedef enum {
TCP_IDLE, // Idle
TCP_OPEN, // Opening connection
TCP_CONN, // Connected
TCP_DATA, // Connected, data received
TCP_NODATA, // Connected, no data received
TCP_CLOSE // Closing connection
} CONN_STATE;
// Storage structure for a single TCP socket
// The positions of the first 3 items are fixed to simplify initialisation
typedef struct {
WORD index; // Index number - must be first
CBUFF rxbuf; // Receive & transmit circular buffers
CBUFF txbuf; // (must be 2nd and 3rd)
ip remip; // remote ip
WORD locport; //local port
WORD remport; //remote port
TCP_STATE state; // Current connection state
WORD server; // Flag to identify server socket
DWORD time; // Time at last state change (msec)
WORD timeout, // Timeout value (0=no timeout)
retries; // Number of retries left
WORD txmss, // Max seg data size for my transmit
rxwin, // Current Rx & Tx window sizes
txwin;
DWORD rxseq, // Seq & ack values in latest Rx segment
rxack;
BYTE txflags, // Latest Tx flags
connflags; // Extra Tx connection flags (push/urgent)
WORD txdlen; // Latest transmit data length
void *app; // Pointer to application-specific data
} TCP_SOCK;
#define nsocks 2
// Telnet option byte values (subset)
#define TEL_IAC 255 // Interpret As Command
#define TEL_DONT 254 // Don't do it
#define TEL_DO 253 // Do it
#define TEL_WONT 252 // Won't do it
#define TEL_WILL 251 // Will do it
#define TEL_ECHO 1 // Echo option
#define TEL_SGA 3 // Suppress Go-Ahead option
#define TEL_AUTH 37 // Authentication option