xref: /nrf52832-nimble/rt-thread/components/net/sal_socket/include/sal_ipaddr.h (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2018-05-18     ChenYong     First version
9  */
10 #ifndef SAL_IPADDR_H__
11 #define SAL_IPADDR_H__
12 
13 #include "sal_type.h"
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 /** IPv4 only: set the IP address given as an u32_t */
20 #define ip4_addr_set_u32(dest_ipaddr, src_u32) ((dest_ipaddr)->addr = (src_u32))
21 /** IPv4 only: get the IP address as an u32_t */
22 #define ip4_addr_get_u32(src_ipaddr) ((src_ipaddr)->addr)
23 
24 #define IP4ADDR_STRLEN_MAX  16
25 
26 /* These macros should be calculated by the preprocessor and are used
27    with compile-time constants only (so that there is no little-endian
28    overhead at runtime). */
29 #define PP_HTONS(x) ((((x) & 0x00ffUL) << 8) | (((x) & 0xff00UL) >> 8))
30 #define PP_NTOHS(x) PP_HTONS(x)
31 #define PP_HTONL(x) ((((x) & 0x000000ffUL) << 24) | \
32                      (((x) & 0x0000ff00UL) <<  8) | \
33                      (((x) & 0x00ff0000UL) >>  8) | \
34                      (((x) & 0xff000000UL) >> 24))
35 #define PP_NTOHL(x) PP_HTONL(x)
36 
37 #define htons(x) (u16_t)PP_HTONS(x)
38 #define ntohs(x) (u16_t)PP_NTOHS(x)
39 #define htonl(x) (u32_t)PP_HTONL(x)
40 #define ntohl(x) (u32_t)PP_NTOHL(x)
41 
42 /* If your port already typedef's in_addr_t, define IN_ADDR_T_DEFINED
43    to prevent this code from redefining it. */
44 #if !defined(in_addr_t) && !defined(IN_ADDR_T_DEFINED)
45 typedef u32_t in_addr_t;
46 #endif
47 
48 struct in_addr
49 {
50     in_addr_t s_addr;
51 };
52 
53 struct in6_addr
54 {
55     union
56     {
57         u32_t u32_addr[4];
58         u8_t u8_addr[16];
59     } un;
60 #define s6_addr  un.u8_addr
61 };
62 
63 enum sal_ip_addr_type
64 {
65     /** IPv4 */
66     IPADDR_TYPE_V4 = 0U,
67     /** IPv6 */
68     IPADDR_TYPE_V6 = 6U,
69     /** IPv4+IPv6 ("dual-stack") */
70     IPADDR_TYPE_ANY = 46U
71 };
72 
73 typedef struct ip4_addr
74 {
75     u32_t addr;
76 } ip4_addr_t;
77 
78 typedef struct ip6_addr
79 {
80     u32_t addr[4];
81 } ip6_addr_t;
82 
83 typedef struct _ip_addr
84 {
85     union
86     {
87         ip6_addr_t ip6;
88         ip4_addr_t ip4;
89     } u_addr;
90     /** @ref sal_ip_addr_type */
91     u8_t type;
92 } ip_addr_t;
93 
94 /** 255.255.255.255 */
95 #define IPADDR_NONE         ((u32_t)0xffffffffUL)
96 /** 127.0.0.1 */
97 #define IPADDR_LOOPBACK     ((u32_t)0x7f000001UL)
98 /** 0.0.0.0 */
99 #define IPADDR_ANY          ((u32_t)0x00000000UL)
100 /** 255.255.255.255 */
101 #define IPADDR_BROADCAST    ((u32_t)0xffffffffUL)
102 
103 /** 255.255.255.255 */
104 #define INADDR_NONE         IPADDR_NONE
105 /** 127.0.0.1 */
106 #define INADDR_LOOPBACK     IPADDR_LOOPBACK
107 /** 0.0.0.0 */
108 #define INADDR_ANY          IPADDR_ANY
109 /** 255.255.255.255 */
110 #define INADDR_BROADCAST    IPADDR_BROADCAST
111 
112 #define IPADDR_BROADCAST_STRING "255.255.255.255"
113 
114 in_addr_t sal_ipaddr_addr(const char *cp);
115 int sal_ip4addr_aton(const char *cp, ip4_addr_t *addr);
116 char *sal_ip4addr_ntoa(const ip4_addr_t *addr);
117 char *sal_ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen);
118 
119 #define inet_addr(cp) sal_ipaddr_addr(cp)
120 #define inet_aton(cp,addr) sal_ip4addr_aton(cp,(ip4_addr_t*)addr)
121 #define inet_ntoa(addr) sal_ip4addr_ntoa((const ip4_addr_t*)&(addr))
122 #define inet_ntoa_r(addr, buf, buflen)  sal_ip4addr_ntoa_r((const ip4_addr_t*)&(addr), buf, buflen)
123 
124 #ifdef __cplusplus
125 }
126 #endif
127 
128 #endif /* SAL_IPADDR_H__ */
129