/[H8]/trunk/PIC/TCP-IP stack/ip.c
ViewVC logotype

Contents of /trunk/PIC/TCP-IP stack/ip.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 54 - (show annotations) (download)
Fri Apr 27 08:42:17 2007 UTC (17 years, 1 month ago) by hedin
File MIME type: text/plain
File size: 9277 byte(s)
Added basic project
1 /*********************************************************************
2 *
3 * PIC IP Module for Microchip TCP/IP Stack
4 *
5 *********************************************************************
6 * FileName: IP.C
7 * Dependencies: string.h
8 * StackTsk.h
9 * Helpers.h
10 * IP.h
11 * Processor: PIC18
12 * Complier: MCC18 v1.00.50 or higher
13 * HITECH PICC-18 V8.10PL1 or higher
14 * Company: Microchip Technology, Inc.
15 *
16 * Software License Agreement
17 *
18 * The software supplied herewith by Microchip Technology Incorporated
19 * (the “Company”) for its PICmicro® Microcontroller is intended and
20 * supplied to you, the Company’s customer, for use solely and
21 * exclusively on Microchip PICmicro Microcontroller products. The
22 * software is owned by the Company and/or its supplier, and is
23 * protected under applicable copyright laws. All rights are reserved.
24 * Any use in violation of the foregoing restrictions may subject the
25 * user to criminal sanctions under applicable laws, as well as to
26 * civil liability for the breach of the terms and conditions of this
27 * license.
28 *
29 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
30 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
31 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
32 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
33 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
34 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
35 *
36 * HiTech PICC18 Compiler Options excluding device selection:
37 * -FAKELOCAL -G -O -Zg -E -C
38 *
39 *
40 *
41 *
42 * Author Date Comment
43 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44 * Nilesh Rajbharti 4/27/01 Original (Rev 1.0)
45 * Nilesh Rajbharti 2/9/02 Cleanup
46 * Nilesh Rajbharti 5/22/02 Rev 2.0 (See version.log for detail)
47 ********************************************************************/
48
49 #include <string.h>
50
51 #include "stacktsk.h"
52 #include "helpers.h"
53 #include "mac.h"
54 #include "ip.h"
55
56 // This is left shifted by 4. Actual value is 0x04.
57 #define IPv4 (0x40)
58 #define IP_VERSION IPv4
59
60 /*
61 * IHL (Internet Header Length) is # of DWORDs in a header.
62 * Since, we do not support options, our IP header length will be
63 * minimum i.e. 20 bytes : IHL = 20 / 4 = 5.
64 */
65 #define IP_IHL (0x05)
66
67 #define IP_SERVICE_NW_CTRL (0x07)
68 #define IP_SERVICE_IN_CTRL (0x06)
69 #define IP_SERVICE_ECP (0x05)
70 #define IP_SERVICE_OVR (0x04)
71 #define IP_SERVICE_FLASH (0x03)
72 #define IP_SERVICE_IMM (0x02)
73 #define IP_SERVICE_PRIOR (0x01)
74 #define IP_SERVICE_ROUTINE (0x00)
75
76 #define IP_SERVICE_N_DELAY (0x00)
77 #define IP_SERCICE_L_DELAY (0x08)
78 #define IP_SERVICE_N_THRPT (0x00)
79 #define IP_SERVICE_H_THRPT (0x10)
80 #define IP_SERVICE_N_RELIB (0x00)
81 #define IP_SERVICE_H_RELIB (0x20)
82
83 #define IP_SERVICE (IP_SERVICE_ROUTINE | IP_SERVICE_N_DELAY)
84
85 #define MY_IP_TTL (100) /* Time-To-Live in Seconds */
86
87
88
89
90 static WORD _Identifier = 0;
91
92
93 static void SwapIPHeader(IP_HEADER* h);
94
95
96
97
98 /*********************************************************************
99 * Function: BOOL IPGetHeader( IP_ADDR *localIP,
100 * NODE_INFO *remote,
101 * BYTE *Protocol,
102 * WORD *len)
103 *
104 * PreCondition: MACGetHeader() == TRUE
105 *
106 * Input: localIP - Local node IP Address as received
107 * in current IP header.
108 * If this information is not required
109 * caller may pass NULL value.
110 * remote - Remote node info
111 * Protocol - Current packet protocol
112 * len - Current packet data length
113 *
114 * Output: TRUE, if valid packet was received
115 * FALSE otherwise
116 *
117 * Side Effects: None
118 *
119 * Note: Only one IP message can be received.
120 * Caller may not transmit and receive a message
121 * at the same time.
122 *
123 ********************************************************************/
124 BOOL IPGetHeader(IP_ADDR *localIP,
125 NODE_INFO *remote,
126 BYTE *protocol,
127 WORD *len)
128 {
129 WORD_VAL ReceivedChecksum;
130 WORD_VAL CalcChecksum;
131 WORD checksums[2];
132 IP_HEADER header;
133 BYTE optionsLen;
134 #define MAX_OPTIONS_LEN (20) // As per RFC 791.
135 BYTE options[MAX_OPTIONS_LEN];
136
137 // Read IP header.
138 MACGetArray((BYTE*)&header, sizeof(header));
139
140 // Make sure that this IPv4 packet.
141 if ( (header.VersionIHL & 0xf0) != IP_VERSION )
142 {
143 goto IPGetHeader_Discard;
144 }
145
146 /*
147 * Calculate options length in this header, if there is any.
148 * IHL is in terms of numbers of 32-bit DWORDs; i.e. actual
149 * length is 4 times IHL.
150 */
151 optionsLen = ((header.VersionIHL & 0x0f) << 2) - sizeof(header);
152
153 /*
154 * If there is any option(s), read it so that we can include them
155 * in checksum calculation.
156 */
157 if ( optionsLen > MAX_OPTIONS_LEN )
158 {
159 goto IPGetHeader_Discard;
160 }
161
162 if ( optionsLen > 0 )
163 MACGetArray(options, optionsLen);
164
165 // Save header checksum; clear it and recalculate it ourselves.
166 ReceivedChecksum.Val = header.HeaderChecksum;
167 header.HeaderChecksum = 0;
168
169 // Calculate checksum of header including options bytes.
170 checksums[0] = ~CalcIPChecksum((BYTE*)&header, sizeof(header));
171
172 // Calculate Options checksum too, if they are present.
173 if ( optionsLen > 0 )
174 checksums[1] = ~CalcIPChecksum((BYTE*)options, optionsLen);
175 else
176 checksums[1] = 0;
177
178 CalcChecksum.Val = CalcIPChecksum((BYTE*)checksums,
179 2 * sizeof(WORD));
180
181 // Network to host conversion.
182 SwapIPHeader(&header);
183
184 // Make sure that checksum is correct and IP version is supported.
185 if ( ReceivedChecksum.Val != CalcChecksum.Val ||
186 (header.VersionIHL & 0xf0) != IP_VERSION )
187
188 {
189 // Bad/Unknown packet. Discard it.
190 goto IPGetHeader_Discard;
191 }
192
193 /*
194 * If caller is intrested, return destination IP address
195 * as seen in this IP header.
196 */
197 if ( localIP )
198 localIP->Val = header.DestAddress.Val;
199
200 remote->IPAddr.Val = header.SourceAddress.Val;
201 *protocol = header.Protocol;
202 *len = header.TotalLength - optionsLen -
203 sizeof(header);
204
205 return TRUE;
206
207 IPGetHeader_Discard:
208 MACDiscardRx();
209 return FALSE;
210
211 }
212
213
214
215
216 /*********************************************************************
217 * Function: WORD IPPutHeader( IP_ADDR *Dest,
218 * BYTE Protocol,
219 * WORD Identifier,
220 * WORD DataLen)
221 *
222 * PreCondition: IPIsTxReady() == TRUE
223 *
224 * Input: Src - Destination node address
225 * Protocol - Current packet protocol
226 * Identifier - Current packet identifier
227 * DataLen - Current packet data length
228 *
229 * Output: Handle to current packet - For use by
230 * IPSendByte() function.
231 *
232 * Side Effects: None
233 *
234 * Note: Only one IP message can be transmitted at any
235 * time.
236 * Caller may not transmit and receive a message
237 * at the same time.
238 *
239 ********************************************************************/
240 WORD IPPutHeader(NODE_INFO *remote,
241 BYTE protocol,
242 WORD len)
243 {
244 IP_HEADER header;
245
246 header.VersionIHL = IP_VERSION | IP_IHL;
247 header.TypeOfService = IP_SERVICE;
248 header.TotalLength = sizeof(header) + len;
249 header.Identification = ++_Identifier;
250 header.FragmentInfo = 0;
251 header.TimeToLive = MY_IP_TTL;
252 header.Protocol = protocol;
253 header.HeaderChecksum = 0;
254 header.SourceAddress.v[0] = MY_IP_BYTE1;
255 header.SourceAddress.v[1] = MY_IP_BYTE2;
256 header.SourceAddress.v[2] = MY_IP_BYTE3;
257 header.SourceAddress.v[3] = MY_IP_BYTE4;
258
259 header.DestAddress.Val = remote->IPAddr.Val;
260
261 SwapIPHeader(&header);
262
263 header.HeaderChecksum = CalcIPChecksum((BYTE*)&header,
264 sizeof(header));
265
266 MACPutHeader(&remote->MACAddr, MAC_IP, (sizeof(header)+len));
267 MACPutArray((BYTE*)&header, sizeof(header));
268
269 return 0x0;
270
271 }
272
273
274 static void SwapIPHeader(IP_HEADER* h)
275 {
276 h->TotalLength = swaps(h->TotalLength);
277 h->Identification = swaps(h->Identification);
278 h->HeaderChecksum = swaps(h->HeaderChecksum);
279 }

  ViewVC Help
Powered by ViewVC 1.1.20