]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ankh/lib/lwip/contrib/src/include/ipv4/lwip/ip_addr.h
update
[l4.git] / l4 / pkg / ankh / lib / lwip / contrib / src / include / ipv4 / lwip / ip_addr.h
1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  *
29  * Author: Adam Dunkels <adam@sics.se>
30  *
31  */
32 #ifndef __LWIP_IP_ADDR_H__
33 #define __LWIP_IP_ADDR_H__
34
35 #include "lwip/opt.h"
36 #include "lwip/def.h"
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 #ifdef PACK_STRUCT_USE_INCLUDES
43 #  include "arch/bpstruct.h"
44 #endif
45 PACK_STRUCT_BEGIN
46 struct _ip_addr {
47   PACK_STRUCT_FIELD(u32_t addr);
48 } PACK_STRUCT_STRUCT;
49 PACK_STRUCT_END
50 #ifdef PACK_STRUCT_USE_INCLUDES
51 #  include "arch/epstruct.h"
52 #endif
53
54 typedef struct _ip_addr ip_addr_t;
55
56 /*
57  * struct ipaddr2 is used in the definition of the ARP packet format in
58  * order to support compilers that don't have structure packing.
59  */
60 #ifdef PACK_STRUCT_USE_INCLUDES
61 #  include "arch/bpstruct.h"
62 #endif
63 PACK_STRUCT_BEGIN
64 struct ip_addr2 {
65   PACK_STRUCT_FIELD(u16_t addrw[2]);
66 } PACK_STRUCT_STRUCT;
67 PACK_STRUCT_END
68 #ifdef PACK_STRUCT_USE_INCLUDES
69 #  include "arch/epstruct.h"
70 #endif
71
72 /* Forward declaration to not include netif.h */
73 struct netif;
74
75 extern const ip_addr_t ip_addr_any;
76 extern const ip_addr_t ip_addr_broadcast;
77
78 /** IP_ADDR_ can be used as a fixed IP address
79  *  for the wildcard and the broadcast address
80  */
81 #define IP_ADDR_ANY         ((ip_addr_t *)&ip_addr_any)
82 #define IP_ADDR_BROADCAST   ((ip_addr_t *)&ip_addr_broadcast)
83
84 /** 255.255.255.255 */
85 #define IPADDR_NONE         ((u32_t)0xffffffffUL)
86 /** 127.0.0.1 */
87 #define IPADDR_LOOPBACK     ((u32_t)0x7f000001UL)
88 /** 0.0.0.0 */
89 #define IPADDR_ANY          ((u32_t)0x00000000UL)
90 /** 255.255.255.255 */
91 #define IPADDR_BROADCAST    ((u32_t)0xffffffffUL)
92
93 /* Definitions of the bits in an Internet address integer.
94
95    On subnets, host and network parts are found according to
96    the subnet mask, not these masks.  */
97 #define IP_CLASSA(a)        ((((u32_t)(a)) & 0x80000000UL) == 0)
98 #define IP_CLASSA_NET       0xff000000
99 #define IP_CLASSA_NSHIFT    24
100 #define IP_CLASSA_HOST      (0xffffffff & ~IP_CLASSA_NET)
101 #define IP_CLASSA_MAX       128
102
103 #define IP_CLASSB(a)        ((((u32_t)(a)) & 0xc0000000UL) == 0x80000000UL)
104 #define IP_CLASSB_NET       0xffff0000
105 #define IP_CLASSB_NSHIFT    16
106 #define IP_CLASSB_HOST      (0xffffffff & ~IP_CLASSB_NET)
107 #define IP_CLASSB_MAX       65536
108
109 #define IP_CLASSC(a)        ((((u32_t)(a)) & 0xe0000000UL) == 0xc0000000UL)
110 #define IP_CLASSC_NET       0xffffff00
111 #define IP_CLASSC_NSHIFT    8
112 #define IP_CLASSC_HOST      (0xffffffff & ~IP_CLASSC_NET)
113
114 #define IP_CLASSD(a)        (((u32_t)(a) & 0xf0000000UL) == 0xe0000000UL)
115 #define IP_CLASSD_NET       0xf0000000          /* These ones aren't really */
116 #define IP_CLASSD_NSHIFT    28                  /*   net and host fields, but */
117 #define IP_CLASSD_HOST      0x0fffffff          /*   routing needn't know. */
118 #define IP_MULTICAST(a)     IP_CLASSD(a)
119
120 #define IP_EXPERIMENTAL(a)  (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
121 #define IP_BADCLASS(a)      (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
122
123 #define IP_LOOPBACKNET      127                 /* official! */
124
125
126 #if BYTE_ORDER == BIG_ENDIAN
127 /** Set an IP address given by the four byte-parts */
128 #define IP4_ADDR(ipaddr, a,b,c,d) \
129         (ipaddr)->addr = ((u32_t)((a) & 0xff) << 24) | \
130                                ((u32_t)((b) & 0xff) << 16) | \
131                                ((u32_t)((c) & 0xff) << 8) | \
132                           (u32_t)((d) & 0xff)
133 #else
134 /** Set an IP address given by the four byte-parts.
135     Little-endian version that prevents the use of htonl. */
136 #define IP4_ADDR(ipaddr, a,b,c,d) \
137         (ipaddr)->addr = ((u32_t)((d) & 0xff) << 24) | \
138                          ((u32_t)((c) & 0xff) << 16) | \
139                          ((u32_t)((b) & 0xff) << 8)  | \
140                           (u32_t)((a) & 0xff)
141 #endif
142
143 /** MEMCPY-like copying of IP addresses where addresses are known to be
144  * 16-bit-aligned if the port is correctly configured (so a port could define
145  * this to copying 2 u16_t's) - no NULL-pointer-checking needed. */
146 #ifndef IPADDR2_COPY
147 #define IPADDR2_COPY(dest, src) SMEMCPY(dest, src, sizeof(ip_addr_t))
148 #endif
149
150 /** Copy IP address - faster than ip_addr_set: no NULL check */
151 #define ip_addr_copy(dest, src) ((dest).addr = (src).addr)
152 /** Safely copy one IP address to another (src may be NULL) */
153 #define ip_addr_set(dest, src) ((dest)->addr = \
154                                     ((src) == NULL ? 0 : \
155                                     (src)->addr))
156 /** Set complete address to zero */
157 #define ip_addr_set_zero(ipaddr)      ((ipaddr)->addr = 0)
158 /** Set address to IPADDR_ANY (no need for htonl()) */
159 #define ip_addr_set_any(ipaddr)       ((ipaddr)->addr = IPADDR_ANY)
160 /** Set address to loopback address */
161 #define ip_addr_set_loopback(ipaddr)  ((ipaddr)->addr = PP_HTONL(IPADDR_LOOPBACK))
162 /** Safely copy one IP address to another and change byte order
163  * from host- to network-order. */
164 #define ip_addr_set_hton(dest, src) ((dest)->addr = \
165                                ((src) == NULL ? 0:\
166                                htonl((src)->addr)))
167 /** IPv4 only: set the IP address given as an u32_t */
168 #define ip4_addr_set_u32(dest_ipaddr, src_u32) ((dest_ipaddr)->addr = (src_u32))
169 /** IPv4 only: get the IP address as an u32_t */
170 #define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr)
171
172 /** Get the network address by combining host address with netmask */
173 #define ip_addr_get_network(target, host, netmask) ((target)->addr = ((host)->addr) & ((netmask)->addr))
174
175 /**
176  * Determine if two address are on the same network.
177  *
178  * @arg addr1 IP address 1
179  * @arg addr2 IP address 2
180  * @arg mask network identifier mask
181  * @return !0 if the network identifiers of both address match
182  */
183 #define ip_addr_netcmp(addr1, addr2, mask) (((addr1)->addr & \
184                                               (mask)->addr) == \
185                                              ((addr2)->addr & \
186                                               (mask)->addr))
187 #define ip_addr_cmp(addr1, addr2) ((addr1)->addr == (addr2)->addr)
188
189 #define ip_addr_isany(addr1) ((addr1) == NULL || (addr1)->addr == IPADDR_ANY)
190
191 u8_t ip_addr_isbroadcast(const ip_addr_t *, const struct netif *);
192
193 #define ip_addr_ismulticast(addr1) (((addr1)->addr & PP_HTONL(0xf0000000UL)) == PP_HTONL(0xe0000000UL))
194
195 #define ip_addr_islinklocal(addr1) (((addr1)->addr & PP_HTONL(0xffff0000UL)) == PP_HTONL(0xa9fe0000UL))
196
197 #define ip_addr_debug_print(debug, ipaddr) \
198   LWIP_DEBUGF(debug, ("%"U16_F".%"U16_F".%"U16_F".%"U16_F,              \
199                       ipaddr != NULL ? ip4_addr1_16(ipaddr) : 0,       \
200                       ipaddr != NULL ? ip4_addr2_16(ipaddr) : 0,       \
201                       ipaddr != NULL ? ip4_addr3_16(ipaddr) : 0,       \
202                       ipaddr != NULL ? ip4_addr4_16(ipaddr) : 0))
203
204 /* Get one byte from the 4-byte address */
205 #define ip4_addr1(ipaddr) (((u8_t*)(ipaddr))[0])
206 #define ip4_addr2(ipaddr) (((u8_t*)(ipaddr))[1])
207 #define ip4_addr3(ipaddr) (((u8_t*)(ipaddr))[2])
208 #define ip4_addr4(ipaddr) (((u8_t*)(ipaddr))[3])
209 /* These are cast to u16_t, with the intent that they are often arguments
210  * to printf using the U16_F format from cc.h. */
211 #define ip4_addr1_16(ipaddr) ((u16_t)ip4_addr1(ipaddr))
212 #define ip4_addr2_16(ipaddr) ((u16_t)ip4_addr2(ipaddr))
213 #define ip4_addr3_16(ipaddr) ((u16_t)ip4_addr3(ipaddr))
214 #define ip4_addr4_16(ipaddr) ((u16_t)ip4_addr4(ipaddr))
215
216 /** For backwards compatibility */
217 #define ip_ntoa(ipaddr)  ipaddr_ntoa(ipaddr)
218
219 u32_t ipaddr_addr(const char *cp);
220 int ipaddr_aton(const char *cp, ip_addr_t *addr);
221 /** returns ptr to static buffer; not reentrant! */
222 char *ipaddr_ntoa(const ip_addr_t *addr);
223 char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen);
224
225 #ifdef __cplusplus
226 }
227 #endif
228
229 #endif /* __LWIP_IP_ADDR_H__ */