📄 icmp.lst
字号:
C51 COMPILER V7.06 ICMP 10/09/2006 21:51:55 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE ICMP
OBJECT MODULE PLACED IN icmp.OBJ
COMPILER INVOKED BY: D:\Program Files\Keil\C51\BIN\C51.EXE icmp.c BROWSE DEBUG OBJECTEXTEND
stmt level source
1 //-----------------------------------------------------------------------------
2 // Net ICMP.C
3 //
4 // This module handles ICMP messages
5 // Refer to RFC 792, 896, 950, 1122, and 1191
6 //-----------------------------------------------------------------------------
7 #include <general.h>
8
9
10 //------------------------------------------------------------------------
11 // This builds a ping response message. It allocates memory for the
12 // entire outgoing message, including Eth and IP headers. See "TCP/IP
13 // Illustrated, Volume 1" Sect 7.2 for info on Ping messages
14 //------------------------------------------------------------------------
15 void ping_send(UCHAR xdata * inbuf, ULONG ipaddr, UINT len)
16 {
17 1 PING_HEADER xdata * ping_in;
18 1 PING_HEADER xdata * ping_out;
19 1 UCHAR xdata * outbuf;
20 1
21 1 ping_in = (PING_HEADER xdata *)(inbuf + 34);
22 1
23 1 // Allocate memory for entire outgoing message
24 1 outbuf = (UCHAR xdata *)malloc(len + 34);
25 1 if (outbuf == NULL)
26 1 {
27 2 return;
28 2 }
29 1
30 1 // Ping response message payload starts at offset 34
31 1 ping_out = (PING_HEADER xdata *)(outbuf + 34);
32 1
33 1 ping_out->msg_type = 0;
34 1 ping_out->msg_code = 0;
35 1 ping_out->checksum = 0;
36 1 ping_out->identifier = ping_in->identifier;
37 1 ping_out->sequence = ping_in->sequence;
38 1
39 1 memcpy(&ping_out->echo_data, &ping_in->echo_data, len - 8);
40 1
41 1 // Compute checksum over the ICMP header plus
42 1 // optional data and insert complement
43 1 ping_out->checksum = ~cksum(outbuf + 34, len);
44 1
45 1
46 1 ip_send(outbuf, ipaddr, ICMP_TYPE, len);
47 1 }
48
49
50
51 //------------------------------------------------------------------------
52 // This builds an outgoing ICMP destination port unreachable response
53 // message. See See "TCP/IP Illustrated, Volume 1" Sect 6.5. This
54 // message is typically sent in response to a UDP message directed
55 // to a port that has no corresponding application running.
C51 COMPILER V7.06 ICMP 10/09/2006 21:51:55 PAGE 2
56 // Todo: The spec says we should return all options that were in
57 // the original incoming IP header. Right now we cut off everything
58 // after the first 20 bytes.
59 //------------------------------------------------------------------------
60 void dest_unreach_send(UCHAR xdata * inbuf, ULONG ipaddr)
61 {
62 1 UCHAR xdata * outbuf;
63 1 ICMP_ERR_HEADER xdata * icmp;
64 1
65 1 // Allocate memory for entire outgoing message
66 1 // including eth and IP haders. Always 70 bytes
67 1 outbuf = (UCHAR xdata *)malloc(70);
68 1 if (outbuf == NULL)
69 1 {
70 2 return;
71 2 }
72 1
73 1 icmp = (ICMP_ERR_HEADER xdata *)(outbuf + 34);
74 1
75 1 // Fill in ICMP error message header
76 1 icmp->msg_type = 3; // destination unreachable
77 1 icmp->msg_code = 3; // port unreachable
78 1 icmp->checksum = 0;
79 1
80 1 // Fill in ICMP error message data
81 1 icmp->msg_data = 0;
82 1
83 1 // Copy in 20 byte original incoming IP header
84 1 // plus 8 bytes of data
85 1 memcpy(&icmp->echo_data, inbuf + 14, 28);
86 1
87 1 // Compute checksum over the 36 byte long ICMP
88 1 // header plus data and insert complement
89 1 icmp->checksum = ~cksum(outbuf + 34, 36);
90 1
91 1 // Forward message to the IP layer
92 1 ip_send(outbuf, ipaddr, ICMP_TYPE, 36);
93 1 }
94
95
96
97
98 //------------------------------------------------------------------------
99 // This handles incoming ICMP messages. See "TCP/IP Illustrated,
100 // Volume 1" Sect 6.2 for discussion of the various ICMP messages
101 //------------------------------------------------------------------------
102 void icmp_rcve(UCHAR xdata * inbuf, UINT len)
103 {
104 1 IP_HEADER * ip;
105 1 UCHAR idata msg_type;
106 1 UINT idata temp;
107 1
108 1 // Allow for 14 bytes eth header
109 1 ip = (IP_HEADER *)(inbuf + 14);
110 1
111 1 // IP header has been adjusted if necessary to always be
112 1 // 20 bytes so message starts at an offset of 34
113 1 // Validate checksum of entire ICMP message incl data
114 1 temp = cksum(inbuf + 34, len);
115 1
116 1 if (temp != 0xFFFF)
117 1 {
C51 COMPILER V7.06 ICMP 10/09/2006 21:51:55 PAGE 3
118 2 return;
119 2 }
120 1
121 1 // Switch on the message type
122 1 msg_type = *(inbuf + 34);
123 1 switch(msg_type)
124 1 {
125 2 case 3:
126 2 break;
127 2
128 2 case 8:
129 2
130 2 ping_send(inbuf, ip->source_ipaddr, len);
131 2 break;
132 2
133 2 default:
134 2 break;
135 2 }
136 1 }
137
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 500 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 31
IDATA SIZE = ---- 3
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 + -