?? udp.lst
字號:
C51 COMPILER V7.09 UDP 07/27/2007 15:55:59 PAGE 1
C51 COMPILER V7.09, COMPILATION OF MODULE UDP
OBJECT MODULE PLACED IN UDP.obj
COMPILER INVOKED BY: F:\Keil\C51\BIN\C51.EXE tcp\UDP.C LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.\UDP.lst) OBJECT(UDP.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 UDP.C
6 //
7 // This module handles UDP messages
8 // Refer to RFC 768, 1122
9 // Also RFC 862 echo, RFC 867 daytime, and RFC 868 time
10 //-----------------------------------------------------------------------------
11
12 #include <stdlib.h>
13
14 #include "net.h"
15 #include "ip.h"
16 #include "cksum.h"
17 #include "serial.h"
18 #include "icmp.h"
19 #include "udp.h"
20 #include "utils.h"
21 #include "tftp.h"
22
23 extern UCHAR debug;
24 extern ulong my_ipaddr;
25 uint sender_udpport;
26 static ulong sender_ipaddr;
27 extern char text[];
28 extern unsigned char TFTP_BUF[];
29 ulong broadcast_ip = 0xFFFFFFFF;
30 //void udp_tftp_service(UCHAR * inbuf, uint len, HANDLE Adapter, ULONG Op);
31 //------------------------------------------------------------------------
32 // UDP Echo service - see RFC 862
33 // This simply echos what it received back to the sender
34 //------------------------------------------------------------------------
35 void udp_echo_service(UCHAR * inbuf, uint len)
36 {
37 1 if (debug)
38 1 {
39 2 //serial_send("ECHO: Nr chars = ");
40 2 // memset(text, 0, 10);
41 2 // itoa(len, text, 10);
42 2 //serial_send(text);
43 2 //serial_send("\n");
44 2 }
45 1 udp_send(inbuf, ECHO_PORT, len);
46 1 }
47
48
49
50 //------------------------------------------------------------------------
51 // This handles outgoing UDP messages
52 // See "TCP/IP Illustrated, Volume 1" Sect 11.1 - 11.3
53 //------------------------------------------------------------------------
54 void udp_send(UCHAR * inbuf, uint port, uint len)
55 {
C51 COMPILER V7.09 UDP 07/27/2007 15:55:59 PAGE 2
56 1 ulong sum;
57 1 uint result;
58 1 UCHAR * outbuf;
59 1 UDP_HEADER * udp;
60 1 IP_HEADER * ip;
61 1 int udplen = 0;
62 1
63 1 // Allocate memory for entire outgoing message including
64 1 // eth & IP headers. Total ethernet message length is:
65 1 // 14 byte eth header + 20 byte IP header + 8 byte UDP header
66 1 // + length of this data
67 1 // outbuf = (UCHAR *)malloc(42 + len);
68 1 outbuf = inbuf;
69 1 if (outbuf == NULL)
70 1 {
71 2 return;
72 2 }
73 1
74 1 udp = (UDP_HEADER *)(outbuf + 34);
75 1 ip = (IP_HEADER *)(outbuf + 14);
76 1
77 1 // Direct message back to the senders port.
78 1 udp->dest_port = sender_udpport;
79 1 udp->source_port = port;
80 1 udp->length = 8 + len;
81 1 udp->checksum = 0;
82 1
83 1 // Fill in data
84 1 // Important do not free receive buffer prior to this
85 1 memcpy((char*)udp->msg_data, (inbuf + 42), len);
86 1
87 1
88 1 // Compute checksum including 12 bytes of pseudoheader
89 1 // Must pre-fill 2 items in outbuf to do this
90 1 // Direct message back to senders ip address
91 1 ip->dest_ipaddr = sender_ipaddr;
92 1 ip->source_ipaddr = my_ipaddr;
93 1
94 1 #ifdef __LITTLEENDIAN__
udp->dest_port = ntohs(udp->dest_port);
udp->source_port = ntohs(udp->source_port);
udplen = udp->length;
udp->length = ntohs(udp->length);
ip->source_ipaddr = ntohs(ip->source_ipaddr);
#endif
102 1
103 1
104 1 // Sum source_ipaddr, dest_ipaddr, and entire UDP message
105 1
106 1 sum = (ulong)cksum(outbuf + 26, 8 + udplen);
107 1
108 1 // Add in the rest of pseudoheader which is
109 1 // zero, protocol id, and UDP length
110 1 sum += (ulong)0x0011;
111 1 sum += (ulong)udplen;
112 1
113 1 // In case there was a carry, add it back around
114 1 result = (uint)(sum + (sum >> 16));
115 1 udp->checksum = ~result;
116 1
117 1 udp->checksum = 0;
C51 COMPILER V7.09 UDP 07/27/2007 15:55:59 PAGE 3
118 1 // if (debug) serial_send("UDP: Sending msg to IP layer\r");
119 1 #ifdef __LITTLEENDIAN__
udp->checksum = ntohs(udp->checksum);
sender_ipaddr = ntohl(sender_ipaddr);
udp->length = ntohs(udp->length);
#endif
124 1
125 1 ip_send(outbuf, sender_ipaddr, UDP_TYPE, udp->length);
126 1
127 1 #ifdef __LITTLEENDIAN__
free(outbuf);
#endif
130 1 }
131
132
133
134 //------------------------------------------------------------------------
135 // This handles incoming UDP messages
136 // See "TCP/IP Illustrated, Volume 1" Sect 11.1 - 11.3
137 //------------------------------------------------------------------------
138 void udp_rcve(UCHAR * inbuf, uint len)
139 {
140 1 uint result;
141 1 UDP_HEADER * udp;
142 1 IP_HEADER * ip;
143 1 ulong sum;
144 1 uint udplen;
145 1
146 1 // Total of eth & IP headers = 34 bytes
147 1 udp = (UDP_HEADER *)(inbuf + 34);
148 1 ip = (IP_HEADER *)(inbuf + 14);
149 1
150 1 // The IP length "len" should be the same as the redundant length
151 1 // udp->length. TCP/IP Illustrated, Vol 2, Sect 23.7 says to use the
152 1 // UDP length, unless IP length < UDP length, in which case the frame
153 1 // should be discarded.
154 1 #ifdef __LITTLEENDIAN__
udp->length = ntohs(udp->length);
#endif
157 1 udplen = udp->length;
158 1 if (len < udplen)
159 1 return;
160 1
161 1 // If the checksum is zero it means that the sender did not compute
162 1 // it and we should not try to check it.
163 1 if (udp->checksum == 0)
164 1 {
165 2 // if (debug) serial_send("UDP: Sender did not compute cksum\r");
166 2 }
167 1 else
168 1 {
169 2 // Compute UDP checksum including 12 byte pseudoheader
170 2 // Sum source_ipaddr, dest_ipaddr, and entire UDP message
171 2 sum = (ulong)cksum(inbuf + 26, 8 + udplen);
172 2
173 2 // Add in the rest of pseudoheader which is
174 2 // zero, protocol id, and UDP length
175 2 sum += (ulong)0x0011;
176 2 sum += (ulong)udplen;
177 2
178 2 // In case there was a carry, add it back around
179 2 result = (uint)(sum + (sum >> 16));
C51 COMPILER V7.09 UDP 07/27/2007 15:55:59 PAGE 4
180 2
181 2 if (result != 0xFFFF)
182 2 {
183 3 // if (debug) serial_send("UDP: Error, bad cksum\r");
184 3 result = 0;
185 3 return;
186 3 }
187 2
188 2 // if (debug) serial_send("UDP: Msg rcvd with good cksum\r");
189 2 }
190 1
191 1 // Capture sender's port number and ip_addr
192 1 // to send return message to
193 1 sender_udpport = udp->source_port;
194 1 sender_ipaddr = ip->source_ipaddr;
195 1
196 1 #ifdef __LITTLEENDIAN__
udp->dest_port = ntohs(udp->dest_port);
#endif
199 1 // See if any applications are on any ports
200 1 switch (udp->dest_port)
201 1 {
202 2 case ECHO_PORT:
203 2 // Pass it the payload length
204 2 #ifdef __LITTLEENDIAN__
udp->dest_port = ntohs(udp->dest_port);
#endif
207 2 udp_echo_service(inbuf, udplen - 8);
208 2 break;
209 2
210 2 case TFTP_PORT:
211 2 #ifdef __LITTLEENDIAN__
udp->dest_port = ntohs(udp->dest_port);
#endif
214 2 tftp_rcv_packet(inbuf, udplen);
215 2 break;
216 2
217 2 default:
218 2 // If no application is registered to handle incoming
219 2 // UDP message then send ICMP destination unreachable
220 2 dest_unreach_send(inbuf, ip->source_ipaddr);
221 2 break;
222 2 }
223 1 }
224
225 void udp_broadcast(UCHAR * inbuf, uint port, uint len)
226 {
227 1
228 1 UCHAR * outbuf;
229 1 UDP_HEADER * udp;
230 1 IP_HEADER * ip;
231 1 int udplen = 0;
232 1 char test[]={'a', 'b', 'c'};
233 1
234 1 outbuf = inbuf;
235 1 if (outbuf == NULL)
236 1 {
237 2 return;
238 2 }
239 1
240 1 udp = (UDP_HEADER *)(outbuf + 34);
241 1 ip = (IP_HEADER *)(outbuf + 14);
C51 COMPILER V7.09 UDP 07/27/2007 15:55:59 PAGE 5
242 1
243 1 // Direct message back to the senders port.
244 1 udp->dest_port = port;
245 1 udp->source_port = port;
246 1 udp->length = 8 + len;
247 1 udp->checksum = 0;
248 1
249 1 // Fill in data
250 1 // Important do not free receive buffer prior to this
251 1 memcpy((UCHAR *)udp->msg_data, &test[0], 3);
252 1
253 1 // Compute checksum including 12 bytes of pseudoheader
254 1 // Must pre-fill 2 items in outbuf to do this
255 1 // Direct message back to senders ip address
256 1 ip->dest_ipaddr = sender_ipaddr;
257 1 ip->source_ipaddr = my_ipaddr;
258 1
259 1 #ifdef __LITTLEENDIAN__
udp->dest_port = ntohs(udp->dest_port);
udp->source_port = ntohs(udp->source_port);
udplen = udp->length;
udp->length = ntohs(udp->length);
ip->source_ipaddr = ntohs(ip->source_ipaddr);
#endif
267 1
268 1 #ifdef __LITTLEENDIAN__
udp->checksum = ntohs(udp->checksum);
sender_ipaddr = ntohl(sender_ipaddr);
udp->length = ntohs(udp->length);
#endif
273 1
274 1 ip_send(outbuf, broadcast_ip, UDP_TYPE, udp->length);
275 1
276 1 #ifdef __LITTLEENDIAN__
free(outbuf);
#endif
279 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1337 ----
CONSTANT SIZE = 3 ----
XDATA SIZE = 10 58
PDATA SIZE = ---- ----
DATA SIZE = ---- ----
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -