3 * Functions common to all TCP/IPv4 modules, such as the byte order functions.
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
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
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
41 #include "lwip/inet.h"
43 /* Here for now until needed in other places in lwIP */
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')
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
61 inet_addr(const char *cp
)
65 if (inet_aton(cp
, &val
)) {
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
83 inet_aton(const char *cp
, struct in_addr
*addr
)
93 * Collect number up to ``.''.
94 * Values are specified as for C:
95 * 0x=hex, 0=octal, 1-9=decimal.
103 if (c
== 'x' || c
== 'X') {
111 val
= (val
* base
) + (int)(c
- '0');
113 } else if (base
== 16 && isxdigit(c
)) {
114 val
= (val
<< 4) | (int)(c
+ 10 - (islower(c
) ? 'a' : 'A'));
123 * a.b.c (with c treated as 16 bits)
124 * a.b (with b treated as 24 bits)
134 * Check for trailing characters.
136 if (c
!= '\0' && (!isprint(c
) || !isspace(c
)))
139 * Concoct the address according to
140 * the number of parts specified.
146 return (0); /* initial nondigit */
148 case 1: /* a -- 32 bits */
151 case 2: /* a.b -- 8.24 bits */
152 if (val
> 0xffffffUL
)
154 val
|= parts
[0] << 24;
157 case 3: /* a.b.c -- 8.8.16 bits */
160 val
|= (parts
[0] << 24) | (parts
[1] << 16);
163 case 4: /* a.b.c.d -- 8.8.8.8 bits */
166 val
|= (parts
[0] << 24) | (parts
[1] << 16) | (parts
[2] << 8);
170 addr
->s_addr
= htonl(val
);
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
183 inet_ntoa(struct in_addr addr
)
186 u32_t s_addr
= addr
.s_addr
;
195 ap
= (u8_t
*)&s_addr
;
196 for(n
= 0; n
< 4; n
++) {
199 rem
= *ap
% (u8_t
)10;
201 inv
[i
++] = '0' + rem
;
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
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
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
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
278 #endif /* (LWIP_PLATFORM_BYTESWAP == 0) && (BYTE_ORDER == LITTLE_ENDIAN) */