OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [lwIP_Demo_Rowley_ARM7/] [lwip-1.1.0/] [src/] [core/] [inet.c] - Blame information for rev 583

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 583 jeremybenn
/*
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
/* This is a reference implementation of the checksum algorithm
52
 
53
 - it may not work on all architectures, and all processors, particularly
54
   if they have issues with alignment and 16 bit access.
55
 
56
 - in this case you will need to port it to your architecture and
57
   #define LWIP_CHKSUM <your_checksum_routine>
58
   in your sys_arch.h
59
*/
60
#ifndef LWIP_CHKSUM
61
#define LWIP_CHKSUM lwip_standard_chksum
62
static u16_t
63
lwip_standard_chksum(void *dataptr, int len)
64
{
65
  u32_t acc;
66
 
67
  LWIP_DEBUGF(INET_DEBUG, ("lwip_chksum(%p, %d)\n", (void *)dataptr, len));
68
  for(acc = 0; len > 1; len -= 2) {
69
      /*    acc = acc + *((u16_t *)dataptr)++;*/
70
    acc += *(u16_t *)dataptr;
71
    dataptr = (void *)((u16_t *)dataptr + 1);
72
  }
73
 
74
  /* add up any odd byte */
75
  if (len == 1) {
76
    acc += htons((u16_t)((*(u8_t *)dataptr) & 0xff) << 8);
77
    LWIP_DEBUGF(INET_DEBUG, ("inet: chksum: odd byte %d\n", (unsigned int)(*(u8_t *)dataptr)));
78
  } else {
79
    LWIP_DEBUGF(INET_DEBUG, ("inet: chksum: no odd byte\n"));
80
  }
81
  acc = (acc >> 16) + (acc & 0xffffUL);
82
 
83
  if ((acc & 0xffff0000) != 0) {
84
    acc = (acc >> 16) + (acc & 0xffffUL);
85
  }
86
 
87
  return (u16_t)acc;
88
}
89
#endif
90
 
91
/* inet_chksum_pseudo:
92
 *
93
 * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
94
 */
95
 
96
u16_t
97
inet_chksum_pseudo(struct pbuf *p,
98
       struct ip_addr *src, struct ip_addr *dest,
99
       u8_t proto, u16_t proto_len)
100
{
101
  u32_t acc;
102
  struct pbuf *q;
103
  u8_t swapped;
104
 
105
  acc = 0;
106
  swapped = 0;
107
  /* iterate through all pbuf in chain */
108
  for(q = p; q != NULL; q = q->next) {
109
    LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n",
110
      (void *)q, (void *)q->next));
111
    acc += LWIP_CHKSUM(q->payload, q->len);
112
    /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%lx \n", acc));*/
113
    while (acc >> 16) {
114
      acc = (acc & 0xffffUL) + (acc >> 16);
115
    }
116
    if (q->len % 2 != 0) {
117
      swapped = 1 - swapped;
118
      acc = ((acc & 0xff) << 8) | ((acc & 0xff00UL) >> 8);
119
    }
120
    /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%lx \n", acc));*/
121
  }
122
 
123
  if (swapped) {
124
    acc = ((acc & 0xff) << 8) | ((acc & 0xff00UL) >> 8);
125
  }
126
  acc += (src->addr & 0xffffUL);
127
  acc += ((src->addr >> 16) & 0xffffUL);
128
  acc += (dest->addr & 0xffffUL);
129
  acc += ((dest->addr >> 16) & 0xffffUL);
130
  acc += (u32_t)htons((u16_t)proto);
131
  acc += (u32_t)htons(proto_len);
132
 
133
  while (acc >> 16) {
134
    acc = (acc & 0xffffUL) + (acc >> 16);
135
  }
136
  LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%lx\n", acc));
137
  return (u16_t)~(acc & 0xffffUL);
138
}
139
 
140
/* inet_chksum:
141
 *
142
 * Calculates the Internet checksum over a portion of memory. Used primarely for IP
143
 * and ICMP.
144
 */
145
 
146
u16_t
147
inet_chksum(void *dataptr, u16_t len)
148
{
149
  u32_t acc;
150
 
151
  acc = LWIP_CHKSUM(dataptr, len);
152
  while (acc >> 16) {
153
    acc = (acc & 0xffff) + (acc >> 16);
154
  }
155
  return (u16_t)~(acc & 0xffff);
156
}
157
 
158
u16_t
159
inet_chksum_pbuf(struct pbuf *p)
160
{
161
  u32_t acc;
162
  struct pbuf *q;
163
  u8_t swapped;
164
 
165
  acc = 0;
166
  swapped = 0;
167
  for(q = p; q != NULL; q = q->next) {
168
    acc += LWIP_CHKSUM(q->payload, q->len);
169
    while (acc >> 16) {
170
      acc = (acc & 0xffffUL) + (acc >> 16);
171
    }
172
    if (q->len % 2 != 0) {
173
      swapped = 1 - swapped;
174
      acc = (acc & 0x00ffUL << 8) | (acc & 0xff00UL >> 8);
175
    }
176
  }
177
 
178
  if (swapped) {
179
    acc = ((acc & 0x00ffUL) << 8) | ((acc & 0xff00UL) >> 8);
180
  }
181
  return (u16_t)~(acc & 0xffffUL);
182
}
183
 
184
/* Here for now until needed in other places in lwIP */
185
#ifndef isascii
186
#define in_range(c, lo, up)  ((u8_t)c >= lo && (u8_t)c <= up)
187
#define isascii(c)           in_range(c, 0x20, 0x7f)
188
#define isdigit(c)           in_range(c, '0', '9')
189
#define isxdigit(c)          (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
190
#define islower(c)           in_range(c, 'a', 'z')
191
#define isspace(c)           (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
192
#endif          
193
 
194
 
195
 /*
196
  * Ascii internet address interpretation routine.
197
  * The value returned is in network order.
198
  */
199
 
200
 /*  */
201
 /* inet_addr */
202
 u32_t inet_addr(const char *cp)
203
 {
204
     struct in_addr val;
205
 
206
     if (inet_aton(cp, &val)) {
207
         return (val.s_addr);
208
     }
209
     return (INADDR_NONE);
210
 }
211
 
212
 /*
213
  * Check whether "cp" is a valid ascii representation
214
  * of an Internet address and convert to a binary address.
215
  * Returns 1 if the address is valid, 0 if not.
216
  * This replaces inet_addr, the return value from which
217
  * cannot distinguish between failure and a local broadcast address.
218
  */
219
 /*  */
220
 /* inet_aton */
221
 int inet_aton(const char *cp, struct in_addr *addr)
222
 {
223
     u32_t val;
224
     int base, n;
225
     char c;
226
     u32_t parts[4];
227
     u32_t* pp = parts;
228
 
229
     c = *cp;
230
     for (;;) {
231
         /*
232
          * Collect number up to ``.''.
233
          * Values are specified as for C:
234
          * 0x=hex, 0=octal, isdigit=decimal.
235
          */
236
         if (!isdigit(c))
237
             return (0);
238
         val = 0; base = 10;
239
         if (c == '0') {
240
             c = *++cp;
241
             if (c == 'x' || c == 'X')
242
                 base = 16, c = *++cp;
243
             else
244
                 base = 8;
245
         }
246
         for (;;) {
247
             if (isdigit(c)) {
248
                 val = (val * base) + (int)(c - '0');
249
                 c = *++cp;
250
             } else if (base == 16 && isxdigit(c)) {
251
                 val = (val << 4) |
252
                     (int)(c + 10 - (islower(c) ? 'a' : 'A'));
253
                 c = *++cp;
254
             } else
255
             break;
256
         }
257
         if (c == '.') {
258
             /*
259
              * Internet format:
260
              *  a.b.c.d
261
              *  a.b.c   (with c treated as 16 bits)
262
              *  a.b (with b treated as 24 bits)
263
              */
264
             if (pp >= parts + 3)
265
                 return (0);
266
             *pp++ = val;
267
             c = *++cp;
268
         } else
269
             break;
270
     }
271
     /*
272
      * Check for trailing characters.
273
      */
274
     if (c != '\0' && (!isascii(c) || !isspace(c)))
275
         return (0);
276
     /*
277
      * Concoct the address according to
278
      * the number of parts specified.
279
      */
280
     n = pp - parts + 1;
281
     switch (n) {
282
 
283
     case 0:
284
         return (0);     /* initial nondigit */
285
 
286
     case 1:             /* a -- 32 bits */
287
         break;
288
 
289
     case 2:             /* a.b -- 8.24 bits */
290
         if (val > 0xffffff)
291
             return (0);
292
         val |= parts[0] << 24;
293
         break;
294
 
295
     case 3:             /* a.b.c -- 8.8.16 bits */
296
         if (val > 0xffff)
297
             return (0);
298
         val |= (parts[0] << 24) | (parts[1] << 16);
299
         break;
300
 
301
     case 4:             /* a.b.c.d -- 8.8.8.8 bits */
302
         if (val > 0xff)
303
             return (0);
304
         val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
305
         break;
306
     }
307
     if (addr)
308
         addr->s_addr = htonl(val);
309
     return (1);
310
 }
311
 
312
/* Convert numeric IP address into decimal dotted ASCII representation.
313
 * returns ptr to static buffer; not reentrant!
314
 */
315
char *inet_ntoa(struct in_addr addr)
316
{
317
  static char str[16];
318
  u32_t s_addr = addr.s_addr;
319
  char inv[3];
320
  char *rp;
321
  u8_t *ap;
322
  u8_t rem;
323
  u8_t n;
324
  u8_t i;
325
 
326
  rp = str;
327
  ap = (u8_t *)&s_addr;
328
  for(n = 0; n < 4; n++) {
329
    i = 0;
330
    do {
331
      rem = *ap % (u8_t)10;
332
      *ap /= (u8_t)10;
333
      inv[i++] = '0' + rem;
334
    } while(*ap);
335
    while(i--)
336
      *rp++ = inv[i];
337
    *rp++ = '.';
338
    ap++;
339
  }
340
  *--rp = 0;
341
  return str;
342
}
343
 
344
 
345
#ifndef BYTE_ORDER
346
#error BYTE_ORDER is not defined
347
#endif
348
#if BYTE_ORDER == LITTLE_ENDIAN
349
 
350
u16_t
351
htons(u16_t n)
352
{
353
  return ((n & 0xff) << 8) | ((n & 0xff00) >> 8);
354
}
355
 
356
u16_t
357
ntohs(u16_t n)
358
{
359
  return htons(n);
360
}
361
 
362
u32_t
363
htonl(u32_t n)
364
{
365
  return ((n & 0xff) << 24) |
366
    ((n & 0xff00) << 8) |
367
    ((n & 0xff0000) >> 8) |
368
    ((n & 0xff000000) >> 24);
369
}
370
 
371
u32_t
372
ntohl(u32_t n)
373
{
374
  return htonl(n);
375
}
376
 
377
#endif /* BYTE_ORDER == LITTLE_ENDIAN */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.