1 /*
2  * @file ipaddr.h
3  *
4  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
5  */
6 
7 #ifndef _IPADDR_H_
8 #define _IPADDR_H_
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 /** @addtogroup aos_sal API
15  *
16  * IP address type and convertion utility definitions.
17  *
18  *  @{
19  */
20 
21 /**
22  * If your port already typedef's in_addr_t, define IN_ADDR_T_DEFINED
23  * to prevent this code from redefining it.
24  */
25 #if !defined(in_addr_t) && !defined(IN_ADDR_T_DEFINED)
26 typedef u32_t in_addr_t;
27 #endif
28 
29 /**
30  * IPv4 address definition.
31  */
32 struct in_addr {
33     in_addr_t s_addr;
34 };
35 
36 /**
37  * IPv6 address definition.
38  */
39 struct in6_addr {
40     union {
41         u32_t u32_addr[4];
42         u8_t  u8_addr[16];
43     } un;
44 #define s6_addr  un.u8_addr
45 };
46 
47 /**
48  * The type of the IP address (v4 or v6).
49  */
50 enum ip_addr_type {
51     IPADDR_TYPE_V4 =   0U, //!< IPv4
52     IPADDR_TYPE_V6 =   6U, //!< IPv6
53     IPADDR_TYPE_ANY = 46U  //!< IPv4+IPv6 ("dual-stack")
54 };
55 
56 /**
57  * IPv4 address defition.
58  */
59 typedef struct ip4_addr {
60     u32_t addr;
61 } ip4_addr_t;
62 
63 /**
64  * IPv6 address defition.
65  */
66 typedef struct ip6_addr {
67     u32_t addr[4];
68 } ip6_addr_t;
69 
70 /**
71  * The generic definition of IP address, can fit to both IPv4 and IPv6 address.
72  */
73 typedef struct _ip_addr {
74     union {
75         ip6_addr_t ip6;
76         ip4_addr_t ip4;
77     } u_addr;  //!< The data of the address
78     u8_t type; //!< @ref ip_addr_type
79 } ip_addr_t;
80 
81 /** 255.255.255.255 */
82 #define IPADDR_NONE         ((u32_t)0xffffffffUL)
83 /** 127.0.0.1 */
84 #define IPADDR_LOOPBACK     ((u32_t)0x7f000001UL)
85 /** 0.0.0.0 */
86 #define IPADDR_ANY          ((u32_t)0x00000000UL)
87 /** 255.255.255.255 */
88 #define IPADDR_BROADCAST    ((u32_t)0xffffffffUL)
89 
90 /** 255.255.255.255 */
91 #define IPADDR_NONE         ((u32_t)0xffffffffUL)
92 /** 127.0.0.1 */
93 #define IPADDR_LOOPBACK     ((u32_t)0x7f000001UL)
94 /** 0.0.0.0 */
95 #define IPADDR_ANY          ((u32_t)0x00000000UL)
96 /** 255.255.255.255 */
97 #define IPADDR_BROADCAST    ((u32_t)0xffffffffUL)
98 
99 /** 255.255.255.255 */
100 #define INADDR_NONE         IPADDR_NONE
101 /** 127.0.0.1 */
102 #define INADDR_LOOPBACK     IPADDR_LOOPBACK
103 /** 0.0.0.0 */
104 #define INADDR_ANY          IPADDR_ANY
105 /** 255.255.255.255 */
106 #define INADDR_BROADCAST    IPADDR_BROADCAST
107 
108 #define IPADDR_BROADCAST_STRING "255.255.255.255"
109 
110 #define IP_CLASSD(a)        (((u32_t)(a) & 0xf0000000UL) == 0xe0000000UL)
111 #define IP_MULTICAST(a)     IP_CLASSD(a)
112 #define IN_MULTICAST(a)     IP_MULTICAST(a)
113 
114 /**
115  * IP address convertion utilities.
116  */
117 in_addr_t ipaddr_addr(const char *cp);
118 int ip4addr_aton(const char *cp, ip4_addr_t *addr);
119 char *ip4addr_ntoa(const ip4_addr_t *addr);
120 
121 /**
122  * The standard socket API mapping of IP address convertion utilities.
123  */
124 #define inet_addr(cp) ipaddr_addr(cp)
125 #define inet_aton(cp,addr) ip4addr_aton(cp,(ip4_addr_t*)addr)
126 #define inet_ntoa(addr) ip4addr_ntoa((const ip4_addr_t*)&(addr))
127 
128 /** @} */ /* end */
129 
130 #ifdef __cplusplus
131 }
132 #endif
133 
134 #endif
135