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