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