Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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