Update.
[glibc.git] / inet / netinet / in.h
blob5034db12063f22d066822fcbf81264d99561a055
1 /* Copyright (C) 1991-2001, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #ifndef _NETINET_IN_H
20 #define _NETINET_IN_H 1
22 #include <features.h>
23 #include <stdint.h>
24 #include <sys/socket.h>
25 #include <bits/types.h>
28 __BEGIN_DECLS
30 /* Standard well-defined IP protocols. */
31 enum
33 IPPROTO_IP = 0, /* Dummy protocol for TCP. */
34 #define IPPROTO_IP IPPROTO_IP
35 IPPROTO_HOPOPTS = 0, /* IPv6 Hop-by-Hop options. */
36 #define IPPROTO_HOPOPTS IPPROTO_HOPOPTS
37 IPPROTO_ICMP = 1, /* Internet Control Message Protocol. */
38 #define IPPROTO_ICMP IPPROTO_ICMP
39 IPPROTO_IGMP = 2, /* Internet Group Management Protocol. */
40 #define IPPROTO_IGMP IPPROTO_IGMP
41 IPPROTO_IPIP = 4, /* IPIP tunnels (older KA9Q tunnels use 94). */
42 #define IPPROTO_IPIP IPPROTO_IPIP
43 IPPROTO_TCP = 6, /* Transmission Control Protocol. */
44 #define IPPROTO_TCP IPPROTO_TCP
45 IPPROTO_EGP = 8, /* Exterior Gateway Protocol. */
46 #define IPPROTO_EGP IPPROTO_EGP
47 IPPROTO_PUP = 12, /* PUP protocol. */
48 #define IPPROTO_PUP IPPROTO_PUP
49 IPPROTO_UDP = 17, /* User Datagram Protocol. */
50 #define IPPROTO_UDP IPPROTO_UDP
51 IPPROTO_IDP = 22, /* XNS IDP protocol. */
52 #define IPPROTO_IDP IPPROTO_IDP
53 IPPROTO_TP = 29, /* SO Transport Protocol Class 4. */
54 #define IPPROTO_TP IPPROTO_TP
55 IPPROTO_IPV6 = 41, /* IPv6 header. */
56 #define IPPROTO_IPV6 IPPROTO_IPV6
57 IPPROTO_ROUTING = 43, /* IPv6 routing header. */
58 #define IPPROTO_ROUTING IPPROTO_ROUTING
59 IPPROTO_FRAGMENT = 44, /* IPv6 fragmentation header. */
60 #define IPPROTO_FRAGMENT IPPROTO_FRAGMENT
61 IPPROTO_RSVP = 46, /* Reservation Protocol. */
62 #define IPPROTO_RSVP IPPROTO_RSVP
63 IPPROTO_GRE = 47, /* General Routing Encapsulation. */
64 #define IPPROTO_GRE IPPROTO_GRE
65 IPPROTO_ESP = 50, /* encapsulating security payload. */
66 #define IPPROTO_ESP IPPROTO_ESP
67 IPPROTO_AH = 51, /* authentication header. */
68 #define IPPROTO_AH IPPROTO_AH
69 IPPROTO_ICMPV6 = 58, /* ICMPv6. */
70 #define IPPROTO_ICMPV6 IPPROTO_ICMPV6
71 IPPROTO_NONE = 59, /* IPv6 no next header. */
72 #define IPPROTO_NONE IPPROTO_NONE
73 IPPROTO_DSTOPTS = 60, /* IPv6 destination options. */
74 #define IPPROTO_DSTOPTS IPPROTO_DSTOPTS
75 IPPROTO_MTP = 92, /* Multicast Transport Protocol. */
76 #define IPPROTO_MTP IPPROTO_MTP
77 IPPROTO_ENCAP = 98, /* Encapsulation Header. */
78 #define IPPROTO_ENCAP IPPROTO_ENCAP
79 IPPROTO_PIM = 103, /* Protocol Independent Multicast. */
80 #define IPPROTO_PIM IPPROTO_PIM
81 IPPROTO_COMP = 108, /* Compression Header Protocol. */
82 #define IPPROTO_COMP IPPROTO_COMP
83 IPPROTO_SCTP = 132, /* Stream Control Transmission Protocol. */
84 #define IPPROTO_SCTP IPPROTO_SCTP
85 IPPROTO_RAW = 255, /* Raw IP packets. */
86 #define IPPROTO_RAW IPPROTO_RAW
87 IPPROTO_MAX
91 /* Type to represent a port. */
92 typedef uint16_t in_port_t;
94 /* Standard well-known ports. */
95 enum
97 IPPORT_ECHO = 7, /* Echo service. */
98 IPPORT_DISCARD = 9, /* Discard transmissions service. */
99 IPPORT_SYSTAT = 11, /* System status service. */
100 IPPORT_DAYTIME = 13, /* Time of day service. */
101 IPPORT_NETSTAT = 15, /* Network status service. */
102 IPPORT_FTP = 21, /* File Transfer Protocol. */
103 IPPORT_TELNET = 23, /* Telnet protocol. */
104 IPPORT_SMTP = 25, /* Simple Mail Transfer Protocol. */
105 IPPORT_TIMESERVER = 37, /* Timeserver service. */
106 IPPORT_NAMESERVER = 42, /* Domain Name Service. */
107 IPPORT_WHOIS = 43, /* Internet Whois service. */
108 IPPORT_MTP = 57,
110 IPPORT_TFTP = 69, /* Trivial File Transfer Protocol. */
111 IPPORT_RJE = 77,
112 IPPORT_FINGER = 79, /* Finger service. */
113 IPPORT_TTYLINK = 87,
114 IPPORT_SUPDUP = 95, /* SUPDUP protocol. */
117 IPPORT_EXECSERVER = 512, /* execd service. */
118 IPPORT_LOGINSERVER = 513, /* rlogind service. */
119 IPPORT_CMDSERVER = 514,
120 IPPORT_EFSSERVER = 520,
122 /* UDP ports. */
123 IPPORT_BIFFUDP = 512,
124 IPPORT_WHOSERVER = 513,
125 IPPORT_ROUTESERVER = 520,
127 /* Ports less than this value are reserved for privileged processes. */
128 IPPORT_RESERVED = 1024,
130 /* Ports greater this value are reserved for (non-privileged) servers. */
131 IPPORT_USERRESERVED = 5000
135 /* Internet address. */
136 typedef uint32_t in_addr_t;
137 struct in_addr
139 in_addr_t s_addr;
143 /* Definitions of the bits in an Internet address integer.
145 On subnets, host and network parts are found according to
146 the subnet mask, not these masks. */
148 #define IN_CLASSA(a) ((((in_addr_t)(a)) & 0x80000000) == 0)
149 #define IN_CLASSA_NET 0xff000000
150 #define IN_CLASSA_NSHIFT 24
151 #define IN_CLASSA_HOST (0xffffffff & ~IN_CLASSA_NET)
152 #define IN_CLASSA_MAX 128
154 #define IN_CLASSB(a) ((((in_addr_t)(a)) & 0xc0000000) == 0x80000000)
155 #define IN_CLASSB_NET 0xffff0000
156 #define IN_CLASSB_NSHIFT 16
157 #define IN_CLASSB_HOST (0xffffffff & ~IN_CLASSB_NET)
158 #define IN_CLASSB_MAX 65536
160 #define IN_CLASSC(a) ((((in_addr_t)(a)) & 0xe0000000) == 0xc0000000)
161 #define IN_CLASSC_NET 0xffffff00
162 #define IN_CLASSC_NSHIFT 8
163 #define IN_CLASSC_HOST (0xffffffff & ~IN_CLASSC_NET)
165 #define IN_CLASSD(a) ((((in_addr_t)(a)) & 0xf0000000) == 0xe0000000)
166 #define IN_MULTICAST(a) IN_CLASSD(a)
168 #define IN_EXPERIMENTAL(a) ((((in_addr_t)(a)) & 0xe0000000) == 0xe0000000)
169 #define IN_BADCLASS(a) ((((in_addr_t)(a)) & 0xf0000000) == 0xf0000000)
171 /* Address to accept any incoming messages. */
172 #define INADDR_ANY ((in_addr_t) 0x00000000)
173 /* Address to send to all hosts. */
174 #define INADDR_BROADCAST ((in_addr_t) 0xffffffff)
175 /* Address indicating an error return. */
176 #define INADDR_NONE ((in_addr_t) 0xffffffff)
178 /* Network number for local host loopback. */
179 #define IN_LOOPBACKNET 127
180 /* Address to loopback in software to local host. */
181 #ifndef INADDR_LOOPBACK
182 # define INADDR_LOOPBACK ((in_addr_t) 0x7f000001) /* Inet 127.0.0.1. */
183 #endif
185 /* Defines for Multicast INADDR. */
186 #define INADDR_UNSPEC_GROUP ((in_addr_t) 0xe0000000) /* 224.0.0.0 */
187 #define INADDR_ALLHOSTS_GROUP ((in_addr_t) 0xe0000001) /* 224.0.0.1 */
188 #define INADDR_ALLRTRS_GROUP ((in_addr_t) 0xe0000002) /* 224.0.0.2 */
189 #define INADDR_MAX_LOCAL_GROUP ((in_addr_t) 0xe00000ff) /* 224.0.0.255 */
192 /* IPv6 address */
193 struct in6_addr
195 union
197 uint8_t u6_addr8[16];
198 uint16_t u6_addr16[8];
199 uint32_t u6_addr32[4];
200 } in6_u;
201 #define s6_addr in6_u.u6_addr8
202 #define s6_addr16 in6_u.u6_addr16
203 #define s6_addr32 in6_u.u6_addr32
206 extern const struct in6_addr in6addr_any; /* :: */
207 extern const struct in6_addr in6addr_loopback; /* ::1 */
208 #define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
209 #define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
211 #define INET_ADDRSTRLEN 16
212 #define INET6_ADDRSTRLEN 46
214 /* Get the definition of the macro to define the common sockaddr members. */
215 #include <bits/socket.h>
218 /* Structure describing an Internet socket address. */
219 struct sockaddr_in
221 __SOCKADDR_COMMON (sin_);
222 in_port_t sin_port; /* Port number. */
223 struct in_addr sin_addr; /* Internet address. */
225 /* Pad to size of `struct sockaddr'. */
226 unsigned char sin_zero[sizeof (struct sockaddr) -
227 __SOCKADDR_COMMON_SIZE -
228 sizeof (in_port_t) -
229 sizeof (struct in_addr)];
232 /* Ditto, for IPv6. */
233 struct sockaddr_in6
235 __SOCKADDR_COMMON (sin6_);
236 in_port_t sin6_port; /* Transport layer port # */
237 uint32_t sin6_flowinfo; /* IPv6 flow information */
238 struct in6_addr sin6_addr; /* IPv6 address */
239 uint32_t sin6_scope_id; /* IPv6 scope-id */
243 /* IPv4 multicast request. */
244 struct ip_mreq
246 /* IP multicast address of group. */
247 struct in_addr imr_multiaddr;
249 /* Local IP address of interface. */
250 struct in_addr imr_interface;
253 struct ip_mreq_source
255 /* IP multicast address of group. */
256 struct in_addr imr_multiaddr;
258 /* IP address of source. */
259 struct in_addr imr_interface;
261 /* IP address of interface. */
262 struct in_addr imr_sourceaddr;
265 /* Likewise, for IPv6. */
266 struct ipv6_mreq
268 /* IPv6 multicast address of group */
269 struct in6_addr ipv6mr_multiaddr;
271 /* local interface */
272 unsigned int ipv6mr_interface;
276 /* Multicast group request. */
277 struct group_req
279 /* Interface index. */
280 uint32_t gr_interface;
282 /* Group address. */
283 struct sockaddr_storage gr_group;
286 struct group_source_req
288 /* Interface index. */
289 uint32_t gsr_interface;
291 /* Group address. */
292 struct sockaddr_storage gsr_group;
294 /* Source address. */
295 struct sockaddr_storage gsr_source;
299 /* Full-state filter operations. */
300 struct ip_msfilter
302 /* IP multicast address of group. */
303 struct in_addr imsf_multiaddr;
305 /* Local IP address of interface. */
306 struct in_addr imsf_interface;
308 /* Filter mode. */
309 uint32_t imsf_fmode;
311 /* Number of source addresses. */
312 uint32_t imsf_numsrc;
313 /* Source addresses. */
314 struct in_addr imsf_slist[1];
317 #define IP_MSFILTER_SIZE(numsrc) (sizeof (struct ip_msfilter) \
318 - sizeof (struct in_addr) \
319 + (numsrc) * sizeof (struct in_addr))
321 struct group_filter
323 /* Interface index. */
324 uint32_t gf_interface;
326 /* Group address. */
327 struct sockaddr_storage gf_group;
329 /* Filter mode. */
330 uint32_t gf_fmode;
332 /* Number of source addresses. */
333 uint32_t gf_numsrc;
334 /* Source addresses. */
335 struct sockaddr_storage gf_slist[1];
338 #define GROUP_FILTER_SIZE(numsrc) (sizeof (struct group_filter) \
339 - sizeof (struct sockaddr_storage) \
340 + ((numsrc) \
341 * sizeof (struct sockaddr_storage)))
344 /* Get system-specific definitions. */
345 #include <bits/in.h>
347 /* Functions to convert between host and network byte order.
349 Please note that these functions normally take `unsigned long int' or
350 `unsigned short int' values as arguments and also return them. But
351 this was a short-sighted decision since on different systems the types
352 may have different representations but the values are always the same. */
354 extern uint32_t ntohl (uint32_t __netlong) __THROW __attribute__ ((__const__));
355 extern uint16_t ntohs (uint16_t __netshort)
356 __THROW __attribute__ ((__const__));
357 extern uint32_t htonl (uint32_t __hostlong)
358 __THROW __attribute__ ((__const__));
359 extern uint16_t htons (uint16_t __hostshort)
360 __THROW __attribute__ ((__const__));
362 #include <endian.h>
364 /* Get machine dependent optimized versions of byte swapping functions. */
365 #include <bits/byteswap.h>
367 #ifdef __OPTIMIZE__
368 /* We can optimize calls to the conversion functions. Either nothing has
369 to be done or we are using directly the byte-swapping functions which
370 often can be inlined. */
371 # if __BYTE_ORDER == __BIG_ENDIAN
372 /* The host byte order is the same as network byte order,
373 so these functions are all just identity. */
374 # define ntohl(x) (x)
375 # define ntohs(x) (x)
376 # define htonl(x) (x)
377 # define htons(x) (x)
378 # else
379 # if __BYTE_ORDER == __LITTLE_ENDIAN
380 # define ntohl(x) __bswap_32 (x)
381 # define ntohs(x) __bswap_16 (x)
382 # define htonl(x) __bswap_32 (x)
383 # define htons(x) __bswap_16 (x)
384 # endif
385 # endif
386 #endif
388 #define IN6_IS_ADDR_UNSPECIFIED(a) \
389 (((__const uint32_t *) (a))[0] == 0 \
390 && ((__const uint32_t *) (a))[1] == 0 \
391 && ((__const uint32_t *) (a))[2] == 0 \
392 && ((__const uint32_t *) (a))[3] == 0)
394 #define IN6_IS_ADDR_LOOPBACK(a) \
395 (((__const uint32_t *) (a))[0] == 0 \
396 && ((__const uint32_t *) (a))[1] == 0 \
397 && ((__const uint32_t *) (a))[2] == 0 \
398 && ((__const uint32_t *) (a))[3] == htonl (1))
400 #define IN6_IS_ADDR_MULTICAST(a) (((__const uint8_t *) (a))[0] == 0xff)
402 #define IN6_IS_ADDR_LINKLOCAL(a) \
403 ((((__const uint32_t *) (a))[0] & htonl (0xffc00000)) \
404 == htonl (0xfe800000))
406 #define IN6_IS_ADDR_SITELOCAL(a) \
407 ((((__const uint32_t *) (a))[0] & htonl (0xffc00000)) \
408 == htonl (0xfec00000))
410 #define IN6_IS_ADDR_V4MAPPED(a) \
411 ((((__const uint32_t *) (a))[0] == 0) \
412 && (((__const uint32_t *) (a))[1] == 0) \
413 && (((__const uint32_t *) (a))[2] == htonl (0xffff)))
415 #define IN6_IS_ADDR_V4COMPAT(a) \
416 ((((__const uint32_t *) (a))[0] == 0) \
417 && (((__const uint32_t *) (a))[1] == 0) \
418 && (((__const uint32_t *) (a))[2] == 0) \
419 && (ntohl (((__const uint32_t *) (a))[3]) > 1))
421 #define IN6_ARE_ADDR_EQUAL(a,b) \
422 ((((__const uint32_t *) (a))[0] == ((__const uint32_t *) (b))[0]) \
423 && (((__const uint32_t *) (a))[1] == ((__const uint32_t *) (b))[1]) \
424 && (((__const uint32_t *) (a))[2] == ((__const uint32_t *) (b))[2]) \
425 && (((__const uint32_t *) (a))[3] == ((__const uint32_t *) (b))[3]))
427 /* Bind socket to a privileged IP port. */
428 extern int bindresvport (int __sockfd, struct sockaddr_in *__sock_in) __THROW;
430 /* The IPv6 version of this function. */
431 extern int bindresvport6 (int __sockfd, struct sockaddr_in6 *__sock_in)
432 __THROW;
435 #define IN6_IS_ADDR_MC_NODELOCAL(a) \
436 (IN6_IS_ADDR_MULTICAST(a) \
437 && ((((__const uint8_t *) (a))[1] & 0xf) == 0x1))
439 #define IN6_IS_ADDR_MC_LINKLOCAL(a) \
440 (IN6_IS_ADDR_MULTICAST(a) \
441 && ((((__const uint8_t *) (a))[1] & 0xf) == 0x2))
443 #define IN6_IS_ADDR_MC_SITELOCAL(a) \
444 (IN6_IS_ADDR_MULTICAST(a) \
445 && ((((__const uint8_t *) (a))[1] & 0xf) == 0x5))
447 #define IN6_IS_ADDR_MC_ORGLOCAL(a) \
448 (IN6_IS_ADDR_MULTICAST(a) \
449 && ((((__const uint8_t *) (a))[1] & 0xf) == 0x8))
451 #define IN6_IS_ADDR_MC_GLOBAL(a) \
452 (IN6_IS_ADDR_MULTICAST(a) \
453 && ((((__const uint8_t *) (a))[1] & 0xf) == 0xe))
455 /* IPv6 packet information. */
456 struct in6_pktinfo
458 struct in6_addr ipi6_addr; /* src/dst IPv6 address */
459 unsigned int ipi6_ifindex; /* send/recv interface index */
463 #ifdef __USE_GNU
464 /* Hop-by-Hop and Destination Options Processing. */
465 extern int inet6_option_space (int __nbytes) __THROW;
466 extern int inet6_option_init (void *__bp, struct cmsghdr **__cmsgp,
467 int __type) __THROW;
468 extern int inet6_option_append (struct cmsghdr *__cmsg,
469 __const uint8_t *__typep, int __multx,
470 int __plusy) __THROW;
471 extern uint8_t *inet6_option_alloc (struct cmsghdr *__cmsg, int __datalen,
472 int __multx, int __plusy) __THROW;
473 extern int inet6_option_next (__const struct cmsghdr *__cmsg,
474 uint8_t **__tptrp) __THROW;
475 extern int inet6_option_find (__const struct cmsghdr *__cmsg,
476 uint8_t **__tptrp, int __type) __THROW;
479 /* Multicast source filter support. */
481 /* Get IPv4 source filter. */
482 extern int getipv4sourcefilter (int __s, struct in_addr __interface,
483 struct in_addr __group, uint32_t *__fmode,
484 uint32_t *__numsrc, struct in_addr *__slist)
485 __THROW;
487 /* Set IPv4 source filter. */
488 extern int setipv4sourcefilter (int __s, struct in_addr __interface,
489 struct in_addr __group, uint32_t __fmode,
490 uint32_t __numsrc,
491 __const struct in_addr *__slist)
492 __THROW;
495 /* Get source filter. */
496 extern int getsourcefilter (int __s, uint32_t __interface,
497 __const struct sockaddr *__group,
498 socklen_t __grouplen, uint32_t *__fmode,
499 uint32_t *__numsrc,
500 struct sockaddr_storage *__slist) __THROW;
502 /* Set source filter. */
503 extern int setsourcefilter (int __s, uint32_t __interface,
504 __const struct sockaddr *__group,
505 socklen_t __grouplen, uint32_t __fmode,
506 uint32_t __numsrc,
507 __const struct sockaddr_storage *__slist) __THROW;
508 #endif /* use GNU */
510 __END_DECLS
512 #endif /* netinet/in.h */