The Pedigree Project  0.1
ip6_addr.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
26 /*
27  * Copyright (c) 2010 Inico Technologies Ltd.
28  * All rights reserved.
29  *
30  * Redistribution and use in source and binary forms, with or without modification,
31  * are permitted provided that the following conditions are met:
32  *
33  * 1. Redistributions of source code must retain the above copyright notice,
34  * this list of conditions and the following disclaimer.
35  * 2. Redistributions in binary form must reproduce the above copyright notice,
36  * this list of conditions and the following disclaimer in the documentation
37  * and/or other materials provided with the distribution.
38  * 3. The name of the author may not be used to endorse or promote products
39  * derived from this software without specific prior written permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
43  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
44  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
46  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
49  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
50  * OF SUCH DAMAGE.
51  *
52  * This file is part of the lwIP TCP/IP stack.
53  *
54  * Author: Ivan Delamer <delamer@inicotech.com>
55  *
56  * Structs and macros for handling IPv6 addresses.
57  *
58  * Please coordinate changes and requests with Ivan Delamer
59  * <delamer@inicotech.com>
60  */
61 #ifndef LWIP_HDR_IP6_ADDR_H
62 #define LWIP_HDR_IP6_ADDR_H
63 
64 #include "lwip/opt.h"
65 #include "def.h"
66 
67 #if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */
68 
69 
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73 
74 
77 struct ip6_addr {
78  u32_t addr[4];
79 };
80 
82 typedef struct ip6_addr ip6_addr_t;
83 
85 #define IP6_ADDR_PART(ip6addr, index, a,b,c,d) \
86  (ip6addr)->addr[index] = PP_HTONL(LWIP_MAKEU32(a,b,c,d))
87 
90 #define IP6_ADDR(ip6addr, idx0, idx1, idx2, idx3) do { \
91  (ip6addr)->addr[0] = idx0; \
92  (ip6addr)->addr[1] = idx1; \
93  (ip6addr)->addr[2] = idx2; \
94  (ip6addr)->addr[3] = idx3; } while(0)
95 
97 #define IP6_ADDR_BLOCK1(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[0]) >> 16) & 0xffff))
98 
99 #define IP6_ADDR_BLOCK2(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[0])) & 0xffff))
100 
101 #define IP6_ADDR_BLOCK3(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[1]) >> 16) & 0xffff))
102 
103 #define IP6_ADDR_BLOCK4(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[1])) & 0xffff))
104 
105 #define IP6_ADDR_BLOCK5(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[2]) >> 16) & 0xffff))
106 
107 #define IP6_ADDR_BLOCK6(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[2])) & 0xffff))
108 
109 #define IP6_ADDR_BLOCK7(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[3]) >> 16) & 0xffff))
110 
111 #define IP6_ADDR_BLOCK8(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[3])) & 0xffff))
112 
114 #define ip6_addr_copy(dest, src) do{(dest).addr[0] = (src).addr[0]; \
115  (dest).addr[1] = (src).addr[1]; \
116  (dest).addr[2] = (src).addr[2]; \
117  (dest).addr[3] = (src).addr[3];}while(0)
118 
119 #define ip6_addr_set(dest, src) do{(dest)->addr[0] = (src) == NULL ? 0 : (src)->addr[0]; \
120  (dest)->addr[1] = (src) == NULL ? 0 : (src)->addr[1]; \
121  (dest)->addr[2] = (src) == NULL ? 0 : (src)->addr[2]; \
122  (dest)->addr[3] = (src) == NULL ? 0 : (src)->addr[3];}while(0)
123 
125 #define ip6_addr_set_zero(ip6addr) do{(ip6addr)->addr[0] = 0; \
126  (ip6addr)->addr[1] = 0; \
127  (ip6addr)->addr[2] = 0; \
128  (ip6addr)->addr[3] = 0;}while(0)
129 
131 #define ip6_addr_set_any(ip6addr) ip6_addr_set_zero(ip6addr)
132 
133 #define ip6_addr_set_loopback(ip6addr) do{(ip6addr)->addr[0] = 0; \
134  (ip6addr)->addr[1] = 0; \
135  (ip6addr)->addr[2] = 0; \
136  (ip6addr)->addr[3] = PP_HTONL(0x00000001UL);}while(0)
137 
139 #define ip6_addr_set_hton(dest, src) do{(dest)->addr[0] = (src) == NULL ? 0 : lwip_htonl((src)->addr[0]); \
140  (dest)->addr[1] = (src) == NULL ? 0 : lwip_htonl((src)->addr[1]); \
141  (dest)->addr[2] = (src) == NULL ? 0 : lwip_htonl((src)->addr[2]); \
142  (dest)->addr[3] = (src) == NULL ? 0 : lwip_htonl((src)->addr[3]);}while(0)
143 
144 
152 #define ip6_addr_netcmp(addr1, addr2) (((addr1)->addr[0] == (addr2)->addr[0]) && \
153  ((addr1)->addr[1] == (addr2)->addr[1]))
154 
155 #define ip6_addr_cmp(addr1, addr2) (((addr1)->addr[0] == (addr2)->addr[0]) && \
156  ((addr1)->addr[1] == (addr2)->addr[1]) && \
157  ((addr1)->addr[2] == (addr2)->addr[2]) && \
158  ((addr1)->addr[3] == (addr2)->addr[3]))
159 
160 #define ip6_get_subnet_id(ip6addr) (lwip_htonl((ip6addr)->addr[2]) & 0x0000ffffUL)
161 
162 #define ip6_addr_isany_val(ip6addr) (((ip6addr).addr[0] == 0) && \
163  ((ip6addr).addr[1] == 0) && \
164  ((ip6addr).addr[2] == 0) && \
165  ((ip6addr).addr[3] == 0))
166 #define ip6_addr_isany(ip6addr) (((ip6addr) == NULL) || ip6_addr_isany_val(*(ip6addr)))
167 
168 #define ip6_addr_isloopback(ip6addr) (((ip6addr)->addr[0] == 0UL) && \
169  ((ip6addr)->addr[1] == 0UL) && \
170  ((ip6addr)->addr[2] == 0UL) && \
171  ((ip6addr)->addr[3] == PP_HTONL(0x00000001UL)))
172 
173 #define ip6_addr_isglobal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xe0000000UL)) == PP_HTONL(0x20000000UL))
174 
175 #define ip6_addr_islinklocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xffc00000UL)) == PP_HTONL(0xfe800000UL))
176 
177 #define ip6_addr_issitelocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xffc00000UL)) == PP_HTONL(0xfec00000UL))
178 
179 #define ip6_addr_isuniquelocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xfe000000UL)) == PP_HTONL(0xfc000000UL))
180 
181 #define ip6_addr_isipv4mappedipv6(ip6addr) (((ip6addr)->addr[0] == 0) && ((ip6addr)->addr[1] == 0) && (((ip6addr)->addr[2]) == PP_HTONL(0x0000FFFFUL)))
182 
183 #define ip6_addr_ismulticast(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff000000UL)) == PP_HTONL(0xff000000UL))
184 #define ip6_addr_multicast_transient_flag(ip6addr) ((ip6addr)->addr[0] & PP_HTONL(0x00100000UL))
185 #define ip6_addr_multicast_prefix_flag(ip6addr) ((ip6addr)->addr[0] & PP_HTONL(0x00200000UL))
186 #define ip6_addr_multicast_rendezvous_flag(ip6addr) ((ip6addr)->addr[0] & PP_HTONL(0x00400000UL))
187 #define ip6_addr_multicast_scope(ip6addr) ((lwip_htonl((ip6addr)->addr[0]) >> 16) & 0xf)
188 #define IP6_MULTICAST_SCOPE_RESERVED 0x0
189 #define IP6_MULTICAST_SCOPE_RESERVED0 0x0
190 #define IP6_MULTICAST_SCOPE_INTERFACE_LOCAL 0x1
191 #define IP6_MULTICAST_SCOPE_LINK_LOCAL 0x2
192 #define IP6_MULTICAST_SCOPE_RESERVED3 0x3
193 #define IP6_MULTICAST_SCOPE_ADMIN_LOCAL 0x4
194 #define IP6_MULTICAST_SCOPE_SITE_LOCAL 0x5
195 #define IP6_MULTICAST_SCOPE_ORGANIZATION_LOCAL 0x8
196 #define IP6_MULTICAST_SCOPE_GLOBAL 0xe
197 #define IP6_MULTICAST_SCOPE_RESERVEDF 0xf
198 #define ip6_addr_ismulticast_iflocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff010000UL))
199 #define ip6_addr_ismulticast_linklocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff020000UL))
200 #define ip6_addr_ismulticast_adminlocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff040000UL))
201 #define ip6_addr_ismulticast_sitelocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff050000UL))
202 #define ip6_addr_ismulticast_orglocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff080000UL))
203 #define ip6_addr_ismulticast_global(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff0e0000UL))
204 
205 /* @todo define get/set for well-know multicast addresses, e.g. ff02::1 */
206 #define ip6_addr_isallnodes_iflocal(ip6addr) (((ip6addr)->addr[0] == PP_HTONL(0xff010000UL)) && \
207  ((ip6addr)->addr[1] == 0UL) && \
208  ((ip6addr)->addr[2] == 0UL) && \
209  ((ip6addr)->addr[3] == PP_HTONL(0x00000001UL)))
210 
211 #define ip6_addr_isallnodes_linklocal(ip6addr) (((ip6addr)->addr[0] == PP_HTONL(0xff020000UL)) && \
212  ((ip6addr)->addr[1] == 0UL) && \
213  ((ip6addr)->addr[2] == 0UL) && \
214  ((ip6addr)->addr[3] == PP_HTONL(0x00000001UL)))
215 #define ip6_addr_set_allnodes_linklocal(ip6addr) do{(ip6addr)->addr[0] = PP_HTONL(0xff020000UL); \
216  (ip6addr)->addr[1] = 0; \
217  (ip6addr)->addr[2] = 0; \
218  (ip6addr)->addr[3] = PP_HTONL(0x00000001UL);}while(0)
219 
220 #define ip6_addr_isallrouters_linklocal(ip6addr) (((ip6addr)->addr[0] == PP_HTONL(0xff020000UL)) && \
221  ((ip6addr)->addr[1] == 0UL) && \
222  ((ip6addr)->addr[2] == 0UL) && \
223  ((ip6addr)->addr[3] == PP_HTONL(0x00000002UL)))
224 #define ip6_addr_set_allrouters_linklocal(ip6addr) do{(ip6addr)->addr[0] = PP_HTONL(0xff020000UL); \
225  (ip6addr)->addr[1] = 0; \
226  (ip6addr)->addr[2] = 0; \
227  (ip6addr)->addr[3] = PP_HTONL(0x00000002UL);}while(0)
228 
229 #define ip6_addr_issolicitednode(ip6addr) ( ((ip6addr)->addr[0] == PP_HTONL(0xff020000UL)) && \
230  ((ip6addr)->addr[2] == PP_HTONL(0x00000001UL)) && \
231  (((ip6addr)->addr[3] & PP_HTONL(0xff000000UL)) == PP_HTONL(0xff000000UL)) )
232 
233 #define ip6_addr_set_solicitednode(ip6addr, if_id) do{(ip6addr)->addr[0] = PP_HTONL(0xff020000UL); \
234  (ip6addr)->addr[1] = 0; \
235  (ip6addr)->addr[2] = PP_HTONL(0x00000001UL); \
236  (ip6addr)->addr[3] = (PP_HTONL(0xff000000UL) | (if_id));}while(0)
237 
238 #define ip6_addr_cmp_solicitednode(ip6addr, sn_addr) (((ip6addr)->addr[0] == PP_HTONL(0xff020000UL)) && \
239  ((ip6addr)->addr[1] == 0) && \
240  ((ip6addr)->addr[2] == PP_HTONL(0x00000001UL)) && \
241  ((ip6addr)->addr[3] == (PP_HTONL(0xff000000UL) | (sn_addr)->addr[3])))
242 
243 /* IPv6 address states. */
244 #define IP6_ADDR_INVALID 0x00
245 #define IP6_ADDR_TENTATIVE 0x08
246 #define IP6_ADDR_TENTATIVE_1 0x09 /* 1 probe sent */
247 #define IP6_ADDR_TENTATIVE_2 0x0a /* 2 probes sent */
248 #define IP6_ADDR_TENTATIVE_3 0x0b /* 3 probes sent */
249 #define IP6_ADDR_TENTATIVE_4 0x0c /* 4 probes sent */
250 #define IP6_ADDR_TENTATIVE_5 0x0d /* 5 probes sent */
251 #define IP6_ADDR_TENTATIVE_6 0x0e /* 6 probes sent */
252 #define IP6_ADDR_TENTATIVE_7 0x0f /* 7 probes sent */
253 #define IP6_ADDR_VALID 0x10 /* This bit marks an address as valid (preferred or deprecated) */
254 #define IP6_ADDR_PREFERRED 0x30
255 #define IP6_ADDR_DEPRECATED 0x10 /* Same as VALID (valid but not preferred) */
256 
257 #define IP6_ADDR_TENTATIVE_COUNT_MASK 0x07 /* 1-7 probes sent */
258 
259 #define ip6_addr_isinvalid(addr_state) (addr_state == IP6_ADDR_INVALID)
260 #define ip6_addr_istentative(addr_state) (addr_state & IP6_ADDR_TENTATIVE)
261 #define ip6_addr_isvalid(addr_state) (addr_state & IP6_ADDR_VALID) /* Include valid, preferred, and deprecated. */
262 #define ip6_addr_ispreferred(addr_state) (addr_state == IP6_ADDR_PREFERRED)
263 #define ip6_addr_isdeprecated(addr_state) (addr_state == IP6_ADDR_DEPRECATED)
264 
265 #define ip6_addr_debug_print_parts(debug, a, b, c, d, e, f, g, h) \
266  LWIP_DEBUGF(debug, ("%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F, \
267  a, b, c, d, e, f, g, h))
268 #define ip6_addr_debug_print(debug, ipaddr) \
269  ip6_addr_debug_print_parts(debug, \
270  (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK1(ipaddr) : 0), \
271  (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK2(ipaddr) : 0), \
272  (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK3(ipaddr) : 0), \
273  (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK4(ipaddr) : 0), \
274  (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK5(ipaddr) : 0), \
275  (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK6(ipaddr) : 0), \
276  (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK7(ipaddr) : 0), \
277  (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK8(ipaddr) : 0))
278 #define ip6_addr_debug_print_val(debug, ipaddr) \
279  ip6_addr_debug_print_parts(debug, \
280  IP6_ADDR_BLOCK1(&(ipaddr)), \
281  IP6_ADDR_BLOCK2(&(ipaddr)), \
282  IP6_ADDR_BLOCK3(&(ipaddr)), \
283  IP6_ADDR_BLOCK4(&(ipaddr)), \
284  IP6_ADDR_BLOCK5(&(ipaddr)), \
285  IP6_ADDR_BLOCK6(&(ipaddr)), \
286  IP6_ADDR_BLOCK7(&(ipaddr)), \
287  IP6_ADDR_BLOCK8(&(ipaddr)))
288 
289 #define IP6ADDR_STRLEN_MAX 46
290 
291 int ip6addr_aton(const char *cp, ip6_addr_t *addr);
293 char *ip6addr_ntoa(const ip6_addr_t *addr);
294 char *ip6addr_ntoa_r(const ip6_addr_t *addr, char *buf, int buflen);
295 
296 
297 
298 #ifdef __cplusplus
299 }
300 #endif
301 
302 #endif /* LWIP_IPV6 */
303 
304 #endif /* LWIP_HDR_IP6_ADDR_H */