?? tcp.lst
字號:
C51 COMPILER V8.08 TCP 11/04/2008 18:45:34 PAGE 1
C51 COMPILER V8.08, COMPILATION OF MODULE TCP
OBJECT MODULE PLACED IN Tcp.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.exe Tcp.c DB OE
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 "C8051f340.h"
21 #include "global.h"
22 ///#include "net.h"
23 #include "cksum.h"
24 #include "ip.h"
25 #include "http.h"
26 #include "tcp.h"
27 extern char xdata outbuf1[];
28
29 // These structures keep track of connection information
30 CONNECTION xdata conxn[5];
31
32 ULONG idata initial_sequence_nr;
33 UINT xdata sender_tcpport;
34 static ULONG xdata sender_ipaddr;
35 UCHAR idata just_closed; // Keeps track of when a conxn closed
36
37 extern ULONG code my_ipaddr;
38 extern UCHAR idata debug;
39 extern char xdata text[];
40
41 // Options: MSS (4 bytes), NOPS (2 bytes), Selective ACK (2 bytes)
42 UCHAR code opt[10] = {
43 0x02, 0x04, 0x05, 0xB4,
44 0x01, 0x01,
45 0x04, 0x02};
46
47
48
49 //------------------------------------------------------------------------
50 // Initialize variables declared in this module
51 //
52 //------------------------------------------------------------------------
53 void init_tcp(void)
54 {
55 1 memset(conxn, 0, sizeof(conxn));
C51 COMPILER V8.08 TCP 11/04/2008 18:45:34 PAGE 2
56 1 just_closed = FALSE;
57 1 initial_sequence_nr = 1;
58 1 }
59
60
61
62 //------------------------------------------------------------------------
63 // This sends a TCP segments that do not include any data.
64 // http_send() in the HTTP module is used to send data.
65 // See "TCP/IP Illustrated, Volume 1" Sect 17.3
66 //------------------------------------------------------------------------
67 void tcp_send(UINT flags, UINT hdr_len, UCHAR nr)
68 {
69 1 ULONG idata sum, dest;
70 1 UINT idata result;
71 1 UCHAR xdata * outbuf;
72 1 TCP_HEADER xdata * tcp;
73 1 IP_HEADER xdata * ip;
74 1
75 1
76 1 // Allocate memory for entire outgoing message including
77 1 // eth & IP headers
78 1 // outbuf = (UCHAR xdata *)malloc(34 + hdr_len);
79 1 // if (outbuf == NULL)
80 1 //{
81 1 // return;
82 1 //}
83 1 outbuf = outbuf1;
84 1
85 1 tcp = (TCP_HEADER xdata *)(outbuf + 34);
86 1 ip = (IP_HEADER xdata *)(outbuf + 14);
87 1
88 1 // If no connection, then message is probably a reset
89 1 // message which goes back to the sender
90 1 // Otherwise, use information from the connection.
91 1 if (nr == NO_CONNECTION)
92 1 {
93 2 tcp->source_port = HTTP_PORT;
94 2 tcp->dest_port = sender_tcpport;
95 2 tcp->sequence = 0;
96 2 tcp->ack_number = 0;
97 2 dest = sender_ipaddr;
98 2 }
99 1 else if (nr < 5)
100 1 {
101 2 // This message is to connected port
102 2 tcp->source_port = HTTP_PORT;
103 2 tcp->dest_port = conxn[nr].port;
104 2 tcp->sequence = conxn[nr].my_sequence;
105 2 tcp->ack_number = conxn[nr].his_sequence;
106 2 dest = conxn[nr].ipaddr;
107 2 }
108 1 else
109 1 {
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 V8.08 TCP 11/04/2008 18:45:34 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 ip_send(outbuf, dest, TCP_TYPE, hdr_len);
146 1
147 1 // (Re)start TCP retransmit timer
148 1 conxn[nr].timer = TCP_TIMEOUT;
149 1 }
150
151
152
153
154 //------------------------------------------------------------------------
155 // This runs every 0.5 seconds. If the other end has not ACK'd
156 // everyting we have sent, it re-sends it. To save RAM space, we
157 // regenerate a segment rather than keeping a bunch of segments
158 // hanging around eating up RAM. A connection should not be in an
159 // opening or closing state when this timer expires, so we simply
160 // send a reset.
161 //
162 // If a connection is in the ESTABLISHED state when the timer expires
163 // then we have just sent a web page so re-send the page
164 //------------------------------------------------------------------------
165 void tcp_retransmit(void)
166 {
167 1 static UCHAR idata retries = 0;
168 1 UCHAR idata nr;
169 1
170 1 // Scan through all active connections
171 1 for (nr = 0; nr < 5; nr++)
172 1 {
173 2 if ((conxn[nr].ipaddr != 0) && (conxn[nr].timer))
174 2 {
175 3 // Decrement the timer and see if it hit 0
176 3 conxn[nr].timer--;
177 3 if (conxn[nr].timer == 0)
178 3 {
179 4 // Socket just timed out. If we are not in ESTABLISHED state
C51 COMPILER V8.08 TCP 11/04/2008 18:45:34 PAGE 4
180 4 // something is amiss so send reset and close connection
181 4 if (conxn[nr].state != STATE_ESTABLISHED)
182 4 {
183 5 // Send reset and close connection
184 5 tcp_send(FLG_RST, 20, nr);
185 5 conxn[nr].ipaddr = 0;
186 5 return;
187 5 }
188 4 else
189 4 {
190 5 // Socket is in ESTABLISHED state. First make sure his
191 5 // ack number is not bogus.
192 5 if (conxn[nr].his_ack > conxn[nr].my_sequence)
193 5 {
194 6 // Send reset and close connection
195 6 tcp_send(FLG_RST, 20, nr);
196 6 conxn[nr].ipaddr = 0;
197 6 return;
198 6 }
199 5
200 5 // We always increment our sequence number immediately
201 5 // after sending, so the ack number from the other end
202 5 // should be equal to our sequence number. If it is less,
203 5 // it means he lost some of our data.
204 5 if (conxn[nr].his_ack < conxn[nr].my_sequence)
205 5 {
206 6 retries++;
207 6 if (retries <= 2)
208 6 {
209 7 // The only thing we send is a web page, and it looks
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -