📄 icmp.lst
字号:
C51 COMPILER V7.07 ICMP 11/25/2003 15:47:47 PAGE 1
C51 COMPILER V7.07, COMPILATION OF MODULE ICMP
OBJECT MODULE PLACED IN ICMP.OBJ
COMPILER INVOKED BY: D:\KEIL707\C51\BIN\C51.EXE ICMP.C OPTIMIZE(9,SPEED) BROWSE DEBUG OBJECTEXTEND
stmt 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 #include <string.h>
11 #include <stdlib.h>
12 #include "C8051f.h"
13 #include "net.h"
14 #include "cksum.h"
15 #include "ip.h"
16 #include "serial.h"
17 #include "icmp.h"
18
19 extern UCHAR idata 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 xdata * inbuf, ULONG ipaddr, UINT len)
28 {
29 1 PING_HEADER xdata * ping_in;
30 1 PING_HEADER xdata * ping_out;
31 1 UCHAR xdata * outbuf;
32 1
33 1 ping_in = (PING_HEADER xdata *)(inbuf + 34);
34 1
35 1 // Allocate memory for entire outgoing message
36 1 outbuf = (UCHAR xdata *)malloc(len + 34);
37 1 if (outbuf == NULL)
38 1 {
39 2 if (debug) serial_send("PING: Oops, out of memory\r");
40 2 return;
41 2 }
42 1
43 1 // Ping response message payload starts at offset 34
44 1 ping_out = (PING_HEADER xdata *)(outbuf + 34);
45 1
46 1 ping_out->msg_type = 0;
47 1 ping_out->msg_code = 0;
48 1 ping_out->checksum = 0;
49 1 ping_out->identifier = ping_in->identifier;
50 1 ping_out->sequence = ping_in->sequence;
51 1
52 1 memcpy(&ping_out->echo_data, &ping_in->echo_data, len - 8);
53 1
54 1 // Compute checksum over the ICMP header plus
55 1 // optional data and insert complement
C51 COMPILER V7.07 ICMP 11/25/2003 15:47:47 PAGE 2
56 1 ping_out->checksum = ~cksum(outbuf + 34, len);
57 1
58 1 if (debug) serial_send("ICMP: Sending response to IP layer\r");
59 1
60 1 ip_send(outbuf, ipaddr, ICMP_TYPE, len);
61 1 }
62
63
64
65 //------------------------------------------------------------------------
66 // This builds an outgoing ICMP destination port unreachable response
67 // message. See See "TCP/IP Illustrated, Volume 1" Sect 6.5. This
68 // message is typically sent in response to a UDP message directed
69 // to a port that has no corresponding application running.
70 // Todo: The spec says we should return all options that were in
71 // the original incoming IP header. Right now we cut off everything
72 // after the first 20 bytes.
73 //------------------------------------------------------------------------
74 void dest_unreach_send(UCHAR xdata * inbuf, ULONG ipaddr)
75 {
76 1 UCHAR xdata * outbuf;
77 1 ICMP_ERR_HEADER xdata * icmp;
78 1
79 1 // Allocate memory for entire outgoing message
80 1 // including eth and IP haders. Always 70 bytes
81 1 outbuf = (UCHAR xdata *)malloc(70);
82 1 if (outbuf == NULL)
83 1 {
84 2 if (debug) serial_send("ICMP: Oops, out of memory\r");
85 2 return;
86 2 }
87 1
88 1 icmp = (ICMP_ERR_HEADER xdata *)(outbuf + 34);
89 1
90 1 // Fill in ICMP error message header
91 1 icmp->msg_type = 3; // destination unreachable
92 1 icmp->msg_code = 3; // port unreachable
93 1 icmp->checksum = 0;
94 1
95 1 // Fill in ICMP error message data
96 1 icmp->msg_data = 0;
97 1
98 1 // Copy in 20 byte original incoming IP header
99 1 // plus 8 bytes of data
100 1 memcpy(&icmp->echo_data, inbuf + 14, 28);
101 1
102 1 // Compute checksum over the 36 byte long ICMP
103 1 // header plus data and insert complement
104 1 icmp->checksum = ~cksum(outbuf + 34, 36);
105 1
106 1 // Forward message to the IP layer
107 1 if (debug) serial_send("ICMP: Sending dest unreach to IP layer\r");
108 1 ip_send(outbuf, ipaddr, ICMP_TYPE, 36);
109 1 }
110
111
112
113
114 //------------------------------------------------------------------------
115 // This handles incoming ICMP messages. See "TCP/IP Illustrated,
116 // Volume 1" Sect 6.2 for discussion of the various ICMP messages
117 //------------------------------------------------------------------------
C51 COMPILER V7.07 ICMP 11/25/2003 15:47:47 PAGE 3
118 void icmp_rcve(UCHAR xdata * inbuf, UINT len)
119 {
120 1 IP_HEADER * ip;
121 1 UCHAR idata msg_type;
122 1 UINT idata temp;
123 1
124 1 // Allow for 14 bytes eth header
125 1 ip = (IP_HEADER *)(inbuf + 14);
126 1
127 1 // IP header has been adjusted if necessary to always be
128 1 // 20 bytes so message starts at an offset of 34
129 1 // Validate checksum of entire ICMP message incl data
130 1 temp = cksum(inbuf + 34, len);
131 1
132 1 if (temp != 0xFFFF)
133 1 {
134 2 if (debug) serial_send("ICMP: Error, cksum bad\r");
135 2 return;
136 2 }
137 1
138 1 // Switch on the message type
139 1 msg_type = *(inbuf + 34);
140 1 switch(msg_type)
141 1 {
142 2 case 3:
143 2 if (debug) serial_send("ICMP: Dest unreachable rcvd\r");
144 2 break;
145 2
146 2 case 8:
147 2 if (debug) serial_send("ICMP: Ping rcvd\r");
148 2 ping_send(inbuf, ip->source_ipaddr, len);
149 2 break;
150 2
151 2 default:
152 2 if (debug) serial_send("ICMP: Error, unknown msg rcvd\r");
153 2 break;
154 2 }
155 1 }
156
157
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 600 ----
CONSTANT SIZE = 231 ----
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 + -