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_ip.c Source File

lwip_ip.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * Common IPv4 and IPv6 code
00004  *
00005  * @defgroup ip IP
00006  * @ingroup callbackstyle_api
00007  *
00008  * @defgroup ip4 IPv4
00009  * @ingroup ip
00010  *
00011  * @defgroup ip6 IPv6
00012  * @ingroup ip
00013  *
00014  * @defgroup ipaddr IP address handling
00015  * @ingroup infrastructure
00016  *
00017  * @defgroup ip4addr IPv4 only
00018  * @ingroup ipaddr
00019  *
00020  * @defgroup ip6addr IPv6 only
00021  * @ingroup ipaddr
00022  */
00023 
00024 /*
00025  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00026  * All rights reserved.
00027  *
00028  * Redistribution and use in source and binary forms, with or without modification,
00029  * are permitted provided that the following conditions are met:
00030  *
00031  * 1. Redistributions of source code must retain the above copyright notice,
00032  *    this list of conditions and the following disclaimer.
00033  * 2. Redistributions in binary form must reproduce the above copyright notice,
00034  *    this list of conditions and the following disclaimer in the documentation
00035  *    and/or other materials provided with the distribution.
00036  * 3. The name of the author may not be used to endorse or promote products
00037  *    derived from this software without specific prior written permission.
00038  *
00039  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00040  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00041  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00042  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00043  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00044  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00045  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00046  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00047  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00048  * OF SUCH DAMAGE.
00049  *
00050  * This file is part of the lwIP TCP/IP stack.
00051  *
00052  * Author: Adam Dunkels <adam@sics.se>
00053  *
00054  */
00055 
00056 #include "lwip/opt.h"
00057 
00058 #if LWIP_IPV4 || LWIP_IPV6
00059 
00060 #include "lwip/ip_addr.h"
00061 #include "lwip/ip.h"
00062 
00063 /** Global data for both IPv4 and IPv6 */
00064 struct ip_globals ip_data;
00065 
00066 #if LWIP_IPV4 && LWIP_IPV6
00067 
00068 const ip_addr_t ip_addr_any_type = IPADDR_ANY_TYPE_INIT;
00069 
00070 /**
00071  * @ingroup ipaddr
00072  * Convert numeric IP address (both versions) into ASCII representation.
00073  * returns ptr to static buffer; not reentrant!
00074  *
00075  * @param addr ip address in network order to convert
00076  * @return pointer to a global static (!) buffer that holds the ASCII
00077  *         representation of addr
00078  */
00079 char *ipaddr_ntoa(const ip_addr_t *addr)
00080 {
00081   if (addr == NULL) {
00082     return NULL;
00083   }
00084   if (IP_IS_V6(addr)) {
00085     return ip6addr_ntoa(ip_2_ip6(addr));
00086   } else {
00087     return ip4addr_ntoa(ip_2_ip4(addr));
00088   }
00089 }
00090 
00091 /**
00092  * @ingroup ipaddr
00093  * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used.
00094  *
00095  * @param addr ip address in network order to convert
00096  * @param buf target buffer where the string is stored
00097  * @param buflen length of buf
00098  * @return either pointer to buf which now holds the ASCII
00099  *         representation of addr or NULL if buf was too small
00100  */
00101 char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)
00102 {
00103   if (addr == NULL) {
00104     return NULL;
00105   }
00106   if (IP_IS_V6(addr)) {
00107     return ip6addr_ntoa_r(ip_2_ip6(addr), buf, buflen);
00108   } else {
00109     return ip4addr_ntoa_r(ip_2_ip4(addr), buf, buflen);
00110   }
00111 }
00112 
00113 /**
00114  * @ingroup ipaddr
00115  * Convert IP address string (both versions) to numeric.
00116  * The version is auto-detected from the string.
00117  *
00118  * @param cp IP address string to convert
00119  * @param addr conversion result is stored here
00120  * @return 1 on success, 0 on error
00121  */
00122 int
00123 ipaddr_aton(const char *cp, ip_addr_t *addr)
00124 {
00125   if (cp != NULL) {
00126     const char *c;
00127     for (c = cp; *c != 0; c++) {
00128       if (*c == ':') {
00129         /* contains a colon: IPv6 address */
00130         if (addr) {
00131           IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V6);
00132         }
00133         return ip6addr_aton(cp, ip_2_ip6(addr));
00134       } else if (*c == '.') {
00135         /* contains a dot: IPv4 address */
00136         break;
00137       }
00138     }
00139     /* call ip4addr_aton as fallback or if IPv4 was found */
00140     if (addr) {
00141       IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V4);
00142     }
00143     return ip4addr_aton(cp, ip_2_ip4(addr));
00144   }
00145   return 0;
00146 }
00147 
00148 /**
00149  * @ingroup lwip_nosys
00150  * If both IP versions are enabled, this function can dispatch packets to the correct one.
00151  * Don't call directly, pass to netif_add() and call netif->input().
00152  */
00153 err_t
00154 ip_input(struct pbuf *p, struct netif *inp)
00155 {
00156   if (p != NULL) {
00157     if (IP_HDR_GET_VERSION(p->payload) == 6) {
00158       return ip6_input(p, inp);
00159     }
00160     return ip4_input(p, inp);
00161   }
00162   return ERR_VAL;
00163 }
00164 
00165 #endif /* LWIP_IPV4 && LWIP_IPV6 */
00166 
00167 #endif /* LWIP_IPV4 || LWIP_IPV6 */