Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
initial version

Who changed what in which revision?

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