]> git.sur5r.net Git - freertos/blob - Demo/Common/ethernet/lwIP/core/inet.c
2c54389e223484abc22c654887e0a01c0fbb1eb8
[freertos] / Demo / Common / ethernet / lwIP / core / inet.c
1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  *
29  * Author: Adam Dunkels <adam@sics.se>
30  *
31  */
32
33
34 /* inet.c
35  *
36  * Functions common to all TCP/IP modules, such as the Internet checksum and the
37  * byte order functions.
38  *
39  */
40
41
42 #include "lwip/opt.h"
43
44 #include "lwip/arch.h"
45
46 #include "lwip/def.h"
47 #include "lwip/inet.h"
48
49 #include "lwip/sys.h"
50
51 /* These are some reference implementations of the checksum algorithm, with the
52  * aim of being simple, correct and fully portable. Checksumming is the
53  * first thing you would want to optimize for your platform. If you create
54  * your own version, link it in and in your sys_arch.h put:
55  * 
56  * #define LWIP_CHKSUM <your_checksum_routine> 
57 */
58 #ifndef LWIP_CHKSUM
59 #define LWIP_CHKSUM lwip_standard_chksum
60
61 #if 1 /* Version A */
62 /**
63  * lwip checksum
64  *
65  * @param dataptr points to start of data to be summed at any boundary
66  * @param len length of data to be summed
67  * @return host order (!) lwip checksum (non-inverted Internet sum) 
68  *
69  * @note accumulator size limits summable length to 64k
70  * @note host endianess is irrelevant (p3 RFC1071)
71  */
72 static u16_t
73 lwip_standard_chksum(void *dataptr, u16_t len)
74 {
75   u32_t acc;
76   u16_t src;
77   u8_t *octetptr;
78
79   acc = 0;
80   /* dataptr may be at odd or even addresses */
81   octetptr = (u8_t*)dataptr;
82   while (len > 1)
83   {
84     /* declare first octet as most significant
85        thus assume network order, ignoring host order */
86     src = (*octetptr) << 8;
87     octetptr++;
88     /* declare second octet as least significant */
89     src |= (*octetptr);
90     octetptr++;
91     acc += src;
92     len -= 2;
93   }
94   if (len > 0)
95   {
96     /* accumulate remaining octet */
97     src = (*octetptr) << 8;
98     acc += src;
99   }
100   /* add deferred carry bits */
101   acc = (acc >> 16) + (acc & 0x0000ffffUL);
102   if ((acc & 0xffff0000) != 0) {
103     acc = (acc >> 16) + (acc & 0x0000ffffUL);
104   }
105   /* This maybe a little confusing: reorder sum using htons()
106      instead of ntohs() since it has a little less call overhead.
107      The caller must invert bits for Internet sum ! */
108   return htons((u16_t)acc);
109 }
110 #endif
111
112 #if 0 /* Version B */
113 /*
114  * Curt McDowell
115  * Broadcom Corp.
116  * csm@broadcom.com
117  *
118  * IP checksum two bytes at a time with support for
119  * unaligned buffer.
120  * Works for len up to and including 0x20000.
121  * by Curt McDowell, Broadcom Corp. 12/08/2005
122  */
123
124 static u16_t
125 lwip_standard_chksum(void *dataptr, int len)
126 {
127   u8_t *pb = dataptr;
128   u16_t *ps, t = 0;
129   u32_t sum = 0;
130   int odd = ((u32_t)pb & 1);
131
132   /* Get aligned to u16_t */
133   if (odd && len > 0) {
134     ((u8_t *)&t)[1] = *pb++;
135     len--;
136   }
137
138   /* Add the bulk of the data */
139   ps = (u16_t *)pb;
140   while (len > 1) {
141     sum += *ps++;
142     len -= 2;
143   }
144
145   /* Consume left-over byte, if any */
146   if (len > 0)
147     ((u8_t *)&t)[0] = *(u8_t *)ps;;
148
149   /* Add end bytes */
150   sum += t;
151
152   /*  Fold 32-bit sum to 16 bits */
153   while (sum >> 16)
154     sum = (sum & 0xffff) + (sum >> 16);
155
156   /* Swap if alignment was odd */
157   if (odd)
158     sum = ((sum & 0xff) << 8) | ((sum & 0xff00) >> 8);
159
160   return sum;
161 }
162 #endif
163
164 #if 0 /* Version C */
165 /**
166  * An optimized checksum routine. Basically, it uses loop-unrolling on
167  * the checksum loop, treating the head and tail bytes specially, whereas
168  * the inner loop acts on 8 bytes at a time. 
169  *
170  * @arg start of buffer to be checksummed. May be an odd byte address.
171  * @len number of bytes in the buffer to be checksummed.
172  * 
173  * by Curt McDowell, Broadcom Corp. December 8th, 2005
174  */
175
176 static u16_t
177 lwip_standard_chksum(void *dataptr, int len)
178 {
179   u8_t *pb = dataptr;
180   u16_t *ps, t = 0;
181   u32_t *pl;
182   u32_t sum = 0, tmp;
183   /* starts at odd byte address? */
184   int odd = ((u32_t)pb & 1);
185
186   if (odd && len > 0) {
187     ((u8_t *)&t)[1] = *pb++;
188     len--;
189   }
190
191   ps = (u16_t *)pb;
192
193   if (((u32_t)ps & 3) && len > 1) {
194     sum += *ps++;
195     len -= 2;
196   }
197
198   pl = (u32_t *)ps;
199
200   while (len > 7)  {
201     tmp = sum + *pl++;          /* ping */
202     if (tmp < sum)
203       tmp++;                    /* add back carry */
204
205     sum = tmp + *pl++;          /* pong */
206     if (sum < tmp)
207       sum++;                    /* add back carry */
208
209     len -= 8;
210   }
211
212   /* make room in upper bits */
213   sum = (sum >> 16) + (sum & 0xffff);
214
215   ps = (u16_t *)pl;
216
217   /* 16-bit aligned word remaining? */
218   while (len > 1) {
219     sum += *ps++;
220     len -= 2;
221   }
222
223   /* dangling tail byte remaining? */
224   if (len > 0)                  /* include odd byte */
225     ((u8_t *)&t)[0] = *(u8_t *)ps;
226
227   sum += t;                     /* add end bytes */
228
229   while (sum >> 16)             /* combine halves */
230     sum = (sum >> 16) + (sum & 0xffff);
231
232   if (odd)
233     sum = ((sum & 0xff) << 8) | ((sum & 0xff00) >> 8);
234
235   return sum;
236 }
237 #endif
238
239 #endif /* LWIP_CHKSUM */
240
241 /* inet_chksum_pseudo:
242  *
243  * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
244  */
245
246 u16_t
247 inet_chksum_pseudo(struct pbuf *p,
248        struct ip_addr *src, struct ip_addr *dest,
249        u8_t proto, u16_t proto_len)
250 {
251   u32_t acc;
252   struct pbuf *q;
253   u8_t swapped;
254
255   acc = 0;
256   swapped = 0;
257   /* iterate through all pbuf in chain */
258   for(q = p; q != NULL; q = q->next) {
259     LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n",
260       (void *)q, (void *)q->next));
261     acc += LWIP_CHKSUM(q->payload, q->len);
262     /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%"X32_F" \n", acc));*/
263     while (acc >> 16) {
264       acc = (acc & 0xffffUL) + (acc >> 16);
265     }
266     if (q->len % 2 != 0) {
267       swapped = 1 - swapped;
268       acc = ((acc & 0xff) << 8) | ((acc & 0xff00UL) >> 8);
269     }
270     /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%"X32_F" \n", acc));*/
271   }
272
273   if (swapped) {
274     acc = ((acc & 0xff) << 8) | ((acc & 0xff00UL) >> 8);
275   }
276   acc += (src->addr & 0xffffUL);
277   acc += ((src->addr >> 16) & 0xffffUL);
278   acc += (dest->addr & 0xffffUL);
279   acc += ((dest->addr >> 16) & 0xffffUL);
280   acc += (u32_t)htons((u16_t)proto);
281   acc += (u32_t)htons(proto_len);
282
283   while (acc >> 16) {
284     acc = (acc & 0xffffUL) + (acc >> 16);
285   }
286   LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F"\n", acc));
287   return (u16_t)~(acc & 0xffffUL);
288 }
289
290 /* inet_chksum:
291  *
292  * Calculates the Internet checksum over a portion of memory. Used primarily for IP
293  * and ICMP.
294  */
295
296 u16_t
297 inet_chksum(void *dataptr, u16_t len)
298 {
299   u32_t acc;
300
301   acc = LWIP_CHKSUM(dataptr, len);
302   while (acc >> 16) {
303     acc = (acc & 0xffff) + (acc >> 16);
304   }
305   return (u16_t)~(acc & 0xffff);
306 }
307
308 u16_t
309 inet_chksum_pbuf(struct pbuf *p)
310 {
311   u32_t acc;
312   struct pbuf *q;
313   u8_t swapped;
314
315   acc = 0;
316   swapped = 0;
317   for(q = p; q != NULL; q = q->next) {
318     acc += LWIP_CHKSUM(q->payload, q->len);
319     while (acc >> 16) {
320       acc = (acc & 0xffffUL) + (acc >> 16);
321     }
322     if (q->len % 2 != 0) {
323       swapped = 1 - swapped;
324       acc = (acc & 0x00ffUL << 8) | (acc & 0xff00UL >> 8);
325     }
326   }
327
328   if (swapped) {
329     acc = ((acc & 0x00ffUL) << 8) | ((acc & 0xff00UL) >> 8);
330   }
331   return (u16_t)~(acc & 0xffffUL);
332 }
333
334 /* Here for now until needed in other places in lwIP */
335 #ifndef isprint
336 #define in_range(c, lo, up)  ((u8_t)c >= lo && (u8_t)c <= up)
337 #define isprint(c)           in_range(c, 0x20, 0x7f)
338 #define isdigit(c)           in_range(c, '0', '9')
339 #define isxdigit(c)          (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
340 #define islower(c)           in_range(c, 'a', 'z')
341 #define isspace(c)           (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
342 #endif    
343     
344 /*
345  * Ascii internet address interpretation routine.
346  * The value returned is in network order.
347  */
348
349 u32_t
350 inet_addr(const char *cp)
351 {
352   struct in_addr val;
353
354   if (inet_aton(cp, &val)) {
355     return (val.s_addr);
356   }
357   return (INADDR_NONE);
358 }
359
360 /*
361  * Check whether "cp" is a valid ascii representation
362  * of an Internet address and convert to a binary address.
363  * Returns 1 if the address is valid, 0 if not.
364  * This replaces inet_addr, the return value from which
365  * cannot distinguish between failure and a local broadcast address.
366  */
367 int
368 inet_aton(const char *cp, struct in_addr *addr)
369 {
370   u32_t val;
371   int base, n, c;
372   u32_t parts[4];
373   u32_t *pp = parts;
374
375   c = *cp;
376   for (;;) {
377     /*
378      * Collect number up to ``.''.
379      * Values are specified as for C:
380      * 0x=hex, 0=octal, 1-9=decimal.
381      */
382     if (!isdigit(c))
383       return (0);
384     val = 0;
385     base = 10;
386     if (c == '0') {
387       c = *++cp;
388       if (c == 'x' || c == 'X') {
389         base = 16;
390         c = *++cp;
391       } else
392         base = 8;
393     }
394     for (;;) {
395       if (isdigit(c)) {
396         val = (val * base) + (int)(c - '0');
397         c = *++cp;
398       } else if (base == 16 && isxdigit(c)) {
399         val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A'));
400         c = *++cp;
401       } else
402         break;
403     }
404     if (c == '.') {
405       /*
406        * Internet format:
407        *  a.b.c.d
408        *  a.b.c   (with c treated as 16 bits)
409        *  a.b (with b treated as 24 bits)
410        */
411       if (pp >= parts + 3)
412         return (0);
413       *pp++ = val;
414       c = *++cp;
415     } else
416       break;
417   }
418   /*
419    * Check for trailing characters.
420    */
421   if (c != '\0' && (!isprint(c) || !isspace(c)))
422     return (0);
423   /*
424    * Concoct the address according to
425    * the number of parts specified.
426    */
427   n = pp - parts + 1;
428   switch (n) {
429
430   case 0:
431     return (0);       /* initial nondigit */
432
433   case 1:             /* a -- 32 bits */
434     break;
435
436   case 2:             /* a.b -- 8.24 bits */
437     if (val > 0xffffff)
438       return (0);
439     val |= parts[0] << 24;
440     break;
441
442   case 3:             /* a.b.c -- 8.8.16 bits */
443     if (val > 0xffff)
444       return (0);
445     val |= (parts[0] << 24) | (parts[1] << 16);
446     break;
447
448   case 4:             /* a.b.c.d -- 8.8.8.8 bits */
449     if (val > 0xff)
450       return (0);
451     val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
452     break;
453   }
454   if (addr)
455     addr->s_addr = htonl(val);
456   return (1);
457 }
458
459 /* Convert numeric IP address into decimal dotted ASCII representation.
460  * returns ptr to static buffer; not reentrant!
461  */
462 char *
463 inet_ntoa(struct in_addr addr)
464 {
465   static char str[16];
466   u32_t s_addr = addr.s_addr;
467   char inv[3];
468   char *rp;
469   u8_t *ap;
470   u8_t rem;
471   u8_t n;
472   u8_t i;
473
474   rp = str;
475   ap = (u8_t *)&s_addr;
476   for(n = 0; n < 4; n++) {
477     i = 0;
478     do {
479       rem = *ap % (u8_t)10;
480       *ap /= (u8_t)10;
481       inv[i++] = '0' + rem;
482     } while(*ap);
483     while(i--)
484       *rp++ = inv[i];
485     *rp++ = '.';
486     ap++;
487   }
488   *--rp = 0;
489   return str;
490 }
491
492 /*
493  * These are reference implementations of the byte swapping functions.
494  * Again with the aim of being simple, correct and fully portable.
495  * Byte swapping is the second thing you would want to optimize. You will
496  * need to port it to your architecture and in your cc.h:
497  * 
498  * #define LWIP_PLATFORM_BYTESWAP 1
499  * #define LWIP_PLATFORM_HTONS(x) <your_htons>
500  * #define LWIP_PLATFORM_HTONL(x) <your_htonl>
501  *
502  * Note ntohs() and ntohl() are merely references to the htonx counterparts.
503  */
504
505 #ifndef BYTE_ORDER
506 #error BYTE_ORDER is not defined
507 #endif
508 #if (LWIP_PLATFORM_BYTESWAP == 0) && (BYTE_ORDER == LITTLE_ENDIAN)
509
510 u16_t
511 htons(u16_t n)
512 {
513   return ((n & 0xff) << 8) | ((n & 0xff00) >> 8);
514 }
515
516 u16_t
517 ntohs(u16_t n)
518 {
519   return htons(n);
520 }
521
522 u32_t
523 htonl(u32_t n)
524 {
525   return ((n & 0xff) << 24) |
526     ((n & 0xff00) << 8) |
527     ((n & 0xff0000) >> 8) |
528     ((n & 0xff000000) >> 24);
529 }
530
531 u32_t
532 ntohl(u32_t n)
533 {
534   return htonl(n);
535 }
536
537 #endif /* (LWIP_PLATFORM_BYTESWAP == 0) && (BYTE_ORDER == LITTLE_ENDIAN) */