📄 udp.lst
字号:
C51 COMPILER V7.06 UDP 06/26/2004 13:41:26 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE UDP
OBJECT MODULE PLACED IN UDP.OBJ
COMPILER INVOKED BY: C:\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 <reg89C58.h>
11 #include <stdio.h>
12 #include "net.h"
13 #include "ip.h"
14 #include "cksum.h"
15 #include "udp.h"
16 #include "24c04.h"
17
18
19 extern UCHAR idata debug;
20 extern ULONG data my_ipaddr;
21 extern bit udpst;
22 extern UCHAR data sdbuf[8];
23 extern bit RSS;
24
25
26
27 UINT xdata sender_udpport;
28
29 static ULONG xdata sender_ipaddr;
30 extern char xdata text[];
31
32 void SD_RS(UCHAR x);
33 void fu_key(UCHAR kk);
34 void delay(UINT i);
35 //extern UCHAR wr[3];
36 //extern UCHAR rd[3];
37 //extern UCHAR data temp0[5];
38 //extern UCHAR data temp1[5];
39
40
41 //------------------------------------------------------------------------
42 // UDP Echo service - see RFC 862
43 // This simply echos what it received back to the sender
44 //------------------------------------------------------------------------
45 void udp_echo_service(UCHAR xdata * inbuf, UINT len)
46 {
47 1
48 1 if (debug)
49 1 {
50 2 // printf("ECHO: Nr chars = ");
51 2 memset(text, 0, 10);
52 2 itoa(len, text, 10);
53 2 //printf(text);
54 2 //printf("\n");
55 2 }
C51 COMPILER V7.06 UDP 06/26/2004 13:41:26 PAGE 2
56 1
57 1 udp_send(inbuf, ECHO_PORT, len);
58 1 }
59
60
61
62 //------------------------------------------------------------------------
63 // This handles outgoing UDP messages
64 // See "TCP/IP Illustrated, Volume 1" Sect 11.1 - 11.3
65 //------------------------------------------------------------------------
66 void udp_send(UCHAR xdata * inbuf, UINT port, UINT len)
67 {
68 1 ULONG idata sum;
69 1 UINT idata result;
70 1 UCHAR xdata * outbuf;
71 1 UDP_HEADER xdata * udp;
72 1 IP_HEADER xdata * ip;
73 1
74 1 // Allocate memory for entire outgoing message including
75 1 // eth & IP headers. Total ethernet message length is:
76 1 // 14 byte eth header + 20 byte IP header + 8 byte UDP header
77 1 // + length of this data
78 1 outbuf = (UCHAR xdata *)malloc(42 + len);
79 1 if (outbuf == NULL)
80 1 {
81 2 // if (debug) printf("UDP: Oops, out of memory\n");
82 2 return;
83 2 }
84 1
85 1 udp = (UDP_HEADER xdata *)(outbuf + 34);
86 1 ip = (IP_HEADER xdata *)(outbuf + 14);
87 1
88 1 // Direct message back to the senders port.
89 1 udp->dest_port = sender_udpport;
90 1 udp->source_port = port;
91 1 udp->length = 8 + len;
92 1 udp->checksum = 0;
93 1
94 1 // Fill in data
95 1 // Important do not free receive buffer prior to this
96 1 memcpy(&udp->msg_data, (inbuf + 42), len);
97 1
98 1
99 1 // Compute checksum including 12 bytes of pseudoheader
100 1 // Must pre-fill 2 items in outbuf to do this
101 1 // Direct message back to senders ip address
102 1 ip->dest_ipaddr = sender_ipaddr;
103 1 ip->source_ipaddr = my_ipaddr;
104 1
105 1
106 1 // Sum source_ipaddr, dest_ipaddr, and entire UDP message
107 1 sum = (ULONG)cksum(outbuf + 26, 8 + udp->length);
108 1
109 1 // Add in the rest of pseudoheader which is
110 1 // zero, protocol id, and UDP length
111 1 sum += (ULONG)0x0011;
112 1 sum += (ULONG)udp->length;
113 1
114 1 // In case there was a carry, add it back around
115 1 result = (UINT)(sum + (sum >> 16));
116 1 udp->checksum = ~result;
117 1 // if (debug) printf("UDP: Sending msg to IP layer\n");
C51 COMPILER V7.06 UDP 06/26/2004 13:41:26 PAGE 3
118 1 ip_send(outbuf, sender_ipaddr, UDP_TYPE, udp->length);
119 1 }
120
121 void udp_qhysend(UCHAR data * sendbuf, UINT hwsource_port,UINT hwremote_port,ULONG hwremote_ipadd, UINT l
-en)
122 {
123 1 ULONG idata sum;
124 1 UINT idata result;
125 1 UCHAR xdata * outbuf;
126 1 UDP_HEADER xdata * udp;
127 1 IP_HEADER xdata * ip;
128 1
129 1 // Allocate memory for entire outgoing message including
130 1 // eth & IP headers. Total ethernet message length is:
131 1 // 14 byte eth header + 20 byte IP header + 8 byte UDP header
132 1 // + length of this data
133 1 outbuf = (UCHAR xdata *)malloc(42 + len);
134 1 if (outbuf == NULL)
135 1 { return;
136 2 }
137 1
138 1 udp = (UDP_HEADER xdata *)(outbuf + 34);
139 1 ip = (IP_HEADER xdata *)(outbuf + 14);
140 1
141 1 // Direct message back to the senders port.
142 1 udp->dest_port = hwremote_port;
143 1 udp->source_port = hwsource_port;
144 1 udp->length = 8 + len;
145 1 udp->checksum = 0;
146 1
147 1 // Fill in data
148 1 // Important do not free receive buffer prior to this
149 1 memcpy(&udp->msg_data, sendbuf, len);
150 1
151 1
152 1 // Compute checksum including 12 bytes of pseudoheader
153 1 // Must pre-fill 2 items in outbuf to do this
154 1 // Direct message back to senders ip address
155 1 ip->dest_ipaddr = hwremote_ipadd;
156 1 ip->source_ipaddr = my_ipaddr;
157 1
158 1
159 1 // Sum source_ipaddr, dest_ipaddr, and entire UDP message
160 1 sum = (ULONG)cksum(outbuf + 26, 8 + udp->length);
161 1
162 1 // Add in the rest of pseudoheader which is
163 1 // zero, protocol id, and UDP length
164 1 sum += (ULONG)0x0011;
165 1 sum += (ULONG)udp->length;
166 1
167 1 // In case there was a carry, add it back around
168 1 result = (UINT)(sum + (sum >> 16));
169 1 udp->checksum = ~result;
170 1 // if (debug) printf("UDP: Sending msg to IP layer\n");
171 1 // ip_send(outbuf, sender_ipaddr, UDP_TYPE, udp->length);
172 1 ip_send(outbuf, ip->dest_ipaddr, UDP_TYPE, udp->length);
173 1 }
174
175 void sysconfig_udp_send(UINT port, UINT len)
176 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -