1 /** 2 * @file 3 * This file (together with sockets.h) aims to provide structs and functions from 4 * - arpa/inet.h 5 * - netinet/in.h 6 * 7 */ 8 9 /* 10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 11 * All rights reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without modification, 14 * are permitted provided that the following conditions are met: 15 * 16 * 1. Redistributions of source code must retain the above copyright notice, 17 * this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright notice, 19 * this list of conditions and the following disclaimer in the documentation 20 * and/or other materials provided with the distribution. 21 * 3. The name of the author may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 33 * OF SUCH DAMAGE. 34 * 35 * This file is part of the lwIP TCP/IP stack. 36 * 37 * Author: Adam Dunkels <adam@sics.se> 38 * 39 */ 40 #ifndef LWIP_HDR_INET_H 41 #define LWIP_HDR_INET_H 42 43 #include "lwip/opt.h" 44 #include "lwip/def.h" 45 #include "lwip/ip_addr.h" 46 #include "lwip/ip6_addr.h" 47 48 #if defined (__cplusplus) && __cplusplus 49 extern "C" { 50 #endif 51 52 53 #if !LWIP_LITEOS_COMPAT 54 /* If your port already typedef's in_addr_t, define IN_ADDR_T_DEFINED 55 to prevent this code from redefining it. */ 56 #if !defined(in_addr_t) && !defined(IN_ADDR_T_DEFINED) 57 typedef u32_t in_addr_t; 58 #endif 59 /** 60 * 61 * @brief Provides a binary notation of the IPv4 address. 62 */ 63 struct in_addr { 64 in_addr_t s_addr; /** < Specifies the s_addr. */ 65 }; 66 67 68 /** 255.255.255.255 */ 69 #define INADDR_NONE IPADDR_NONE 70 /** 127.0.0.1 */ 71 #define INADDR_LOOPBACK IPADDR_LOOPBACK 72 /** 0.0.0.0 */ 73 #define INADDR_ANY IPADDR_ANY 74 /** 255.255.255.255 */ 75 #define INADDR_BROADCAST IPADDR_BROADCAST 76 77 78 /* Definitions of the bits in an (IPv4) Internet address integer. 79 80 On subnets, host and network parts are found according to 81 the subnet mask, not these masks. */ 82 #define IN_CLASSA(a) IP_CLASSA(a) 83 #define IN_CLASSA_NET IP_CLASSA_NET 84 #define IN_CLASSA_NSHIFT IP_CLASSA_NSHIFT 85 #define IN_CLASSA_HOST IP_CLASSA_HOST 86 #define IN_CLASSA_MAX IP_CLASSA_MAX 87 88 #define IN_CLASSB(b) IP_CLASSB(b) 89 #define IN_CLASSB_NET IP_CLASSB_NET 90 #define IN_CLASSB_NSHIFT IP_CLASSB_NSHIFT 91 #define IN_CLASSB_HOST IP_CLASSB_HOST 92 #define IN_CLASSB_MAX IP_CLASSB_MAX 93 94 #define IN_CLASSC(c) IP_CLASSC(c) 95 #define IN_CLASSC_NET IP_CLASSC_NET 96 #define IN_CLASSC_NSHIFT IP_CLASSC_NSHIFT 97 #define IN_CLASSC_HOST IP_CLASSC_HOST 98 #define IN_CLASSC_MAX IP_CLASSC_MAX 99 100 #define IN_CLASSD(d) IP_CLASSD(d) 101 #define IN_CLASSD_NET IP_CLASSD_NET /* These ones aren't really */ 102 #define IN_CLASSD_NSHIFT IP_CLASSD_NSHIFT /* net and host fields, but */ 103 #define IN_CLASSD_HOST IP_CLASSD_HOST /* routing needn't know. */ 104 #define IN_CLASSD_MAX IP_CLASSD_MAX 105 106 #define IN_MULTICAST(a) IP_MULTICAST(a) 107 108 #define IN_EXPERIMENTAL(a) IP_EXPERIMENTAL(a) 109 #define IN_BADCLASS(a) IP_BADCLASS(a) 110 111 #define IN_LOOPBACKNET IP_LOOPBACKNET 112 113 struct in6_addr { 114 union { 115 u32_t u32_addr[4]; 116 u8_t u8_addr[16]; 117 } un; 118 #define s6_addr un.u8_addr 119 }; 120 121 /** 122 * This macro is used to initialize a variable of type struct in6_addr 123 * to the IPv6 wildcard address. 124 */ 125 #ifndef IN6ADDR_ANY_INIT 126 #define IN6ADDR_ANY_INIT {{{0, 0, 0, 0}}} 127 #endif 128 /** 129 * This macro is used to initialize a variable of type struct in6_addr 130 * to the IPv6 loopback address. 131 */ 132 #ifndef IN6ADDR_LOOPBACK_INIT 133 #define IN6ADDR_LOOPBACK_INIT {{{0, 0, 0, PP_HTONL(1)}}} 134 #endif 135 136 #endif /* !LWIP_LITEOS_COMPAT */ 137 138 139 /** This variable is initialized by the system to contain the wildcard IPv6 address. */ 140 extern const struct in6_addr in6addr_any; 141 /* [Added new structure for ipv6 loopback as per section 3.9 of rfc 2553/3493] */ 142 extern const struct in6_addr in6addr_loopback; 143 144 #ifndef INET_ADDRSTRLEN 145 #define INET_ADDRSTRLEN IP4ADDR_STRLEN_MAX 146 #endif 147 #if LWIP_IPV6 148 #ifndef INET6_ADDRSTRLEN 149 #define INET6_ADDRSTRLEN IP6ADDR_STRLEN_MAX 150 #endif 151 #endif 152 153 #if LWIP_IPV4 154 155 #define inet_addr_from_ip4addr(target_inaddr, source_ipaddr) ((target_inaddr)->s_addr = ip4_addr_get_u32(source_ipaddr)) 156 #define inet_addr_to_ip4addr(target_ipaddr, source_inaddr) (ip4_addr_set_u32(target_ipaddr, (source_inaddr)->s_addr)) 157 158 /* directly map this to the lwip internal functions */ 159 /** 160 * @defgroup INET_Interfaces INET Interfaces 161 * This section contains information about INET interfaces. 162 * @ingroup User_interfaces 163 */ 164 /** 165 * @ingroup INET_Interfaces 166 * @brief 167 * This function is used to convert the IPv4 address from string notation (number and dot format) 168 * to binary notation of network byte order. 169 * 170 * @param[in] cp Indicates a pointer to the IPv4 address string. 171 * 172 * @returns 173 * Valid IP address: On success, in unsigned int (32-bit) format. \n 174 * IPADDR_NONE: On failure \n 175 176 * @note 177 * This interface will be available as function if LWIP_INET_ADDR_FUNC is enabled, 178 * otherwise it will be available as macro. 179 */ 180 #if LWIP_INET_ADDR_FUNC 181 unsigned int inet_addr(const char *cp); 182 #else 183 #define inet_addr(cp) ipaddr_addr(cp) 184 #endif 185 186 /* 187 Func Name: inet_aton 188 */ 189 /** 190 * @ingroup INET_Interfaces 191 * @brief 192 * This function is used to convert an IPv4 address from string notation (number and dot format) 193 * to binary notation of network byte order and store it in the structure that the addr parameter points to. 194 * 195 * @param[in] cp Indicates a pointer to an IPv4 address string. 196 * @param[out] addr Indicates a the generated binary notation of the IPv4 address. 197 * @returns 198 * 1 : On success\n 199 * 0 : On failure \n 200 * @note 201 * - This interface is similar to inet_addr(). The only difference is that the generated binary notation 202 * of IPv4 address is updated in the addr parameter, instead of returning it. 203 * - This interface is available as a function if LWIP_INET_ATON_FUNC is enabled, 204 * otherwise it will be available as macro. 205 */ 206 #if LWIP_INET_ATON_FUNC 207 int inet_aton(const char *cp, struct in_addr *addr); 208 #else 209 #define inet_aton(cp, addr) ip4addr_aton(cp, (ip4_addr_t*)addr) 210 #endif 211 212 213 /* 214 Func Name: inet_ntoa 215 */ 216 /** 217 * @ingroup INET_Interfaces 218 * @brief 219 * This function is used to convert an IPv4 address from binary notation (network byte order) 220 * to string notation (number and dot format). 221 * 222 * @param[in] in Indicates a pointer to the binary notation of an IPv4 address. 223 * 224 * @returns 225 * Valid pointer: On success, returns a pointer to the string notation of the IPv4 address. \n 226 * NULL: On failure \n 227 * 228 * @par Note 229 * This interface is available as a function if LWIP_INET_NTOA_FUNC is enabled, 230 * otherwise it will be available as macro. 231 * 232 */ 233 #if LWIP_INET_NTOA_FUNC 234 char *inet_ntoa (struct in_addr in); 235 #else 236 #define inet_ntoa(addr) ip4addr_ntoa((const ip4_addr_t*)&(addr)) 237 #endif 238 /** 239 * @ingroup INET_Interfaces 240 241 242 * @brief 243 * This function is used to convert an IPv4 address from binary notation (network byte order) 244 * to string notation (number and dot format). This is the reentrant API of inet_ntoa. The 245 * generated string notation IPv4 address is updated in the buffer passed by the user. 246 * 247 * @param[in] addr Indicates the binary notation of an IPv4 address. 248 * @param[out] buf Indicates a pointer to a user buffer, in which the output string format 249 of the IPv4 address is updated. 250 * @param[in] buflen Indicates the length of the user buffer. 251 * 252 * @returns 253 * Valid pointer : On success, returns a pointer to the buffer passed by the user in buf. \n 254 * NULL : On failure \n 255 * 256 257 * @note 258 * This interface is available as only macro. 259 * 260 */ 261 #define inet_ntoa_r(addr, buf, buflen) ip4addr_ntoa_r((const ip4_addr_t*)&(addr), buf, buflen) 262 263 #endif /* LWIP_IPV4 */ 264 265 #if LWIP_IPV6 266 267 #if LWIP_LITEOS_COMPAT 268 #define inet6_addr_from_ip6addr(target_in6addr, source_ip6addr) { \ 269 (void)memcpy((target_in6addr)->s6_addr, (source_ip6addr)->addr, sizeof(struct ip6_addr)); } 270 #define inet6_addr_to_ip6addr(target_ip6addr, source_in6addr) { \ 271 (void)memcpy((target_ip6addr)->addr, (source_in6addr)->s6_addr, sizeof(struct ip6_addr)); } 272 #else 273 #define inet6_addr_from_ip6addr(target_in6addr, source_ip6addr) do { \ 274 (target_in6addr)->un.u32_addr[0] = (source_ip6addr)->addr[0]; \ 275 (target_in6addr)->un.u32_addr[1] = (source_ip6addr)->addr[1]; \ 276 (target_in6addr)->un.u32_addr[2] = (source_ip6addr)->addr[2]; \ 277 (target_in6addr)->un.u32_addr[3] = (source_ip6addr)->addr[3]; } while (0) 278 279 #define inet6_addr_to_ip6addr(target_ip6addr, source_in6addr) do { \ 280 (target_ip6addr)->addr[0] = (source_in6addr)->un.u32_addr[0]; \ 281 (target_ip6addr)->addr[1] = (source_in6addr)->un.u32_addr[1]; \ 282 (target_ip6addr)->addr[2] = (source_in6addr)->un.u32_addr[2]; \ 283 (target_ip6addr)->addr[3] = (source_in6addr)->un.u32_addr[3]; } while (0) 284 #endif 285 286 /* directly map this to the lwip internal functions */ 287 #define inet6_aton(cp, addr) ip6addr_aton(cp, (ip6_addr_t*)addr) 288 #define inet6_ntoa(addr) ip6addr_ntoa((const ip6_addr_t*)&(addr)) 289 /** 290 * @ingroup INET_Interfaces 291 * @brief 292 * This interface is used to convert an IPv6 address from binary notation (network byte order) 293 * to string notation (colon format). This is the reentrant API of inet6_ntoa. The 294 * generated string notation IPv6 address is updated in the buffer passed by the user. 295 * 296 * @param[in] addr Indicates the binary notation of an IPv6 address. 297 * @param[out] buf Indicates a pointer to a user buffer, in which the output string 298 format of the IPv6 address is updated. 299 * @param[in] buflen Indicates the length of the user buffer. 300 * 301 * @returns 302 * Valid pointer: On success, returns a pointer to the buffer passed by the user in buf. \n 303 * NULL: On failure \n 304 305 * 306 * @par Note 307 * This interface is available as only macro. 308 309 * 310 */ 311 #define inet6_ntoa_r(addr, buf, buflen) ip6addr_ntoa_r((const ip6_addr_t*)&(addr), buf, buflen) 312 313 #endif /* LWIP_IPV6 */ 314 315 316 #if defined (__cplusplus) && __cplusplus 317 } 318 #endif 319 320 #endif /* LWIP_HDR_INET_H */ 321