Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwip_ip4_addr.c Source File

lwip_ip4_addr.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * This is the IPv4 address tools implementation.
00004  *
00005  */
00006 
00007 /*
00008  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00009  * All rights reserved.
00010  *
00011  * Redistribution and use in source and binary forms, with or without modification,
00012  * are permitted provided that the following conditions are met:
00013  *
00014  * 1. Redistributions of source code must retain the above copyright notice,
00015  *    this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright notice,
00017  *    this list of conditions and the following disclaimer in the documentation
00018  *    and/or other materials provided with the distribution.
00019  * 3. The name of the author may not be used to endorse or promote products
00020  *    derived from this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00023  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00024  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00025  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00026  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00027  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00030  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00031  * OF SUCH DAMAGE.
00032  *
00033  * This file is part of the lwIP TCP/IP stack.
00034  *
00035  * Author: Adam Dunkels <adam@sics.se>
00036  *
00037  */
00038 
00039 #include "lwip/opt.h"
00040 
00041 #if LWIP_IPV4
00042 
00043 #include "lwip/ip_addr.h"
00044 #include "lwip/netif.h"
00045 
00046 /* used by IP4_ADDR_ANY and IP_ADDR_BROADCAST in ip_addr.h */
00047 const ip_addr_t ip_addr_any = IPADDR4_INIT(IPADDR_ANY);
00048 const ip_addr_t ip_addr_broadcast = IPADDR4_INIT(IPADDR_BROADCAST);
00049 
00050 /**
00051  * Determine if an address is a broadcast address on a network interface
00052  *
00053  * @param addr address to be checked
00054  * @param netif the network interface against which the address is checked
00055  * @return returns non-zero if the address is a broadcast address
00056  */
00057 u8_t
00058 ip4_addr_isbroadcast_u32(u32_t addr, const struct netif *netif)
00059 {
00060   ip4_addr_t ipaddr;
00061   ip4_addr_set_u32(&ipaddr, addr);
00062 
00063   /* all ones (broadcast) or all zeroes (old skool broadcast) */
00064   if ((~addr == IPADDR_ANY) ||
00065       (addr == IPADDR_ANY)) {
00066     return 1;
00067     /* no broadcast support on this network interface? */
00068   } else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0) {
00069     /* the given address cannot be a broadcast address
00070      * nor can we check against any broadcast addresses */
00071     return 0;
00072     /* address matches network interface address exactly? => no broadcast */
00073   } else if (addr == ip4_addr_get_u32(netif_ip4_addr(netif))) {
00074     return 0;
00075     /*  on the same (sub) network... */
00076   } else if (ip4_addr_netcmp(&ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif))
00077              /* ...and host identifier bits are all ones? =>... */
00078              && ((addr & ~ip4_addr_get_u32(netif_ip4_netmask(netif))) ==
00079                  (IPADDR_BROADCAST & ~ip4_addr_get_u32(netif_ip4_netmask(netif))))) {
00080     /* => network broadcast address */
00081     return 1;
00082   } else {
00083     return 0;
00084   }
00085 }
00086 
00087 /** Checks if a netmask is valid (starting with ones, then only zeros)
00088  *
00089  * @param netmask the IPv4 netmask to check (in network byte order!)
00090  * @return 1 if the netmask is valid, 0 if it is not
00091  */
00092 u8_t
00093 ip4_addr_netmask_valid(u32_t netmask)
00094 {
00095   u32_t mask;
00096   u32_t nm_hostorder = lwip_htonl(netmask);
00097 
00098   /* first, check for the first zero */
00099   for (mask = 1UL << 31 ; mask != 0; mask >>= 1) {
00100     if ((nm_hostorder & mask) == 0) {
00101       break;
00102     }
00103   }
00104   /* then check that there is no one */
00105   for (; mask != 0; mask >>= 1) {
00106     if ((nm_hostorder & mask) != 0) {
00107       /* there is a one after the first zero -> invalid */
00108       return 0;
00109     }
00110   }
00111   /* no one after the first zero -> valid */
00112   return 1;
00113 }
00114 
00115 /**
00116  * Ascii internet address interpretation routine.
00117  * The value returned is in network order.
00118  *
00119  * @param cp IP address in ascii representation (e.g. "127.0.0.1")
00120  * @return ip address in network order
00121  */
00122 u32_t
00123 ipaddr_addr(const char *cp)
00124 {
00125   ip4_addr_t val;
00126 
00127   if (ip4addr_aton(cp, &val)) {
00128     return ip4_addr_get_u32(&val);
00129   }
00130   return (IPADDR_NONE);
00131 }
00132 
00133 /**
00134  * Check whether "cp" is a valid ascii representation
00135  * of an Internet address and convert to a binary address.
00136  * Returns 1 if the address is valid, 0 if not.
00137  * This replaces inet_addr, the return value from which
00138  * cannot distinguish between failure and a local broadcast address.
00139  *
00140  * @param cp IP address in ascii representation (e.g. "127.0.0.1")
00141  * @param addr pointer to which to save the ip address in network order
00142  * @return 1 if cp could be converted to addr, 0 on failure
00143  */
00144 int
00145 ip4addr_aton(const char *cp, ip4_addr_t *addr)
00146 {
00147   u32_t val;
00148   u8_t base;
00149   char c;
00150   u32_t parts[4];
00151   u32_t *pp = parts;
00152 
00153   c = *cp;
00154   for (;;) {
00155     /*
00156      * Collect number up to ``.''.
00157      * Values are specified as for C:
00158      * 0x=hex, 0=octal, 1-9=decimal.
00159      */
00160     if (!lwip_isdigit(c)) {
00161       return 0;
00162     }
00163     val = 0;
00164     base = 10;
00165     if (c == '0') {
00166       c = *++cp;
00167       if (c == 'x' || c == 'X') {
00168         base = 16;
00169         c = *++cp;
00170       } else {
00171         base = 8;
00172       }
00173     }
00174     for (;;) {
00175       if (lwip_isdigit(c)) {
00176         val = (val * base) + (u32_t)(c - '0');
00177         c = *++cp;
00178       } else if (base == 16 && lwip_isxdigit(c)) {
00179         val = (val << 4) | (u32_t)(c + 10 - (lwip_islower(c) ? 'a' : 'A'));
00180         c = *++cp;
00181       } else {
00182         break;
00183       }
00184     }
00185     if (c == '.') {
00186       /*
00187        * Internet format:
00188        *  a.b.c.d
00189        *  a.b.c   (with c treated as 16 bits)
00190        *  a.b (with b treated as 24 bits)
00191        */
00192       if (pp >= parts + 3) {
00193         return 0;
00194       }
00195       *pp++ = val;
00196       c = *++cp;
00197     } else {
00198       break;
00199     }
00200   }
00201   /*
00202    * Check for trailing characters.
00203    */
00204   if (c != '\0' && !lwip_isspace(c)) {
00205     return 0;
00206   }
00207   /*
00208    * Concoct the address according to
00209    * the number of parts specified.
00210    */
00211   switch (pp - parts + 1) {
00212 
00213     case 0:
00214       return 0;       /* initial nondigit */
00215 
00216     case 1:             /* a -- 32 bits */
00217       break;
00218 
00219     case 2:             /* a.b -- 8.24 bits */
00220       if (val > 0xffffffUL) {
00221         return 0;
00222       }
00223       if (parts[0] > 0xff) {
00224         return 0;
00225       }
00226       val |= parts[0] << 24;
00227       break;
00228 
00229     case 3:             /* a.b.c -- 8.8.16 bits */
00230       if (val > 0xffff) {
00231         return 0;
00232       }
00233       if ((parts[0] > 0xff) || (parts[1] > 0xff)) {
00234         return 0;
00235       }
00236       val |= (parts[0] << 24) | (parts[1] << 16);
00237       break;
00238 
00239     case 4:             /* a.b.c.d -- 8.8.8.8 bits */
00240       if (val > 0xff) {
00241         return 0;
00242       }
00243       if ((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff)) {
00244         return 0;
00245       }
00246       val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
00247       break;
00248     default:
00249       LWIP_ASSERT("unhandled", 0);
00250       break;
00251   }
00252   if (addr) {
00253     ip4_addr_set_u32(addr, lwip_htonl(val));
00254   }
00255   return 1;
00256 }
00257 
00258 /**
00259  * Convert numeric IP address into decimal dotted ASCII representation.
00260  * returns ptr to static buffer; not reentrant!
00261  *
00262  * @param addr ip address in network order to convert
00263  * @return pointer to a global static (!) buffer that holds the ASCII
00264  *         representation of addr
00265  */
00266 char *
00267 ip4addr_ntoa(const ip4_addr_t *addr)
00268 {
00269   static char str[IP4ADDR_STRLEN_MAX];
00270   return ip4addr_ntoa_r(addr, str, IP4ADDR_STRLEN_MAX);
00271 }
00272 
00273 /**
00274  * Same as ip4addr_ntoa, but reentrant since a user-supplied buffer is used.
00275  *
00276  * @param addr ip address in network order to convert
00277  * @param buf target buffer where the string is stored
00278  * @param buflen length of buf
00279  * @return either pointer to buf which now holds the ASCII
00280  *         representation of addr or NULL if buf was too small
00281  */
00282 char *
00283 ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen)
00284 {
00285   u32_t s_addr;
00286   char inv[3];
00287   char *rp;
00288   u8_t *ap;
00289   u8_t rem;
00290   u8_t n;
00291   u8_t i;
00292   int len = 0;
00293 
00294   s_addr = ip4_addr_get_u32(addr);
00295 
00296   rp = buf;
00297   ap = (u8_t *)&s_addr;
00298   for (n = 0; n < 4; n++) {
00299     i = 0;
00300     do {
00301       rem = *ap % (u8_t)10;
00302       *ap /= (u8_t)10;
00303       inv[i++] = (char)('0' + rem);
00304     } while (*ap);
00305     while (i--) {
00306       if (len++ >= buflen) {
00307         return NULL;
00308       }
00309       *rp++ = inv[i];
00310     }
00311     if (len++ >= buflen) {
00312       return NULL;
00313     }
00314     *rp++ = '.';
00315     ap++;
00316   }
00317   *--rp = 0;
00318   return buf;
00319 }
00320 
00321 #endif /* LWIP_IPV4 */