📄 udp.lst
字号:
C51 COMPILER V8.02 UDP 09/21/2006 20:16:33 PAGE 1
C51 COMPILER V8.02, COMPILATION OF MODULE UDP
OBJECT MODULE PLACED IN UDP.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE UDP.c BROWSE DEBUG OBJECTEXTEND
line level source
1 #include <general.h>
2
3 extern xdata union IP_address temp_ip_address; //用于存放临时IP地址
4 extern xdata union Ethernet_address my_ethernet_address; //本机的以太网地址
5 extern xdata union IP_address my_ip_address; //本机的ip地址
6 extern unsigned int frameindex; //IP包的序列号
7 extern xdata union netcard rxdnet;
8 xdata union netcard UDPSend; // 用于UDP发送缓冲区
9
10
11 extern xdata struct wait arpwait; //用于等待ARP.
12
13
14 extern xdata union IP_address sender_ipaddr; //保存发送者的IP地址
15 unsigned int xdata sender_udpport; //保存发送者端口
16 unsigned char idata UDPdebug; //用于调试
17
18 //------------------------------------------------------------------------
19 //函数功能:生成UDP包CRC校验
20 //
21 //入参:发送区指针,UDP包的长度(包括头部)
22 //
23 //
24 //
25 //
26 //作者:
27 //
28 //注意:
29 //
30 //
31 //注释: Mingtree
32 //日期: 2004-11-10
33 //------------------------------------------------------------------------
34 void createudpcrc(union netcard xdata *pTxdnet,unsigned int len)//生成UCP包CRC校验
35 {
36 1 unsigned int result;
37 1
38 1 pTxdnet->ipframe.ttl=0;
39 1 pTxdnet->ipframe.crc=len;
40 1 pTxdnet->ipframe.protocal=0x11;
41 1
42 1 result=checksum(&(pTxdnet->ippacket.ippacket[4]),12+ len);
43 1 pTxdnet->tcpframe.crc=result;
44 1
45 1 }
46 //------------------------------------------------------------------------
47 //函数功能:对UDP头进行校验,错误返回0,正确返回1
48 //
49 //入参: 无
50 //
51 //
52 //
53 //
54 //作者:
55 //
C51 COMPILER V8.02 UDP 09/21/2006 20:16:33 PAGE 2
56 //注意:
57 //
58 //
59 //注释: Mingtree
60 //日期: 2004-11-10
61 //------------------------------------------------------------------------
62 unsigned char verifyudpcrc(union netcard xdata *pRxdnet)//对ucp头进行校验,错误返回0,正确返回1
63 {
64 1 unsigned int crc;
65 1
66 1 pRxdnet->ipframe.ttl=0;
67 1 pRxdnet->ipframe.protocal=0x11;
68 1 //将IP包的16位首部检验和替换成UCP包的长度
69 1 pRxdnet->ipframe.crc=pRxdnet->ipframe.totallength-(pRxdnet->ipframe.verandihl&0x0f)*4;
70 1 crc=checksum(&(pRxdnet->ippacket.ippacket[4]),pRxdnet->ipframe.crc+12);
71 1
72 1 if(crc==0) return (1);
73 1 return(0);
74 1 }
75
76 //------------------------------------------------------------------------
77 //函数功能:发送UDP包(数据包)
78 //
79 //入参:flags:TCP包的标志,index_conn:表示哪个连接发数据,hdr_len:整个TCP的长度
80 //
81 //
82 //
83 //
84 //作者: Mingtree
85 //
86 //注意:
87 //
88 //
89 //注释: Mingtree
90 //日期: 2004-12-5
91 //------------------------------------------------------------------------
92
93 void udp_send(union netcard xdata *pTxdnet,unsigned char xdata * psource,unsigned int len)
94 {
95 1 unsigned int i;
96 1 //头部20字节
97 1 pTxdnet->udpframe.sourceport = UDP_PORT; //源端口默认为1550
98 1 pTxdnet->udpframe.destport = sender_udpport;
99 1 pTxdnet->udpframe.length = len; //注意:有待改进
100 1 pTxdnet->udpframe.crc = 0;
101 1 //计算检验和
102 1 pTxdnet->ipframe.sourceip[0] = my_ip_address.words[0];
103 1 pTxdnet->ipframe.sourceip[1] = my_ip_address.words[1];
104 1 pTxdnet->ipframe.destip[0] = sender_ipaddr.words[0];
105 1 pTxdnet->ipframe.destip[1] = sender_ipaddr.words[1];
106 1
107 1 for(i=0;i<len-8;i++)
108 1 pTxdnet->udpframe.udpdata[i]=psource[i];
109 1
110 1 createudpcrc(pTxdnet,len);
111 1 ip_send(pTxdnet, sender_ipaddr, UDP_TYPE, len);
112 1 }
113
114 void udp_rcve(union netcard xdata *pRxdnet)
115 {
116 1 unsigned int len;
117 1 //compute the len of udp from the ip head.
C51 COMPILER V8.02 UDP 09/21/2006 20:16:33 PAGE 3
118 1 len=pRxdnet->ipframe.totallength-(pRxdnet->ipframe.verandihl&0x0f)*4;
119 1
120 1 // The IP length "len" should be the same as the redundant length
121 1 // udp->length. TCP/IP Illustrated, Vol 2, Sect 23.7 says to use the
122 1 // UDP length, unless IP length < UDP length, in which case the frame
123 1 // should be discarded.
124 1 if (len < pRxdnet->udpframe.length) return;
125 1
126 1 // If the checksum is zero it means that the sender did not compute
127 1 // it and we should not try to check it.
128 1 if (pRxdnet->udpframe.crc == 0)
129 1 {
130 2 if (UDPdebug) PrintStr("UDP: Sender did not compute cksum\r");
131 2 }
132 1 else
133 1 {
134 2 if(verifyudpcrc(pRxdnet))
135 2 {
136 3 if (UDPdebug) PrintStr("UDP: Msg rcvd with good cksum\r");
137 3 // Capture sender's port number and ip_addr
138 3 // to send return message to
139 3 sender_udpport = pRxdnet->udpframe.sourceport;
140 3 sender_ipaddr.words[0]=pRxdnet->ipframe.sourceip[0];
141 3 sender_ipaddr.words[1]=pRxdnet->ipframe.sourceip[1];
142 3
143 3 // See if any applications are on any ports
144 3 switch (pRxdnet->udpframe.destport)
145 3 {
146 4 case ECHO_PORT:
147 4 // Pass it the payload length
148 4 // udp_echo_service(inbuf, udp->length - 8);
149 4 break;
150 4
151 4 default:
152 4 // If no application is registered to handle incoming
153 4 // UDP message then send ICMP destination unreachable
154 4 udp_send(&UDPSend, pRxdnet->udpframe.udpdata,len);
155 4 break;
156 4 }
157 3 }
158 2 else
159 2 {
160 3 if (UDPdebug) PrintStr("UDP: Error, bad cksum\r");
161 3 return;
162 3 }
163 2
164 2 }
165 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 786 ----
CONSTANT SIZE = 89 ----
XDATA SIZE = 1520 ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 14
IDATA SIZE = 1 ----
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 + -