The Pedigree Project  0.1
ip4_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 
25 /*
26  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without modification,
30  * are permitted provided that the following conditions are met:
31  *
32  * 1. Redistributions of source code must retain the above copyright notice,
33  * this list of conditions and the following disclaimer.
34  * 2. Redistributions in binary form must reproduce the above copyright notice,
35  * this list of conditions and the following disclaimer in the documentation
36  * and/or other materials provided with the distribution.
37  * 3. The name of the author may not be used to endorse or promote products
38  * derived from this software without specific prior written permission.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
43  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
45  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
48  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
49  * OF SUCH DAMAGE.
50  *
51  * This file is part of the lwIP TCP/IP stack.
52  *
53  * Author: Adam Dunkels <adam@sics.se>
54  *
55  */
56 #ifndef LWIP_HDR_IP4_ADDR_H
57 #define LWIP_HDR_IP4_ADDR_H
58 
59 #include "lwip/opt.h"
60 #include "lwip/def.h"
61 
62 #if LWIP_IPV4
63 
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
70 struct ip4_addr {
71  u32_t addr;
72 };
73 
76 typedef struct ip4_addr ip4_addr_t;
77 
82 #ifdef PACK_STRUCT_USE_INCLUDES
83 # include "arch/bpstruct.h"
84 #endif
86 struct ip4_addr2 {
87  PACK_STRUCT_FIELD(u16_t addrw[2]);
88 } PACK_STRUCT_STRUCT;
90 #ifdef PACK_STRUCT_USE_INCLUDES
91 # include "arch/epstruct.h"
92 #endif
93 
94 /* Forward declaration to not include netif.h */
95 struct netif;
96 
98 #define IPADDR_NONE ((u32_t)0xffffffffUL)
99 
100 #define IPADDR_LOOPBACK ((u32_t)0x7f000001UL)
101 
102 #define IPADDR_ANY ((u32_t)0x00000000UL)
103 
104 #define IPADDR_BROADCAST ((u32_t)0xffffffffUL)
105 
106 /* Definitions of the bits in an Internet address integer.
107 
108  On subnets, host and network parts are found according to
109  the subnet mask, not these masks. */
110 #define IP_CLASSA(a) ((((u32_t)(a)) & 0x80000000UL) == 0)
111 #define IP_CLASSA_NET 0xff000000
112 #define IP_CLASSA_NSHIFT 24
113 #define IP_CLASSA_HOST (0xffffffff & ~IP_CLASSA_NET)
114 #define IP_CLASSA_MAX 128
115 
116 #define IP_CLASSB(a) ((((u32_t)(a)) & 0xc0000000UL) == 0x80000000UL)
117 #define IP_CLASSB_NET 0xffff0000
118 #define IP_CLASSB_NSHIFT 16
119 #define IP_CLASSB_HOST (0xffffffff & ~IP_CLASSB_NET)
120 #define IP_CLASSB_MAX 65536
121 
122 #define IP_CLASSC(a) ((((u32_t)(a)) & 0xe0000000UL) == 0xc0000000UL)
123 #define IP_CLASSC_NET 0xffffff00
124 #define IP_CLASSC_NSHIFT 8
125 #define IP_CLASSC_HOST (0xffffffff & ~IP_CLASSC_NET)
126 
127 #define IP_CLASSD(a) (((u32_t)(a) & 0xf0000000UL) == 0xe0000000UL)
128 #define IP_CLASSD_NET 0xf0000000 /* These ones aren't really */
129 #define IP_CLASSD_NSHIFT 28 /* net and host fields, but */
130 #define IP_CLASSD_HOST 0x0fffffff /* routing needn't know. */
131 #define IP_MULTICAST(a) IP_CLASSD(a)
132 
133 #define IP_EXPERIMENTAL(a) (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
134 #define IP_BADCLASS(a) (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
135 
136 #define IP_LOOPBACKNET 127 /* official! */
137 
139 #define IP4_ADDR(ipaddr, a,b,c,d) (ipaddr)->addr = PP_HTONL(LWIP_MAKEU32(a,b,c,d))
140 
144 #ifndef IPADDR2_COPY
145 #define IPADDR2_COPY(dest, src) SMEMCPY(dest, src, sizeof(ip4_addr_t))
146 #endif
147 
149 #define ip4_addr_copy(dest, src) ((dest).addr = (src).addr)
150 
151 #define ip4_addr_set(dest, src) ((dest)->addr = \
152  ((src) == NULL ? 0 : \
153  (src)->addr))
154 
155 #define ip4_addr_set_zero(ipaddr) ((ipaddr)->addr = 0)
156 
157 #define ip4_addr_set_any(ipaddr) ((ipaddr)->addr = IPADDR_ANY)
158 
159 #define ip4_addr_set_loopback(ipaddr) ((ipaddr)->addr = PP_HTONL(IPADDR_LOOPBACK))
160 
161 #define ip4_addr_isloopback(ipaddr) (((ipaddr)->addr & PP_HTONL(IP_CLASSA_NET)) == PP_HTONL(((u32_t)IP_LOOPBACKNET) << 24))
162 
164 #define ip4_addr_set_hton(dest, src) ((dest)->addr = \
165  ((src) == NULL ? 0:\
166  lwip_htonl((src)->addr)))
167 
168 #define ip4_addr_set_u32(dest_ipaddr, src_u32) ((dest_ipaddr)->addr = (src_u32))
169 
170 #define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr)
171 
173 #define ip4_addr_get_network(target, host, netmask) do { ((target)->addr = ((host)->addr) & ((netmask)->addr)); } while(0)
174 
183 #define ip4_addr_netcmp(addr1, addr2, mask) (((addr1)->addr & \
184  (mask)->addr) == \
185  ((addr2)->addr & \
186  (mask)->addr))
187 #define ip4_addr_cmp(addr1, addr2) ((addr1)->addr == (addr2)->addr)
188 
189 #define ip4_addr_isany_val(addr1) ((addr1).addr == IPADDR_ANY)
190 #define ip4_addr_isany(addr1) ((addr1) == NULL || ip4_addr_isany_val(*(addr1)))
191 
192 #define ip4_addr_isbroadcast(addr1, netif) ip4_addr_isbroadcast_u32((addr1)->addr, netif)
193 u8_t ip4_addr_isbroadcast_u32(u32_t addr, const struct netif *netif);
194 
195 #define ip_addr_netmask_valid(netmask) ip4_addr_netmask_valid((netmask)->addr)
196 u8_t ip4_addr_netmask_valid(u32_t netmask);
197 
198 #define ip4_addr_ismulticast(addr1) (((addr1)->addr & PP_HTONL(0xf0000000UL)) == PP_HTONL(0xe0000000UL))
199 
200 #define ip4_addr_islinklocal(addr1) (((addr1)->addr & PP_HTONL(0xffff0000UL)) == PP_HTONL(0xa9fe0000UL))
201 
202 #define ip4_addr_debug_print_parts(debug, a, b, c, d) \
203  LWIP_DEBUGF(debug, ("%" U16_F ".%" U16_F ".%" U16_F ".%" U16_F, a, b, c, d))
204 #define ip4_addr_debug_print(debug, ipaddr) \
205  ip4_addr_debug_print_parts(debug, \
206  (u16_t)((ipaddr) != NULL ? ip4_addr1_16(ipaddr) : 0), \
207  (u16_t)((ipaddr) != NULL ? ip4_addr2_16(ipaddr) : 0), \
208  (u16_t)((ipaddr) != NULL ? ip4_addr3_16(ipaddr) : 0), \
209  (u16_t)((ipaddr) != NULL ? ip4_addr4_16(ipaddr) : 0))
210 #define ip4_addr_debug_print_val(debug, ipaddr) \
211  ip4_addr_debug_print_parts(debug, \
212  ip4_addr1_16(&(ipaddr)), \
213  ip4_addr2_16(&(ipaddr)), \
214  ip4_addr3_16(&(ipaddr)), \
215  ip4_addr4_16(&(ipaddr)))
216 
217 /* Get one byte from the 4-byte address */
218 #define ip4_addr1(ipaddr) (((const u8_t*)(&(ipaddr)->addr))[0])
219 #define ip4_addr2(ipaddr) (((const u8_t*)(&(ipaddr)->addr))[1])
220 #define ip4_addr3(ipaddr) (((const u8_t*)(&(ipaddr)->addr))[2])
221 #define ip4_addr4(ipaddr) (((const u8_t*)(&(ipaddr)->addr))[3])
222 /* These are cast to u16_t, with the intent that they are often arguments
223  * to printf using the U16_F format from cc.h. */
224 #define ip4_addr1_16(ipaddr) ((u16_t)ip4_addr1(ipaddr))
225 #define ip4_addr2_16(ipaddr) ((u16_t)ip4_addr2(ipaddr))
226 #define ip4_addr3_16(ipaddr) ((u16_t)ip4_addr3(ipaddr))
227 #define ip4_addr4_16(ipaddr) ((u16_t)ip4_addr4(ipaddr))
228 
229 #define IP4ADDR_STRLEN_MAX 16
230 
232 #define ip_ntoa(ipaddr) ipaddr_ntoa(ipaddr)
233 
234 u32_t ipaddr_addr(const char *cp);
235 int ip4addr_aton(const char *cp, ip4_addr_t *addr);
237 char *ip4addr_ntoa(const ip4_addr_t *addr);
238 char *ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen);
239 
240 #ifdef __cplusplus
241 }
242 #endif
243 
244 #endif /* LWIP_IPV4 */
245 
246 #endif /* LWIP_HDR_IP_ADDR_H */
#define PACK_STRUCT_BEGIN
Definition: arch.h:261
Definition: netif.h:244
#define PACK_STRUCT_END
Definition: arch.h:270
#define PACK_STRUCT_FIELD(x)
Definition: arch.h:292