• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if defined (__cplusplus) && __cplusplus
46 extern "C" {
47 #endif
48 
49 
50 /**
51  * @cond liteos This is the aligned version of ip4_addr_t,
52    used as local variable, on the stack, etc.
53  * @endcond   */
54 struct ip4_addr {
55   u32_t addr;
56 };
57 
58 /** ip4_addr_t uses a struct for convenience only, so that the same defines can
59  * operate both on ip4_addr_t as well as on ip4_addr_p_t. */
60 typedef struct ip4_addr ip4_addr_t;
61 
62 /* Forward declaration to not include netif.h */
63 struct netif;
64 
65 /** 255.255.255.255 */
66 #define IPADDR_NONE         ((u32_t)0xffffffffUL)
67 /** 127.0.0.1 */
68 #define IPADDR_LOOPBACK     ((u32_t)0x7f000001UL)
69 /** 0.0.0.0 */
70 #define IPADDR_ANY          ((u32_t)0x00000000UL)
71 /** 255.255.255.255 */
72 #define IPADDR_BROADCAST    ((u32_t)0xffffffffUL)
73 
74 /* Definitions of the bits in an Internet address integer.
75 
76    On subnets, host and network parts are found according to
77    the subnet mask, not these masks.  */
78 #define IP_CLASSA(a)        ((((u32_t)(a)) & 0x80000000UL) == 0)
79 #define IP_CLASSA_NET       0xff000000
80 #define IP_CLASSA_NSHIFT    24
81 #define IP_CLASSA_HOST      (0xffffffff & ~IP_CLASSA_NET)
82 #define IP_CLASSA_MAX       128
83 
84 #define IP_CLASSB(a)        ((((u32_t)(a)) & 0xc0000000UL) == 0x80000000UL)
85 #define IP_CLASSB_NET       0xffff0000
86 #define IP_CLASSB_NSHIFT    16
87 #define IP_CLASSB_HOST      (0xffffffff & ~IP_CLASSB_NET)
88 #define IP_CLASSB_MAX       65536
89 
90 #define IP_CLASSC(a)        ((((u32_t)(a)) & 0xe0000000UL) == 0xc0000000UL)
91 #define IP_CLASSC_NET       0xffffff00
92 #define IP_CLASSC_NSHIFT    8
93 #define IP_CLASSC_HOST      (0xffffffff & ~IP_CLASSC_NET)
94 
95 #define IP_CLASSD(a)        (((u32_t)(a) & 0xf0000000UL) == 0xe0000000UL)
96 #define IP_CLASSD_NET       0xf0000000          /* These ones aren't really */
97 #define IP_CLASSD_NSHIFT    28                  /*   net and host fields, but */
98 #define IP_CLASSD_HOST      0x0fffffff          /*   routing needn't know. */
99 #define IP_MULTICAST(a)     IP_CLASSD(a)
100 
101 #define IP_EXPERIMENTAL(a)  (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
102 #define IP_BADCLASS(a)      (((u32_t)(a) & 0xf0000000UL) == 0xf0000000UL)
103 
104 #define IP_LOOPBACKNET      127                 /* official! */
105 
106 /** Set an IP address given by the four byte-parts */
107 #define IP4_ADDR(ipaddr, a, b, c, d) (ipaddr)->addr = PP_HTONL(LWIP_MAKEU32(a, b, c, d))
108 
109 /** Copy IP address - faster than ip4_addr_set: no NULL check */
110 #define ip4_addr_copy(dest, src) ((dest).addr = (src).addr)
111 /** Safely copy one IP address to another (src may be NULL) */
112 #define ip4_addr_set(dest, src) ((dest)->addr = ((src) == NULL ? 0 : (src)->addr))
113 /** Set complete address to zero */
114 #define ip4_addr_set_zero(ipaddr) ((ipaddr)->addr = 0)
115 /** Set address to IPADDR_ANY (no need for lwip_htonl()) */
116 #define ip4_addr_set_any(ipaddr) ((ipaddr)->addr = IPADDR_ANY)
117 /** Set address to loopback address */
118 #define ip4_addr_set_loopback(ipaddr) ((ipaddr)->addr = PP_HTONL(IPADDR_LOOPBACK))
119 /** Check if an address is in the loopback region */
120 #define ip4_addr_isloopback(ipaddr) (((ipaddr)->addr & PP_HTONL(IP_CLASSA_NET)) == \
121                                       PP_HTONL(((u32_t)IP_LOOPBACKNET) << 24))
122 /** Safely copy one IP address to another and change byte order
123  * from host- to network-order. */
124 #define ip4_addr_set_hton(dest, src) ((dest)->addr = ((src) == NULL ? 0 : lwip_htonl((src)->addr)))
125 /** IPv4 only: set the IP address given as an u32_t */
126 #define ip4_addr_set_u32(dest_ipaddr, src_u32) ((dest_ipaddr)->addr = (src_u32))
127 /** IPv4 only: get the IP address as an u32_t */
128 #define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr)
129 
130 /** Get the network address by combining host address with netmask */
131 #define ip4_addr_get_network(target, host, netmask) do { \
132   ((target)->addr = ((host)->addr) & ((netmask)->addr)); } while (0)
133 
134 /**
135  * Determine if two address are on the same network.
136  *
137  * @arg addr1 IP address 1
138  * @arg addr2 IP address 2
139  * @arg mask network identifier mask
140  * @return !0 if the network identifiers of both address match
141  */
142 #define ip4_addr_netcmp(addr1, addr2, mask) (((addr1)->addr & \
143                                               (mask)->addr) == \
144                                              ((addr2)->addr & \
145                                               (mask)->addr))
146 #define ip4_addr_cmp(addr1, addr2) ((addr1)->addr == (addr2)->addr)
147 
148 #define ip4_addr_isany_val(addr1)   ((addr1).addr == IPADDR_ANY)
149 #define ip4_addr_isnone_val(addr1)   ((addr1).addr == IPADDR_NONE)
150 #define ip4_addr_isany(addr1) ((addr1) == NULL || ip4_addr_isany_val(*(addr1)))
151 #define ip4_addr_isnone(addr1) ((addr1) == NULL || ip4_addr_isnone_val(*(addr1)))
152 #define ip4_addr_isbroadcast(addr1, netif) ip4_addr_isbroadcast_u32((addr1)->addr, netif)
153 u8_t ip4_addr_isbroadcast_u32(u32_t addr, const struct netif *netif);
154 
155 #if LWIP_ENABLE_LOS_SHELL_CMD || LWIP_IOCTL_IF || LWIP_ENABLE_BASIC_SHELL_CMD
156 #define ip_addr_netmask_valid(netmask) ip4_addr_netmask_valid((netmask)->addr)
157 u8_t ip4_addr_netmask_valid(u32_t netmask);
158 #endif
159 
160 #define ip4_addr_ismulticast(addr1) (((addr1)->addr & PP_HTONL(0xf0000000UL)) == PP_HTONL(0xe0000000UL))
161 
162 #define ip4_addr_islinklocal(addr1) (((addr1)->addr & PP_HTONL(0xffff0000UL)) == PP_HTONL(0xa9fe0000UL))
163 
164 #ifdef LWIP_DEBUG
165 #define ip4_addr_debug_print_parts(debug, a, b, c, d) \
166   LWIP_DEBUGF(debug, ("%" U16_F ".%" U16_F ".%" U16_F ".%" U16_F, a, b, c, d))
167 #define ip4_addr_debug_print(debug, ipaddr) \
168   ip4_addr_debug_print_parts(debug, \
169                       (u16_t)((ipaddr) != NULL ? ip4_addr1_16(ipaddr) : 0),       \
170                       (u16_t)((ipaddr) != NULL ? ip4_addr2_16(ipaddr) : 0),       \
171                       (u16_t)((ipaddr) != NULL ? ip4_addr3_16(ipaddr) : 0),       \
172                       (u16_t)((ipaddr) != NULL ? ip4_addr4_16(ipaddr) : 0))
173 #define ip4_addr_debug_print_val(debug, ipaddr) \
174   ip4_addr_debug_print_parts(debug, \
175                       ip4_addr1_16_val(ipaddr),       \
176                       ip4_addr2_16_val(ipaddr),       \
177                       ip4_addr3_16_val(ipaddr),       \
178                       ip4_addr4_16_val(ipaddr))
179 #else
180 #define ip4_addr_debug_print_parts(debug, a, b, c, d)
181 #define ip4_addr_debug_print(debug, ipaddr)
182 #define ip4_addr_debug_print_val(debug, ipaddr)
183 #endif
184 
185 
186 /* Get one byte from the 4-byte address */
187 #define ip4_addr_get_byte(ipaddr, idx) (((const u8_t*)(&(ipaddr)->addr))[idx])
188 #define ip4_addr1(ipaddr) ip4_addr_get_byte(ipaddr, 0)
189 #define ip4_addr2(ipaddr) ip4_addr_get_byte(ipaddr, 1)
190 #define ip4_addr3(ipaddr) ip4_addr_get_byte(ipaddr, 2)
191 #define ip4_addr4(ipaddr) ip4_addr_get_byte(ipaddr, 3)
192 /* Get one byte from the 4-byte address, but argument is 'ip4_addr_t',
193  * not a pointer */
194 #define ip4_addr_get_byte_val(ipaddr, idx) ((u8_t)(((ipaddr).addr >> (idx * 8)) & 0xff))
195 #define ip4_addr1_val(ipaddr) ip4_addr_get_byte_val(ipaddr, 0)
196 #define ip4_addr2_val(ipaddr) ip4_addr_get_byte_val(ipaddr, 1)
197 #define ip4_addr3_val(ipaddr) ip4_addr_get_byte_val(ipaddr, 2)
198 #define ip4_addr4_val(ipaddr) ip4_addr_get_byte_val(ipaddr, 3)
199 /* These are cast to u16_t, with the intent that they are often arguments
200  * to printf using the U16_F format from cc.h. */
201 #define ip4_addr1_16(ipaddr) ((u16_t)ip4_addr1(ipaddr))
202 #define ip4_addr2_16(ipaddr) ((u16_t)ip4_addr2(ipaddr))
203 #define ip4_addr3_16(ipaddr) ((u16_t)ip4_addr3(ipaddr))
204 #define ip4_addr4_16(ipaddr) ((u16_t)ip4_addr4(ipaddr))
205 #define ip4_addr1_16_val(ipaddr) ((u16_t)ip4_addr1_val(ipaddr))
206 #define ip4_addr2_16_val(ipaddr) ((u16_t)ip4_addr2_val(ipaddr))
207 #define ip4_addr3_16_val(ipaddr) ((u16_t)ip4_addr3_val(ipaddr))
208 #define ip4_addr4_16_val(ipaddr) ((u16_t)ip4_addr4_val(ipaddr))
209 
210 #define IP4ADDR_STRLEN_MAX  16
211 #define IP4ADDR_STR_PARTS   4
212 
213 /** For backwards compatibility */
214 #define ip_ntoa(ipaddr)  ipaddr_ntoa(ipaddr)
215 
216 u32_t ipaddr_addr(const char *cp);
217 int ip4addr_aton(const char *cp, ip4_addr_t *addr);
218 /** returns ptr to static buffer; not reentrant! */
219 char *ip4addr_ntoa(const ip4_addr_t *addr);
220 char *ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen);
221 
222 int inet_pton4(const char *src, unsigned char *dst);
223 const char *lwip_inet_ntop4(const unsigned char *src, char *dst, u32_t size);
224 
225 #if defined (__cplusplus) && __cplusplus
226 }
227 #endif
228 
229 #endif /* LWIP_IPV4 */
230 
231 #endif /* LWIP_HDR_IP_ADDR_H */
232