KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
in.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 netinet/in.h
4 Copyright (C) 2006, 2007, 2010 Lawrence Sebald
5
6*/
7
8/** \file netinet/in.h
9 \brief Definitions for the Internet address family.
10 \ingroup networking_ip
11
12 This file contains the standard definitions (as directed by the POSIX 2008
13 standard) for internet-related functionality in the AF_INET address family.
14 This does is not guaranteed to have everything that one might have in a
15 fully-standard compliant implementation of the POSIX standard.
16
17 \author Lawrence Sebald
18*/
19
20#ifndef __NETINET_IN_H
21#define __NETINET_IN_H
22
23#include <kos/cdefs.h>
24
25__BEGIN_DECLS
26
27/* Bring in <inttypes.h> to grab the uint16_t and uint32_t types (for in_port_t
28 and in_addr_t below), along with uint8_t. Bring in <sys/socket.h> for the
29 sa_family_t type, as required. IEEE Std 1003.1-2008 specifically states that
30 <netinet/in.h> can make visible all symbols from both <inttypes.h> and
31 <sys/socket.h>. */
32#include <inttypes.h>
33#include <sys/socket.h>
34
35/** \brief 16-bit type used to store a value for an internet port.
36 \ingroup networking_ip
37*/
38typedef uint16_t in_port_t;
39
40/** \brief 32-bit value used to store an IPv4 address.
41 \ingroup networking_ipv4
42*/
43typedef uint32_t in_addr_t;
44
45/** \brief Structure used to store an IPv4 address.
46 \headerfile netinet/in.h
47 \ingroup networking_ipv4
48*/
52
53/** \brief Structure used to store an IPv6 address.
54 \headerfile netinet/in.h
55 \ingroup networking_ipv6
56*/
57struct in6_addr {
58 union {
59 uint8_t __s6_addr8[16];
60 uint16_t __s6_addr16[8];
61 uint32_t __s6_addr32[4];
62 uint64_t __s6_addr64[2];
64#define s6_addr __s6_addr.__s6_addr8
65};
66
67/* Bring in <arpa/inet.h> to make ntohl/ntohs/htonl/htons visible, as per IEEE
68 Std 1003.1-2008 (the standard specifically states that <netinet/in.h> may
69 make all symbols from <arpa/inet.h> visible. The <arpa/inet.h> header
70 actually needs the stuff above, so that's why we include it here. */
71#include <arpa/inet.h>
72
73/** \brief Structure used to store an IPv4 address for a socket.
74 \ingroup networking_ipv4
75
76 This structure is the standard way to set up addresses for sockets in the
77 AF_INET address family. Generally you will not send one of these directly
78 to a function, but rather will cast it to a struct sockaddr. Also, this
79 structure contains the old sin_zero member which is no longer required by
80 the standard (for compatibility with applications that expect it).
81
82 \headerfile netinet/in.h
83*/
85 /** \brief Family for the socket. Must be AF_INET. */
87
88 /** \brief Port for the socket. Must be in network byte order. */
90
91 /** \brief Address for the socket. Must be in network byte order. */
93
94 /** \brief Empty space, ignored for all intents and purposes. */
95 unsigned char sin_zero[8];
96};
97
98/** \brief Structure used to store an IPv6 address for a socket.
99 \ingroup networking_ipv6
100
101 This structure is the standard way to set up addresses for sockets in the
102 AF_INET6 address family. Generally you will not send one of these directly
103 to a function, but rather will cast it to a struct sockaddr.
104
105 \headerfile netinet/in.h
106 */
108 /** \brief Family for the socket. Must be AF_INET6. */
110
111 /** \brief Port for the socket. Must be in network byte order. */
113
114 /** \brief Traffic class and flow information. */
116
117 /** \brief Address for the socket. Must be in network byte order. */
119
120 /** \brief Set of interfaces for a scope. */
122};
123
124/** \brief Local IPv4 host address.
125 \ingroup networking_ipv4
126
127 This address can be used by many things if you prefer to not specify the
128 local address, and would rather it be detected automatically.
129*/
130#define INADDR_ANY 0x00000000
131
132/** \brief IPv4 broadcast address.
133 \ingroup networking_ipv4
134
135 This address is the normal IPv4 broadcast address (255.255.255.255).
136*/
137#define INADDR_BROADCAST 0xFFFFFFFF
138
139/** \brief IPv4 error address.
140 \ingroup networking_ipv4
141
142 This address is non-standard, but is available on many systems. It is used
143 to detect failure from some functions that normally return addresses (such
144 as the inet_addr function).
145*/
146#define INADDR_NONE 0xFFFFFFFF
147
148/** \brief Initialize an IPv6 local host address.
149 \ingroup networking_ipv6
150
151 This macro can be used to initialize a struct in6_addr to any local address.
152 It functions similarly to INADDR_ANY for IPv4.
153*/
154#define IN6ADDR_ANY_INIT {{{ 0, 0, 0, 0, 0, 0, 0, 0, \
155 0, 0, 0, 0, 0, 0, 0, 0 }}}
156
157/** \brief Initialize an IPv6 loopback address.
158 \ingroup networking_ipv6
159
160 This macro can be used to initialize a struct in6_addr to the loopback
161 address.
162*/
163#define IN6ADDR_LOOPBACK_INIT {{{ 0, 0, 0, 0, 0, 0, 0, 0, \
164 0, 0, 0, 0, 0, 0, 0, 1 }}}
165
166/** \brief IPv6 local host address.
167 \ingroup networking_ipv6
168
169 This constant variable contains the IPv6 local host address.
170*/
171extern const struct in6_addr in6addr_any;
172
173/** \brief IPv6 loopback address.
174 \ingroup networking_ipv6
175
176 This constant variable contains the IPv6 loopback address.
177*/
178extern const struct in6_addr in6addr_loopback;
179
180/** \brief Length of a string form of a maximal IPv4 address.
181 \ingroup networking_ipv4
182*/
183#define INET_ADDRSTRLEN 16
184
185/** \brief Length of a string form of a maximal IPv6 address.
186 \ingroup networking_ipv6
187*/
188#define INET6_ADDRSTRLEN 46
189
190/** \brief Internet Protocol Version 4.
191 \ingroup networking_ip
192*/
193#define IPPROTO_IP 0
194
195/** \brief Internet Control Message Protocol.
196 \ingroup networking_ip
197*/
198#define IPPROTO_ICMP 1
199
200/** \brief Transmission Control Protocol.
201 \ingroup networking_ip
202*/
203#define IPPROTO_TCP 6
204
205/** \brief User Datagram Protocol.
206 \ingroup networking_ip
207*/
208#define IPPROTO_UDP 17
209
210/** \brief Internet Protocol Version 6.
211 \ingroup networking_ip
212*/
213#define IPPROTO_IPV6 41
214
215/** \brief Lightweight User Datagram Protocol.
216 \ingroup networking_ip
217*/
218#define IPPROTO_UDPLITE 136
219
220/** \defgroup ipv4_opts Options
221 \brief Options for v4 of the Internet Protocol
222 \ingroup networking_ipv4
223
224 These are the various socket-level options that can be accessed with the
225 setsockopt() and getsockopt() functions for the IPPROTO_IP level value.
226
227 As there isn't really a full standard list of these defined in the SUS
228 (apparently), only ones that we support are listed here.
229
230 \see so_opts
231 \see ipv6_opts
232 \see udp_opts
233 \see udplite_opts
234 \see tcp_opts
235
236 @{
237*/
238
239#define IP_TTL 24 /**< \brief TTL for unicast (get/set) */
240#define IP_TOS 26 /**< \brief Type of service (get/set) */
241
242/** @} */
243
244/** \defgroup ipv6_opts Options
245 \brief Options for v6 of the Internet Protocol
246 \ingroup networking_ipv6
247
248 These are the various socket-level options that can be accessed with the
249 setsockopt() and getsockopt() functions for the IPPROTO_IPV6 level value.
250
251 Not all of these are currently supported, but they are listed for
252 completeness.
253
254 \see so_opts
255 \see ipv4_opts
256 \see udp_opts
257 \see udplite_opts
258 \see tcp_opts
259
260 @{
261*/
262
263#define IPV6_JOIN_GROUP 17 /**< \brief Join a multicast group (set) */
264#define IPV6_LEAVE_GROUP 18 /**< \brief Leave a multicast group (set) */
265#define IPV6_MULTICAST_HOPS 19 /**< \brief Hop limit for multicast (get/set) */
266#define IPV6_MULTICAST_IF 20 /**< \brief Multicast interface (get/set) */
267#define IPV6_MULTICAST_LOOP 21 /**< \brief Multicasts loopback (get/set) */
268#define IPV6_UNICAST_HOPS 22 /**< \brief Hop limit for unicast (get/set) */
269#define IPV6_V6ONLY 23 /**< \brief IPv6 only -- no IPv4 (get/set) */
270
271/** @} */
272
273/** \brief Test if an IPv6 Address is unspecified.
274 \ingroup networking_ipv6
275
276 This macro tests whether an IPv6 address (struct in6_addr *) is an
277 unspecified address.
278
279 \param a The address to test (struct in6_addr *)
280
281 \return Nonzero if the address is unspecified, 0 otherwise.
282*/
283#define IN6_IS_ADDR_UNSPECIFIED(a) \
284 ((a)->__s6_addr.__s6_addr32[0] == 0 && \
285 (a)->__s6_addr.__s6_addr32[1] == 0 && \
286 (a)->__s6_addr.__s6_addr32[2] == 0 && \
287 (a)->__s6_addr.__s6_addr32[3] == 0)
288
289/** \brief Test if an IPv6 Address is a loopback address.
290 \ingroup networking_ipv6
291
292 This macro tests whether an IPv6 address (struct in6_addr *) is a
293 loopback address.
294
295 \param a The address to test (struct in6_addr *)
296
297 \return Nonzero if the address is a loopback, 0 otherwise.
298*/
299#define IN6_IS_ADDR_LOOPBACK(a) \
300 ((a)->__s6_addr.__s6_addr32[0] == 0 && \
301 (a)->__s6_addr.__s6_addr32[1] == 0 && \
302 (a)->__s6_addr.__s6_addr32[2] == 0 && \
303 (a)->__s6_addr.__s6_addr16[6] == 0 && \
304 (a)->__s6_addr.__s6_addr8[14] == 0 && \
305 (a)->__s6_addr.__s6_addr8[15] == 1)
306
307/** \brief Test if an IPv6 Address is an IPv4 mapped address.
308 \ingroup networking_ipv6
309
310 This macro tests whether an IPv6 address (struct in6_addr *) is an IPv4
311 mapped address.
312
313 \param a The address to test (struct in6_addr *)
314
315 \return Nonzero if the address is IPv4 mapped, 0 otherwise.
316*/
317#define IN6_IS_ADDR_V4MAPPED(a) \
318 ((a)->__s6_addr.__s6_addr32[0] == 0 && \
319 (a)->__s6_addr.__s6_addr32[1] == 0 && \
320 (a)->__s6_addr.__s6_addr16[4] == 0 && \
321 (a)->__s6_addr.__s6_addr16[5] == 0xFFFF)
322
323/** \brief Test if an IPv6 Address is an IPv4 compatibility address.
324 \ingroup networking_ipv6
325
326 This macro tests whether an IPv6 address (struct in6_addr *) is an IPv4
327 compatibility address.
328
329 \param a The address to test (struct in6_addr *)
330
331 \return Nonzero if the address is IPv4 compat, 0 otherwise.
332*/
333#define IN6_IS_ADDR_V4COMPAT(a) \
334 ((a)->__s6_addr.__s6_addr32[0] == 0 && \
335 (a)->__s6_addr.__s6_addr32[1] == 0 && \
336 (a)->__s6_addr.__s6_addr32[2] == 0 && \
337 (a)->__s6_addr.__s6_addr32[3] != 0 && \
338 (a)->__s6_addr.__s6_addr8[15] != 1)
339
340/** \brief Test if an IPv6 Address is a link-local address.
341 \ingroup networking_ipv6
342
343 This macro tests whether an IPv6 address (struct in6_addr *) is a link-local
344 address.
345
346 \param a The address to test (struct in6_addr *)
347
348 \return Nonzero if the address is link-local, 0 otherwise.
349*/
350#define IN6_IS_ADDR_LINKLOCAL(a) \
351 (((a)->__s6_addr.__s6_addr8[0] == 0xFE) && \
352 (((a)->__s6_addr.__s6_addr8[1] & 0xC0) == 0x80))
353
354/** \brief Test if an IPv6 Address is a site-local address.
355 \ingroup networking_ipv6
356
357 This macro tests whether an IPv6 address (struct in6_addr *) is a site-local
358 address.
359
360 \param a The address to test (struct in6_addr *)
361
362 \return Nonzero if the address is site-local, 0 otherwise.
363*/
364#define IN6_IS_ADDR_SITELOCAL(a) \
365 (((a)->__s6_addr.__s6_addr8[0] == 0xFE) && \
366 (((a)->__s6_addr.__s6_addr8[1] & 0xC0) == 0xC0))
367
368/** \brief Test if an IPv6 Address is a multicast address.
369 \ingroup networking_ipv6
370
371 This macro tests whether an IPv6 address (struct in6_addr *) is a multicast
372 address.
373
374 \param a The address to test (struct in6_addr *)
375
376 \return Nonzero if the address is multicast, 0 otherwise.
377*/
378#define IN6_IS_ADDR_MULTICAST(a) \
379 ((a)->__s6_addr.__s6_addr8[0] == 0xFF)
380
381/** \brief Test if an IPv6 Address is a node-local multicast address.
382 \ingroup networking_ipv6
383
384 This macro tests whether an IPv6 address (struct in6_addr *) is a node-local
385 multicast address.
386
387 \param a The address to test (struct in6_addr *)
388
389 \return Nonzero if the address is a node-local multicast
390 address, 0 otherwise.
391*/
392#define IN6_IS_ADDR_MC_NODELOCAL(a) \
393 (IN6_IS_ADDR_MULTICAST(a) && \
394 (((a)->__s6_addr.__s6_addr8[1] & 0x0F) == 0x01))
395
396/** \brief Test if an IPv6 Address is a link-local multicast address.
397 \ingroup networking_ipv6
398
399 This macro tests whether an IPv6 address (struct in6_addr *) is a link-local
400 multicast address.
401
402 \param a The address to test (struct in6_addr *)
403
404 \return Nonzero if the address is a link-local multicast
405 address, 0 otherwise.
406*/
407#define IN6_IS_ADDR_MC_LINKLOCAL(a) \
408 (IN6_IS_ADDR_MULTICAST(a) && \
409 (((a)->__s6_addr.__s6_addr8[1] & 0x0F) == 0x02))
410
411/** \brief Test if an IPv6 Address is a site-local multicast address.
412 \ingroup networking_ipv6
413
414 This macro tests whether an IPv6 address (struct in6_addr *) is a site-local
415 multicast address.
416
417 \param a The address to test (struct in6_addr *)
418
419 \return Nonzero if the address is a site-local multicast
420 address, 0 otherwise.
421*/
422#define IN6_IS_ADDR_MC_SITELOCAL(a) \
423 (IN6_IS_ADDR_MULTICAST(a) && \
424 (((a)->__s6_addr.__s6_addr8[1] & 0x0F) == 0x05))
425
426/** \brief Test if an IPv6 Address is an organization-local multicast address.
427 \ingroup networking_ipv6
428
429 This macro tests whether an IPv6 address (struct in6_addr *) is an
430 organization-local multicast address.
431
432 \param a The address to test (struct in6_addr *)
433
434 \return Nonzero if the address is an organization-local
435 multicast address, 0 otherwise.
436*/
437#define IN6_IS_ADDR_MC_ORGLOCAL(a) \
438 (IN6_IS_ADDR_MULTICAST(a) && \
439 (((a)->__s6_addr.__s6_addr8[1] & 0x0F) == 0x08))
440
441/** \brief Test if an IPv6 Address is a global multicast address.
442 \ingroup networking_ipv6
443
444 This macro tests whether an IPv6 address (struct in6_addr *) is a global
445 multicast address.
446
447 \param a The address to test (struct in6_addr *)
448
449 \return Nonzero if the address is a global multicast address,
450 0 otherwise.
451*/
452#define IN6_IS_ADDR_MC_GLOBAL(a) \
453 (IN6_IS_ADDR_MULTICAST(a) && \
454 (((a)->__s6_addr.__s6_addr8[1] & 0x0F) == 0x0E))
455
456__END_DECLS
457
458#endif /* __NETINET_IN_H */
Various common macros used throughout the codebase.
uint16_t in_port_t
16-bit type used to store a value for an internet port.
Definition in.h:38
uint32_t in_addr_t
32-bit value used to store an IPv4 address.
Definition in.h:43
const struct in6_addr in6addr_loopback
IPv6 loopback address.
const struct in6_addr in6addr_any
IPv6 local host address.
__uint8_t sa_family_t
Socket address family type.
Definition socket.h:42
Definitions for internet operations.
Main sockets header.
Structure used to store an IPv6 address.
Definition in.h:57
union in6_addr::@1 __s6_addr
uint8_t __s6_addr8[16]
Definition in.h:59
uint16_t __s6_addr16[8]
Definition in.h:60
uint64_t __s6_addr64[2]
Definition in.h:62
uint32_t __s6_addr32[4]
Definition in.h:61
Structure used to store an IPv4 address.
Definition in.h:49
in_addr_t s_addr
Definition in.h:50
Structure used to store an IPv6 address for a socket.
Definition in.h:107
in_port_t sin6_port
Port for the socket.
Definition in.h:112
struct in6_addr sin6_addr
Address for the socket.
Definition in.h:118
uint32_t sin6_flowinfo
Traffic class and flow information.
Definition in.h:115
uint32_t sin6_scope_id
Set of interfaces for a scope.
Definition in.h:121
sa_family_t sin6_family
Family for the socket.
Definition in.h:109
Structure used to store an IPv4 address for a socket.
Definition in.h:84
unsigned char sin_zero[8]
Empty space, ignored for all intents and purposes.
Definition in.h:95
struct in_addr sin_addr
Address for the socket.
Definition in.h:92
sa_family_t sin_family
Family for the socket.
Definition in.h:86
in_port_t sin_port
Port for the socket.
Definition in.h:89