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