in.h
7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright (C) 2013-15 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @addtogroup posix_sockets
* @{
*/
/**
* @file
* @brief Main socket header
* @see <a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/netinet_in.h.html">
* The Open Group Base Specifications Issue 7, <netinet/in.h>
* </a>
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#ifndef _NETINET_IN_H
#define _NETINET_IN_H
#include <inttypes.h>
#include <sys/socket.h>
#include "net/protnum.h"
#include "net/ipv6/addr.h"
#include "sys/bytes.h"
#ifdef __cplusplus
extern "C" {
#endif
#define INET_ADDRSTRLEN (16) /**< Length of the string form for IPv4. */
#define INET6_ADDRSTRLEN (46) /**< Length of the string form for IPv6. */
/**
* IPv4 local host address.
*/
#define INADDR_ANY ((in_addr_t)0x00000000)
/**
* IPv4 broadcast address.
*/
#define INADDR_BROADCAST ((in_addr_t)0xffffffff)
/**
* IPv6 wildcard address.
*/
#define IN6ADDR_ANY_INIT IPV6_ADDR_UNSPECIFIED
/**
* IPv6 loopback address.
*/
#define IN6ADDR_LOOPBACK_INIT IPV6_ADDR_LOOPBACK
/**
* @name IPv6 address macros
* @{
*/
/**
* @brief Check if address is the unspecified address (`::`).
*
* @param[in] addr address of type `const struct in6_addr *`
*
* @return 0, when an address is not the unspecified address.
* @return any other value otherwise.
*/
#define IN6_IS_ADDR_UNSPECIFIED(addr) ((int)(ipv6_addr_is_unspecified((const ipv6_addr_t *)(addr))))
/**
* @brief Check if address is the loopback address (`::1`).
*
* @param[in] addr address of type `const struct in6_addr *`
*
* @return 0, when an address is not the loopback address.
* @return any other value otherwise.
*/
#define IN6_IS_ADDR_LOOPBACK(addr) ((int)(ipv6_addr_is_loopback((const ipv6_addr_t *)(addr))))
/**
* @brief Check if address is a multicast address.
*
* @param[in] addr address of type `const struct in6_addr *`
*
* @return 0, when an address is not a multicast address.
* @return any other value otherwise.
*/
#define IN6_IS_ADDR_MULTICAST(addr) ((int)(ipv6_addr_is_multicast((const ipv6_addr_t *)(addr))))
/**
* @brief Check if address is a link-local address.
*
* @param[in] addr address of type `const struct in6_addr *`
*
* @return 0, when an address is not a link-local address.
* @return any other value otherwise.
*/
#define IN6_IS_ADDR_LINKLOCAL(addr) ((int)(ipv6_addr_is_link_local((const ipv6_addr_t *)addr)))
/**
* @brief Check if address is a site-local address.
*
* @param[in] addr address of type `const struct in6_addr *`
*
* @return 0, when an address is not a site-local address.
* @return any other value otherwise.
*/
#define IN6_IS_ADDR_SITELOCAL(addr) ((int)(ipv6_addr_is_site_local((const ipv6_addr_t *)addr)))
/**
* @brief Check if address is an IPv4 mapped address.
*
* @param[in] addr address of type `const struct in6_addr *`
*
* @return 0, when an address is not an IPv4 mapped address.
* @return any other value otherwise.
*/
#define IN6_IS_ADDR_V4MAPPED(addr) ((int)(ipv6_addr_is_ipv4_mapped((const ipv6_addr_t *)addr)))
/**
* @brief Check if address is an IPv4-compatible address.
*
* @param[in] addr address of type `const struct in6_addr *`
*
* @return 0, when an address is not an IPv4-compatible address.
* @return any other value otherwise.
*/
#define IN6_IS_ADDR_V4COMPAT(addr) ((int)(ipv6_addr_is_ipv4_compat((const ipv6_addr_t *)addr)))
/**
* @brief Check if address is a multicast node-local address.
*
* @param[in] addr address of type `const struct in6_addr *`
*
* @return 0, when an address is not a multicast node-local address.
* @return any other value otherwise.
*/
#define IN6_IS_ADDR_MC_NODELOCAL(addr) (IN6_IS_ADDR_MULTICAST(addr) && \
(int)((addr->s6_addr[1] & 0x0f) == IPV6_ADDR_MCAST_SCP_IF_LOCAL))
/**
* @brief Check if address is a multicast link-local address.
*
* @param[in] addr address of type `const struct in6_addr *`
*
* @return 0, when an address is not a multicast link-local address.
* @return any other value otherwise.
*/
#define IN6_IS_ADDR_MC_LINKLOCAL(addr) (IN6_IS_ADDR_MULTICAST(addr) && \
(int)((addr->s6_addr[1] & 0x0f) == IPV6_ADDR_MCAST_SCP_LINK_LOCAL))
/**
* @brief Check if address is a multicast site-local address.
*
* @param[in] addr address of type `const struct in6_addr *`
*
* @return 0, when an address is not a multicast site-local address.
* @return any other value otherwise.
*/
#define IN6_IS_ADDR_MC_SITELOCAL(addr) (IN6_IS_ADDR_MULTICAST(addr) && \
(int)((addr->s6_addr[1] & 0x0f) == IPV6_ADDR_MCAST_SCP_SITE_LOCAL))
/**
* @brief Check if address is a multicast organization-local address.
*
* @param[in] addr address of type `const struct in6_addr *`
*
* @return 0, when an address is not a multicast organization-local address.
* @return any other value otherwise.
*/
#define IN6_IS_ADDR_MC_ORGLOCAL(addr) (IN6_IS_ADDR_MULTICAST(addr) && \
(int)((addr->s6_addr[1] & 0x0f) == IPV6_ADDR_MCAST_SCP_ORG_LOCAL))
/**
* @brief Check if address is a multicast global address.
*
* @param[in] addr address of type `const struct in6_addr *`
*
* @return 0, when an address is not a multicast global address.
* @return any other value otherwise.
*/
#define IN6_IS_ADDR_MC_GLOBAL(addr) (IN6_IS_ADDR_MULTICAST(addr) && \
(int)((addr->s6_addr[1] & 0x0f) == IPV6_ADDR_MCAST_SCP_GLOBAL))
/** @} */
/**
* @name Protocol numbers for option
* @{
*/
#define IPPROTO_IP (PROTNUM_IPV4) /**< Internet Protocol version 4 */
#define IPPROTO_IPV6 (PROTNUM_IPV6) /**< Internet Protocol version 6 */
#define IPPROTO_ICMP (PROTNUM_ICMP) /**< Internet Control Message Protocol */
#define IPPROTO_ICMPV6 (PROTNUM_ICMPV6) /**< ICMP for IPv6 */
#define IPPROTO_RAW (PROTNUM_RESERVED) /**< Raw IP packets protocol */
#define IPPROTO_TCP (PROTNUM_TCP) /**< Transmission control protocol */
#define IPPROTO_UDP (PROTNUM_UDP) /**< User datagram protocol */
/** @} */
/**
* @todo IPv6 option names
*/
typedef uint16_t in_port_t; /**< Internet port type */
typedef uint32_t in_addr_t; /**< IPv4 address type */
/**
* IPv4 address structure type.
*/
struct in_addr {
in_addr_t s_addr; /**< an IPv4 address */
};
/**
* @brief IPv6 address structure type.
*/
struct in6_addr {
uint8_t s6_addr[16]; /**< unsigned 8-bit integer array */
};
/**
* @brief IPv4 socket address type.
* @extends struct sockaddr
*/
struct sockaddr_in {
sa_family_t sin_family; /**< Protocol family, always AF_INET */
in_port_t sin_port; /**< Port number */
struct in_addr sin_addr; /**< IPv4 address */
};
/**
* IPv6 socket address type.
* @extends struct sockaddr
*/
struct sockaddr_in6 {
/**
* Protocol family, always AF_INET6. Member of struct sockaddr_in6
*/
int sin6_family; /**< Protocol family, always AF_INET6 */
in_port_t sin6_port; /**< Port number */
uint32_t sin6_flowinfo; /**< IPv6 traffic class and flow information */
struct in6_addr sin6_addr; /**< IPv6 address */
uint32_t sin6_scope_id; /**< Set of interfaces for a scope */
};
/**
* @brief IPv6 multicast request.
*/
struct ipv6_mreq {
struct in6_addr ipv6mr_multiaddr; /**< an IPv6 multicast address */
unsigned ipv6mr_interface; /**< interface index, leave 0 for default */
};
/**
* IPv6 socket address for the wildcard address.
*/
extern const struct in6_addr in6addr_any;
/**
* IPv6 socket address for the loopback address.
*/
extern const struct in6_addr in6addr_loopback;
#ifdef __cplusplus
}
#endif
/**
* @}
*/
#endif /* _NETINET_IN_H */