?? icmp1.lst
字號:
C51 COMPILER V7.09 ICMP1 06/28/2007 09:51:21 PAGE 1
C51 COMPILER V7.09, COMPILATION OF MODULE ICMP1
OBJECT MODULE PLACED IN ICMP1.obj
COMPILER INVOKED BY: F:\Keil\C51\BIN\C51.EXE tcp\ICMP1.C LARGE BROWSE DEBUG OBJECTEXTEND PRINT(.\ICMP1.lst) OBJECT(ICMP1
-.obj)
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 "net1.h"
13 #include "cksum.h"
14 #include "ip1.h"
15 #include "serial1.h"
16 #include "icmp1.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 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 *)(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 memcpy((char*)&ping_out->echo_data, "abcdefghijklmnopqrstuvwabcdefghi", len - 8);
54 1
C51 COMPILER V7.09 ICMP1 06/28/2007 09:51:21 PAGE 2
55 1
56 1 #ifdef __LITTLEENDIAN__
57 1 //ping_out->identifier = ReadUInt16(&ping_out->identifier, 0);
58 1 //ping_out->sequence = ReadUInt16(&ping_out->sequence, 0);
59 1 #endif
60 1
61 1 // Compute checksum over the ICMP header plus
62 1 // optional data and insert complement
63 1 ping_out->checksum = ~cksum(outbuf + 34, len);
64 1 #ifdef __LITTLEENDIAN__
65 1 ping_out->checksum = ntohs(ping_out->checksum);
66 1
67 1 #endif
68 1
69 1 //if (debug) serial_send("ICMP: Sending response to IP layer\r");
70 1
71 1 ip_send1(outbuf, ipaddr, ICMP_TYPE, len);
72 1
73 1 #ifdef __LITTLEENDIAN__
74 1 free(outbuf);
75 1 #endif
76 1 }
77
78
79
80 //------------------------------------------------------------------------
81 // This builds an outgoing ICMP destination port unreachable response
82 // message. See See "TCP/IP Illustrated, Volume 1" Sect 6.5. This
83 // message is typically sent in response to a UDP message directed
84 // to a port that has no corresponding application running.
85 // Todo: The spec says we should return all options that were in
86 // the original incoming IP header. Right now we cut off everything
87 // after the first 20 bytes.
88 //------------------------------------------------------------------------
89 void dest_unreach_send(UCHAR * inbuf, ulong ipaddr)
90 {
91 1 UCHAR * outbuf;
92 1 ICMP_ERR_HEADER * icmp;
93 1
94 1 // Allocate memory for entire outgoing message
95 1 // including eth and IP haders. Always 70 bytes
96 1 outbuf = (UCHAR *)malloc(70);
97 1 if (outbuf == NULL)
98 1 {
99 2 // if (debug) serial_send("ICMP: Oops, out of memory\r");
100 2 return;
101 2 }
102 1
103 1 icmp = (ICMP_ERR_HEADER *)(outbuf + 34);
104 1
105 1 // Fill in ICMP error message header
106 1 icmp->msg_type = 3; // destination unreachable
107 1 icmp->msg_code = 3; // port unreachable
108 1 icmp->checksum = 0;
109 1
110 1 // Fill in ICMP error message data
111 1 icmp->msg_data = 0;
112 1
113 1 // Copy in 20 byte original incoming IP header
114 1 // plus 8 bytes of data
115 1 memcpy((char*)&icmp->echo_data, inbuf + 14, 28);
116 1
C51 COMPILER V7.09 ICMP1 06/28/2007 09:51:21 PAGE 3
117 1 // Compute checksum over the 36 byte long ICMP
118 1 // header plus data and insert complement
119 1 icmp->checksum = ~cksum(outbuf + 34, 36);
120 1
121 1 // Forward message to the IP layer
122 1 //if (debug) serial_send("ICMP: Sending dest unreach to IP layer\r");
123 1 #ifdef __LITTLEENDIAN__
124 1 icmp->checksum = ntohs(icmp->checksum);
125 1
126 1 #endif
127 1 ip_send1(outbuf, ipaddr, ICMP_TYPE, 36);
128 1
129 1 #ifdef __LITTLEENDIAN__
130 1 free(outbuf);
131 1 #endif
132 1 }
133
134
135
136
137 //------------------------------------------------------------------------
138 // This handles incoming ICMP messages. See "TCP/IP Illustrated,
139 // Volume 1" Sect 6.2 for discussion of the various ICMP messages
140 //------------------------------------------------------------------------
141 void icmp_rcve(UCHAR * inbuf, uint len)
142 {
143 1 IP_HEADER * ip;
144 1 UCHAR msg_type;
145 1 uint temp;
146 1
147 1 // Allow for 14 bytes eth header
148 1 ip = (IP_HEADER *)(inbuf + 14);
149 1
150 1 // IP header has been adjusted if necessary to always be
151 1 // 20 bytes so message starts at an offset of 34
152 1 // Validate checksum of entire ICMP message incl data
153 1 temp = cksum(inbuf + 34, len);
154 1
155 1 if (temp != 0xFFFF)
156 1 {
157 2 //if (debug)
158 2 // serial_send("ICMP: Error, cksum bad\n");
159 2 return;
160 2 }
161 1
162 1 // Switch on the message type
163 1 msg_type = *(inbuf + 34);
164 1 switch(msg_type)
165 1 {
166 2 case 3:
167 2 //if (debug) serial_send("ICMP: Dest unreachable rcvd\r");
168 2 break;
169 2
170 2 case 8:
171 2 //if (debug) serial_send("ICMP: Ping rcvd\r");
172 2 ping_send(inbuf, ip->source_ipaddr, len);
173 2 break;
174 2
175 2 default:
176 2 //if (debug)
177 2 // serial_send("ICMP: Error, unknown msg rcvd\n");
178 2 // CapSendPacket(Adapter, Opcode, len, inbuf);
C51 COMPILER V7.09 ICMP1 06/28/2007 09:51:21 PAGE 4
179 2 break;
180 2 }
181 1 }
182
183
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 862 ----
CONSTANT SIZE = 33 ----
XDATA SIZE = ---- 35
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 + -