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