![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Embedded C project:18/12/2014
Dependencies: DS1307 TextLCD mbed
NetServices/lwip/core/ipv4/ip_addr.c@0:8d87bc453349, 2014-12-18 (annotated)
- Committer:
- ninoderkinderen
- Date:
- Thu Dec 18 09:35:49 2014 +0000
- Revision:
- 0:8d87bc453349
Programma embedded C
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ninoderkinderen | 0:8d87bc453349 | 1 | /** |
ninoderkinderen | 0:8d87bc453349 | 2 | * @file |
ninoderkinderen | 0:8d87bc453349 | 3 | * This is the IPv4 address tools implementation. |
ninoderkinderen | 0:8d87bc453349 | 4 | * |
ninoderkinderen | 0:8d87bc453349 | 5 | */ |
ninoderkinderen | 0:8d87bc453349 | 6 | |
ninoderkinderen | 0:8d87bc453349 | 7 | /* |
ninoderkinderen | 0:8d87bc453349 | 8 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
ninoderkinderen | 0:8d87bc453349 | 9 | * All rights reserved. |
ninoderkinderen | 0:8d87bc453349 | 10 | * |
ninoderkinderen | 0:8d87bc453349 | 11 | * Redistribution and use in source and binary forms, with or without modification, |
ninoderkinderen | 0:8d87bc453349 | 12 | * are permitted provided that the following conditions are met: |
ninoderkinderen | 0:8d87bc453349 | 13 | * |
ninoderkinderen | 0:8d87bc453349 | 14 | * 1. Redistributions of source code must retain the above copyright notice, |
ninoderkinderen | 0:8d87bc453349 | 15 | * this list of conditions and the following disclaimer. |
ninoderkinderen | 0:8d87bc453349 | 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
ninoderkinderen | 0:8d87bc453349 | 17 | * this list of conditions and the following disclaimer in the documentation |
ninoderkinderen | 0:8d87bc453349 | 18 | * and/or other materials provided with the distribution. |
ninoderkinderen | 0:8d87bc453349 | 19 | * 3. The name of the author may not be used to endorse or promote products |
ninoderkinderen | 0:8d87bc453349 | 20 | * derived from this software without specific prior written permission. |
ninoderkinderen | 0:8d87bc453349 | 21 | * |
ninoderkinderen | 0:8d87bc453349 | 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
ninoderkinderen | 0:8d87bc453349 | 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
ninoderkinderen | 0:8d87bc453349 | 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
ninoderkinderen | 0:8d87bc453349 | 25 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
ninoderkinderen | 0:8d87bc453349 | 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
ninoderkinderen | 0:8d87bc453349 | 27 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
ninoderkinderen | 0:8d87bc453349 | 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
ninoderkinderen | 0:8d87bc453349 | 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
ninoderkinderen | 0:8d87bc453349 | 30 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
ninoderkinderen | 0:8d87bc453349 | 31 | * OF SUCH DAMAGE. |
ninoderkinderen | 0:8d87bc453349 | 32 | * |
ninoderkinderen | 0:8d87bc453349 | 33 | * This file is part of the lwIP TCP/IP stack. |
ninoderkinderen | 0:8d87bc453349 | 34 | * |
ninoderkinderen | 0:8d87bc453349 | 35 | * Author: Adam Dunkels <adam@sics.se> |
ninoderkinderen | 0:8d87bc453349 | 36 | * |
ninoderkinderen | 0:8d87bc453349 | 37 | */ |
ninoderkinderen | 0:8d87bc453349 | 38 | |
ninoderkinderen | 0:8d87bc453349 | 39 | #include "lwip/opt.h" |
ninoderkinderen | 0:8d87bc453349 | 40 | #include "lwip/ip_addr.h" |
ninoderkinderen | 0:8d87bc453349 | 41 | #include "lwip/netif.h" |
ninoderkinderen | 0:8d87bc453349 | 42 | |
ninoderkinderen | 0:8d87bc453349 | 43 | /* used by IP_ADDR_ANY and IP_ADDR_BROADCAST in ip_addr.h */ |
ninoderkinderen | 0:8d87bc453349 | 44 | const ip_addr_t ip_addr_any = { IPADDR_ANY }; |
ninoderkinderen | 0:8d87bc453349 | 45 | const ip_addr_t ip_addr_broadcast = { IPADDR_BROADCAST }; |
ninoderkinderen | 0:8d87bc453349 | 46 | |
ninoderkinderen | 0:8d87bc453349 | 47 | /** |
ninoderkinderen | 0:8d87bc453349 | 48 | * Determine if an address is a broadcast address on a network interface |
ninoderkinderen | 0:8d87bc453349 | 49 | * |
ninoderkinderen | 0:8d87bc453349 | 50 | * @param addr address to be checked |
ninoderkinderen | 0:8d87bc453349 | 51 | * @param netif the network interface against which the address is checked |
ninoderkinderen | 0:8d87bc453349 | 52 | * @return returns non-zero if the address is a broadcast address |
ninoderkinderen | 0:8d87bc453349 | 53 | */ |
ninoderkinderen | 0:8d87bc453349 | 54 | u8_t |
ninoderkinderen | 0:8d87bc453349 | 55 | ip4_addr_isbroadcast(u32_t addr, const struct netif *netif) |
ninoderkinderen | 0:8d87bc453349 | 56 | { |
ninoderkinderen | 0:8d87bc453349 | 57 | ip_addr_t ipaddr; |
ninoderkinderen | 0:8d87bc453349 | 58 | ip4_addr_set_u32(&ipaddr, addr); |
ninoderkinderen | 0:8d87bc453349 | 59 | |
ninoderkinderen | 0:8d87bc453349 | 60 | /* all ones (broadcast) or all zeroes (old skool broadcast) */ |
ninoderkinderen | 0:8d87bc453349 | 61 | if ((~addr == IPADDR_ANY) || |
ninoderkinderen | 0:8d87bc453349 | 62 | (addr == IPADDR_ANY)) { |
ninoderkinderen | 0:8d87bc453349 | 63 | return 1; |
ninoderkinderen | 0:8d87bc453349 | 64 | /* no broadcast support on this network interface? */ |
ninoderkinderen | 0:8d87bc453349 | 65 | } else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0) { |
ninoderkinderen | 0:8d87bc453349 | 66 | /* the given address cannot be a broadcast address |
ninoderkinderen | 0:8d87bc453349 | 67 | * nor can we check against any broadcast addresses */ |
ninoderkinderen | 0:8d87bc453349 | 68 | return 0; |
ninoderkinderen | 0:8d87bc453349 | 69 | /* address matches network interface address exactly? => no broadcast */ |
ninoderkinderen | 0:8d87bc453349 | 70 | } else if (addr == ip4_addr_get_u32(&netif->ip_addr)) { |
ninoderkinderen | 0:8d87bc453349 | 71 | return 0; |
ninoderkinderen | 0:8d87bc453349 | 72 | /* on the same (sub) network... */ |
ninoderkinderen | 0:8d87bc453349 | 73 | } else if (ip_addr_netcmp(&ipaddr, &(netif->ip_addr), &(netif->netmask)) |
ninoderkinderen | 0:8d87bc453349 | 74 | /* ...and host identifier bits are all ones? =>... */ |
ninoderkinderen | 0:8d87bc453349 | 75 | && ((addr & ~ip4_addr_get_u32(&netif->netmask)) == |
ninoderkinderen | 0:8d87bc453349 | 76 | (IPADDR_BROADCAST & ~ip4_addr_get_u32(&netif->netmask)))) { |
ninoderkinderen | 0:8d87bc453349 | 77 | /* => network broadcast address */ |
ninoderkinderen | 0:8d87bc453349 | 78 | return 1; |
ninoderkinderen | 0:8d87bc453349 | 79 | } else { |
ninoderkinderen | 0:8d87bc453349 | 80 | return 0; |
ninoderkinderen | 0:8d87bc453349 | 81 | } |
ninoderkinderen | 0:8d87bc453349 | 82 | } |
ninoderkinderen | 0:8d87bc453349 | 83 | |
ninoderkinderen | 0:8d87bc453349 | 84 | /** Checks if a netmask is valid (starting with ones, then only zeros) |
ninoderkinderen | 0:8d87bc453349 | 85 | * |
ninoderkinderen | 0:8d87bc453349 | 86 | * @param netmask the IPv4 netmask to check (in network byte order!) |
ninoderkinderen | 0:8d87bc453349 | 87 | * @return 1 if the netmask is valid, 0 if it is not |
ninoderkinderen | 0:8d87bc453349 | 88 | */ |
ninoderkinderen | 0:8d87bc453349 | 89 | u8_t |
ninoderkinderen | 0:8d87bc453349 | 90 | ip4_addr_netmask_valid(u32_t netmask) |
ninoderkinderen | 0:8d87bc453349 | 91 | { |
ninoderkinderen | 0:8d87bc453349 | 92 | u32_t mask; |
ninoderkinderen | 0:8d87bc453349 | 93 | u32_t nm_hostorder = lwip_htonl(netmask); |
ninoderkinderen | 0:8d87bc453349 | 94 | |
ninoderkinderen | 0:8d87bc453349 | 95 | /* first, check for the first zero */ |
ninoderkinderen | 0:8d87bc453349 | 96 | for (mask = 1U << 31 ; mask != 0; mask >>= 1) { |
ninoderkinderen | 0:8d87bc453349 | 97 | if ((nm_hostorder & mask) == 0) { |
ninoderkinderen | 0:8d87bc453349 | 98 | break; |
ninoderkinderen | 0:8d87bc453349 | 99 | } |
ninoderkinderen | 0:8d87bc453349 | 100 | } |
ninoderkinderen | 0:8d87bc453349 | 101 | /* then check that there is no one */ |
ninoderkinderen | 0:8d87bc453349 | 102 | for (; mask != 0; mask >>= 1) { |
ninoderkinderen | 0:8d87bc453349 | 103 | if ((nm_hostorder & mask) != 0) { |
ninoderkinderen | 0:8d87bc453349 | 104 | /* there is a one after the first zero -> invalid */ |
ninoderkinderen | 0:8d87bc453349 | 105 | return 0; |
ninoderkinderen | 0:8d87bc453349 | 106 | } |
ninoderkinderen | 0:8d87bc453349 | 107 | } |
ninoderkinderen | 0:8d87bc453349 | 108 | /* no one after the first zero -> valid */ |
ninoderkinderen | 0:8d87bc453349 | 109 | return 1; |
ninoderkinderen | 0:8d87bc453349 | 110 | } |
ninoderkinderen | 0:8d87bc453349 | 111 | |
ninoderkinderen | 0:8d87bc453349 | 112 | /* Here for now until needed in other places in lwIP */ |
ninoderkinderen | 0:8d87bc453349 | 113 | #ifndef isprint |
ninoderkinderen | 0:8d87bc453349 | 114 | #define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up) |
ninoderkinderen | 0:8d87bc453349 | 115 | #define isprint(c) in_range(c, 0x20, 0x7f) |
ninoderkinderen | 0:8d87bc453349 | 116 | #define isdigit(c) in_range(c, '0', '9') |
ninoderkinderen | 0:8d87bc453349 | 117 | #define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F')) |
ninoderkinderen | 0:8d87bc453349 | 118 | #define islower(c) in_range(c, 'a', 'z') |
ninoderkinderen | 0:8d87bc453349 | 119 | #define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v') |
ninoderkinderen | 0:8d87bc453349 | 120 | #endif |
ninoderkinderen | 0:8d87bc453349 | 121 | |
ninoderkinderen | 0:8d87bc453349 | 122 | /** |
ninoderkinderen | 0:8d87bc453349 | 123 | * Ascii internet address interpretation routine. |
ninoderkinderen | 0:8d87bc453349 | 124 | * The value returned is in network order. |
ninoderkinderen | 0:8d87bc453349 | 125 | * |
ninoderkinderen | 0:8d87bc453349 | 126 | * @param cp IP address in ascii represenation (e.g. "127.0.0.1") |
ninoderkinderen | 0:8d87bc453349 | 127 | * @return ip address in network order |
ninoderkinderen | 0:8d87bc453349 | 128 | */ |
ninoderkinderen | 0:8d87bc453349 | 129 | u32_t |
ninoderkinderen | 0:8d87bc453349 | 130 | ipaddr_addr(const char *cp) |
ninoderkinderen | 0:8d87bc453349 | 131 | { |
ninoderkinderen | 0:8d87bc453349 | 132 | ip_addr_t val; |
ninoderkinderen | 0:8d87bc453349 | 133 | |
ninoderkinderen | 0:8d87bc453349 | 134 | if (ipaddr_aton(cp, &val)) { |
ninoderkinderen | 0:8d87bc453349 | 135 | return ip4_addr_get_u32(&val); |
ninoderkinderen | 0:8d87bc453349 | 136 | } |
ninoderkinderen | 0:8d87bc453349 | 137 | return (IPADDR_NONE); |
ninoderkinderen | 0:8d87bc453349 | 138 | } |
ninoderkinderen | 0:8d87bc453349 | 139 | |
ninoderkinderen | 0:8d87bc453349 | 140 | /** |
ninoderkinderen | 0:8d87bc453349 | 141 | * Check whether "cp" is a valid ascii representation |
ninoderkinderen | 0:8d87bc453349 | 142 | * of an Internet address and convert to a binary address. |
ninoderkinderen | 0:8d87bc453349 | 143 | * Returns 1 if the address is valid, 0 if not. |
ninoderkinderen | 0:8d87bc453349 | 144 | * This replaces inet_addr, the return value from which |
ninoderkinderen | 0:8d87bc453349 | 145 | * cannot distinguish between failure and a local broadcast address. |
ninoderkinderen | 0:8d87bc453349 | 146 | * |
ninoderkinderen | 0:8d87bc453349 | 147 | * @param cp IP address in ascii represenation (e.g. "127.0.0.1") |
ninoderkinderen | 0:8d87bc453349 | 148 | * @param addr pointer to which to save the ip address in network order |
ninoderkinderen | 0:8d87bc453349 | 149 | * @return 1 if cp could be converted to addr, 0 on failure |
ninoderkinderen | 0:8d87bc453349 | 150 | */ |
ninoderkinderen | 0:8d87bc453349 | 151 | int |
ninoderkinderen | 0:8d87bc453349 | 152 | ipaddr_aton(const char *cp, ip_addr_t *addr) |
ninoderkinderen | 0:8d87bc453349 | 153 | { |
ninoderkinderen | 0:8d87bc453349 | 154 | u32_t val; |
ninoderkinderen | 0:8d87bc453349 | 155 | u8_t base; |
ninoderkinderen | 0:8d87bc453349 | 156 | char c; |
ninoderkinderen | 0:8d87bc453349 | 157 | u32_t parts[4]; |
ninoderkinderen | 0:8d87bc453349 | 158 | u32_t *pp = parts; |
ninoderkinderen | 0:8d87bc453349 | 159 | |
ninoderkinderen | 0:8d87bc453349 | 160 | c = *cp; |
ninoderkinderen | 0:8d87bc453349 | 161 | for (;;) { |
ninoderkinderen | 0:8d87bc453349 | 162 | /* |
ninoderkinderen | 0:8d87bc453349 | 163 | * Collect number up to ``.''. |
ninoderkinderen | 0:8d87bc453349 | 164 | * Values are specified as for C: |
ninoderkinderen | 0:8d87bc453349 | 165 | * 0x=hex, 0=octal, 1-9=decimal. |
ninoderkinderen | 0:8d87bc453349 | 166 | */ |
ninoderkinderen | 0:8d87bc453349 | 167 | if (!isdigit(c)) |
ninoderkinderen | 0:8d87bc453349 | 168 | return (0); |
ninoderkinderen | 0:8d87bc453349 | 169 | val = 0; |
ninoderkinderen | 0:8d87bc453349 | 170 | base = 10; |
ninoderkinderen | 0:8d87bc453349 | 171 | if (c == '0') { |
ninoderkinderen | 0:8d87bc453349 | 172 | c = *++cp; |
ninoderkinderen | 0:8d87bc453349 | 173 | if (c == 'x' || c == 'X') { |
ninoderkinderen | 0:8d87bc453349 | 174 | base = 16; |
ninoderkinderen | 0:8d87bc453349 | 175 | c = *++cp; |
ninoderkinderen | 0:8d87bc453349 | 176 | } else |
ninoderkinderen | 0:8d87bc453349 | 177 | base = 8; |
ninoderkinderen | 0:8d87bc453349 | 178 | } |
ninoderkinderen | 0:8d87bc453349 | 179 | for (;;) { |
ninoderkinderen | 0:8d87bc453349 | 180 | if (isdigit(c)) { |
ninoderkinderen | 0:8d87bc453349 | 181 | val = (val * base) + (int)(c - '0'); |
ninoderkinderen | 0:8d87bc453349 | 182 | c = *++cp; |
ninoderkinderen | 0:8d87bc453349 | 183 | } else if (base == 16 && isxdigit(c)) { |
ninoderkinderen | 0:8d87bc453349 | 184 | val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A')); |
ninoderkinderen | 0:8d87bc453349 | 185 | c = *++cp; |
ninoderkinderen | 0:8d87bc453349 | 186 | } else |
ninoderkinderen | 0:8d87bc453349 | 187 | break; |
ninoderkinderen | 0:8d87bc453349 | 188 | } |
ninoderkinderen | 0:8d87bc453349 | 189 | if (c == '.') { |
ninoderkinderen | 0:8d87bc453349 | 190 | /* |
ninoderkinderen | 0:8d87bc453349 | 191 | * Internet format: |
ninoderkinderen | 0:8d87bc453349 | 192 | * a.b.c.d |
ninoderkinderen | 0:8d87bc453349 | 193 | * a.b.c (with c treated as 16 bits) |
ninoderkinderen | 0:8d87bc453349 | 194 | * a.b (with b treated as 24 bits) |
ninoderkinderen | 0:8d87bc453349 | 195 | */ |
ninoderkinderen | 0:8d87bc453349 | 196 | if (pp >= parts + 3) { |
ninoderkinderen | 0:8d87bc453349 | 197 | return (0); |
ninoderkinderen | 0:8d87bc453349 | 198 | } |
ninoderkinderen | 0:8d87bc453349 | 199 | *pp++ = val; |
ninoderkinderen | 0:8d87bc453349 | 200 | c = *++cp; |
ninoderkinderen | 0:8d87bc453349 | 201 | } else |
ninoderkinderen | 0:8d87bc453349 | 202 | break; |
ninoderkinderen | 0:8d87bc453349 | 203 | } |
ninoderkinderen | 0:8d87bc453349 | 204 | /* |
ninoderkinderen | 0:8d87bc453349 | 205 | * Check for trailing characters. |
ninoderkinderen | 0:8d87bc453349 | 206 | */ |
ninoderkinderen | 0:8d87bc453349 | 207 | if (c != '\0' && !isspace(c)) { |
ninoderkinderen | 0:8d87bc453349 | 208 | return (0); |
ninoderkinderen | 0:8d87bc453349 | 209 | } |
ninoderkinderen | 0:8d87bc453349 | 210 | /* |
ninoderkinderen | 0:8d87bc453349 | 211 | * Concoct the address according to |
ninoderkinderen | 0:8d87bc453349 | 212 | * the number of parts specified. |
ninoderkinderen | 0:8d87bc453349 | 213 | */ |
ninoderkinderen | 0:8d87bc453349 | 214 | switch (pp - parts + 1) { |
ninoderkinderen | 0:8d87bc453349 | 215 | |
ninoderkinderen | 0:8d87bc453349 | 216 | case 0: |
ninoderkinderen | 0:8d87bc453349 | 217 | return (0); /* initial nondigit */ |
ninoderkinderen | 0:8d87bc453349 | 218 | |
ninoderkinderen | 0:8d87bc453349 | 219 | case 1: /* a -- 32 bits */ |
ninoderkinderen | 0:8d87bc453349 | 220 | break; |
ninoderkinderen | 0:8d87bc453349 | 221 | |
ninoderkinderen | 0:8d87bc453349 | 222 | case 2: /* a.b -- 8.24 bits */ |
ninoderkinderen | 0:8d87bc453349 | 223 | if (val > 0xffffffUL) { |
ninoderkinderen | 0:8d87bc453349 | 224 | return (0); |
ninoderkinderen | 0:8d87bc453349 | 225 | } |
ninoderkinderen | 0:8d87bc453349 | 226 | val |= parts[0] << 24; |
ninoderkinderen | 0:8d87bc453349 | 227 | break; |
ninoderkinderen | 0:8d87bc453349 | 228 | |
ninoderkinderen | 0:8d87bc453349 | 229 | case 3: /* a.b.c -- 8.8.16 bits */ |
ninoderkinderen | 0:8d87bc453349 | 230 | if (val > 0xffff) { |
ninoderkinderen | 0:8d87bc453349 | 231 | return (0); |
ninoderkinderen | 0:8d87bc453349 | 232 | } |
ninoderkinderen | 0:8d87bc453349 | 233 | val |= (parts[0] << 24) | (parts[1] << 16); |
ninoderkinderen | 0:8d87bc453349 | 234 | break; |
ninoderkinderen | 0:8d87bc453349 | 235 | |
ninoderkinderen | 0:8d87bc453349 | 236 | case 4: /* a.b.c.d -- 8.8.8.8 bits */ |
ninoderkinderen | 0:8d87bc453349 | 237 | if (val > 0xff) { |
ninoderkinderen | 0:8d87bc453349 | 238 | return (0); |
ninoderkinderen | 0:8d87bc453349 | 239 | } |
ninoderkinderen | 0:8d87bc453349 | 240 | val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); |
ninoderkinderen | 0:8d87bc453349 | 241 | break; |
ninoderkinderen | 0:8d87bc453349 | 242 | default: |
ninoderkinderen | 0:8d87bc453349 | 243 | LWIP_ASSERT("unhandled", 0); |
ninoderkinderen | 0:8d87bc453349 | 244 | break; |
ninoderkinderen | 0:8d87bc453349 | 245 | } |
ninoderkinderen | 0:8d87bc453349 | 246 | if (addr) { |
ninoderkinderen | 0:8d87bc453349 | 247 | ip4_addr_set_u32(addr, htonl(val)); |
ninoderkinderen | 0:8d87bc453349 | 248 | } |
ninoderkinderen | 0:8d87bc453349 | 249 | return (1); |
ninoderkinderen | 0:8d87bc453349 | 250 | } |
ninoderkinderen | 0:8d87bc453349 | 251 | |
ninoderkinderen | 0:8d87bc453349 | 252 | /** |
ninoderkinderen | 0:8d87bc453349 | 253 | * Convert numeric IP address into decimal dotted ASCII representation. |
ninoderkinderen | 0:8d87bc453349 | 254 | * returns ptr to static buffer; not reentrant! |
ninoderkinderen | 0:8d87bc453349 | 255 | * |
ninoderkinderen | 0:8d87bc453349 | 256 | * @param addr ip address in network order to convert |
ninoderkinderen | 0:8d87bc453349 | 257 | * @return pointer to a global static (!) buffer that holds the ASCII |
ninoderkinderen | 0:8d87bc453349 | 258 | * represenation of addr |
ninoderkinderen | 0:8d87bc453349 | 259 | */ |
ninoderkinderen | 0:8d87bc453349 | 260 | char * |
ninoderkinderen | 0:8d87bc453349 | 261 | ipaddr_ntoa(const ip_addr_t *addr) |
ninoderkinderen | 0:8d87bc453349 | 262 | { |
ninoderkinderen | 0:8d87bc453349 | 263 | static char str[16]; |
ninoderkinderen | 0:8d87bc453349 | 264 | return ipaddr_ntoa_r(addr, str, 16); |
ninoderkinderen | 0:8d87bc453349 | 265 | } |
ninoderkinderen | 0:8d87bc453349 | 266 | |
ninoderkinderen | 0:8d87bc453349 | 267 | /** |
ninoderkinderen | 0:8d87bc453349 | 268 | * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used. |
ninoderkinderen | 0:8d87bc453349 | 269 | * |
ninoderkinderen | 0:8d87bc453349 | 270 | * @param addr ip address in network order to convert |
ninoderkinderen | 0:8d87bc453349 | 271 | * @param buf target buffer where the string is stored |
ninoderkinderen | 0:8d87bc453349 | 272 | * @param buflen length of buf |
ninoderkinderen | 0:8d87bc453349 | 273 | * @return either pointer to buf which now holds the ASCII |
ninoderkinderen | 0:8d87bc453349 | 274 | * representation of addr or NULL if buf was too small |
ninoderkinderen | 0:8d87bc453349 | 275 | */ |
ninoderkinderen | 0:8d87bc453349 | 276 | char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen) |
ninoderkinderen | 0:8d87bc453349 | 277 | { |
ninoderkinderen | 0:8d87bc453349 | 278 | u32_t s_addr; |
ninoderkinderen | 0:8d87bc453349 | 279 | char inv[3]; |
ninoderkinderen | 0:8d87bc453349 | 280 | char *rp; |
ninoderkinderen | 0:8d87bc453349 | 281 | u8_t *ap; |
ninoderkinderen | 0:8d87bc453349 | 282 | u8_t rem; |
ninoderkinderen | 0:8d87bc453349 | 283 | u8_t n; |
ninoderkinderen | 0:8d87bc453349 | 284 | u8_t i; |
ninoderkinderen | 0:8d87bc453349 | 285 | int len = 0; |
ninoderkinderen | 0:8d87bc453349 | 286 | |
ninoderkinderen | 0:8d87bc453349 | 287 | s_addr = ip4_addr_get_u32(addr); |
ninoderkinderen | 0:8d87bc453349 | 288 | |
ninoderkinderen | 0:8d87bc453349 | 289 | rp = buf; |
ninoderkinderen | 0:8d87bc453349 | 290 | ap = (u8_t *)&s_addr; |
ninoderkinderen | 0:8d87bc453349 | 291 | for(n = 0; n < 4; n++) { |
ninoderkinderen | 0:8d87bc453349 | 292 | i = 0; |
ninoderkinderen | 0:8d87bc453349 | 293 | do { |
ninoderkinderen | 0:8d87bc453349 | 294 | rem = *ap % (u8_t)10; |
ninoderkinderen | 0:8d87bc453349 | 295 | *ap /= (u8_t)10; |
ninoderkinderen | 0:8d87bc453349 | 296 | inv[i++] = '0' + rem; |
ninoderkinderen | 0:8d87bc453349 | 297 | } while(*ap); |
ninoderkinderen | 0:8d87bc453349 | 298 | while(i--) { |
ninoderkinderen | 0:8d87bc453349 | 299 | if (len++ >= buflen) { |
ninoderkinderen | 0:8d87bc453349 | 300 | return NULL; |
ninoderkinderen | 0:8d87bc453349 | 301 | } |
ninoderkinderen | 0:8d87bc453349 | 302 | *rp++ = inv[i]; |
ninoderkinderen | 0:8d87bc453349 | 303 | } |
ninoderkinderen | 0:8d87bc453349 | 304 | if (len++ >= buflen) { |
ninoderkinderen | 0:8d87bc453349 | 305 | return NULL; |
ninoderkinderen | 0:8d87bc453349 | 306 | } |
ninoderkinderen | 0:8d87bc453349 | 307 | *rp++ = '.'; |
ninoderkinderen | 0:8d87bc453349 | 308 | ap++; |
ninoderkinderen | 0:8d87bc453349 | 309 | } |
ninoderkinderen | 0:8d87bc453349 | 310 | *--rp = 0; |
ninoderkinderen | 0:8d87bc453349 | 311 | return buf; |
ninoderkinderen | 0:8d87bc453349 | 312 | } |