2007-08-09 22:21:44 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* This is the IPv4 address tools implementation.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
/*
|
2004-02-07 00:30:03 +00:00
|
|
|
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
2002-10-19 12:59:30 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
|
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the lwIP TCP/IP stack.
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-09-07 23:01:59 +00:00
|
|
|
#include "lwip/opt.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
#include "lwip/ip_addr.h"
|
2004-04-28 23:18:20 +00:00
|
|
|
#include "lwip/netif.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2010-02-04 18:47:44 +00:00
|
|
|
/* used by IP_ADDR_ANY and IP_ADDR_BROADCAST in ip_addr.h */
|
|
|
|
const ip_addr_t ip_addr_any = { IPADDR_ANY };
|
|
|
|
const ip_addr_t ip_addr_broadcast = { IPADDR_BROADCAST };
|
2004-02-24 21:59:17 +00:00
|
|
|
|
2007-06-08 11:51:57 +00:00
|
|
|
/**
|
|
|
|
* Determine if an address is a broadcast address on a network interface
|
2004-04-23 16:45:06 +00:00
|
|
|
*
|
2004-04-28 23:40:53 +00:00
|
|
|
* @param addr address to be checked
|
2004-04-23 16:45:06 +00:00
|
|
|
* @param netif the network interface against which the address is checked
|
|
|
|
* @return returns non-zero if the address is a broadcast address
|
2004-02-24 21:59:17 +00:00
|
|
|
*/
|
2010-03-28 10:28:32 +00:00
|
|
|
u8_t ip_addr_isbroadcast(const ip_addr_t *addr, const struct netif *netif)
|
2004-02-24 21:59:17 +00:00
|
|
|
{
|
2006-04-04 07:21:54 +00:00
|
|
|
u32_t addr2test;
|
|
|
|
|
2010-02-04 17:21:32 +00:00
|
|
|
addr2test = ip4_addr_get_u32(addr);
|
2004-02-24 21:59:17 +00:00
|
|
|
/* all ones (broadcast) or all zeroes (old skool broadcast) */
|
2010-02-04 17:21:32 +00:00
|
|
|
if ((~addr2test == IPADDR_ANY) ||
|
|
|
|
(addr2test == IPADDR_ANY))
|
2004-02-24 21:59:17 +00:00
|
|
|
return 1;
|
2004-04-28 23:40:53 +00:00
|
|
|
/* no broadcast support on this network interface? */
|
2004-04-28 23:36:39 +00:00
|
|
|
else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0)
|
2004-04-29 21:49:12 +00:00
|
|
|
/* the given address cannot be a broadcast address
|
2004-04-28 23:40:53 +00:00
|
|
|
* nor can we check against any broadcast addresses */
|
2004-02-24 21:59:17 +00:00
|
|
|
return 0;
|
2004-03-11 21:20:10 +00:00
|
|
|
/* address matches network interface address exactly? => no broadcast */
|
2010-02-04 17:21:32 +00:00
|
|
|
else if (addr2test == ip4_addr_get_u32(&netif->ip_addr))
|
2004-02-24 21:59:17 +00:00
|
|
|
return 0;
|
2004-11-25 13:57:05 +00:00
|
|
|
/* on the same (sub) network... */
|
2004-11-25 14:03:31 +00:00
|
|
|
else if (ip_addr_netcmp(addr, &(netif->ip_addr), &(netif->netmask))
|
2004-11-25 13:57:05 +00:00
|
|
|
/* ...and host identifier bits are all ones? =>... */
|
2010-02-04 17:21:32 +00:00
|
|
|
&& ((addr2test & ~ip4_addr_get_u32(&netif->netmask)) ==
|
|
|
|
(IPADDR_BROADCAST & ~ip4_addr_get_u32(&netif->netmask))))
|
2004-11-25 13:57:05 +00:00
|
|
|
/* => network broadcast address */
|
2004-02-24 21:59:17 +00:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2010-01-29 08:20:32 +00:00
|
|
|
|
|
|
|
/* Here for now until needed in other places in lwIP */
|
|
|
|
#ifndef isprint
|
|
|
|
#define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up)
|
|
|
|
#define isprint(c) in_range(c, 0x20, 0x7f)
|
|
|
|
#define isdigit(c) in_range(c, '0', '9')
|
|
|
|
#define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
|
|
|
|
#define islower(c) in_range(c, 'a', 'z')
|
|
|
|
#define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ascii internet address interpretation routine.
|
|
|
|
* The value returned is in network order.
|
|
|
|
*
|
|
|
|
* @param cp IP address in ascii represenation (e.g. "127.0.0.1")
|
|
|
|
* @return ip address in network order
|
|
|
|
*/
|
|
|
|
u32_t
|
|
|
|
ipaddr_addr(const char *cp)
|
|
|
|
{
|
2010-02-04 18:47:44 +00:00
|
|
|
ip_addr_t val;
|
2010-01-29 08:20:32 +00:00
|
|
|
|
|
|
|
if (ipaddr_aton(cp, &val)) {
|
2010-02-04 17:21:32 +00:00
|
|
|
return ip4_addr_get_u32(&val);
|
2010-01-29 08:20:32 +00:00
|
|
|
}
|
|
|
|
return (IPADDR_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether "cp" is a valid ascii representation
|
|
|
|
* of an Internet address and convert to a binary address.
|
|
|
|
* Returns 1 if the address is valid, 0 if not.
|
|
|
|
* This replaces inet_addr, the return value from which
|
|
|
|
* cannot distinguish between failure and a local broadcast address.
|
|
|
|
*
|
|
|
|
* @param cp IP address in ascii represenation (e.g. "127.0.0.1")
|
|
|
|
* @param addr pointer to which to save the ip address in network order
|
|
|
|
* @return 1 if cp could be converted to addr, 0 on failure
|
|
|
|
*/
|
|
|
|
int
|
2010-02-04 18:47:44 +00:00
|
|
|
ipaddr_aton(const char *cp, ip_addr_t *addr)
|
2010-01-29 08:20:32 +00:00
|
|
|
{
|
|
|
|
u32_t val;
|
|
|
|
u8_t base;
|
|
|
|
char c;
|
|
|
|
u32_t parts[4];
|
|
|
|
u32_t *pp = parts;
|
|
|
|
|
|
|
|
c = *cp;
|
|
|
|
for (;;) {
|
|
|
|
/*
|
|
|
|
* Collect number up to ``.''.
|
|
|
|
* Values are specified as for C:
|
|
|
|
* 0x=hex, 0=octal, 1-9=decimal.
|
|
|
|
*/
|
|
|
|
if (!isdigit(c))
|
|
|
|
return (0);
|
|
|
|
val = 0;
|
|
|
|
base = 10;
|
|
|
|
if (c == '0') {
|
|
|
|
c = *++cp;
|
|
|
|
if (c == 'x' || c == 'X') {
|
|
|
|
base = 16;
|
|
|
|
c = *++cp;
|
|
|
|
} else
|
|
|
|
base = 8;
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
if (isdigit(c)) {
|
|
|
|
val = (val * base) + (int)(c - '0');
|
|
|
|
c = *++cp;
|
|
|
|
} else if (base == 16 && isxdigit(c)) {
|
|
|
|
val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A'));
|
|
|
|
c = *++cp;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c == '.') {
|
|
|
|
/*
|
|
|
|
* Internet format:
|
|
|
|
* a.b.c.d
|
|
|
|
* a.b.c (with c treated as 16 bits)
|
|
|
|
* a.b (with b treated as 24 bits)
|
|
|
|
*/
|
2010-03-26 14:07:05 +00:00
|
|
|
if (pp >= parts + 3) {
|
2010-01-29 08:20:32 +00:00
|
|
|
return (0);
|
2010-03-26 14:07:05 +00:00
|
|
|
}
|
2010-01-29 08:20:32 +00:00
|
|
|
*pp++ = val;
|
|
|
|
c = *++cp;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Check for trailing characters.
|
|
|
|
*/
|
2010-03-26 14:07:05 +00:00
|
|
|
if (c != '\0' && !isspace(c)) {
|
2010-01-29 08:20:32 +00:00
|
|
|
return (0);
|
2010-03-26 14:07:05 +00:00
|
|
|
}
|
2010-01-29 08:20:32 +00:00
|
|
|
/*
|
|
|
|
* Concoct the address according to
|
|
|
|
* the number of parts specified.
|
|
|
|
*/
|
|
|
|
switch (pp - parts + 1) {
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
return (0); /* initial nondigit */
|
|
|
|
|
|
|
|
case 1: /* a -- 32 bits */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* a.b -- 8.24 bits */
|
2010-03-26 14:07:05 +00:00
|
|
|
if (val > 0xffffffUL) {
|
2010-01-29 08:20:32 +00:00
|
|
|
return (0);
|
2010-03-26 14:07:05 +00:00
|
|
|
}
|
2010-01-29 08:20:32 +00:00
|
|
|
val |= parts[0] << 24;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: /* a.b.c -- 8.8.16 bits */
|
2010-03-26 14:07:05 +00:00
|
|
|
if (val > 0xffff) {
|
2010-01-29 08:20:32 +00:00
|
|
|
return (0);
|
2010-03-26 14:07:05 +00:00
|
|
|
}
|
2010-01-29 08:20:32 +00:00
|
|
|
val |= (parts[0] << 24) | (parts[1] << 16);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4: /* a.b.c.d -- 8.8.8.8 bits */
|
2010-03-26 14:07:05 +00:00
|
|
|
if (val > 0xff) {
|
2010-01-29 08:20:32 +00:00
|
|
|
return (0);
|
2010-03-26 14:07:05 +00:00
|
|
|
}
|
2010-01-29 08:20:32 +00:00
|
|
|
val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
|
|
|
|
break;
|
2010-03-26 14:07:05 +00:00
|
|
|
default:
|
|
|
|
LWIP_ASSERT("unhandled", 0);
|
|
|
|
break;
|
2010-01-29 08:20:32 +00:00
|
|
|
}
|
2010-02-04 17:21:32 +00:00
|
|
|
if (addr) {
|
|
|
|
ip4_addr_set_u32(addr, htonl(val));
|
|
|
|
}
|
2010-01-29 08:20:32 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert numeric IP address into decimal dotted ASCII representation.
|
|
|
|
* returns ptr to static buffer; not reentrant!
|
|
|
|
*
|
|
|
|
* @param addr ip address in network order to convert
|
|
|
|
* @return pointer to a global static (!) buffer that holds the ASCII
|
|
|
|
* represenation of addr
|
|
|
|
*/
|
|
|
|
char *
|
2010-03-28 10:28:32 +00:00
|
|
|
ipaddr_ntoa(const ip_addr_t *addr)
|
2010-01-29 08:20:32 +00:00
|
|
|
{
|
|
|
|
static char str[16];
|
2010-02-08 20:14:21 +00:00
|
|
|
return ipaddr_ntoa_r(addr, str, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used.
|
|
|
|
*
|
|
|
|
* @param addr ip address in network order to convert
|
|
|
|
* @param buf target buffer where the string is stored
|
|
|
|
* @param buflen length of buf
|
|
|
|
* @return either pointer to buf which now holds the ASCII
|
|
|
|
* representation of addr or NULL if buf was too small
|
|
|
|
*/
|
2010-03-28 10:28:32 +00:00
|
|
|
char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)
|
2010-02-08 20:14:21 +00:00
|
|
|
{
|
2010-02-04 17:21:32 +00:00
|
|
|
u32_t s_addr;
|
2010-01-29 08:20:32 +00:00
|
|
|
char inv[3];
|
|
|
|
char *rp;
|
|
|
|
u8_t *ap;
|
|
|
|
u8_t rem;
|
|
|
|
u8_t n;
|
|
|
|
u8_t i;
|
2010-02-08 20:14:21 +00:00
|
|
|
int len = 0;
|
2010-01-29 08:20:32 +00:00
|
|
|
|
2010-02-04 17:21:32 +00:00
|
|
|
s_addr = ip4_addr_get_u32(addr);
|
|
|
|
|
2010-02-08 20:14:21 +00:00
|
|
|
rp = buf;
|
2010-01-29 08:20:32 +00:00
|
|
|
ap = (u8_t *)&s_addr;
|
|
|
|
for(n = 0; n < 4; n++) {
|
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
rem = *ap % (u8_t)10;
|
|
|
|
*ap /= (u8_t)10;
|
|
|
|
inv[i++] = '0' + rem;
|
|
|
|
} while(*ap);
|
2010-02-08 20:14:21 +00:00
|
|
|
while(i--) {
|
|
|
|
if (len++ >= buflen) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-01-29 08:20:32 +00:00
|
|
|
*rp++ = inv[i];
|
2010-02-08 20:14:21 +00:00
|
|
|
}
|
|
|
|
if (len++ >= buflen) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-01-29 08:20:32 +00:00
|
|
|
*rp++ = '.';
|
|
|
|
ap++;
|
|
|
|
}
|
|
|
|
*--rp = 0;
|
2010-02-08 20:14:21 +00:00
|
|
|
return buf;
|
|
|
|
}
|