1 |
/*********************************************************************
|
2 |
*
|
3 |
* ICMP Module for Microchip TCP/IP Stack
|
4 |
*
|
5 |
*********************************************************************
|
6 |
* FileName: ICMP.C
|
7 |
* Dependencies: ICMP.h
|
8 |
* string.h
|
9 |
* StackTsk.h
|
10 |
* Helpers.h
|
11 |
* IP.h
|
12 |
* MAC.h
|
13 |
* Processor: PIC18
|
14 |
* Complier: MCC18 v1.00.50 or higher
|
15 |
* HITECH PICC-18 V8.10PL1 or higher
|
16 |
* Company: Microchip Technology, Inc.
|
17 |
*
|
18 |
* Software License Agreement
|
19 |
*
|
20 |
* The software supplied herewith by Microchip Technology Incorporated
|
21 |
* (the “Company”) for its PICmicro® Microcontroller is intended and
|
22 |
* supplied to you, the Company’s customer, for use solely and
|
23 |
* exclusively on Microchip PICmicro Microcontroller products. The
|
24 |
* software is owned by the Company and/or its supplier, and is
|
25 |
* protected under applicable copyright laws. All rights are reserved.
|
26 |
* Any use in violation of the foregoing restrictions may subject the
|
27 |
* user to criminal sanctions under applicable laws, as well as to
|
28 |
* civil liability for the breach of the terms and conditions of this
|
29 |
* license.
|
30 |
*
|
31 |
* THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
|
32 |
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
|
33 |
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
34 |
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
|
35 |
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
|
36 |
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
|
37 |
*
|
38 |
* HiTech PICC18 Compiler Options excluding device selection:
|
39 |
* -FAKELOCAL -G -O -Zg -E -C
|
40 |
*
|
41 |
*
|
42 |
*
|
43 |
*
|
44 |
* Author Date Comment
|
45 |
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
46 |
* Nilesh Rajbharti 4/30/01 Original (Rev 1.0)
|
47 |
* Nilesh Rajbharti 2/9/02 Cleanup
|
48 |
* Nilesh Rajbharti 5/22/02 Rev 2.0 (See version.log for detail)
|
49 |
********************************************************************/
|
50 |
|
51 |
#include <string.h>
|
52 |
#include "stacktsk.h"
|
53 |
#include "helpers.h"
|
54 |
#include "icmp.h"
|
55 |
#include "ip.h"
|
56 |
#include "mac.h"
|
57 |
|
58 |
#if !defined(STACK_USE_ICMP)
|
59 |
#error You have selected to not include ICMP. Remove this file from \
|
60 |
your project to reduce code size.
|
61 |
#endif
|
62 |
|
63 |
|
64 |
#define MAX_ICMP_DATA (32)
|
65 |
|
66 |
/*
|
67 |
* ICMP packet definition
|
68 |
*/
|
69 |
typedef struct _ICMP_PACKET
|
70 |
{
|
71 |
BYTE Type;
|
72 |
BYTE Code;
|
73 |
WORD Checksum;
|
74 |
WORD Identifier;
|
75 |
WORD SequenceNumber;
|
76 |
BYTE Data[MAX_ICMP_DATA];
|
77 |
} ICMP_PACKET;
|
78 |
#define ICMP_HEADER_SIZE (sizeof(ICMP_PACKET) - MAX_ICMP_DATA)
|
79 |
|
80 |
static void SwapICMPPacket(ICMP_PACKET* p);
|
81 |
|
82 |
|
83 |
/*********************************************************************
|
84 |
* Function: BOOL ICMPGet(ICMP_CODE *code,
|
85 |
* BYTE *data,
|
86 |
* BYTE *len,
|
87 |
* WORD *id,
|
88 |
* WORD *seq)
|
89 |
*
|
90 |
* PreCondition: MAC buffer contains ICMP type packet.
|
91 |
*
|
92 |
* Input: code - Buffer to hold ICMP code value
|
93 |
* data - Buffer to hold ICMP data
|
94 |
* len - Buffer to hold ICMP data length
|
95 |
* id - Buffer to hold ICMP id
|
96 |
* seq - Buffer to hold ICMP seq
|
97 |
*
|
98 |
* Output: TRUE if valid ICMP packet was received
|
99 |
* FALSE otherwise.
|
100 |
*
|
101 |
* Side Effects: None
|
102 |
*
|
103 |
* Overview: None
|
104 |
*
|
105 |
* Note: None
|
106 |
********************************************************************/
|
107 |
BOOL ICMPGet(ICMP_CODE *code,
|
108 |
BYTE *data,
|
109 |
BYTE *len,
|
110 |
WORD *id,
|
111 |
WORD *seq)
|
112 |
{
|
113 |
ICMP_PACKET packet;
|
114 |
WORD checksums[2];
|
115 |
WORD CalcChecksum;
|
116 |
WORD ReceivedChecksum;
|
117 |
|
118 |
MACGetArray((BYTE*)&packet, ICMP_HEADER_SIZE);
|
119 |
|
120 |
ReceivedChecksum = packet.Checksum;
|
121 |
packet.Checksum = 0;
|
122 |
|
123 |
checksums[0] = ~CalcIPChecksum((BYTE*)&packet, ICMP_HEADER_SIZE);
|
124 |
|
125 |
*len -= ICMP_HEADER_SIZE;
|
126 |
|
127 |
MACGetArray(data, *len);
|
128 |
checksums[1] = ~CalcIPChecksum(data, *len);
|
129 |
|
130 |
CalcChecksum = CalcIPChecksum((BYTE*)checksums, 2 * sizeof(WORD));
|
131 |
|
132 |
SwapICMPPacket(&packet);
|
133 |
|
134 |
*code = packet.Type;
|
135 |
*id = packet.Identifier;
|
136 |
*seq = packet.SequenceNumber;
|
137 |
|
138 |
return ( CalcChecksum == ReceivedChecksum );
|
139 |
}
|
140 |
|
141 |
/*********************************************************************
|
142 |
* Function: void ICMPPut(NODE_INFO *remote,
|
143 |
* ICMP_CODE code,
|
144 |
* BYTE *data,
|
145 |
* BYTE len,
|
146 |
* WORD id,
|
147 |
* WORD seq)
|
148 |
*
|
149 |
* PreCondition: ICMPIsTxReady() == TRUE
|
150 |
*
|
151 |
* Input: remote - Remote node info
|
152 |
* code - ICMP_ECHO_REPLY or ICMP_ECHO_REQUEST
|
153 |
* data - Data bytes
|
154 |
* len - Number of bytes to send
|
155 |
* id - ICMP identifier
|
156 |
* seq - ICMP sequence number
|
157 |
*
|
158 |
* Output: None
|
159 |
*
|
160 |
* Side Effects: None
|
161 |
*
|
162 |
* Note: A ICMP packet is created and put on MAC.
|
163 |
*
|
164 |
********************************************************************/
|
165 |
void ICMPPut(NODE_INFO *remote,
|
166 |
ICMP_CODE code,
|
167 |
BYTE *data,
|
168 |
BYTE len,
|
169 |
WORD id,
|
170 |
WORD seq)
|
171 |
{
|
172 |
ICMP_PACKET packet;
|
173 |
|
174 |
packet.Code = 0;
|
175 |
packet.Type = code;
|
176 |
packet.Checksum = 0;
|
177 |
packet.Identifier = id;
|
178 |
packet.SequenceNumber = seq;
|
179 |
|
180 |
memcpy((void*)packet.Data, (void*)data, len);
|
181 |
|
182 |
SwapICMPPacket(&packet);
|
183 |
|
184 |
packet.Checksum = CalcIPChecksum((BYTE*)&packet,
|
185 |
(WORD)(ICMP_HEADER_SIZE + len));
|
186 |
|
187 |
IPPutHeader(remote,
|
188 |
IP_PROT_ICMP,
|
189 |
(WORD)(ICMP_HEADER_SIZE + len));
|
190 |
|
191 |
IPPutArray((BYTE*)&packet, (WORD)(ICMP_HEADER_SIZE + len));
|
192 |
|
193 |
MACFlush();
|
194 |
}
|
195 |
|
196 |
/*********************************************************************
|
197 |
* Function: void SwapICMPPacket(ICMP_PACKET* p)
|
198 |
*
|
199 |
* PreCondition: None
|
200 |
*
|
201 |
* Input: p - ICMP packet header
|
202 |
*
|
203 |
* Output: ICMP packet is swapped
|
204 |
*
|
205 |
* Side Effects: None
|
206 |
*
|
207 |
* Overview: None
|
208 |
*
|
209 |
* Note: None
|
210 |
********************************************************************/
|
211 |
static void SwapICMPPacket(ICMP_PACKET* p)
|
212 |
{
|
213 |
p->Identifier = swaps(p->Identifier);
|
214 |
p->SequenceNumber = swaps(p->SequenceNumber);
|
215 |
p->Checksum = swaps(p->Checksum);
|
216 |
}
|