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