📄 udp1.lst
字号:
C51 COMPILER V7.09 UDP1 06/28/2007 09:51:20 PAGE 1
C51 COMPILER V7.09, COMPILATION OF MODULE UDP1
OBJECT MODULE PLACED IN UDP1.obj
COMPILER INVOKED BY: F:\Keil\C51\BIN\C51.EXE tcp\UDP1.C LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.\UDP1.lst) OBJECT(UDP1.ob
-j)
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 "net1.h"
15 #include "ip1.h"
16 #include "cksum.h"
17 #include "serial1.h"
18 #include "icmp1.h"
19 #include "udp1.h"
20 #include "utils.h"
21
22 extern UCHAR debug;
23 extern ulong my_ipaddr;
24 uint sender_udpport;
25 static ulong sender_ipaddr;
26 extern char text[];
27
28
29 //void udp_tftp_service(UCHAR * inbuf, uint len, HANDLE Adapter, ULONG Op);
30 //------------------------------------------------------------------------
31 // UDP Echo service - see RFC 862
32 // This simply echos what it received back to the sender
33 //------------------------------------------------------------------------
34 void udp_echo_service(UCHAR * inbuf, uint len)
35 {
36 1 if (debug)
37 1 {
38 2 //serial_send("ECHO: Nr chars = ");
39 2 // memset(text, 0, 10);
40 2 // itoa(len, text, 10);
41 2 //serial_send(text);
42 2 //serial_send("\n");
43 2 }
44 1 udp_send1(inbuf, ECHO_PORT, len);
45 1 }
46
47
48
49 //------------------------------------------------------------------------
50 // This handles outgoing UDP messages
51 // See "TCP/IP Illustrated, Volume 1" Sect 11.1 - 11.3
52 //------------------------------------------------------------------------
53 void udp_send1(UCHAR * inbuf, uint port, uint len)
54 {
C51 COMPILER V7.09 UDP1 06/28/2007 09:51:20 PAGE 2
55 1 ulong sum;
56 1 uint result;
57 1 UCHAR * outbuf;
58 1 UDP_HEADER * udp;
59 1 IP_HEADER * ip;
60 1
61 1 // Allocate memory for entire outgoing message including
62 1 // eth & IP headers. Total ethernet message length is:
63 1 // 14 byte eth header + 20 byte IP header + 8 byte UDP header
64 1 // + length of this data
65 1 outbuf = (UCHAR *)malloc(42 + len);
66 1 if (outbuf == NULL)
67 1 {
68 2 // if (debug) serial_send("UDP: Oops, out of memory\r");
69 2 return;
70 2 }
71 1
72 1 udp = (UDP_HEADER *)(outbuf + 34);
73 1 ip = (IP_HEADER *)(outbuf + 14);
74 1
75 1 // Direct message back to the senders port.
76 1 udp->dest_port = sender_udpport;
77 1 udp->source_port = port;
78 1 udp->length = 8 + len;
79 1 udp->checksum = 0;
80 1
81 1 // Fill in data
82 1 // Important do not free receive buffer prior to this
83 1 memcpy((char*)udp->msg_data, (inbuf + 42), len);
84 1
85 1
86 1 // Compute checksum including 12 bytes of pseudoheader
87 1 // Must pre-fill 2 items in outbuf to do this
88 1 // Direct message back to senders ip address
89 1 ip->dest_ipaddr = sender_ipaddr;
90 1 ip->source_ipaddr = my_ipaddr;
91 1
92 1 #ifndef __LITTLEENDIAN__
udp->dest_port = ntohs(udp->dest_port);
udp->source_port = ntohs(udp->source_port);
udp->length = ntohs(udp->length);
ip->source_ipaddr = ntohs(ip->source_ipaddr);
#endif
98 1
99 1
100 1 // Sum source_ipaddr, dest_ipaddr, and entire UDP message
101 1 sum = (ulong)cksum(outbuf + 26, 8 + ntohs(udp->length));
102 1
103 1 // Add in the rest of pseudoheader which is
104 1 // zero, protocol id, and UDP length
105 1 sum += (ulong)0x0011;
106 1 sum += (ulong)udp->length;
107 1
108 1 // In case there was a carry, add it back around
109 1 result = (uint)(sum + (sum >> 16));
110 1 udp->checksum = ~result;
111 1 // if (debug) serial_send("UDP: Sending msg to IP layer\r");
112 1 #ifdef __LITTLEENDIAN__
113 1 udp->checksum = ntohs(udp->checksum);
114 1 sender_ipaddr = ntohl(sender_ipaddr);
115 1 #endif
116 1 ip_send1(outbuf, sender_ipaddr, UDP_TYPE, ntohs(udp->length));
C51 COMPILER V7.09 UDP1 06/28/2007 09:51:20 PAGE 3
117 1 #ifdef __LITTLEENDIAN__
118 1 free(outbuf);
119 1 #endif
120 1 }
121
122
123
124 //------------------------------------------------------------------------
125 // This handles incoming UDP messages
126 // See "TCP/IP Illustrated, Volume 1" Sect 11.1 - 11.3
127 //------------------------------------------------------------------------
128 void udp_rcve(UCHAR * inbuf, uint len)
129 {
130 1 uint result;
131 1 UDP_HEADER * udp;
132 1 IP_HEADER * ip;
133 1 ulong sum;
134 1 uint udplen;
135 1
136 1 // Total of eth & IP headers = 34 bytes
137 1 udp = (UDP_HEADER *)(inbuf + 34);
138 1 ip = (IP_HEADER *)(inbuf + 14);
139 1
140 1 // The IP length "len" should be the same as the redundant length
141 1 // udp->length. TCP/IP Illustrated, Vol 2, Sect 23.7 says to use the
142 1 // UDP length, unless IP length < UDP length, in which case the frame
143 1 // should be discarded.
144 1
145 1 udplen = udp->length;
146 1 #ifdef __LITTLEENDIAN__
147 1 udplen = ntohs(udp->length);
148 1 #endif
149 1
150 1 if (len < udplen)
151 1 return;
152 1
153 1 // If the checksum is zero it means that the sender did not compute
154 1 // it and we should not try to check it.
155 1 if (udp->checksum == 0)
156 1 {
157 2 // if (debug) serial_send("UDP: Sender did not compute cksum\r");
158 2 }
159 1 else
160 1 {
161 2 // Compute UDP checksum including 12 byte pseudoheader
162 2 // Sum source_ipaddr, dest_ipaddr, and entire UDP message
163 2 sum = (ulong)cksum(inbuf + 26, 8 + udplen);
164 2
165 2 // Add in the rest of pseudoheader which is
166 2 // zero, protocol id, and UDP length
167 2 sum += (ulong)0x0011;
168 2 sum += (ulong)udplen;
169 2
170 2 // In case there was a carry, add it back around
171 2 result = (uint)(sum + (sum >> 16));
172 2
173 2 if (result != 0xFFFF)
174 2 {
175 3 // if (debug) serial_send("UDP: Error, bad cksum\r");
176 3 return;
177 3 }
178 2
C51 COMPILER V7.09 UDP1 06/28/2007 09:51:20 PAGE 4
179 2 // if (debug) serial_send("UDP: Msg rcvd with good cksum\r");
180 2 }
181 1
182 1 // Capture sender's port number and ip_addr
183 1 // to send return message to
184 1 sender_udpport = udp->source_port;
185 1 sender_ipaddr = ip->source_ipaddr;
186 1
187 1 #ifdef __LITTLEENDIAN__
188 1 udp->dest_port = ntohs(udp->dest_port);
189 1 #endif
190 1 // See if any applications are on any ports
191 1 switch (udp->dest_port)
192 1 {
193 2 case ECHO_PORT:
194 2 // Pass it the payload length
195 2 #ifdef __LITTLEENDIAN__
196 2 udp->dest_port = ntohs(udp->dest_port);
197 2 #endif
198 2 udp_echo_service(inbuf, udplen - 8);
199 2 break;
200 2
201 2 case TFTP_PORT:
202 2 #ifdef __LITTLEENDIAN__
203 2 udp->dest_port = ntohs(udp->dest_port);
204 2 #endif
205 2 // udp_tftp_service(inbuf, udplen - 8);
206 2 break;
207 2
208 2 default:
209 2 // If no application is registered to handle incoming
210 2 // UDP message then send ICMP destination unreachable
211 2 dest_unreach_send(inbuf, ip->source_ipaddr);
212 2 break;
213 2 }
214 1 }
215
216
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1031 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = 4 29
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 + -