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