Update.
[glibc.git] / inet / netinet / in.h
blob4fd64d782973f1c03f5a89efff43f27374ca7970
1 /* Copyright (C) 1991-1999, 2000 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #ifndef _NETINET_IN_H
20 #define _NETINET_IN_H 1
22 #include <features.h>
23 #include <limits.h>
24 #include <stdint.h>
26 #include <sys/types.h>
27 #include <bits/socket.h>
30 __BEGIN_DECLS
32 /* Standard well-defined IP protocols. */
33 enum
35 IPPROTO_IP = 0, /* Dummy protocol for TCP. */
36 IPPROTO_HOPOPTS = 0, /* IPv6 Hop-by-Hop options. */
37 IPPROTO_ICMP = 1, /* Internet Control Message Protocol. */
38 IPPROTO_IGMP = 2, /* Internet Group Management Protocol. */
39 IPPROTO_IPIP = 4, /* IPIP tunnels (older KA9Q tunnels use 94). */
40 IPPROTO_TCP = 6, /* Transmission Control Protocol. */
41 IPPROTO_EGP = 8, /* Exterior Gateway Protocol. */
42 IPPROTO_PUP = 12, /* PUP protocol. */
43 IPPROTO_UDP = 17, /* User Datagram Protocol. */
44 IPPROTO_IDP = 22, /* XNS IDP protocol. */
45 IPPROTO_TP = 29, /* SO Transport Protocol Class 4. */
46 IPPROTO_IPV6 = 41, /* IPv6 header. */
47 IPPROTO_ROUTING = 43, /* IPv6 routing header. */
48 IPPROTO_FRAGMENT = 44, /* IPv6 fragmentation header. */
49 IPPROTO_RSVP = 46, /* Reservation Protocol. */
50 IPPROTO_GRE = 47, /* General Routing Encapsulation. */
51 IPPROTO_ESP = 50, /* encapsulating security payload. */
52 IPPROTO_AH = 51, /* authentication header. */
53 IPPROTO_ICMPV6 = 58, /* ICMPv6. */
54 IPPROTO_NONE = 59, /* IPv6 no next header. */
55 IPPROTO_DSTOPTS = 60, /* IPv6 destination options. */
56 IPPROTO_MTP = 92, /* Multicast Transport Protocol. */
57 IPPROTO_ENCAP = 98, /* Encapsulation Header. */
58 IPPROTO_PIM = 103, /* Protocol Independent Multicast. */
59 IPPROTO_COMP = 108, /* Compression Header Protocol. */
60 IPPROTO_RAW = 255, /* Raw IP packets. */
61 IPPROTO_MAX
65 /* Type to represent a port. */
66 typedef uint16_t in_port_t;
68 /* Standard well-known ports. */
69 enum
71 IPPORT_ECHO = 7, /* Echo service. */
72 IPPORT_DISCARD = 9, /* Discard transmissions service. */
73 IPPORT_SYSTAT = 11, /* System status service. */
74 IPPORT_DAYTIME = 13, /* Time of day service. */
75 IPPORT_NETSTAT = 15, /* Network status service. */
76 IPPORT_FTP = 21, /* File Transfer Protocol. */
77 IPPORT_TELNET = 23, /* Telnet protocol. */
78 IPPORT_SMTP = 25, /* Simple Mail Transfer Protocol. */
79 IPPORT_TIMESERVER = 37, /* Timeserver service. */
80 IPPORT_NAMESERVER = 42, /* Domain Name Service. */
81 IPPORT_WHOIS = 43, /* Internet Whois service. */
82 IPPORT_MTP = 57,
84 IPPORT_TFTP = 69, /* Trivial File Transfer Protocol. */
85 IPPORT_RJE = 77,
86 IPPORT_FINGER = 79, /* Finger service. */
87 IPPORT_TTYLINK = 87,
88 IPPORT_SUPDUP = 95, /* SUPDUP protocol. */
91 IPPORT_EXECSERVER = 512, /* execd service. */
92 IPPORT_LOGINSERVER = 513, /* rlogind service. */
93 IPPORT_CMDSERVER = 514,
94 IPPORT_EFSSERVER = 520,
96 /* UDP ports. */
97 IPPORT_BIFFUDP = 512,
98 IPPORT_WHOSERVER = 513,
99 IPPORT_ROUTESERVER = 520,
101 /* Ports less than this value are reserved for privileged processes. */
102 IPPORT_RESERVED = 1024,
104 /* Ports greater this value are reserved for (non-privileged) servers. */
105 IPPORT_USERRESERVED = 5000
109 /* Internet address. */
110 typedef uint32_t in_addr_t;
111 struct in_addr
113 in_addr_t s_addr;
117 /* Definitions of the bits in an Internet address integer.
119 On subnets, host and network parts are found according to
120 the subnet mask, not these masks. */
122 #define IN_CLASSA(a) ((((in_addr_t)(a)) & 0x80000000) == 0)
123 #define IN_CLASSA_NET 0xff000000
124 #define IN_CLASSA_NSHIFT 24
125 #define IN_CLASSA_HOST (0xffffffff & ~IN_CLASSA_NET)
126 #define IN_CLASSA_MAX 128
128 #define IN_CLASSB(a) ((((in_addr_t)(a)) & 0xc0000000) == 0x80000000)
129 #define IN_CLASSB_NET 0xffff0000
130 #define IN_CLASSB_NSHIFT 16
131 #define IN_CLASSB_HOST (0xffffffff & ~IN_CLASSB_NET)
132 #define IN_CLASSB_MAX 65536
134 #define IN_CLASSC(a) ((((in_addr_t)(a)) & 0xe0000000) == 0xc0000000)
135 #define IN_CLASSC_NET 0xffffff00
136 #define IN_CLASSC_NSHIFT 8
137 #define IN_CLASSC_HOST (0xffffffff & ~IN_CLASSC_NET)
139 #define IN_CLASSD(a) ((((in_addr_t)(a)) & 0xf0000000) == 0xe0000000)
140 #define IN_MULTICAST(a) IN_CLASSD(a)
142 #define IN_EXPERIMENTAL(a) ((((in_addr_t)(a)) & 0xe0000000) == 0xe0000000)
143 #define IN_BADCLASS(a) ((((in_addr_t)(a)) & 0xf0000000) == 0xf0000000)
145 /* Address to accept any incoming messages. */
146 #define INADDR_ANY ((in_addr_t) 0x00000000)
147 /* Address to send to all hosts. */
148 #define INADDR_BROADCAST ((in_addr_t) 0xffffffff)
149 /* Address indicating an error return. */
150 #define INADDR_NONE ((in_addr_t) 0xffffffff)
152 /* Network number for local host loopback. */
153 #define IN_LOOPBACKNET 127
154 /* Address to loopback in software to local host. */
155 #ifndef INADDR_LOOPBACK
156 # define INADDR_LOOPBACK ((in_addr_t) 0x7f000001) /* Inet 127.0.0.1. */
157 #endif
159 /* Defines for Multicast INADDR. */
160 #define INADDR_UNSPEC_GROUP ((in_addr_t) 0xe0000000) /* 224.0.0.0 */
161 #define INADDR_ALLHOSTS_GROUP ((in_addr_t) 0xe0000001) /* 224.0.0.1 */
162 #define INADDR_ALLRTRS_GROUP ((in_addr_t) 0xe0000002) /* 224.0.0.2 */
163 #define INADDR_MAX_LOCAL_GROUP ((in_addr_t) 0xe00000ff) /* 224.0.0.255 */
166 /* IPv6 address */
167 struct in6_addr
169 union
171 uint8_t u6_addr8[16];
172 uint16_t u6_addr16[8];
173 uint32_t u6_addr32[4];
174 #if ULONG_MAX > 0xffffffff
175 uint64_t u6_addr64[2];
176 #endif
177 } in6_u;
178 #define s6_addr in6_u.u6_addr8
179 #define s6_addr16 in6_u.u6_addr16
180 #define s6_addr32 in6_u.u6_addr32
181 #define s6_addr64 in6_u.u6_addr64
184 extern const struct in6_addr in6addr_any; /* :: */
185 extern const struct in6_addr in6addr_loopback; /* ::1 */
186 #define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
187 #define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
189 #define INET_ADDRSTRLEN 16
190 #define INET6_ADDRSTRLEN 46
192 /* Get the definition of the macro to define the common sockaddr members. */
193 #include <bits/sockaddr.h>
196 /* Structure describing an Internet socket address. */
197 struct sockaddr_in
199 __SOCKADDR_COMMON (sin_);
200 in_port_t sin_port; /* Port number. */
201 struct in_addr sin_addr; /* Internet address. */
203 /* Pad to size of `struct sockaddr'. */
204 unsigned char sin_zero[sizeof (struct sockaddr) -
205 __SOCKADDR_COMMON_SIZE -
206 sizeof (in_port_t) -
207 sizeof (struct in_addr)];
210 /* Ditto, for IPv6. */
211 struct sockaddr_in6
213 __SOCKADDR_COMMON (sin6_);
214 in_port_t sin6_port; /* Transport layer port # */
215 uint32_t sin6_flowinfo; /* IPv6 flow information */
216 struct in6_addr sin6_addr; /* IPv6 address */
217 uint32_t sin6_scope_id; /* IPv6 scope-id */
220 /* IPv6 multicast request. */
221 struct ipv6_mreq
223 /* IPv6 multicast address of group */
224 struct in6_addr ipv6mr_multiaddr;
226 /* local interface */
227 unsigned int ipv6mr_interface;
230 /* Get system-specific definitions. */
231 #include <bits/in.h>
233 /* Functions to convert between host and network byte order.
235 Please note that these functions normally take `unsigned long int' or
236 `unsigned short int' values as arguments and also return them. But
237 this was a short-sighted decision since on different systems the types
238 may have different representations but the values are always the same. */
240 extern uint32_t ntohl (uint32_t __netlong) __THROW __attribute__ ((__const__));
241 extern uint16_t ntohs (uint16_t __netshort)
242 __THROW __attribute__ ((__const__));
243 extern uint32_t htonl (uint32_t __hostlong)
244 __THROW __attribute__ ((__const__));
245 extern uint16_t htons (uint16_t __hostshort)
246 __THROW __attribute__ ((__const__));
248 #include <endian.h>
250 /* Get machine dependent optimized versions of byte swapping functions. */
251 #include <bits/byteswap.h>
253 #ifdef __OPTIMIZE__
254 /* We can optimize calls to the conversion functions. Either nothing has
255 to be done or we are using directly the byte-swapping functions which
256 often can be inlined. */
257 # if __BYTE_ORDER == __BIG_ENDIAN
258 /* The host byte order is the same as network byte order,
259 so these functions are all just identity. */
260 # define ntohl(x) (x)
261 # define ntohs(x) (x)
262 # define htonl(x) (x)
263 # define htons(x) (x)
264 # else
265 # if __BYTE_ORDER == __LITTLE_ENDIAN
266 # define ntohl(x) __bswap_32 (x)
267 # define ntohs(x) __bswap_16 (x)
268 # define htonl(x) __bswap_32 (x)
269 # define htons(x) __bswap_16 (x)
270 # endif
271 # endif
272 #endif
274 #define IN6_IS_ADDR_UNSPECIFIED(a) \
275 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
276 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
278 #define IN6_IS_ADDR_LOOPBACK(a) \
279 (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
280 ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == htonl (1))
282 #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
284 #define IN6_IS_ADDR_LINKLOCAL(a) \
285 ((((uint32_t *) (a))[0] & htonl (0xffc00000)) == htonl (0xfe800000))
287 #define IN6_IS_ADDR_SITELOCAL(a) \
288 ((((uint32_t *) (a))[0] & htonl (0xffc00000)) == htonl (0xfec00000))
290 #define IN6_IS_ADDR_V4MAPPED(a) \
291 ((((uint32_t *) (a))[0] == 0) && (((uint32_t *) (a))[1] == 0) && \
292 (((uint32_t *) (a))[2] == htonl (0xffff)))
294 #define IN6_IS_ADDR_V4COMPAT(a) \
295 ((((uint32_t *) (a))[0] == 0) && (((uint32_t *) (a))[1] == 0) && \
296 (((uint32_t *) (a))[2] == 0) && (ntohl (((uint32_t *) (a))[3]) > 1))
298 #define IN6_ARE_ADDR_EQUAL(a,b) \
299 ((((uint32_t *) (a))[0] == ((uint32_t *) (b))[0]) && \
300 (((uint32_t *) (a))[1] == ((uint32_t *) (b))[1]) && \
301 (((uint32_t *) (a))[2] == ((uint32_t *) (b))[2]) && \
302 (((uint32_t *) (a))[3] == ((uint32_t *) (b))[3]))
304 /* Bind socket to a privileged IP port. */
305 extern int bindresvport (int __sockfd, struct sockaddr_in *__sock_in) __THROW;
307 /* The IPv6 version of this function. */
308 extern int bindresvport6 (int __sockfd, struct sockaddr_in6 *__sock_in)
309 __THROW;
312 #define IN6_IS_ADDR_MC_NODELOCAL(a) \
313 (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0x1))
315 #define IN6_IS_ADDR_MC_LINKLOCAL(a) \
316 (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0x2))
318 #define IN6_IS_ADDR_MC_SITELOCAL(a) \
319 (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0x5))
321 #define IN6_IS_ADDR_MC_ORGLOCAL(a) \
322 (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0x8))
324 #define IN6_IS_ADDR_MC_GLOBAL(a) \
325 (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0xe))
327 /* IPv6 packet information. */
328 struct in6_pktinfo
330 struct in6_addr ipi6_addr; /* src/dst IPv6 address */
331 unsigned int ipi6_ifindex; /* send/recv interface index */
334 __END_DECLS
336 #endif /* netinet/in.h */