?? tcp.lst
字號:
C51 COMPILER V8.02 TCP 09/21/2006 20:16:32 PAGE 1
C51 COMPILER V8.02, COMPILATION OF MODULE TCP
OBJECT MODULE PLACED IN TCP.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE TCP.c BROWSE DEBUG OBJECTEXTEND
line level source
1 #include <general.h>
2
3 extern xdata union IP_address temp_ip_address; //用于存放臨時IP地址
4 extern xdata union Ethernet_address my_ethernet_address; //本機的以太網地址
5 extern xdata union IP_address my_ip_address; //本機的ip地址
6 extern xdata union IP_address mask_ip_address;
7 extern xdata union IP_address gateway_ip_address;
8 extern unsigned int frameindex;
9 extern xdata union netcard rxdnet;
10 xdata union netcard TCPSend; // 用于TCP發送緩沖區 //IP包的序列號
11 xdata union IP_address my_ServerIP;
12 //-------------跟超時重發有關的設置------------------------
13
14 TCPBUF xdata Queen[QUEENLEN]; //允許有QUEENLEN個數據包在隊列里
15 unsigned char xdata TCPBuf[BUFLEN]; //緩沖區
16
17 // Options: MSS (4 bytes), NOPS (2 bytes), Selective ACK (2 bytes)
18 unsigned char code opt[10] = {
19 0x02, 0x04, 0x05, 0xB4,
20 0x01, 0x01,
21 0x04, 0x02};
22
23
24 // 最大5個連接
25 CONNECTION xdata conxn[NO_CONNECTION];
26
27 //初始化序號,根據時間在改變
28 unsigned long xdata initial_sequence_nr;
29 unsigned char idata just_closed; // Keeps track of when a conxn closed
30
31
32 xdata struct wait arpwait; //用于等待ARP.
33
34
35 xdata union sw Server_PORT;
36 //#define HTTP_PORT 3330
37
38
39 xdata union IP_address sender_ipaddr; //保存發送者的IP地址
40 unsigned int xdata sender_tcpport; //保存發送者端口
41 unsigned char idata debug; //用于調試
42
43
44 extern unsigned char data WriteBuf; //寫內容
45 extern unsigned char data addr0,addr1; //地址
46 sbit P11=P1^1;
47 //------------------------------------------------------------------------
48 //函數功能:生成TCP包CRC校驗
49 //
50 //入參:發送區指針,TCP包的長度(包括頭部)
51 //
52 //
53 //
54 //
55 //作者:
C51 COMPILER V8.02 TCP 09/21/2006 20:16:32 PAGE 2
56 //
57 //注意:
58 //
59 //
60 //注釋: Mingtree
61 //日期: 2004-11-10
62 //------------------------------------------------------------------------
63 void createtcpcrc(union netcard xdata *pTxdnet,unsigned int len)//生成TCP包CRC校驗
64 {
65 1
66 1 unsigned long sum;
67 1 unsigned int result;
68 1 unsigned int result2;
69 1 unsigned char hdr_len;
70 1
71 1
72 1 //計算檢驗和,包括偽頭部,TCP頭部,數據
73 1 // Compute checksum including 12 bytes of pseudoheader
74 1 // Must pre-fill 2 items in ip header to do this
75 1 //計算TCP頭部長度
76 1 hdr_len=(pTxdnet->tcpframe.offset)>>2;
77 1
78 1
79 1 //-------------------------------------------------------------
80 1 // 算法1
81 1
82 1 // Sum source_ipaddr, dest_ipaddr, and entire TCP message
83 1 sum = (unsigned long)checksum(&(pTxdnet->ippacket.ippacket[6]) , 8 + len);
84 1
85 1 // Add in the rest of pseudoheader which is
86 1 // protocol id and TCP segment length
87 1 sum += (unsigned long)0x0006;
88 1 sum += (unsigned long)len;
89 1
90 1 // In case there was a carry, add it back around
91 1 result2 = (unsigned int)(sum + (sum >> 16));
92 1 // pTxdnet->tcpframe.crc = ~result;
93 1 result2=~result2;
94 1
95 1 //-----------------------------------------------------------
96 1
97 1
98 1 //-----------------------------------------------------------
99 1 // 算法2
100 1
101 1 pTxdnet->ipframe.ttl=0;
102 1 pTxdnet->ipframe.crc=len;
103 1 pTxdnet->ipframe.protocal=0x0006;
104 1
105 1 result=checksum(&(pTxdnet->ippacket.ippacket[4]),12+ len);
106 1 pTxdnet->tcpframe.crc=result;
107 1 //-----------------------------------------------------------
108 1
109 1 }
110 //------------------------------------------------------------------------
111 //函數功能:對tcp頭進行校驗,錯誤返回0,正確返回1
112 //
113 //入參: 無
114 //
115 //
116 //
117 //
C51 COMPILER V8.02 TCP 09/21/2006 20:16:32 PAGE 3
118 //作者:
119 //
120 //注意:
121 //
122 //
123 //注釋: Mingtree
124 //日期: 2004-11-10
125 //------------------------------------------------------------------------
126 unsigned char verifytcpcrc(union netcard xdata *pRxdnet)//對tcp頭進行校驗,錯誤返回0,正確返回1
127 {
128 1 unsigned int crc;
129 1
130 1 pRxdnet->ipframe.ttl=0;
131 1 pRxdnet->ipframe.protocal=0x0006;
132 1 //將IP包的16位首部檢驗和替換成TCP包的長度
133 1 pRxdnet->ipframe.crc=pRxdnet->ipframe.totallength-(pRxdnet->ipframe.verandihl&0x0f)*4;
134 1 crc=checksum(&(pRxdnet->ippacket.ippacket[4]),pRxdnet->ipframe.crc+12);
135 1
136 1 if(crc==0) return (1);
137 1 return(0);
138 1 }
139 //------------------------------------------------------------------------
140 //函數功能:發送IP包
141 //
142 //入參: 發送緩沖區,接收方IP地址,協議類型,低層協議數據包長度(包括頭部)
143 //
144 //
145 //
146 //
147 //作者:
148 //
149 //注意:
150 //
151 //
152 //注釋: Mingtree
153 //日期: 2004-11-29
154 //------------------------------------------------------------------------
155 void ip_send(union netcard xdata *pTxdnet,union IP_address ip, unsigned char proto_id, unsigned int len)
156 {
157 1 xdata union Ethernet_address temp;
158 1 //構建以太網包
159 1 pTxdnet->etherframe.protocal=0x0800;
160 1 pTxdnet->etherframe.uSourceID[0]=my_ethernet_address.words[0];
161 1 pTxdnet->etherframe.uSourceID[1]=my_ethernet_address.words[1];
162 1 pTxdnet->etherframe.uSourceID[2]=my_ethernet_address.words[2];
163 1 pTxdnet->ipframe.verandihl=0x45;
164 1 pTxdnet->ipframe.typeofserver=0x00;
165 1 pTxdnet->ipframe.totallength=20+len;
166 1 pTxdnet->ipframe.ttl=0x80;
167 1 pTxdnet->ipframe.frameindex=frameindex;
168 1 frameindex++;
169 1 pTxdnet->ipframe.segment=0x0000;
170 1 pTxdnet->ipframe.protocal=proto_id;
171 1 pTxdnet->ipframe.crc=0;
172 1 pTxdnet->ipframe.destip[0]=ip.words[0];
173 1 pTxdnet->ipframe.destip[1]=ip.words[1];
174 1 pTxdnet->ipframe.sourceip[0]=my_ip_address.words[0];
175 1 pTxdnet->ipframe.sourceip[1]=my_ip_address.words[1];
176 1 pTxdnet->ipframe.crc=createipheadcrc(pTxdnet);
177 1
178 1 //判斷對方IP是否在同一網段內
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -