STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
ip4_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_HDR_IP4_ADDR_H
33 #define LWIP_HDR_IP4_ADDR_H
34 
35 #include "lwip/opt.h"
36 #include "lwip/def.h"
37 
38 #if LWIP_IPV4
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /* This is the aligned version of ip4_addr_t,
45  used as local variable, on the stack, etc. */
46 struct ip4_addr {
47  u32_t addr;
48 };
49 
50 /* This is the packed version of ip4_addr_t,
51  used in network headers that are itself packed */
52 #ifdef PACK_STRUCT_USE_INCLUDES
53 # include "arch/bpstruct.h"
54 #endif
56 struct ip4_addr_packed {
60 #ifdef PACK_STRUCT_USE_INCLUDES
61 # include "arch/epstruct.h"
62 #endif
63 
66 typedef struct ip4_addr ip4_addr_t;
67 typedef struct ip4_addr_packed ip4_addr_p_t;
68 
69 /*
70  * struct ipaddr2 is used in the definition of the ARP packet format in
71  * order to support compilers that don't have structure packing.
72  */
73 #ifdef PACK_STRUCT_USE_INCLUDES
74 # include "arch/bpstruct.h"
75 #endif
77 struct ip4_addr2 {
78  PACK_STRUCT_FIELD(u16_t addrw[2]);
81 #ifdef PACK_STRUCT_USE_INCLUDES
82 # include "arch/epstruct.h"
83 #endif
84 
85 /* Forward declaration to not include netif.h */
86 struct netif;
87 
89 #define IPADDR_NONE ((u32_t)0xffffffffUL)
90 
91 #define IPADDR_LOOPBACK ((u32_t)0x7f000001UL)
92 
93 #define IPADDR_ANY ((u32_t)0x00000000UL)
94 
95 #define IPADDR_BROADCAST ((u32_t)0xffffffffUL)
96 
97 /* Definitions of the bits in an Internet address integer.
98 
99  On subnets, host and network parts are found according to
100  the subnet mask, not these masks. */
101 #define IP_CLASSA(a) ((((u32_t)(a)) & 0x80000000UL) == 0)
102 #define IP_CLASSA_NET 0xff000000
103 #define IP_CLASSA_NSHIFT 24
104 #define IP_CLASSA_HOST (0xffffffff & ~IP_CLASSA_NET)
105 #define IP_CLASSA_MAX 128
106 
107 #define IP_CLASSB(a) ((((u32_t)(a)) & 0xc0000000UL) == 0x80000000UL)
108 #define IP_CLASSB_NET 0xffff0000
109 #define IP_CLASSB_NSHIFT 16
110 #define IP_CLASSB_HOST (0xffffffff & ~IP_CLASSB_NET)
111 #define IP_CLASSB_MAX 65536
112 
113 #define IP_CLASSC(a) ((((u32_t)(a)) & 0xe0000000UL) == 0xc0000000UL)
114 #define IP_CLASSC_NET 0xffffff00
115 #define IP_CLASSC_NSHIFT 8
116 #define IP_CLASSC_HOST (0xffffffff & ~IP_CLASSC_NET)
117 
118 #define IP_CLASSD(a) (((u32_t)(a) & 0xf0000000UL) == 0xe0000000UL)
119 #define IP_CLASSD_NET 0xf0000000 /* These ones aren't really */
120 #define IP_CLASSD_NSHIFT 28 /* net and host fields, but */
121 #define IP_CLASSD_HOST 0x0fffffff /* routing needn't know. */
122 #define IP_MULTICAST(a) IP_CLASSD(a)
123 
124 #define IP_EXPERIMENTAL(a) (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
125 #define IP_BADCLASS(a) (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
126 
127 #define IP_LOOPBACKNET 127 /* official! */
128 
129 
130 #if BYTE_ORDER == BIG_ENDIAN
131 
132 #define IP4_ADDR(ipaddr, a,b,c,d) \
133  (ipaddr)->addr = ((u32_t)((a) & 0xff) << 24) | \
134  ((u32_t)((b) & 0xff) << 16) | \
135  ((u32_t)((c) & 0xff) << 8) | \
136  (u32_t)((d) & 0xff)
137 #else
138 
140 #define IP4_ADDR(ipaddr, a,b,c,d) \
141  (ipaddr)->addr = ((u32_t)((d) & 0xff) << 24) | \
142  ((u32_t)((c) & 0xff) << 16) | \
143  ((u32_t)((b) & 0xff) << 8) | \
144  (u32_t)((a) & 0xff)
145 #endif
146 
150 #ifndef IPADDR2_COPY
151 #define IPADDR2_COPY(dest, src) SMEMCPY(dest, src, sizeof(ip4_addr_t))
152 #endif
153 
155 #define ip4_addr_copy(dest, src) ((dest).addr = (src).addr)
156 
157 #define ip4_addr_set(dest, src) ((dest)->addr = \
158  ((src) == NULL ? 0 : \
159  (src)->addr))
160 
161 #define ip4_addr_set_zero(ipaddr) ((ipaddr)->addr = 0)
162 
163 #define ip4_addr_set_any(ipaddr) ((ipaddr)->addr = IPADDR_ANY)
164 
165 #define ip4_addr_set_loopback(ipaddr) ((ipaddr)->addr = PP_HTONL(IPADDR_LOOPBACK))
166 
167 #define ip4_addr_isloopback(ipaddr) (((ipaddr)->addr & PP_HTONL(IP_CLASSA_NET)) == PP_HTONL(((u32_t)IP_LOOPBACKNET) << 24))
168 
170 #define ip4_addr_set_hton(dest, src) ((dest)->addr = \
171  ((src) == NULL ? 0:\
172  htonl((src)->addr)))
173 
174 #define ip4_addr_set_u32(dest_ipaddr, src_u32) ((dest_ipaddr)->addr = (src_u32))
175 
176 #define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr)
177 
179 #define ip4_addr_get_network(target, host, netmask) do { ((target)->addr = ((host)->addr) & ((netmask)->addr)); } while(0)
180 
189 #define ip4_addr_netcmp(addr1, addr2, mask) (((addr1)->addr & \
190  (mask)->addr) == \
191  ((addr2)->addr & \
192  (mask)->addr))
193 #define ip4_addr_cmp(addr1, addr2) ((addr1)->addr == (addr2)->addr)
194 
195 #define ip4_addr_isany_val(addr1) ((addr1).addr == IPADDR_ANY)
196 #define ip4_addr_isany(addr1) ((addr1) == NULL || ip4_addr_isany_val(*(addr1)))
197 
198 #define ip4_addr_isbroadcast(addr1, netif) ip4_addr_isbroadcast_u32((addr1)->addr, netif)
199 u8_t ip4_addr_isbroadcast_u32(u32_t addr, const struct netif *netif);
200 
201 #define ip_addr_netmask_valid(netmask) ip4_addr_netmask_valid((netmask)->addr)
202 u8_t ip4_addr_netmask_valid(u32_t netmask);
203 
204 #define ip4_addr_ismulticast(addr1) (((addr1)->addr & PP_HTONL(0xf0000000UL)) == PP_HTONL(0xe0000000UL))
205 
206 #define ip4_addr_islinklocal(addr1) (((addr1)->addr & PP_HTONL(0xffff0000UL)) == PP_HTONL(0xa9fe0000UL))
207 
208 #define ip4_addr_debug_print_parts(debug, a, b, c, d) \
209  LWIP_DEBUGF(debug, ("%" U16_F ".%" U16_F ".%" U16_F ".%" U16_F, a, b, c, d))
210 #define ip4_addr_debug_print(debug, ipaddr) \
211  ip4_addr_debug_print_parts(debug, \
212  (ipaddr) != NULL ? ip4_addr1_16(ipaddr) : 0, \
213  (ipaddr) != NULL ? ip4_addr2_16(ipaddr) : 0, \
214  (ipaddr) != NULL ? ip4_addr3_16(ipaddr) : 0, \
215  (ipaddr) != NULL ? ip4_addr4_16(ipaddr) : 0)
216 #define ip4_addr_debug_print_val(debug, ipaddr) \
217  ip4_addr_debug_print_parts(debug, \
218  ip4_addr1_16(&(ipaddr)), \
219  ip4_addr2_16(&(ipaddr)), \
220  ip4_addr3_16(&(ipaddr)), \
221  ip4_addr4_16(&(ipaddr)))
222 
223 /* Get one byte from the 4-byte address */
224 #define ip4_addr1(ipaddr) (((const u8_t*)(&(ipaddr)->addr))[0])
225 #define ip4_addr2(ipaddr) (((const u8_t*)(&(ipaddr)->addr))[1])
226 #define ip4_addr3(ipaddr) (((const u8_t*)(&(ipaddr)->addr))[2])
227 #define ip4_addr4(ipaddr) (((const u8_t*)(&(ipaddr)->addr))[3])
228 /* These are cast to u16_t, with the intent that they are often arguments
229  * to printf using the U16_F format from cc.h. */
230 #define ip4_addr1_16(ipaddr) ((u16_t)ip4_addr1(ipaddr))
231 #define ip4_addr2_16(ipaddr) ((u16_t)ip4_addr2(ipaddr))
232 #define ip4_addr3_16(ipaddr) ((u16_t)ip4_addr3(ipaddr))
233 #define ip4_addr4_16(ipaddr) ((u16_t)ip4_addr4(ipaddr))
234 
235 #define IP4ADDR_STRLEN_MAX 16
236 #define IPADDR_STRLEN_MAX IP4ADDR_STRLEN_MAX
237 
239 #define ip_ntoa(ipaddr) ipaddr_ntoa(ipaddr)
240 
241 u32_t ipaddr_addr(const char *cp);
242 int ip4addr_aton(const char *cp, ip4_addr_t *addr);
244 char *ip4addr_ntoa(const ip4_addr_t *addr);
245 char *ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen);
246 
247 #ifdef __cplusplus
248 }
249 #endif
250 
251 #endif /* LWIP_IPV4 */
252 
253 #endif /* LWIP_HDR_IP_ADDR_H */
254 
#define PACK_STRUCT_STRUCT
Definition: arch.h:68
#define PACK_STRUCT_FIELD(x)
Definition: arch.h:72
unsigned long u32_t
Definition: cc.h:42
Definition: netif.h:182
#define PACK_STRUCT_BEGIN
Definition: arch.h:60
unsigned char u8_t
Definition: cc.h:38
#define PACK_STRUCT_END
Definition: arch.h:64
unsigned short u16_t
Definition: cc.h:40