📄 tcp.lst
字号:
C51 COMPILER V7.09 TCP 07/27/2007 15:11:25 PAGE 1
C51 COMPILER V7.09, COMPILATION OF MODULE TCP
OBJECT MODULE PLACED IN TCP.obj
COMPILER INVOKED BY: F:\Keil\C51\BIN\C51.EXE tcp\TCP.C LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.\TCP.lst) OBJECT(TCP.obj)
line level source
1 //-----------------------------------------------------------------------------
2 // Copyright (c) 2002 Jim Brady
3 // Do not use commercially without author's permission
4 // Last revised August 2002
5 // Net TCP.C
6 //
7 // This module handles TCP segments
8 // Refer to RFC 793, 896, 1122, 1323, 2018, 2581
9 //
10 // A "connection" is a unique combination of 4 items: His IP address,
11 // his port number, my IP address, and my port number.
12 //
13 // Note that a SYN and a FIN count as a byte of data, but a RST does
14 // not count. Neither do any of the other flags.
15 // See "TCP/IP Illustrated, Volume 1" Sect 17.3 for info on flags
16 //-----------------------------------------------------------------------------
17
18 #include <stdlib.h>
19 #include <ctype.h> // toupper
20 #include "net.h"
21 #include "cksum.h"
22 #include "serial.h"
23 #include "ip.h"
24 #include "tcp.h"
25 #include "tcpserver.h"
26 #include "utils.h"
27
28
29 // These structures keep track of connection information
30 CONNECTION conxn[5];
31
32 ulong volatile initial_sequence_nr;
33 uint volatile sender_tcpport;
34 static ulong sender_ipaddr;
35 UCHAR just_closed; // Keeps track of when a conxn closed
36
37 extern ulong my_ipaddr;
38 extern UCHAR debug;
39 extern char text[];
40
41 // Options: MSS (4 bytes), NOPS (2 bytes), Selective ACK (2 bytes)
42 UCHAR opt[10] = {
43 0x02, 0x04, 0x05, 0xB4,
44 0x01, 0x01,
45 0x04, 0x02};
46
47
48
49
50 //------------------------------------------------------------------------
51 // Initialize variables declared in this module
52 //
53 //------------------------------------------------------------------------
54 void init_tcp(void)
55 {
C51 COMPILER V7.09 TCP 07/27/2007 15:11:25 PAGE 2
56 1 memset((char*)conxn, 0, sizeof(conxn));
57 1 just_closed = FALSE;
58 1 initial_sequence_nr = 1;
59 1 }
60
61
62
63 //------------------------------------------------------------------------
64 // This sends a TCP segments that do not include any data.
65 // http_send() in the HTTP module is used to send data.
66 // See "TCP/IP Illustrated, Volume 1" Sect 17.3
67 //------------------------------------------------------------------------
68 //输入参数:
69 //flags: sun,fin,rst,ack等标志以及数据偏移等的设置;
70 //hdr_len: TCP报文首部长度
71 //nr: 连接号
72 //功能:发送不包含任何数据段的TCP报文,比如请求连接或者说释放连接;
73 //http_send发送包含数据段的TCP报文.
74 void tcp_send(uint flags, uint hdr_len, UCHAR nr)
75 {
76 1 ulong sum, dest;
77 1 uint result;
78 1 UCHAR * outbuf;
79 1 TCP_HEADER * tcp;
80 1 IP_HEADER * ip;
81 1
82 1 // Allocate memory for entire outgoing message including
83 1 // eth & IP headers
84 1 //TCP层申请发送缓冲区,包括TCP和IP以及Eth首部
85 1 // outbuf = (UCHAR *)malloc(34 + hdr_len); //每次都申请内存,但不释放?
86 1 outbuf = TX_BUFF;
87 1 if (outbuf == NULL)
88 1 {
89 2 // if (debug) serial_send("TCP: Oops, out of memory\n");
90 2 return;
91 2 }
92 1
93 1
94 1 tcp = (TCP_HEADER *)(outbuf + 34); //IP首部20字节
95 1 ip = (IP_HEADER *)(outbuf + 14); //ethernet v2首部14字节
96 1
97 1 // If no connection, then message is probably a reset
98 1 // message which goes back to the sender
99 1 // Otherwise, use information from the connection.
100 1 if (nr == NO_CONNECTION)
101 1 {
102 2 tcp->source_port = HTTP_PORT;
103 2 tcp->dest_port = sender_tcpport;
104 2 tcp->sequence = 0;
105 2 tcp->ack_number = 0;
106 2 dest = sender_ipaddr;
107 2 }
108 1 else if (nr < 5)
109 1 {
110 2 // This message is to connected port
111 2 tcp->source_port = HTTP_PORT; //源端口
112 2 tcp->dest_port = conxn[nr].port; //目的端口
113 2 tcp->sequence = conxn[nr].my_sequence; //序号: 本报文段所发数据的第一个字节的序号
114 2 tcp->ack_number = conxn[nr].his_sequence; //确认号: 期望收到对方的下一个报文段的数据的第一个字节的
-序号
115 2 dest = conxn[nr].ipaddr;
116 2 }
C51 COMPILER V7.09 TCP 07/27/2007 15:11:25 PAGE 3
117 1 else
118 1 {
119 2 // if (debug) serial_send("TCP: Oops, sock nr out of range\n");
120 2 free(outbuf);
121 2 return;
122 2 }
123 1
124 1 // Total segment length = header length
125 1
126 1 // Insert header len
127 1 tcp->flags = (hdr_len << 10) | flags;
128 1 tcp->window = 1024; //窗口: 用来控制对方发送的数据量.
129 1
130 1 tcp->checksum = 0;
131 1 tcp->urgent_ptr = 0; //紧急指针:
132 1
133 1 // Sending SYN with header options
134 1 if (hdr_len == 28)
135 1 {
136 2 memcpy((char*)tcp->options, opt, 8);
137 2 }
138 1
139 1 // Compute checksum including 12 bytes of pseudoheader
140 1 // Must pre-fill 2 items in ip header to do this
141 1 ip->dest_ipaddr = dest;
142 1 ip->source_ipaddr = my_ipaddr;
143 1
144 1 #ifdef __LITTLEENDIAN__
tcp->flags = ntohs(tcp->flags);
tcp->window = ntohs(tcp->window);
tcp->source_port = ntohs(tcp->source_port);
tcp->sequence = ntohl(tcp->sequence);
tcp->dest_port = ntohs(tcp->dest_port);
tcp->ack_number = ntohl(tcp->ack_number);
ip->source_ipaddr = ntohl(my_ipaddr);
#endif
153 1
154 1 // Sum source_ipaddr, dest_ipaddr, and entire TCP message
155 1 sum = (ulong)cksum(outbuf + 26, 8 + hdr_len);
156 1
157 1 // Add in the rest of pseudoheader which is
158 1 // protocol id and TCP segment length
159 1 sum += (ulong)0x0006;
160 1 #ifdef __LITTLEENDIAN__
// hdr_len = ntohs(&hdr_len);
#endif
163 1 sum += (ulong)hdr_len;
164 1
165 1 // In case there was a carry, add it back around
166 1 result = (uint)(sum + (sum >> 16));
167 1 tcp->checksum = ~result;
168 1
169 1 #ifdef __LITTLEENDIAN__
tcp->checksum = ntohs(tcp->checksum);
dest = ntohl(dest);
// hdr_len = ntohs(&hdr_len);
#endif
174 1
175 1 // if (debug) serial_send("TCP: Sending msg to IP layer\n");
176 1
177 1 ip_send(outbuf, dest, TCP_TYPE, hdr_len);
178 1
C51 COMPILER V7.09 TCP 07/27/2007 15:11:25 PAGE 4
179 1 #ifdef __LITTLEENDIAN__
free(outbuf);
#endif
182 1
183 1 // (Re)start TCP retransmit timer
184 1 // conxn[nr].timer = TCP_TIMEOUT; //重发定时器2
185 1 conxn[nr].timer = TCP_TIMEOUT*2;
186 1 }
187
188
189
190
191 //------------------------------------------------------------------------
192 // This runs every 0.5 seconds. If the other end has not ACK'd
193 // everyting we have sent, it re-sends it. To save RAM space, we
194 // regenerate a segment rather than keeping a bunch of segments
195 // hanging around eating up RAM. A connection should not be in an
196 // opening or closing state when this timer expires, so we simply
197 // send a reset.
198 //
199 // If a connection is in the ESTABLISHED state when the timer expires
200 // then we have just sent a web page so re-send the page
201 //------------------------------------------------------------------------
202 void tcp_retransmit(void)
203 {
204 1
205 1 static UCHAR retries = 0;
206 1 UCHAR nr;
207 1
208 1 // Scan through all active connections
209 1 for (nr = 0; nr < 5; nr++)
210 1 {
211 2 if ((conxn[nr].ipaddr != 0) && (conxn[nr].timer))
212 2 {
213 3 // Decrement the timer and see if it hit 0
214 3 conxn[nr].timer--;
215 3 //如果定时器到, 即2秒,
216 3 if (conxn[nr].timer == 0)
217 3 {
218 4 // Socket just timed out. If we are not in ESTABLISHED state
219 4 // something is amiss so send reset and close connection
220 4 //不在Established阶段,发送rst要求,释放连接;
221 4 if (conxn[nr].state != STATE_ESTABLISHED)
222 4 {
223 5 // Send reset and close connection
224 5 // if (debug) serial_send("TCP: Timeout, sending reset\n");
225 5 tcp_send(FLG_RST, 20, nr);
226 5 conxn[nr].ipaddr = 0;
227 5 return;
228 5 }
229 4 else
230 4 {
231 5 // Socket is in ESTABLISHED state. First make sure his
232 5 // ack number is not bogus.
233 5 if (conxn[nr].his_ack > conxn[nr].my_sequence) //如果当前连接的对端确认号大于本端发送号,乱
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -