KallistiOS  2.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
in.h
Go to the documentation of this file.
1 /* KallistiOS 2.0.0
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 lcoal 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 /** \defgroup ipv4_opts IPv4 protocol level options
186 
187  These are the various socket-level optoins that can be accessed with the
188  setsockopt() and getsockopt() fnctions for the IPPROTO_IP level value.
189 
190  As there isn't really a full standard list of these defined in the SUS
191  (apparently), only ones that we support are listed here.
192 
193  \see so_opts
194  \see ipv6_opts
195 
196  @{
197 */
198 #define IP_TTL 24 /**< \brief TTL for unicast (get/set) */
199 /** @} */
200 
201 /** \defgroup ipv6_opts IPv6 protocol level options
202 
203  These are the various socket-level options that can be accessed with the
204  setsockopt() and getsockopt() functions for the IPPROTO_IPV6 level value.
205 
206  Not all of these are currently supported, but they are listed for
207  completeness.
208 
209  \see so_opts
210  \see ipv4_opts
211 
212  @{
213 */
214 #define IPV6_JOIN_GROUP 17 /**< \brief Join a multicast group (set) */
215 #define IPV6_LEAVE_GROUP 18 /**< \brief Leave a multicast group (set) */
216 #define IPV6_MULTICAST_HOPS 19 /**< \brief Hop limit for multicast (get/set) */
217 #define IPV6_MULTICAST_IF 20 /**< \brief Multicast interface (get/set) */
218 #define IPV6_MULTICAST_LOOP 21 /**< \brief Multicasts loopback (get/set) */
219 #define IPV6_UNICAST_HOPS 22 /**< \brief Hop limit for unicast (get/set) */
220 #define IPV6_V6ONLY 23 /**< \brief IPv6 only -- no IPv4 (get/set) */
221 /** @} */
222 
223 /** \brief Test if an IPv6 Address is unspecified.
224 
225  This macro tests whether an IPv6 address (struct in6_addr *) is an
226  unspecified address.
227 
228  \param a The address to test (struct in6_addr *)
229  \return Nonzero if the address is unspecified, 0 otherwise.
230 */
231 #define IN6_IS_ADDR_UNSPECIFIED(a) \
232  ((a)->__s6_addr.__s6_addr32[0] == 0 && \
233  (a)->__s6_addr.__s6_addr32[1] == 0 && \
234  (a)->__s6_addr.__s6_addr32[2] == 0 && \
235  (a)->__s6_addr.__s6_addr32[3] == 0)
236 
237 /** \brief Test if an IPv6 Address is a loopback address.
238 
239  This macro tests whether an IPv6 address (struct in6_addr *) is a
240  loopback address.
241 
242  \param a The address to test (struct in6_addr *)
243  \return Nonzero if the address is a loopback, 0 otherwise.
244 */
245 #define IN6_IS_ADDR_LOOPBACK(a) \
246  ((a)->__s6_addr.__s6_addr32[0] == 0 && \
247  (a)->__s6_addr.__s6_addr32[1] == 0 && \
248  (a)->__s6_addr.__s6_addr32[2] == 0 && \
249  (a)->__s6_addr.__s6_addr16[6] == 0 && \
250  (a)->__s6_addr.__s6_addr8[14] == 0 && \
251  (a)->__s6_addr.__s6_addr8[15] == 1)
252 
253 /** \brief Test if an IPv6 Address is an IPv4 mapped address.
254 
255  This macro tests whether an IPv6 address (struct in6_addr *) is an IPv4
256  mapped address.
257 
258  \param a The address to test (struct in6_addr *)
259  \return Nonzero if the address is IPv4 mapped, 0 otherwise.
260 */
261 #define IN6_IS_ADDR_V4MAPPED(a) \
262  ((a)->__s6_addr.__s6_addr32[0] == 0 && \
263  (a)->__s6_addr.__s6_addr32[1] == 0 && \
264  (a)->__s6_addr.__s6_addr16[4] == 0 && \
265  (a)->__s6_addr.__s6_addr16[5] == 0xFFFF)
266 
267 /** \brief Test if an IPv6 Address is an IPv4 compatibility address.
268 
269  This macro tests whether an IPv6 address (struct in6_addr *) is an IPv4
270  compatibility address.
271 
272  \param a The address to test (struct in6_addr *)
273  \return Nonzero if the address is IPv4 compat, 0 otherwise.
274 */
275 #define IN6_IS_ADDR_V4COMPAT(a) \
276  ((a)->__s6_addr.__s6_addr32[0] == 0 && \
277  (a)->__s6_addr.__s6_addr32[1] == 0 && \
278  (a)->__s6_addr.__s6_addr32[2] == 0 && \
279  (a)->__s6_addr.__s6_addr32[3] != 0 && \
280  (a)->__s6_addr.__s6_addr8[15] != 1)
281 
282 /** \brief Test if an IPv6 Address is a link-local address.
283 
284  This macro tests whether an IPv6 address (struct in6_addr *) is a link-local
285  address.
286 
287  \param a The address to test (struct in6_addr *)
288  \return Nonzero if the address is link-local, 0 otherwise.
289 */
290 #define IN6_IS_ADDR_LINKLOCAL(a) \
291  (((a)->__s6_addr.__s6_addr8[0] == 0xFE) && \
292  (((a)->__s6_addr.__s6_addr8[1] & 0xC0) == 0x80))
293 
294 /** \brief Test if an IPv6 Address is a site-local address.
295 
296  This macro tests whether an IPv6 address (struct in6_addr *) is a site-local
297  address.
298 
299  \param a The address to test (struct in6_addr *)
300  \return Nonzero if the address is site-local, 0 otherwise.
301 */
302 #define IN6_IS_ADDR_SITELOCAL(a) \
303  (((a)->__s6_addr.__s6_addr8[0] == 0xFE) && \
304  (((a)->__s6_addr.__s6_addr8[1] & 0xC0) == 0xC0))
305 /** \brief Test if an IPv6 Address is a multicast address.
306 
307  This macro tests whether an IPv6 address (struct in6_addr *) is a multicast
308  address.
309 
310  \param a The address to test (struct in6_addr *)
311  \return Nonzero if the address is multicast, 0 otherwise.
312 */
313 #define IN6_IS_ADDR_MULTICAST(a) \
314  ((a)->__s6_addr.__s6_addr8[0] == 0xFF)
315 
316 /** \brief Test if an IPv6 Address is a node-local multicast address.
317 
318  This macro tests whether an IPv6 address (struct in6_addr *) is a node-local
319  multicast address.
320 
321  \param a The address to test (struct in6_addr *)
322  \return Nonzero if the address is a node-local multicast
323  address, 0 otherwise.
324 */
325 #define IN6_IS_ADDR_MC_NODELOCAL(a) \
326  (IN6_IS_ADDR_MULTICAST(a) && \
327  (((a)->__s6_addr.__s6_addr8[1] & 0x0F) == 0x01))
328 
329 /** \brief Test if an IPv6 Address is a link-local multicast address.
330 
331  This macro tests whether an IPv6 address (struct in6_addr *) is a link-local
332  multicast address.
333 
334  \param a The address to test (struct in6_addr *)
335  \return Nonzero if the address is a link-local multicast
336  address, 0 otherwise.
337 */
338 #define IN6_IS_ADDR_MC_LINKLOCAL(a) \
339  (IN6_IS_ADDR_MULTICAST(a) && \
340  (((a)->__s6_addr.__s6_addr8[1] & 0x0F) == 0x02))
341 
342 /** \brief Test if an IPv6 Address is a site-local multicast address.
343 
344  This macro tests whether an IPv6 address (struct in6_addr *) is a site-local
345  multicast address.
346 
347  \param a The address to test (struct in6_addr *)
348  \return Nonzero if the address is a site-local multicast
349  address, 0 otherwise.
350 */
351 #define IN6_IS_ADDR_MC_SITELOCAL(a) \
352  (IN6_IS_ADDR_MULTICAST(a) && \
353  (((a)->__s6_addr.__s6_addr8[1] & 0x0F) == 0x05))
354 
355 /** \brief Test if an IPv6 Address is an organization-local multicast address.
356 
357  This macro tests whether an IPv6 address (struct in6_addr *) is an
358  organization-local multicast address.
359 
360  \param a The address to test (struct in6_addr *)
361  \return Nonzero if the address is an organization-local
362  multicast address, 0 otherwise.
363 */
364 #define IN6_IS_ADDR_MC_ORGLOCAL(a) \
365  (IN6_IS_ADDR_MULTICAST(a) && \
366  (((a)->__s6_addr.__s6_addr8[1] & 0x0F) == 0x08))
367 
368 /** \brief Test if an IPv6 Address is a global multicast address.
369 
370  This macro tests whether an IPv6 address (struct in6_addr *) is a global
371  multicast address.
372 
373  \param a The address to test (struct in6_addr *)
374  \return Nonzero if the address is a global multicast address,
375  0 otherwise.
376 */
377 #define IN6_IS_ADDR_MC_GLOBAL(a) \
378  (IN6_IS_ADDR_MULTICAST(a) && \
379  (((a)->__s6_addr.__s6_addr8[1] & 0x0F) == 0x0E))
380 
381 __END_DECLS
382 
383 #endif /* __NETINET_IN_H */