Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ip4_addr.h Source File

ip4_addr.h

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * IPv4 address API
00004  */
00005 
00006 /*
00007  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without modification,
00011  * are permitted provided that the following conditions are met:
00012  *
00013  * 1. Redistributions of source code must retain the above copyright notice,
00014  *    this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright notice,
00016  *    this list of conditions and the following disclaimer in the documentation
00017  *    and/or other materials provided with the distribution.
00018  * 3. The name of the author may not be used to endorse or promote products
00019  *    derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00023  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00024  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00026  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00029  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00030  * OF SUCH DAMAGE.
00031  *
00032  * This file is part of the lwIP TCP/IP stack.
00033  *
00034  * Author: Adam Dunkels <adam@sics.se>
00035  *
00036  */
00037 #ifndef LWIP_HDR_IP4_ADDR_H
00038 #define LWIP_HDR_IP4_ADDR_H
00039 
00040 #include "lwip/opt.h"
00041 #include "lwip/def.h"
00042 
00043 #if LWIP_IPV4
00044 
00045 #ifdef __cplusplus
00046 extern "C" {
00047 #endif
00048 
00049 /** This is the aligned version of ip4_addr_t,
00050    used as local variable, on the stack, etc. */
00051 struct ip4_addr {
00052   u32_t addr;
00053 };
00054 
00055 /** ip4_addr_t uses a struct for convenience only, so that the same defines can
00056  * operate both on ip4_addr_t as well as on ip4_addr_p_t. */
00057 typedef struct ip4_addr ip4_addr_t;
00058 
00059 /* Forward declaration to not include netif.h */
00060 struct netif;
00061 
00062 /** 255.255.255.255 */
00063 #define IPADDR_NONE         ((u32_t)0xffffffffUL)
00064 /** 127.0.0.1 */
00065 #define IPADDR_LOOPBACK     ((u32_t)0x7f000001UL)
00066 /** 0.0.0.0 */
00067 #define IPADDR_ANY          ((u32_t)0x00000000UL)
00068 /** 255.255.255.255 */
00069 #define IPADDR_BROADCAST    ((u32_t)0xffffffffUL)
00070 
00071 /* Definitions of the bits in an Internet address integer.
00072 
00073    On subnets, host and network parts are found according to
00074    the subnet mask, not these masks.  */
00075 #define IP_CLASSA(a)        ((((u32_t)(a)) & 0x80000000UL) == 0)
00076 #define IP_CLASSA_NET       0xff000000
00077 #define IP_CLASSA_NSHIFT    24
00078 #define IP_CLASSA_HOST      (0xffffffff & ~IP_CLASSA_NET)
00079 #define IP_CLASSA_MAX       128
00080 
00081 #define IP_CLASSB(a)        ((((u32_t)(a)) & 0xc0000000UL) == 0x80000000UL)
00082 #define IP_CLASSB_NET       0xffff0000
00083 #define IP_CLASSB_NSHIFT    16
00084 #define IP_CLASSB_HOST      (0xffffffff & ~IP_CLASSB_NET)
00085 #define IP_CLASSB_MAX       65536
00086 
00087 #define IP_CLASSC(a)        ((((u32_t)(a)) & 0xe0000000UL) == 0xc0000000UL)
00088 #define IP_CLASSC_NET       0xffffff00
00089 #define IP_CLASSC_NSHIFT    8
00090 #define IP_CLASSC_HOST      (0xffffffff & ~IP_CLASSC_NET)
00091 
00092 #define IP_CLASSD(a)        (((u32_t)(a) & 0xf0000000UL) == 0xe0000000UL)
00093 #define IP_CLASSD_NET       0xf0000000          /* These ones aren't really */
00094 #define IP_CLASSD_NSHIFT    28                  /*   net and host fields, but */
00095 #define IP_CLASSD_HOST      0x0fffffff          /*   routing needn't know. */
00096 #define IP_MULTICAST(a)     IP_CLASSD(a)
00097 
00098 #define IP_EXPERIMENTAL(a)  (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
00099 #define IP_BADCLASS(a)      (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
00100 
00101 #define IP_LOOPBACKNET      127                 /* official! */
00102 
00103 /** Set an IP address given by the four byte-parts */
00104 #define IP4_ADDR(ipaddr, a,b,c,d)  (ipaddr)->addr = PP_HTONL(LWIP_MAKEU32(a,b,c,d))
00105 
00106 /** Copy IP address - faster than ip4_addr_set: no NULL check */
00107 #define ip4_addr_copy(dest, src) ((dest).addr = (src).addr)
00108 /** Safely copy one IP address to another (src may be NULL) */
00109 #define ip4_addr_set(dest, src) ((dest)->addr = \
00110                                     ((src) == NULL ? 0 : \
00111                                     (src)->addr))
00112 /** Set complete address to zero */
00113 #define ip4_addr_set_zero(ipaddr)     ((ipaddr)->addr = 0)
00114 /** Set address to IPADDR_ANY (no need for lwip_htonl()) */
00115 #define ip4_addr_set_any(ipaddr)      ((ipaddr)->addr = IPADDR_ANY)
00116 /** Set address to loopback address */
00117 #define ip4_addr_set_loopback(ipaddr) ((ipaddr)->addr = PP_HTONL(IPADDR_LOOPBACK))
00118 /** Check if an address is in the loopback region */
00119 #define ip4_addr_isloopback(ipaddr)    (((ipaddr)->addr & PP_HTONL(IP_CLASSA_NET)) == PP_HTONL(((u32_t)IP_LOOPBACKNET) << 24))
00120 /** Safely copy one IP address to another and change byte order
00121  * from host- to network-order. */
00122 #define ip4_addr_set_hton(dest, src) ((dest)->addr = \
00123                                ((src) == NULL ? 0:\
00124                                lwip_htonl((src)->addr)))
00125 /** IPv4 only: set the IP address given as an u32_t */
00126 #define ip4_addr_set_u32(dest_ipaddr, src_u32) ((dest_ipaddr)->addr = (src_u32))
00127 /** IPv4 only: get the IP address as an u32_t */
00128 #define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr)
00129 
00130 /** Get the network address by combining host address with netmask */
00131 #define ip4_addr_get_network(target, host, netmask) do { ((target)->addr = ((host)->addr) & ((netmask)->addr)); } while(0)
00132 
00133 /**
00134  * Determine if two address are on the same network.
00135  *
00136  * @arg addr1 IP address 1
00137  * @arg addr2 IP address 2
00138  * @arg mask network identifier mask
00139  * @return !0 if the network identifiers of both address match
00140  */
00141 #define ip4_addr_netcmp(addr1, addr2, mask) (((addr1)->addr & \
00142                                               (mask)->addr) == \
00143                                              ((addr2)->addr & \
00144                                               (mask)->addr))
00145 #define ip4_addr_cmp(addr1, addr2) ((addr1)->addr == (addr2)->addr)
00146 
00147 #define ip4_addr_isany_val(addr1)   ((addr1).addr == IPADDR_ANY)
00148 #define ip4_addr_isany(addr1) ((addr1) == NULL || ip4_addr_isany_val(*(addr1)))
00149 
00150 #define ip4_addr_isbroadcast(addr1, netif) ip4_addr_isbroadcast_u32((addr1)->addr, netif)
00151 u8_t ip4_addr_isbroadcast_u32(u32_t addr, const struct netif *netif);
00152 
00153 #define ip_addr_netmask_valid(netmask) ip4_addr_netmask_valid((netmask)->addr)
00154 u8_t ip4_addr_netmask_valid(u32_t netmask);
00155 
00156 #define ip4_addr_ismulticast(addr1) (((addr1)->addr & PP_HTONL(0xf0000000UL)) == PP_HTONL(0xe0000000UL))
00157 
00158 #define ip4_addr_islinklocal(addr1) (((addr1)->addr & PP_HTONL(0xffff0000UL)) == PP_HTONL(0xa9fe0000UL))
00159 
00160 #define ip4_addr_debug_print_parts(debug, a, b, c, d) \
00161   LWIP_DEBUGF(debug, ("%" U16_F ".%" U16_F ".%" U16_F ".%" U16_F, a, b, c, d))
00162 #define ip4_addr_debug_print(debug, ipaddr) \
00163   ip4_addr_debug_print_parts(debug, \
00164                       (u16_t)((ipaddr) != NULL ? ip4_addr1_16(ipaddr) : 0),       \
00165                       (u16_t)((ipaddr) != NULL ? ip4_addr2_16(ipaddr) : 0),       \
00166                       (u16_t)((ipaddr) != NULL ? ip4_addr3_16(ipaddr) : 0),       \
00167                       (u16_t)((ipaddr) != NULL ? ip4_addr4_16(ipaddr) : 0))
00168 #define ip4_addr_debug_print_val(debug, ipaddr) \
00169   ip4_addr_debug_print_parts(debug, \
00170                       ip4_addr1_16_val(ipaddr),       \
00171                       ip4_addr2_16_val(ipaddr),       \
00172                       ip4_addr3_16_val(ipaddr),       \
00173                       ip4_addr4_16_val(ipaddr))
00174 
00175 /* Get one byte from the 4-byte address */
00176 #define ip4_addr_get_byte(ipaddr, idx) (((const u8_t*)(&(ipaddr)->addr))[idx])
00177 #define ip4_addr1(ipaddr) ip4_addr_get_byte(ipaddr, 0)
00178 #define ip4_addr2(ipaddr) ip4_addr_get_byte(ipaddr, 1)
00179 #define ip4_addr3(ipaddr) ip4_addr_get_byte(ipaddr, 2)
00180 #define ip4_addr4(ipaddr) ip4_addr_get_byte(ipaddr, 3)
00181 /* Get one byte from the 4-byte address, but argument is 'ip4_addr_t',
00182  * not a pointer */
00183 #define ip4_addr_get_byte_val(ipaddr, idx) ((u8_t)(((ipaddr).addr >> (idx * 8)) & 0xff))
00184 #define ip4_addr1_val(ipaddr) ip4_addr_get_byte_val(ipaddr, 0)
00185 #define ip4_addr2_val(ipaddr) ip4_addr_get_byte_val(ipaddr, 1)
00186 #define ip4_addr3_val(ipaddr) ip4_addr_get_byte_val(ipaddr, 2)
00187 #define ip4_addr4_val(ipaddr) ip4_addr_get_byte_val(ipaddr, 3)
00188 /* These are cast to u16_t, with the intent that they are often arguments
00189  * to printf using the U16_F format from cc.h. */
00190 #define ip4_addr1_16(ipaddr) ((u16_t)ip4_addr1(ipaddr))
00191 #define ip4_addr2_16(ipaddr) ((u16_t)ip4_addr2(ipaddr))
00192 #define ip4_addr3_16(ipaddr) ((u16_t)ip4_addr3(ipaddr))
00193 #define ip4_addr4_16(ipaddr) ((u16_t)ip4_addr4(ipaddr))
00194 #define ip4_addr1_16_val(ipaddr) ((u16_t)ip4_addr1_val(ipaddr))
00195 #define ip4_addr2_16_val(ipaddr) ((u16_t)ip4_addr2_val(ipaddr))
00196 #define ip4_addr3_16_val(ipaddr) ((u16_t)ip4_addr3_val(ipaddr))
00197 #define ip4_addr4_16_val(ipaddr) ((u16_t)ip4_addr4_val(ipaddr))
00198 
00199 #define IP4ADDR_STRLEN_MAX  16
00200 
00201 /** For backwards compatibility */
00202 #define ip_ntoa(ipaddr)  ipaddr_ntoa(ipaddr)
00203 
00204 u32_t ipaddr_addr(const char *cp);
00205 int ip4addr_aton(const char *cp, ip4_addr_t *addr);
00206 /** returns ptr to static buffer; not reentrant! */
00207 char *ip4addr_ntoa(const ip4_addr_t *addr);
00208 char *ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen);
00209 
00210 #ifdef __cplusplus
00211 }
00212 #endif
00213 
00214 #endif /* LWIP_IPV4 */
00215 
00216 #endif /* LWIP_HDR_IP_ADDR_H */