📄 icmp.lst
字号:
C51 COMPILER V7.09 ICMP 07/27/2007 15:11:25 PAGE 1
C51 COMPILER V7.09, COMPILATION OF MODULE ICMP
OBJECT MODULE PLACED IN ICMP.obj
COMPILER INVOKED BY: F:\Keil\C51\BIN\C51.EXE tcp\ICMP.C LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.\ICMP.lst) OBJECT(ICMP.ob
-j)
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 ICMP.C
6 //
7 // This module handles ICMP messages
8 // Refer to RFC 792, 896, 950, 1122, and 1191
9 //-----------------------------------------------------------------------------
10
11 #include <stdlib.h>
12 #include "net.h"
13 #include "cksum.h"
14 #include "ip.h"
15 #include "serial.h"
16 #include "icmp.h"
17 #include "utils.h"
18
19 extern UCHAR debug;
20
21
22 //------------------------------------------------------------------------
23 // This builds a ping response message. It allocates memory for the
24 // entire outgoing message, including Eth and IP headers. See "TCP/IP
25 // Illustrated, Volume 1" Sect 7.2 for info on Ping messages
26 //------------------------------------------------------------------------
27 void ping_send(UCHAR * inbuf, ulong ipaddr, uint len )
28 {
29 1 PING_HEADER * ping_in;
30 1 PING_HEADER * ping_out;
31 1 UCHAR * outbuf;
32 1
33 1 ping_in = (PING_HEADER *)(inbuf + 34);
34 1
35 1 // Allocate memory for entire outgoing message
36 1 // outbuf = (UCHAR *)malloc(len + 34);
37 1 outbuf = TX_BUFF;
38 1 if (outbuf == NULL)
39 1 {
40 2 // if (debug) serial_send("PING: Oops, out of memory\r");
41 2 return;
42 2 }
43 1
44 1 // Ping response message payload starts at offset 34
45 1 ping_out = (PING_HEADER *)(outbuf + 34);
46 1
47 1 ping_out->msg_type = 0;
48 1 ping_out->msg_code = 0;
49 1 ping_out->checksum = 0;
50 1 ping_out->identifier = ping_in->identifier;
51 1 ping_out->sequence = ping_in->sequence;
52 1
53 1 // memcpy(&ping_out->echo_data, &ping_in->echo_data, len - 8);
54 1 memcpy((char*)&ping_out->echo_data, "abcdefghijklmnopqrstuvwabcdefghi", len - 8);
C51 COMPILER V7.09 ICMP 07/27/2007 15:11:25 PAGE 2
55 1
56 1
57 1 #ifdef __LITTLEENDIAN__
//ping_out->identifier = ReadUInt16(&ping_out->identifier, 0);
//ping_out->sequence = ReadUInt16(&ping_out->sequence, 0);
#endif
61 1
62 1 // Compute checksum over the ICMP header plus
63 1 // optional data and insert complement
64 1 ping_out->checksum = ~cksum(outbuf + 34, len);
65 1 #ifdef __LITTLEENDIAN__
ping_out->checksum = ntohs(ping_out->checksum);
#endif
69 1
70 1 //if (debug) serial_send("ICMP: Sending response to IP layer\r");
71 1
72 1 ip_send(outbuf, ipaddr, ICMP_TYPE, len);
73 1
74 1 #ifdef __LITTLEENDIAN__
free(outbuf);
#endif
77 1 }
78
79
80
81 //------------------------------------------------------------------------
82 // This builds an outgoing ICMP destination port unreachable response
83 // message. See See "TCP/IP Illustrated, Volume 1" Sect 6.5. This
84 // message is typically sent in response to a UDP message directed
85 // to a port that has no corresponding application running.
86 // Todo: The spec says we should return all options that were in
87 // the original incoming IP header. Right now we cut off everything
88 // after the first 20 bytes.
89 //------------------------------------------------------------------------
90 void dest_unreach_send(UCHAR * inbuf, ulong ipaddr)
91 {
92 1 UCHAR * outbuf;
93 1 ICMP_ERR_HEADER * icmp;
94 1
95 1 // Allocate memory for entire outgoing message
96 1 // including eth and IP haders. Always 70 bytes
97 1 // outbuf = (UCHAR *)malloc(70);
98 1 if (outbuf == NULL)
99 1 {
100 2 // if (debug) serial_send("ICMP: Oops, out of memory\r");
101 2 return;
102 2 }
103 1
104 1 icmp = (ICMP_ERR_HEADER *)(outbuf + 34);
105 1
106 1 // Fill in ICMP error message header
107 1 icmp->msg_type = 3; // destination unreachable
108 1 icmp->msg_code = 3; // port unreachable
109 1 icmp->checksum = 0;
110 1
111 1 // Fill in ICMP error message data
112 1 icmp->msg_data = 0;
113 1
114 1 // Copy in 20 byte original incoming IP header
115 1 // plus 8 bytes of data
116 1 memcpy((char*)&icmp->echo_data, inbuf + 14, 28);
C51 COMPILER V7.09 ICMP 07/27/2007 15:11:25 PAGE 3
117 1
118 1 // Compute checksum over the 36 byte long ICMP
119 1 // header plus data and insert complement
120 1 icmp->checksum = ~cksum(outbuf + 34, 36);
121 1
122 1 // Forward message to the IP layer
123 1 //if (debug) serial_send("ICMP: Sending dest unreach to IP layer\r");
124 1 #ifdef __LITTLEENDIAN__
icmp->checksum = ntohs(icmp->checksum);
#endif
128 1 ip_send(outbuf, ipaddr, ICMP_TYPE, 36);
129 1
130 1 #ifdef __LITTLEENDIAN__
free(outbuf);
#endif
133 1 }
134
135
136
137
138 //------------------------------------------------------------------------
139 // This handles incoming ICMP messages. See "TCP/IP Illustrated,
140 // Volume 1" Sect 6.2 for discussion of the various ICMP messages
141 //------------------------------------------------------------------------
142 void icmp_rcve(UCHAR * inbuf, uint len)
143 {
144 1 IP_HEADER * ip;
145 1 UCHAR msg_type;
146 1 uint temp;
147 1
148 1 // Allow for 14 bytes eth header
149 1 ip = (IP_HEADER *)(inbuf + 14);
150 1
151 1 // IP header has been adjusted if necessary to always be
152 1 // 20 bytes so message starts at an offset of 34
153 1 // Validate checksum of entire ICMP message incl data
154 1 temp = cksum(inbuf + 34, len);
155 1
156 1 if (temp != 0xFFFF)
157 1 {
158 2 //if (debug)
159 2 // serial_send("ICMP: Error, cksum bad\n");
160 2 return;
161 2 }
162 1
163 1 // Switch on the message type
164 1 msg_type = *(inbuf + 34);
165 1 switch(msg_type)
166 1 {
167 2 case 3:
168 2 //if (debug) serial_send("ICMP: Dest unreachable rcvd\r");
169 2 break;
170 2
171 2 case 8:
172 2 //if (debug) serial_send("ICMP: Ping rcvd\r");
173 2 ping_send(inbuf, ip->source_ipaddr, len);
174 2 break;
175 2
176 2 default:
177 2 //if (debug)
178 2 // serial_send("ICMP: Error, unknown msg rcvd\n");
C51 COMPILER V7.09 ICMP 07/27/2007 15:11:25 PAGE 4
179 2 // CapSendPacket(Adapter, Opcode, len, inbuf);
180 2 break;
181 2 }
182 1 }
183
184
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 735 ----
CONSTANT SIZE = 33 ----
XDATA SIZE = ---- 39
PDATA SIZE = ---- ----
DATA SIZE = ---- ----
IDATA SIZE = ---- ----
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 + -