📄 tcp.lst
字号:
C51 COMPILER V7.06 TCP 05/19/2004 15:18:11 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE TCP
OBJECT MODULE PLACED IN TCP.OBJ
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE TCP.C LARGE BROWSE DEBUG OBJECTEXTEND
stmt level source
1 //-----------------------------------------------------------------------------
2 // Net TCP.C
3 //
4 // This module handles TCP segments
5 // Refer to RFC 793, 896, 1122, 1323, 2018, 2581
6 //
7 // A "connection" is a unique combination of 4 items: His IP address,
8 // his port number, my IP address, and my port number.
9 //
10 // Note that a SYN and a FIN count as a byte of data, but a RST does
11 // not count. Neither do any of the other flags.
12 // See "TCP/IP Illustrated, Volume 1" Sect 17.3 for info on flags
13 //-----------------------------------------------------------------------------
14 #include <string.h>
15 #include <stdlib.h>
16 #include <ctype.h> // toupper
17 #include <reg52.h>
18 #include <stdio.h>
19 #include "net.h"
20 #include "cksum.h"
21 #include "serial.h"
22 #include "ip.h"
23 #include "http.h"
24 #include "tcp.h"
25
26
27 // These structures keep track of connection information
28 CONNECTION xdata conxn[5];
29
30 ULONG idata initial_sequence_nr;
31 UINT xdata sender_tcpport;
32 static ULONG xdata sender_ipaddr;
33 UCHAR idata just_closed; // Keeps track of when a conxn closed
34
35 extern ULONG code my_ipaddr;
36 extern UCHAR idata debug;
37 extern char xdata text[];
38
39 // Options: MSS (4 bytes), NOPS (2 bytes), Selective ACK (2 bytes)
40 UCHAR code opt[10] = {
41 0x02, 0x04, 0x05, 0xB4,
42 0x01, 0x01,
43 0x04, 0x02};
44
45
46
47 //------------------------------------------------------------------------
48 // Initialize variables declared in this module
49 //
50 //------------------------------------------------------------------------
51 void init_tcp(void)
52 {
53 1 memset(conxn, 0, sizeof(conxn));
54 1 just_closed = FALSE;
55 1 initial_sequence_nr = 1;
C51 COMPILER V7.06 TCP 05/19/2004 15:18:11 PAGE 2
56 1 }
57
58
59
60 //------------------------------------------------------------------------
61 // This sends a TCP segments that do not include any data.
62 // http_send() in the HTTP module is used to send data.
63 // See "TCP/IP Illustrated, Volume 1" Sect 17.3
64 //------------------------------------------------------------------------
65 void tcp_send(UINT flags, UINT hdr_len, UCHAR nr)
66 {
67 1 ULONG idata sum, dest;
68 1 UINT idata result;
69 1 UCHAR xdata * outbuf;
70 1 TCP_HEADER xdata * tcp;
71 1 IP_HEADER xdata * ip;
72 1
73 1
74 1 // Allocate memory for entire outgoing message including
75 1 // eth & IP headers
76 1 outbuf = (UCHAR xdata *)malloc(34 + hdr_len);
77 1 if (outbuf == NULL)
78 1 {
79 2 if (debug) printf("TCP: Oops, out of memory\n");
80 2 return;
81 2 }
82 1
83 1
84 1 tcp = (TCP_HEADER xdata *)(outbuf + 34);
85 1 ip = (IP_HEADER xdata *)(outbuf + 14);
86 1
87 1 // If no connection, then message is probably a reset
88 1 // message which goes back to the sender
89 1 // Otherwise, use information from the connection.
90 1 if (nr == NO_CONNECTION)
91 1 {
92 2 tcp->source_port = HTTP_PORT;
93 2 tcp->dest_port = sender_tcpport;
94 2 tcp->sequence = 0;
95 2 tcp->ack_number = 0;
96 2 dest = sender_ipaddr;
97 2 }
98 1 else if (nr < 5)
99 1 {
100 2 // This message is to connected port
101 2 tcp->source_port = HTTP_PORT;
102 2 tcp->dest_port = conxn[nr].port;
103 2 tcp->sequence = conxn[nr].my_sequence;
104 2 tcp->ack_number = conxn[nr].his_sequence;
105 2 dest = conxn[nr].ipaddr;
106 2 }
107 1 else
108 1 {
109 2 if (debug) printf("TCP: Oops, sock nr out of range\n");
110 2 free(outbuf);
111 2 return;
112 2 }
113 1
114 1 // Total segment length = header length
115 1
116 1 // Insert header len
117 1 tcp->flags = (hdr_len << 10) | flags;
C51 COMPILER V7.06 TCP 05/19/2004 15:18:11 PAGE 3
118 1 tcp->window = 1024;
119 1 tcp->checksum = 0;
120 1 tcp->urgent_ptr = 0;
121 1
122 1 // Sending SYN with header options
123 1 if (hdr_len == 28)
124 1 {
125 2 memcpy(&tcp->options, opt, 8);
126 2 }
127 1
128 1 // Compute checksum including 12 bytes of pseudoheader
129 1 // Must pre-fill 2 items in ip header to do this
130 1 ip->dest_ipaddr = dest;
131 1 ip->source_ipaddr = my_ipaddr;
132 1
133 1 // Sum source_ipaddr, dest_ipaddr, and entire TCP message
134 1 sum = (ULONG)cksum(outbuf + 26, 8 + hdr_len);
135 1
136 1 // Add in the rest of pseudoheader which is
137 1 // protocol id and TCP segment length
138 1 sum += (ULONG)0x0006;
139 1 sum += (ULONG)hdr_len;
140 1
141 1 // In case there was a carry, add it back around
142 1 result = (UINT)(sum + (sum >> 16));
143 1 tcp->checksum = ~result;
144 1
145 1 if (debug) printf("TCP: Sending msg to IP layer\n");
146 1 ip_send(outbuf, dest, TCP_TYPE, hdr_len);
147 1
148 1 // (Re)start TCP retransmit timer
149 1 conxn[nr].timer = TCP_TIMEOUT;
150 1 }
151
152
153
154
155 //------------------------------------------------------------------------
156 // This runs every 0.5 seconds. If the other end has not ACK'd
157 // everyting we have sent, it re-sends it. To save RAM space, we
158 // regenerate a segment rather than keeping a bunch of segments
159 // hanging around eating up RAM. A connection should not be in an
160 // opening or closing state when this timer expires, so we simply
161 // send a reset.
162 //
163 // If a connection is in the ESTABLISHED state when the timer expires
164 // then we have just sent a web page so re-send the page
165 //------------------------------------------------------------------------
166 void tcp_retransmit(void)
167 {
168 1 static UCHAR idata retries = 0;
169 1 UCHAR idata nr;
170 1
171 1 // Scan through all active connections
172 1 for (nr = 0; nr < 5; nr++)
173 1 {
174 2 if ((conxn[nr].ipaddr != 0) && (conxn[nr].timer))
175 2 {
176 3 // Decrement the timer and see if it hit 0
177 3 conxn[nr].timer--;
178 3 if (conxn[nr].timer == 0)
179 3 {
C51 COMPILER V7.06 TCP 05/19/2004 15:18:11 PAGE 4
180 4 // Socket just timed out. If we are not in ESTABLISHED state
181 4 // something is amiss so send reset and close connection
182 4 if (conxn[nr].state != STATE_ESTABLISHED)
183 4 {
184 5 // Send reset and close connection
185 5 if (debug) printf("TCP: Timeout, sending reset\n");
186 5 tcp_send(FLG_RST, 20, nr);
187 5 conxn[nr].ipaddr = 0;
188 5 return;
189 5 }
190 4 else
191 4 {
192 5 // Socket is in ESTABLISHED state. First make sure his
193 5 // ack number is not bogus.
194 5 if (conxn[nr].his_ack > conxn[nr].my_sequence)
195 5 {
196 6 // Send reset and close connection
197 6 if (debug) printf("TCP: Timeout, sending reset\n");
198 6 tcp_send(FLG_RST, 20, nr);
199 6 conxn[nr].ipaddr = 0;
200 6 return;
201 6 }
202 5
203 5 // We always increment our sequence number immediately
204 5 // after sending, so the ack number from the other end
205 5 // should be equal to our sequence number. If it is less,
206 5 // it means he lost some of our data.
207 5 if (conxn[nr].his_ack < conxn[nr].my_sequence)
208 5 {
209 6 retries++;
210 6 if (retries <= 2)
211 6 {
212 7 // The only thing we send is a web page, and it looks
213 7 // like other end did not get it, so resend
214 7 // but do not increase my sequence number
215 7 if (debug) printf("TCP: Timeout, resending data\n");
216 7 http_server(conxn[nr].query, 0, nr, 1);
217 7 conxn[nr].inactivity = INACTIVITY_TIME;
218 7 }
219 6 else
220 6 {
221 7 if (debug) printf("TCP: Giving up, sending reset\n");
222 7 // Send reset and close connection
223 7 tcp_send(FLG_RST, 20, nr);
224 7 conxn[nr].ipaddr = 0;
225 7 }
226 6 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -