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.
Dependents: mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more
ip6_addr.h
00001 /** 00002 * @file 00003 * 00004 * IPv6 addresses. 00005 */ 00006 00007 /* 00008 * Copyright (c) 2010 Inico Technologies Ltd. 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: Ivan Delamer <delamer@inicotech.com> 00036 * 00037 * Structs and macros for handling IPv6 addresses. 00038 * 00039 * Please coordinate changes and requests with Ivan Delamer 00040 * <delamer@inicotech.com> 00041 */ 00042 #ifndef LWIP_HDR_IP6_ADDR_H 00043 #define LWIP_HDR_IP6_ADDR_H 00044 00045 #include "lwip/opt.h" 00046 #include "def.h" 00047 00048 #if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */ 00049 00050 00051 #ifdef __cplusplus 00052 extern "C" { 00053 #endif 00054 00055 00056 /** This is the aligned version of ip6_addr_t, 00057 used as local variable, on the stack, etc. */ 00058 struct ip6_addr { 00059 u32_t addr[4]; 00060 }; 00061 00062 /** IPv6 address */ 00063 typedef struct ip6_addr ip6_addr_t; 00064 00065 /** Set an IPv6 partial address given by byte-parts */ 00066 #define IP6_ADDR_PART(ip6addr, index, a,b,c,d) \ 00067 (ip6addr)->addr[index] = PP_HTONL(LWIP_MAKEU32(a,b,c,d)) 00068 00069 /** Set a full IPv6 address by passing the 4 u32_t indices in network byte order 00070 (use PP_HTONL() for constants) */ 00071 #define IP6_ADDR(ip6addr, idx0, idx1, idx2, idx3) do { \ 00072 (ip6addr)->addr[0] = idx0; \ 00073 (ip6addr)->addr[1] = idx1; \ 00074 (ip6addr)->addr[2] = idx2; \ 00075 (ip6addr)->addr[3] = idx3; } while(0) 00076 00077 /** Access address in 16-bit block */ 00078 #define IP6_ADDR_BLOCK1(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[0]) >> 16) & 0xffff)) 00079 /** Access address in 16-bit block */ 00080 #define IP6_ADDR_BLOCK2(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[0])) & 0xffff)) 00081 /** Access address in 16-bit block */ 00082 #define IP6_ADDR_BLOCK3(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[1]) >> 16) & 0xffff)) 00083 /** Access address in 16-bit block */ 00084 #define IP6_ADDR_BLOCK4(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[1])) & 0xffff)) 00085 /** Access address in 16-bit block */ 00086 #define IP6_ADDR_BLOCK5(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[2]) >> 16) & 0xffff)) 00087 /** Access address in 16-bit block */ 00088 #define IP6_ADDR_BLOCK6(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[2])) & 0xffff)) 00089 /** Access address in 16-bit block */ 00090 #define IP6_ADDR_BLOCK7(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[3]) >> 16) & 0xffff)) 00091 /** Access address in 16-bit block */ 00092 #define IP6_ADDR_BLOCK8(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[3])) & 0xffff)) 00093 00094 /** Copy IPv6 address - faster than ip6_addr_set: no NULL check */ 00095 #define ip6_addr_copy(dest, src) do{(dest).addr[0] = (src).addr[0]; \ 00096 (dest).addr[1] = (src).addr[1]; \ 00097 (dest).addr[2] = (src).addr[2]; \ 00098 (dest).addr[3] = (src).addr[3];}while(0) 00099 /** Safely copy one IPv6 address to another (src may be NULL) */ 00100 #define ip6_addr_set(dest, src) do{(dest)->addr[0] = (src) == NULL ? 0 : (src)->addr[0]; \ 00101 (dest)->addr[1] = (src) == NULL ? 0 : (src)->addr[1]; \ 00102 (dest)->addr[2] = (src) == NULL ? 0 : (src)->addr[2]; \ 00103 (dest)->addr[3] = (src) == NULL ? 0 : (src)->addr[3];}while(0) 00104 00105 /** Set complete address to zero */ 00106 #define ip6_addr_set_zero(ip6addr) do{(ip6addr)->addr[0] = 0; \ 00107 (ip6addr)->addr[1] = 0; \ 00108 (ip6addr)->addr[2] = 0; \ 00109 (ip6addr)->addr[3] = 0;}while(0) 00110 00111 /** Set address to ipv6 'any' (no need for lwip_htonl()) */ 00112 #define ip6_addr_set_any(ip6addr) ip6_addr_set_zero(ip6addr) 00113 /** Set address to ipv6 loopback address */ 00114 #define ip6_addr_set_loopback(ip6addr) do{(ip6addr)->addr[0] = 0; \ 00115 (ip6addr)->addr[1] = 0; \ 00116 (ip6addr)->addr[2] = 0; \ 00117 (ip6addr)->addr[3] = PP_HTONL(0x00000001UL);}while(0) 00118 /** Safely copy one IPv6 address to another and change byte order 00119 * from host- to network-order. */ 00120 #define ip6_addr_set_hton(dest, src) do{(dest)->addr[0] = (src) == NULL ? 0 : lwip_htonl((src)->addr[0]); \ 00121 (dest)->addr[1] = (src) == NULL ? 0 : lwip_htonl((src)->addr[1]); \ 00122 (dest)->addr[2] = (src) == NULL ? 0 : lwip_htonl((src)->addr[2]); \ 00123 (dest)->addr[3] = (src) == NULL ? 0 : lwip_htonl((src)->addr[3]);}while(0) 00124 00125 00126 /** 00127 * Determine if two IPv6 address are on the same network. 00128 * 00129 * @arg addr1 IPv6 address 1 00130 * @arg addr2 IPv6 address 2 00131 * @return !0 if the network identifiers of both address match 00132 */ 00133 #define ip6_addr_netcmp(addr1, addr2) (((addr1)->addr[0] == (addr2)->addr[0]) && \ 00134 ((addr1)->addr[1] == (addr2)->addr[1])) 00135 00136 #define ip6_addr_cmp(addr1, addr2) (((addr1)->addr[0] == (addr2)->addr[0]) && \ 00137 ((addr1)->addr[1] == (addr2)->addr[1]) && \ 00138 ((addr1)->addr[2] == (addr2)->addr[2]) && \ 00139 ((addr1)->addr[3] == (addr2)->addr[3])) 00140 00141 #define ip6_get_subnet_id(ip6addr) (lwip_htonl((ip6addr)->addr[2]) & 0x0000ffffUL) 00142 00143 #define ip6_addr_isany_val(ip6addr) (((ip6addr).addr[0] == 0) && \ 00144 ((ip6addr).addr[1] == 0) && \ 00145 ((ip6addr).addr[2] == 0) && \ 00146 ((ip6addr).addr[3] == 0)) 00147 #define ip6_addr_isany(ip6addr) (((ip6addr) == NULL) || ip6_addr_isany_val(*(ip6addr))) 00148 00149 #define ip6_addr_isloopback(ip6addr) (((ip6addr)->addr[0] == 0UL) && \ 00150 ((ip6addr)->addr[1] == 0UL) && \ 00151 ((ip6addr)->addr[2] == 0UL) && \ 00152 ((ip6addr)->addr[3] == PP_HTONL(0x00000001UL))) 00153 00154 #define ip6_addr_isglobal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xe0000000UL)) == PP_HTONL(0x20000000UL)) 00155 00156 #define ip6_addr_islinklocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xffc00000UL)) == PP_HTONL(0xfe800000UL)) 00157 00158 #define ip6_addr_issitelocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xffc00000UL)) == PP_HTONL(0xfec00000UL)) 00159 00160 #define ip6_addr_isuniquelocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xfe000000UL)) == PP_HTONL(0xfc000000UL)) 00161 00162 #define ip6_addr_isipv6mappedipv4(ip6addr) (((ip6addr)->addr[0] == 0) && ((ip6addr)->addr[1] == 0) && (((ip6addr)->addr[2]) == PP_HTONL(0x0000FFFFUL))) 00163 00164 #define ip6_addr_ismulticast(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff000000UL)) == PP_HTONL(0xff000000UL)) 00165 #define ip6_addr_multicast_transient_flag(ip6addr) ((ip6addr)->addr[0] & PP_HTONL(0x00100000UL)) 00166 #define ip6_addr_multicast_prefix_flag(ip6addr) ((ip6addr)->addr[0] & PP_HTONL(0x00200000UL)) 00167 #define ip6_addr_multicast_rendezvous_flag(ip6addr) ((ip6addr)->addr[0] & PP_HTONL(0x00400000UL)) 00168 #define ip6_addr_multicast_scope(ip6addr) ((lwip_htonl((ip6addr)->addr[0]) >> 16) & 0xf) 00169 #define IP6_MULTICAST_SCOPE_RESERVED 0x0 00170 #define IP6_MULTICAST_SCOPE_RESERVED0 0x0 00171 #define IP6_MULTICAST_SCOPE_INTERFACE_LOCAL 0x1 00172 #define IP6_MULTICAST_SCOPE_LINK_LOCAL 0x2 00173 #define IP6_MULTICAST_SCOPE_RESERVED3 0x3 00174 #define IP6_MULTICAST_SCOPE_ADMIN_LOCAL 0x4 00175 #define IP6_MULTICAST_SCOPE_SITE_LOCAL 0x5 00176 #define IP6_MULTICAST_SCOPE_ORGANIZATION_LOCAL 0x8 00177 #define IP6_MULTICAST_SCOPE_GLOBAL 0xe 00178 #define IP6_MULTICAST_SCOPE_RESERVEDF 0xf 00179 #define ip6_addr_ismulticast_iflocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff010000UL)) 00180 #define ip6_addr_ismulticast_linklocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff020000UL)) 00181 #define ip6_addr_ismulticast_adminlocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff040000UL)) 00182 #define ip6_addr_ismulticast_sitelocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff050000UL)) 00183 #define ip6_addr_ismulticast_orglocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff080000UL)) 00184 #define ip6_addr_ismulticast_global(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff0e0000UL)) 00185 00186 /* @todo define get/set for well-know multicast addresses, e.g. ff02::1 */ 00187 #define ip6_addr_isallnodes_iflocal(ip6addr) (((ip6addr)->addr[0] == PP_HTONL(0xff010000UL)) && \ 00188 ((ip6addr)->addr[1] == 0UL) && \ 00189 ((ip6addr)->addr[2] == 0UL) && \ 00190 ((ip6addr)->addr[3] == PP_HTONL(0x00000001UL))) 00191 00192 #define ip6_addr_isallnodes_linklocal(ip6addr) (((ip6addr)->addr[0] == PP_HTONL(0xff020000UL)) && \ 00193 ((ip6addr)->addr[1] == 0UL) && \ 00194 ((ip6addr)->addr[2] == 0UL) && \ 00195 ((ip6addr)->addr[3] == PP_HTONL(0x00000001UL))) 00196 #define ip6_addr_set_allnodes_linklocal(ip6addr) do{(ip6addr)->addr[0] = PP_HTONL(0xff020000UL); \ 00197 (ip6addr)->addr[1] = 0; \ 00198 (ip6addr)->addr[2] = 0; \ 00199 (ip6addr)->addr[3] = PP_HTONL(0x00000001UL);}while(0) 00200 00201 #define ip6_addr_isallrouters_linklocal(ip6addr) (((ip6addr)->addr[0] == PP_HTONL(0xff020000UL)) && \ 00202 ((ip6addr)->addr[1] == 0UL) && \ 00203 ((ip6addr)->addr[2] == 0UL) && \ 00204 ((ip6addr)->addr[3] == PP_HTONL(0x00000002UL))) 00205 #define ip6_addr_set_allrouters_linklocal(ip6addr) do{(ip6addr)->addr[0] = PP_HTONL(0xff020000UL); \ 00206 (ip6addr)->addr[1] = 0; \ 00207 (ip6addr)->addr[2] = 0; \ 00208 (ip6addr)->addr[3] = PP_HTONL(0x00000002UL);}while(0) 00209 00210 #define ip6_addr_issolicitednode(ip6addr) ( ((ip6addr)->addr[0] == PP_HTONL(0xff020000UL)) && \ 00211 ((ip6addr)->addr[2] == PP_HTONL(0x00000001UL)) && \ 00212 (((ip6addr)->addr[3] & PP_HTONL(0xff000000UL)) == PP_HTONL(0xff000000UL)) ) 00213 00214 #define ip6_addr_set_solicitednode(ip6addr, if_id) do{(ip6addr)->addr[0] = PP_HTONL(0xff020000UL); \ 00215 (ip6addr)->addr[1] = 0; \ 00216 (ip6addr)->addr[2] = PP_HTONL(0x00000001UL); \ 00217 (ip6addr)->addr[3] = (PP_HTONL(0xff000000UL) | (if_id));}while(0) 00218 00219 #define ip6_addr_cmp_solicitednode(ip6addr, sn_addr) (((ip6addr)->addr[0] == PP_HTONL(0xff020000UL)) && \ 00220 ((ip6addr)->addr[1] == 0) && \ 00221 ((ip6addr)->addr[2] == PP_HTONL(0x00000001UL)) && \ 00222 ((ip6addr)->addr[3] == (PP_HTONL(0xff000000UL) | (sn_addr)->addr[3]))) 00223 00224 /* IPv6 address states. */ 00225 #define IP6_ADDR_INVALID 0x00 00226 #define IP6_ADDR_TENTATIVE 0x08 00227 #define IP6_ADDR_TENTATIVE_1 0x09 /* 1 probe sent */ 00228 #define IP6_ADDR_TENTATIVE_2 0x0a /* 2 probes sent */ 00229 #define IP6_ADDR_TENTATIVE_3 0x0b /* 3 probes sent */ 00230 #define IP6_ADDR_TENTATIVE_4 0x0c /* 4 probes sent */ 00231 #define IP6_ADDR_TENTATIVE_5 0x0d /* 5 probes sent */ 00232 #define IP6_ADDR_TENTATIVE_6 0x0e /* 6 probes sent */ 00233 #define IP6_ADDR_TENTATIVE_7 0x0f /* 7 probes sent */ 00234 #define IP6_ADDR_VALID 0x10 /* This bit marks an address as valid (preferred or deprecated) */ 00235 #define IP6_ADDR_PREFERRED 0x30 00236 #define IP6_ADDR_DEPRECATED 0x10 /* Same as VALID (valid but not preferred) */ 00237 00238 #define IP6_ADDR_TENTATIVE_COUNT_MASK 0x07 /* 1-7 probes sent */ 00239 00240 #define ip6_addr_isinvalid(addr_state) (addr_state == IP6_ADDR_INVALID) 00241 #define ip6_addr_istentative(addr_state) (addr_state & IP6_ADDR_TENTATIVE) 00242 #define ip6_addr_isvalid(addr_state) (addr_state & IP6_ADDR_VALID) /* Include valid, preferred, and deprecated. */ 00243 #define ip6_addr_ispreferred(addr_state) (addr_state == IP6_ADDR_PREFERRED) 00244 #define ip6_addr_isdeprecated(addr_state) (addr_state == IP6_ADDR_DEPRECATED) 00245 00246 #define ip6_addr_debug_print_parts(debug, a, b, c, d, e, f, g, h) \ 00247 LWIP_DEBUGF(debug, ("%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F, \ 00248 a, b, c, d, e, f, g, h)) 00249 #define ip6_addr_debug_print(debug, ipaddr) \ 00250 ip6_addr_debug_print_parts(debug, \ 00251 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK1(ipaddr) : 0), \ 00252 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK2(ipaddr) : 0), \ 00253 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK3(ipaddr) : 0), \ 00254 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK4(ipaddr) : 0), \ 00255 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK5(ipaddr) : 0), \ 00256 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK6(ipaddr) : 0), \ 00257 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK7(ipaddr) : 0), \ 00258 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK8(ipaddr) : 0)) 00259 #define ip6_addr_debug_print_val(debug, ipaddr) \ 00260 ip6_addr_debug_print_parts(debug, \ 00261 IP6_ADDR_BLOCK1(&(ipaddr)), \ 00262 IP6_ADDR_BLOCK2(&(ipaddr)), \ 00263 IP6_ADDR_BLOCK3(&(ipaddr)), \ 00264 IP6_ADDR_BLOCK4(&(ipaddr)), \ 00265 IP6_ADDR_BLOCK5(&(ipaddr)), \ 00266 IP6_ADDR_BLOCK6(&(ipaddr)), \ 00267 IP6_ADDR_BLOCK7(&(ipaddr)), \ 00268 IP6_ADDR_BLOCK8(&(ipaddr))) 00269 00270 #define IP6ADDR_STRLEN_MAX 46 00271 00272 int ip6addr_aton(const char *cp, ip6_addr_t *addr); 00273 /** returns ptr to static buffer; not reentrant! */ 00274 char *ip6addr_ntoa(const ip6_addr_t *addr); 00275 char *ip6addr_ntoa_r(const ip6_addr_t *addr, char *buf, int buflen); 00276 00277 00278 00279 #ifdef __cplusplus 00280 } 00281 #endif 00282 00283 #endif /* LWIP_IPV6 */ 00284 00285 #endif /* LWIP_HDR_IP6_ADDR_H */
Generated on Tue Jul 12 2022 11:02:25 by
1.7.2