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