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