FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Common / ethernet / lwIP_132 / src / core / ipv4 / inet.c
blob69baf1d5083e322b8683f1571a97a44c910e6334
1 /**
2 * @file
3 * Functions common to all TCP/IPv4 modules, such as the byte order functions.
5 */
7 /*
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 * OF SUCH DAMAGE.
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
39 #include "lwip/opt.h"
41 #include "lwip/inet.h"
43 /* Here for now until needed in other places in lwIP */
44 #ifndef isprint
45 #define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up)
46 #define isprint(c) in_range(c, 0x20, 0x7f)
47 #define isdigit(c) in_range(c, '0', '9')
48 #define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
49 #define islower(c) in_range(c, 'a', 'z')
50 #define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
51 #endif
53 /**
54 * Ascii internet address interpretation routine.
55 * The value returned is in network order.
57 * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
58 * @return ip address in network order
60 u32_t
61 inet_addr(const char *cp)
63 struct in_addr val;
65 if (inet_aton(cp, &val)) {
66 return (val.s_addr);
68 return (INADDR_NONE);
71 /**
72 * Check whether "cp" is a valid ascii representation
73 * of an Internet address and convert to a binary address.
74 * Returns 1 if the address is valid, 0 if not.
75 * This replaces inet_addr, the return value from which
76 * cannot distinguish between failure and a local broadcast address.
78 * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
79 * @param addr pointer to which to save the ip address in network order
80 * @return 1 if cp could be converted to addr, 0 on failure
82 int
83 inet_aton(const char *cp, struct in_addr *addr)
85 u32_t val;
86 u8_t base;
87 char c;
88 u32_t parts[4];
89 u32_t *pp = parts;
91 c = *cp;
92 for (;;) {
94 * Collect number up to ``.''.
95 * Values are specified as for C:
96 * 0x=hex, 0=octal, 1-9=decimal.
98 if (!isdigit(c))
99 return (0);
100 val = 0;
101 base = 10;
102 if (c == '0') {
103 c = *++cp;
104 if (c == 'x' || c == 'X') {
105 base = 16;
106 c = *++cp;
107 } else
108 base = 8;
110 for (;;) {
111 if (isdigit(c)) {
112 val = (val * base) + (int)(c - '0');
113 c = *++cp;
114 } else if (base == 16 && isxdigit(c)) {
115 val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A'));
116 c = *++cp;
117 } else
118 break;
120 if (c == '.') {
122 * Internet format:
123 * a.b.c.d
124 * a.b.c (with c treated as 16 bits)
125 * a.b (with b treated as 24 bits)
127 if (pp >= parts + 3)
128 return (0);
129 *pp++ = val;
130 c = *++cp;
131 } else
132 break;
135 * Check for trailing characters.
137 if (c != '\0' && !isspace(c))
138 return (0);
140 * Concoct the address according to
141 * the number of parts specified.
143 switch (pp - parts + 1) {
145 case 0:
146 return (0); /* initial nondigit */
148 case 1: /* a -- 32 bits */
149 break;
151 case 2: /* a.b -- 8.24 bits */
152 if (val > 0xffffffUL)
153 return (0);
154 val |= parts[0] << 24;
155 break;
157 case 3: /* a.b.c -- 8.8.16 bits */
158 if (val > 0xffff)
159 return (0);
160 val |= (parts[0] << 24) | (parts[1] << 16);
161 break;
163 case 4: /* a.b.c.d -- 8.8.8.8 bits */
164 if (val > 0xff)
165 return (0);
166 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
167 break;
169 if (addr)
170 addr->s_addr = htonl(val);
171 return (1);
175 * Convert numeric IP address into decimal dotted ASCII representation.
176 * returns ptr to static buffer; not reentrant!
178 * @param addr ip address in network order to convert
179 * @return pointer to a global static (!) buffer that holds the ASCII
180 * represenation of addr
182 char *
183 inet_ntoa(struct in_addr addr)
185 static char str[16];
186 u32_t s_addr = addr.s_addr;
187 char inv[3];
188 char *rp;
189 u8_t *ap;
190 u8_t rem;
191 u8_t n;
192 u8_t i;
194 rp = str;
195 ap = (u8_t *)&s_addr;
196 for(n = 0; n < 4; n++) {
197 i = 0;
198 do {
199 rem = *ap % (u8_t)10;
200 *ap /= (u8_t)10;
201 inv[i++] = '0' + rem;
202 } while(*ap);
203 while(i--)
204 *rp++ = inv[i];
205 *rp++ = '.';
206 ap++;
208 *--rp = 0;
209 return str;
213 * These are reference implementations of the byte swapping functions.
214 * Again with the aim of being simple, correct and fully portable.
215 * Byte swapping is the second thing you would want to optimize. You will
216 * need to port it to your architecture and in your cc.h:
218 * #define LWIP_PLATFORM_BYTESWAP 1
219 * #define LWIP_PLATFORM_HTONS(x) <your_htons>
220 * #define LWIP_PLATFORM_HTONL(x) <your_htonl>
222 * Note ntohs() and ntohl() are merely references to the htonx counterparts.
225 #if (LWIP_PLATFORM_BYTESWAP == 0) && (BYTE_ORDER == LITTLE_ENDIAN)
228 * Convert an u16_t from host- to network byte order.
230 * @param n u16_t in host byte order
231 * @return n in network byte order
233 u16_t
234 htons(u16_t n)
236 return ((n & 0xff) << 8) | ((n & 0xff00) >> 8);
240 * Convert an u16_t from network- to host byte order.
242 * @param n u16_t in network byte order
243 * @return n in host byte order
245 u16_t
246 ntohs(u16_t n)
248 return htons(n);
252 * Convert an u32_t from host- to network byte order.
254 * @param n u32_t in host byte order
255 * @return n in network byte order
257 u32_t
258 htonl(u32_t n)
260 return ((n & 0xff) << 24) |
261 ((n & 0xff00) << 8) |
262 ((n & 0xff0000UL) >> 8) |
263 ((n & 0xff000000UL) >> 24);
267 * Convert an u32_t from network- to host byte order.
269 * @param n u32_t in network byte order
270 * @return n in host byte order
272 u32_t
273 ntohl(u32_t n)
275 return htonl(n);
278 #endif /* (LWIP_PLATFORM_BYTESWAP == 0) && (BYTE_ORDER == LITTLE_ENDIAN) */