📄 udp.lst
字号:
C51 COMPILER V6.23a UDP 04/01/2003 10:13:10 PAGE 1
C51 COMPILER V6.23a, COMPILATION OF MODULE UDP
OBJECT MODULE PLACED IN Udp.OBJ
COMPILER INVOKED BY: C:\KEIL\C51\BIN\C51.EXE Udp.c DB OE
stmt 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 #include <string.h>
12 #include <stdlib.h>
13 #include "C8051f.h"
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
21 extern UCHAR idata debug;
22 extern ULONG code my_ipaddr;
23 UINT xdata sender_udpport;
24 static ULONG xdata sender_ipaddr;
25 extern char xdata text[];
26
27
28 //------------------------------------------------------------------------
29 // UDP Echo service - see RFC 862
30 // This simply echos what it received back to the sender
31 //------------------------------------------------------------------------
32 void udp_echo_service(UCHAR xdata * inbuf, UINT len)
33 {
34 1 if (debug)
35 1 {
36 2 serial_send("ECHO: Nr chars = ");
37 2 memset(text, 0, 10);
38 2 itoa(len, text, 10);
39 2 serial_send(text);
40 2 serial_send("\r");
41 2 }
42 1 udp_send(inbuf, ECHO_PORT, len);
43 1 }
44
45
46
47 //------------------------------------------------------------------------
48 // This handles outgoing UDP messages
49 // See "TCP/IP Illustrated, Volume 1" Sect 11.1 - 11.3
50 //------------------------------------------------------------------------
51 void udp_send(UCHAR xdata * inbuf, UINT port, UINT len)
52 {
53 1 ULONG idata sum;
54 1 UINT idata result;
55 1 UCHAR xdata * outbuf;
C51 COMPILER V6.23a UDP 04/01/2003 10:13:10 PAGE 2
56 1 UDP_HEADER xdata * udp;
57 1 IP_HEADER xdata * ip;
58 1
59 1 // Allocate memory for entire outgoing message including
60 1 // eth & IP headers. Total ethernet message length is:
61 1 // 14 byte eth header + 20 byte IP header + 8 byte UDP header
62 1 // + length of this data
63 1 outbuf = (UCHAR xdata *)malloc(42 + len);
64 1 if (outbuf == NULL)
65 1 {
66 2 if (debug) serial_send("UDP: Oops, out of memory\r");
67 2 return;
68 2 }
69 1
70 1 udp = (UDP_HEADER xdata *)(outbuf + 34);
71 1 ip = (IP_HEADER xdata *)(outbuf + 14);
72 1
73 1 // Direct message back to the senders port.
74 1 udp->dest_port = sender_udpport;
75 1 udp->source_port = port;
76 1 udp->length = 8 + len;
77 1 udp->checksum = 0;
78 1
79 1 // Fill in data
80 1 // Important do not free receive buffer prior to this
81 1 memcpy(&udp->msg_data, (inbuf + 42), len);
82 1
83 1
84 1 // Compute checksum including 12 bytes of pseudoheader
85 1 // Must pre-fill 2 items in outbuf to do this
86 1 // Direct message back to senders ip address
87 1 ip->dest_ipaddr = sender_ipaddr;
88 1 ip->source_ipaddr = my_ipaddr;
89 1
90 1
91 1 // Sum source_ipaddr, dest_ipaddr, and entire UDP message
92 1 sum = (ULONG)cksum(outbuf + 26, 8 + udp->length);
93 1
94 1 // Add in the rest of pseudoheader which is
95 1 // zero, protocol id, and UDP length
96 1 sum += (ULONG)0x0011;
97 1 sum += (ULONG)udp->length;
98 1
99 1 // In case there was a carry, add it back around
100 1 result = (UINT)(sum + (sum >> 16));
101 1 udp->checksum = ~result;
102 1 if (debug) serial_send("UDP: Sending msg to IP layer\r");
103 1 ip_send(outbuf, sender_ipaddr, UDP_TYPE, udp->length);
104 1 }
105
106
107
108 //------------------------------------------------------------------------
109 // This handles incoming UDP messages
110 // See "TCP/IP Illustrated, Volume 1" Sect 11.1 - 11.3
111 //------------------------------------------------------------------------
112 void udp_rcve(UCHAR xdata * inbuf, UINT len)
113 {
114 1 UINT idata result;
115 1 UDP_HEADER xdata * udp;
116 1 IP_HEADER xdata * ip;
117 1 ULONG idata sum;
C51 COMPILER V6.23a UDP 04/01/2003 10:13:10 PAGE 3
118 1
119 1 // Total of eth & IP headers = 34 bytes
120 1 udp = (UDP_HEADER xdata *)(inbuf + 34);
121 1 ip = (IP_HEADER xdata *)(inbuf + 14);
122 1
123 1 // The IP length "len" should be the same as the redundant length
124 1 // udp->length. TCP/IP Illustrated, Vol 2, Sect 23.7 says to use the
125 1 // UDP length, unless IP length < UDP length, in which case the frame
126 1 // should be discarded.
127 1 if (len < udp->length) return;
128 1
129 1 // If the checksum is zero it means that the sender did not compute
130 1 // it and we should not try to check it.
131 1 if (udp->checksum == 0)
132 1 {
133 2 if (debug) serial_send("UDP: Sender did not compute cksum\r");
134 2 }
135 1 else
136 1 {
137 2 // Compute UDP checksum including 12 byte pseudoheader
138 2 // Sum source_ipaddr, dest_ipaddr, and entire UDP message
139 2 sum = (ULONG)cksum(inbuf + 26, 8 + udp->length);
140 2
141 2 // Add in the rest of pseudoheader which is
142 2 // zero, protocol id, and UDP length
143 2 sum += (ULONG)0x0011;
144 2 sum += (ULONG)udp->length;
145 2
146 2 // In case there was a carry, add it back around
147 2 result = (UINT)(sum + (sum >> 16));
148 2
149 2 if (result != 0xFFFF)
150 2 {
151 3 if (debug) serial_send("UDP: Error, bad cksum\r");
152 3 return;
153 3 }
154 2
155 2 if (debug) serial_send("UDP: Msg rcvd with good cksum\r");
156 2 }
157 1
158 1 // Capture sender's port number and ip_addr
159 1 // to send return message to
160 1 sender_udpport = udp->source_port;
161 1 sender_ipaddr = ip->source_ipaddr;
162 1
163 1 // See if any applications are on any ports
164 1 switch (udp->dest_port)
165 1 {
166 2 case ECHO_PORT:
167 2 // Pass it the payload length
168 2 udp_echo_service(inbuf, udp->length - 8);
169 2 break;
170 2
171 2 default:
172 2 // If no application is registered to handle incoming
173 2 // UDP message then send ICMP destination unreachable
174 2 dest_unreach_send(inbuf, ip->source_ipaddr);
175 2 break;
176 2 }
177 1 }
178
C51 COMPILER V6.23a UDP 04/01/2003 10:13:10 PAGE 4
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 913 ----
CONSTANT SIZE = 165 ----
XDATA SIZE = 6 ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 22
IDATA SIZE = ---- 12
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 + -