uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ip_addr.h
Go to the documentation of this file.
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 /* This is the aligned version of ip_addr_t,
43  used as local variable, on the stack, etc. */
44 struct ip_addr {
46 };
47 
48 /* This is the packed version of ip_addr_t,
49  used in network headers that are itself packed */
50 #ifdef PACK_STRUCT_USE_INCLUDES
51 # include "arch/bpstruct.h"
52 #endif
58 #ifdef PACK_STRUCT_USE_INCLUDES
59 # include "arch/epstruct.h"
60 #endif
61 
64 typedef struct ip_addr ip_addr_t;
65 typedef struct ip_addr_packed ip_addr_p_t;
66 
67 /*
68  * struct ipaddr2 is used in the definition of the ARP packet format in
69  * order to support compilers that don't have structure packing.
70  */
71 #ifdef PACK_STRUCT_USE_INCLUDES
72 # include "arch/bpstruct.h"
73 #endif
75 struct ip_addr2 {
76  PACK_STRUCT_FIELD(u16_t addrw[2]);
79 #ifdef PACK_STRUCT_USE_INCLUDES
80 # include "arch/epstruct.h"
81 #endif
82 
83 /* Forward declaration to not include netif.h */
84 struct netif;
85 
86 extern const ip_addr_t ip_addr_any;
87 extern const ip_addr_t ip_addr_broadcast;
88 
92 #define IP_ADDR_ANY ((ip_addr_t *)&ip_addr_any)
93 #define IP_ADDR_BROADCAST ((ip_addr_t *)&ip_addr_broadcast)
94 
96 #define IPADDR_NONE ((u32_t)0xffffffffUL)
97 
98 #define IPADDR_LOOPBACK ((u32_t)0x7f000001UL)
99 
100 #define IPADDR_ANY ((u32_t)0x00000000UL)
101 
102 #define IPADDR_BROADCAST ((u32_t)0xffffffffUL)
103 
104 /* Definitions of the bits in an Internet address integer.
105 
106  On subnets, host and network parts are found according to
107  the subnet mask, not these masks. */
108 #define IP_CLASSA(a) ((((u32_t)(a)) & 0x80000000UL) == 0)
109 #define IP_CLASSA_NET 0xff000000
110 #define IP_CLASSA_NSHIFT 24
111 #define IP_CLASSA_HOST (0xffffffff & ~IP_CLASSA_NET)
112 #define IP_CLASSA_MAX 128
113 
114 #define IP_CLASSB(a) ((((u32_t)(a)) & 0xc0000000UL) == 0x80000000UL)
115 #define IP_CLASSB_NET 0xffff0000
116 #define IP_CLASSB_NSHIFT 16
117 #define IP_CLASSB_HOST (0xffffffff & ~IP_CLASSB_NET)
118 #define IP_CLASSB_MAX 65536
119 
120 #define IP_CLASSC(a) ((((u32_t)(a)) & 0xe0000000UL) == 0xc0000000UL)
121 #define IP_CLASSC_NET 0xffffff00
122 #define IP_CLASSC_NSHIFT 8
123 #define IP_CLASSC_HOST (0xffffffff & ~IP_CLASSC_NET)
124 
125 #define IP_CLASSD(a) (((u32_t)(a) & 0xf0000000UL) == 0xe0000000UL)
126 #define IP_CLASSD_NET 0xf0000000 /* These ones aren't really */
127 #define IP_CLASSD_NSHIFT 28 /* net and host fields, but */
128 #define IP_CLASSD_HOST 0x0fffffff /* routing needn't know. */
129 #define IP_MULTICAST(a) IP_CLASSD(a)
130 
131 #define IP_EXPERIMENTAL(a) (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
132 #define IP_BADCLASS(a) (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
133 
134 #define IP_LOOPBACKNET 127 /* official! */
135 
136 
137 #if BYTE_ORDER == BIG_ENDIAN
138 
139 #define IP4_ADDR(ipaddr, a,b,c,d) \
140  (ipaddr)->addr = ((u32_t)((a) & 0xff) << 24) | \
141  ((u32_t)((b) & 0xff) << 16) | \
142  ((u32_t)((c) & 0xff) << 8) | \
143  (u32_t)((d) & 0xff)
144 #else
145 
147 #define IP4_ADDR(ipaddr, a,b,c,d) \
148  (ipaddr)->addr = ((u32_t)((d) & 0xff) << 24) | \
149  ((u32_t)((c) & 0xff) << 16) | \
150  ((u32_t)((b) & 0xff) << 8) | \
151  (u32_t)((a) & 0xff)
152 #endif
153 
157 #ifndef IPADDR2_COPY
158 #define IPADDR2_COPY(dest, src) SMEMCPY(dest, src, sizeof(ip_addr_t))
159 #endif
160 
162 #define ip_addr_copy(dest, src) ((dest).addr = (src).addr)
163 
164 #define ip_addr_set(dest, src) ((dest)->addr = \
165  ((src) == NULL ? 0 : \
166  (src)->addr))
167 
168 #define ip_addr_set_zero(ipaddr) ((ipaddr)->addr = 0)
169 
170 #define ip_addr_set_any(ipaddr) ((ipaddr)->addr = IPADDR_ANY)
171 
172 #define ip_addr_set_loopback(ipaddr) ((ipaddr)->addr = PP_HTONL(IPADDR_LOOPBACK))
173 
175 #define ip_addr_set_hton(dest, src) ((dest)->addr = \
176  ((src) == NULL ? 0:\
177  htonl((src)->addr)))
178 
179 #define ip4_addr_set_u32(dest_ipaddr, src_u32) ((dest_ipaddr)->addr = (src_u32))
180 
181 #define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr)
182 
184 #define ip_addr_get_network(target, host, netmask) ((target)->addr = ((host)->addr) & ((netmask)->addr))
185 
194 #define ip_addr_netcmp(addr1, addr2, mask) (((addr1)->addr & \
195  (mask)->addr) == \
196  ((addr2)->addr & \
197  (mask)->addr))
198 #define ip_addr_cmp(addr1, addr2) ((addr1)->addr == (addr2)->addr)
199 
200 #define ip_addr_isany(addr1) ((addr1) == NULL || (addr1)->addr == IPADDR_ANY)
201 
202 #define ip_addr_isbroadcast(ipaddr, netif) ip4_addr_isbroadcast((ipaddr)->addr, (netif))
203 u8_t ip4_addr_isbroadcast(u32_t addr, const struct netif *netif);
204 
205 #define ip_addr_netmask_valid(netmask) ip4_addr_netmask_valid((netmask)->addr)
207 
208 #define ip_addr_ismulticast(addr1) (((addr1)->addr & PP_HTONL(0xf0000000UL)) == PP_HTONL(0xe0000000UL))
209 
210 #define ip_addr_islinklocal(addr1) (((addr1)->addr & PP_HTONL(0xffff0000UL)) == PP_HTONL(0xa9fe0000UL))
211 
212 #define ip_addr_debug_print(debug, ipaddr) \
213  LWIP_DEBUGF(debug, ("%"U16_F".%"U16_F".%"U16_F".%"U16_F, \
214  ipaddr != NULL ? ip4_addr1_16(ipaddr) : 0, \
215  ipaddr != NULL ? ip4_addr2_16(ipaddr) : 0, \
216  ipaddr != NULL ? ip4_addr3_16(ipaddr) : 0, \
217  ipaddr != NULL ? ip4_addr4_16(ipaddr) : 0))
218 
219 /* Get one byte from the 4-byte address */
220 #define ip4_addr1(ipaddr) (((u8_t*)(ipaddr))[0])
221 #define ip4_addr2(ipaddr) (((u8_t*)(ipaddr))[1])
222 #define ip4_addr3(ipaddr) (((u8_t*)(ipaddr))[2])
223 #define ip4_addr4(ipaddr) (((u8_t*)(ipaddr))[3])
224 /* These are cast to u16_t, with the intent that they are often arguments
225  * to printf using the U16_F format from cc.h. */
226 #define ip4_addr1_16(ipaddr) ((u16_t)ip4_addr1(ipaddr))
227 #define ip4_addr2_16(ipaddr) ((u16_t)ip4_addr2(ipaddr))
228 #define ip4_addr3_16(ipaddr) ((u16_t)ip4_addr3(ipaddr))
229 #define ip4_addr4_16(ipaddr) ((u16_t)ip4_addr4(ipaddr))
230 
232 #define ip_ntoa(ipaddr) ipaddr_ntoa(ipaddr)
233 
234 u32_t ipaddr_addr(const char *cp);
235 int ipaddr_aton(const char *cp, ip_addr_t *addr);
237 char *ipaddr_ntoa(const ip_addr_t *addr);
238 char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen);
239 
240 #ifdef __cplusplus
241 }
242 #endif
243 
244 #endif /* __LWIP_IP_ADDR_H__ */