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