Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ip4_addr.h
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 /** This is the packed version of ip4_addr_t, 00056 used in network headers that are itself packed */ 00057 #ifdef PACK_STRUCT_USE_INCLUDES 00058 # include "arch/bpstruct.h" 00059 #endif 00060 PACK_STRUCT_BEGIN 00061 struct ip4_addr_packed { 00062 PACK_STRUCT_FIELD(u32_t addr); 00063 } PACK_STRUCT_STRUCT; 00064 PACK_STRUCT_END 00065 #ifdef PACK_STRUCT_USE_INCLUDES 00066 # include "arch/epstruct.h" 00067 #endif 00068 00069 /** ip4_addr_t uses a struct for convenience only, so that the same defines can 00070 * operate both on ip4_addr_t as well as on ip4_addr_p_t. */ 00071 typedef struct ip4_addr ip4_addr_t; 00072 typedef struct ip4_addr_packed ip4_addr_p_t; 00073 00074 /** 00075 * struct ipaddr2 is used in the definition of the ARP packet format in 00076 * order to support compilers that don't have structure packing. 00077 */ 00078 #ifdef PACK_STRUCT_USE_INCLUDES 00079 # include "arch/bpstruct.h" 00080 #endif 00081 PACK_STRUCT_BEGIN 00082 struct ip4_addr2 { 00083 PACK_STRUCT_FIELD(u16_t addrw[2]); 00084 } PACK_STRUCT_STRUCT; 00085 PACK_STRUCT_END 00086 #ifdef PACK_STRUCT_USE_INCLUDES 00087 # include "arch/epstruct.h" 00088 #endif 00089 00090 /* Forward declaration to not include netif.h */ 00091 struct netif; 00092 00093 /** 255.255.255.255 */ 00094 #define IPADDR_NONE ((u32_t)0xffffffffUL) 00095 /** 127.0.0.1 */ 00096 #define IPADDR_LOOPBACK ((u32_t)0x7f000001UL) 00097 /** 0.0.0.0 */ 00098 #define IPADDR_ANY ((u32_t)0x00000000UL) 00099 /** 255.255.255.255 */ 00100 #define IPADDR_BROADCAST ((u32_t)0xffffffffUL) 00101 00102 /* Definitions of the bits in an Internet address integer. 00103 00104 On subnets, host and network parts are found according to 00105 the subnet mask, not these masks. */ 00106 #define IP_CLASSA(a) ((((u32_t)(a)) & 0x80000000UL) == 0) 00107 #define IP_CLASSA_NET 0xff000000 00108 #define IP_CLASSA_NSHIFT 24 00109 #define IP_CLASSA_HOST (0xffffffff & ~IP_CLASSA_NET) 00110 #define IP_CLASSA_MAX 128 00111 00112 #define IP_CLASSB(a) ((((u32_t)(a)) & 0xc0000000UL) == 0x80000000UL) 00113 #define IP_CLASSB_NET 0xffff0000 00114 #define IP_CLASSB_NSHIFT 16 00115 #define IP_CLASSB_HOST (0xffffffff & ~IP_CLASSB_NET) 00116 #define IP_CLASSB_MAX 65536 00117 00118 #define IP_CLASSC(a) ((((u32_t)(a)) & 0xe0000000UL) == 0xc0000000UL) 00119 #define IP_CLASSC_NET 0xffffff00 00120 #define IP_CLASSC_NSHIFT 8 00121 #define IP_CLASSC_HOST (0xffffffff & ~IP_CLASSC_NET) 00122 00123 #define IP_CLASSD(a) (((u32_t)(a) & 0xf0000000UL) == 0xe0000000UL) 00124 #define IP_CLASSD_NET 0xf0000000 /* These ones aren't really */ 00125 #define IP_CLASSD_NSHIFT 28 /* net and host fields, but */ 00126 #define IP_CLASSD_HOST 0x0fffffff /* routing needn't know. */ 00127 #define IP_MULTICAST(a) IP_CLASSD(a) 00128 00129 #define IP_EXPERIMENTAL(a) (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL) 00130 #define IP_BADCLASS(a) (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL) 00131 00132 #define IP_LOOPBACKNET 127 /* official! */ 00133 00134 00135 #if BYTE_ORDER == BIG_ENDIAN 00136 /** Set an IP address given by the four byte-parts */ 00137 #define IP4_ADDR(ipaddr, a,b,c,d) \ 00138 (ipaddr)->addr = ((u32_t)((a) & 0xff) << 24) | \ 00139 ((u32_t)((b) & 0xff) << 16) | \ 00140 ((u32_t)((c) & 0xff) << 8) | \ 00141 (u32_t)((d) & 0xff) 00142 #else 00143 /** Set an IP address given by the four byte-parts. 00144 Little-endian version that prevents the use of htonl. */ 00145 #define IP4_ADDR(ipaddr, a,b,c,d) \ 00146 (ipaddr)->addr = ((u32_t)((d) & 0xff) << 24) | \ 00147 ((u32_t)((c) & 0xff) << 16) | \ 00148 ((u32_t)((b) & 0xff) << 8) | \ 00149 (u32_t)((a) & 0xff) 00150 #endif 00151 00152 /** MEMCPY-like copying of IP addresses where addresses are known to be 00153 * 16-bit-aligned if the port is correctly configured (so a port could define 00154 * this to copying 2 u16_t's) - no NULL-pointer-checking needed. */ 00155 #ifndef IPADDR2_COPY 00156 #define IPADDR2_COPY(dest, src) SMEMCPY(dest, src, sizeof(ip4_addr_t)) 00157 #endif 00158 00159 /** Copy IP address - faster than ip4_addr_set: no NULL check */ 00160 #define ip4_addr_copy(dest, src) ((dest).addr = (src).addr) 00161 /** Safely copy one IP address to another (src may be NULL) */ 00162 #define ip4_addr_set(dest, src) ((dest)->addr = \ 00163 ((src) == NULL ? 0 : \ 00164 (src)->addr)) 00165 /** Set complete address to zero */ 00166 #define ip4_addr_set_zero(ipaddr) ((ipaddr)->addr = 0) 00167 /** Set address to IPADDR_ANY (no need for htonl()) */ 00168 #define ip4_addr_set_any(ipaddr) ((ipaddr)->addr = IPADDR_ANY) 00169 /** Set address to loopback address */ 00170 #define ip4_addr_set_loopback(ipaddr) ((ipaddr)->addr = PP_HTONL(IPADDR_LOOPBACK)) 00171 /** Check if an address is in the loopback region */ 00172 #define ip4_addr_isloopback(ipaddr) (((ipaddr)->addr & PP_HTONL(IP_CLASSA_NET)) == PP_HTONL(((u32_t)IP_LOOPBACKNET) << 24)) 00173 /** Safely copy one IP address to another and change byte order 00174 * from host- to network-order. */ 00175 #define ip4_addr_set_hton(dest, src) ((dest)->addr = \ 00176 ((src) == NULL ? 0:\ 00177 htonl((src)->addr))) 00178 /** IPv4 only: set the IP address given as an u32_t */ 00179 #define ip4_addr_set_u32(dest_ipaddr, src_u32) ((dest_ipaddr)->addr = (src_u32)) 00180 /** IPv4 only: get the IP address as an u32_t */ 00181 #define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr) 00182 00183 /** Get the network address by combining host address with netmask */ 00184 #define ip4_addr_get_network(target, host, netmask) do { ((target)->addr = ((host)->addr) & ((netmask)->addr)); } while(0) 00185 00186 /** 00187 * Determine if two address are on the same network. 00188 * 00189 * @arg addr1 IP address 1 00190 * @arg addr2 IP address 2 00191 * @arg mask network identifier mask 00192 * @return !0 if the network identifiers of both address match 00193 */ 00194 #define ip4_addr_netcmp(addr1, addr2, mask) (((addr1)->addr & \ 00195 (mask)->addr) == \ 00196 ((addr2)->addr & \ 00197 (mask)->addr)) 00198 #define ip4_addr_cmp(addr1, addr2) ((addr1)->addr == (addr2)->addr) 00199 00200 #define ip4_addr_isany_val(addr1) ((addr1).addr == IPADDR_ANY) 00201 #define ip4_addr_isany(addr1) ((addr1) == NULL || ip4_addr_isany_val(*(addr1))) 00202 00203 #define ip4_addr_isbroadcast(addr1, netif) ip4_addr_isbroadcast_u32((addr1)->addr, netif) 00204 u8_t ip4_addr_isbroadcast_u32(u32_t addr, const struct netif *netif); 00205 00206 #define ip_addr_netmask_valid(netmask) ip4_addr_netmask_valid((netmask)->addr) 00207 u8_t ip4_addr_netmask_valid(u32_t netmask); 00208 00209 #define ip4_addr_ismulticast(addr1) (((addr1)->addr & PP_HTONL(0xf0000000UL)) == PP_HTONL(0xe0000000UL)) 00210 00211 #define ip4_addr_islinklocal(addr1) (((addr1)->addr & PP_HTONL(0xffff0000UL)) == PP_HTONL(0xa9fe0000UL)) 00212 00213 #define ip4_addr_debug_print_parts(debug, a, b, c, d) \ 00214 LWIP_DEBUGF(debug, ("%" U16_F ".%" U16_F ".%" U16_F ".%" U16_F, a, b, c, d)) 00215 #define ip4_addr_debug_print(debug, ipaddr) \ 00216 ip4_addr_debug_print_parts(debug, \ 00217 (u16_t)((ipaddr) != NULL ? ip4_addr1_16(ipaddr) : 0), \ 00218 (u16_t)((ipaddr) != NULL ? ip4_addr2_16(ipaddr) : 0), \ 00219 (u16_t)((ipaddr) != NULL ? ip4_addr3_16(ipaddr) : 0), \ 00220 (u16_t)((ipaddr) != NULL ? ip4_addr4_16(ipaddr) : 0)) 00221 #define ip4_addr_debug_print_val(debug, ipaddr) \ 00222 ip4_addr_debug_print_parts(debug, \ 00223 ip4_addr1_16(&(ipaddr)), \ 00224 ip4_addr2_16(&(ipaddr)), \ 00225 ip4_addr3_16(&(ipaddr)), \ 00226 ip4_addr4_16(&(ipaddr))) 00227 00228 /* Get one byte from the 4-byte address */ 00229 #define ip4_addr1(ipaddr) (((const u8_t*)(&(ipaddr)->addr))[0]) 00230 #define ip4_addr2(ipaddr) (((const u8_t*)(&(ipaddr)->addr))[1]) 00231 #define ip4_addr3(ipaddr) (((const u8_t*)(&(ipaddr)->addr))[2]) 00232 #define ip4_addr4(ipaddr) (((const u8_t*)(&(ipaddr)->addr))[3]) 00233 /* These are cast to u16_t, with the intent that they are often arguments 00234 * to printf using the U16_F format from cc.h. */ 00235 #define ip4_addr1_16(ipaddr) ((u16_t)ip4_addr1(ipaddr)) 00236 #define ip4_addr2_16(ipaddr) ((u16_t)ip4_addr2(ipaddr)) 00237 #define ip4_addr3_16(ipaddr) ((u16_t)ip4_addr3(ipaddr)) 00238 #define ip4_addr4_16(ipaddr) ((u16_t)ip4_addr4(ipaddr)) 00239 00240 #define IP4ADDR_STRLEN_MAX 16 00241 00242 /** For backwards compatibility */ 00243 #define ip_ntoa(ipaddr) ipaddr_ntoa(ipaddr) 00244 00245 u32_t ipaddr_addr(const char *cp); 00246 int ip4addr_aton(const char *cp, ip4_addr_t *addr); 00247 /** returns ptr to static buffer; not reentrant! */ 00248 char *ip4addr_ntoa(const ip4_addr_t *addr); 00249 char *ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen); 00250 00251 #ifdef __cplusplus 00252 } 00253 #endif 00254 00255 #endif /* LWIP_IPV4 */ 00256 00257 #endif /* LWIP_HDR_IP_ADDR_H */
Generated on Tue Jul 12 2022 17:34:44 by
