]> git.ipfire.org Git - thirdparty/glibc.git/blob - inet/netinet/in.h
Update.
[thirdparty/glibc.git] / inet / netinet / in.h
1 /* Copyright (C) 1991-1999, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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. */
18
19 #ifndef _NETINET_IN_H
20 #define _NETINET_IN_H 1
21
22 #include <features.h>
23 #include <limits.h>
24 #include <stdint.h>
25
26 #include <sys/types.h>
27 #include <bits/socket.h>
28
29
30 __BEGIN_DECLS
31
32 /* Standard well-defined IP protocols. */
33 enum
34 {
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
62 };
63
64
65 /* Type to represent a port. */
66 typedef uint16_t in_port_t;
67
68 /* Standard well-known ports. */
69 enum
70 {
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,
83
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. */
89
90
91 IPPORT_EXECSERVER = 512, /* execd service. */
92 IPPORT_LOGINSERVER = 513, /* rlogind service. */
93 IPPORT_CMDSERVER = 514,
94 IPPORT_EFSSERVER = 520,
95
96 /* UDP ports. */
97 IPPORT_BIFFUDP = 512,
98 IPPORT_WHOSERVER = 513,
99 IPPORT_ROUTESERVER = 520,
100
101 /* Ports less than this value are reserved for privileged processes. */
102 IPPORT_RESERVED = 1024,
103
104 /* Ports greater this value are reserved for (non-privileged) servers. */
105 IPPORT_USERRESERVED = 5000
106 };
107
108
109 /* Internet address. */
110 typedef uint32_t in_addr_t;
111 struct in_addr
112 {
113 in_addr_t s_addr;
114 };
115
116
117 /* Definitions of the bits in an Internet address integer.
118
119 On subnets, host and network parts are found according to
120 the subnet mask, not these masks. */
121
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
127
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
133
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)
138
139 #define IN_CLASSD(a) ((((in_addr_t)(a)) & 0xf0000000) == 0xe0000000)
140 #define IN_MULTICAST(a) IN_CLASSD(a)
141
142 #define IN_EXPERIMENTAL(a) ((((in_addr_t)(a)) & 0xe0000000) == 0xe0000000)
143 #define IN_BADCLASS(a) ((((in_addr_t)(a)) & 0xf0000000) == 0xf0000000)
144
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)
151
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
158
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 */
164
165
166 /* IPv6 address */
167 struct in6_addr
168 {
169 union
170 {
171 uint8_t u6_addr8[16];
172 uint16_t u6_addr16[8];
173 uint32_t u6_addr32[4];
174 } in6_u;
175 #define s6_addr in6_u.u6_addr8
176 #define s6_addr16 in6_u.u6_addr16
177 #define s6_addr32 in6_u.u6_addr32
178 };
179
180 extern const struct in6_addr in6addr_any; /* :: */
181 extern const struct in6_addr in6addr_loopback; /* ::1 */
182 #define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
183 #define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
184
185 #define INET_ADDRSTRLEN 16
186 #define INET6_ADDRSTRLEN 46
187
188 /* Get the definition of the macro to define the common sockaddr members. */
189 #include <bits/sockaddr.h>
190
191
192 /* Structure describing an Internet socket address. */
193 struct sockaddr_in
194 {
195 __SOCKADDR_COMMON (sin_);
196 in_port_t sin_port; /* Port number. */
197 struct in_addr sin_addr; /* Internet address. */
198
199 /* Pad to size of `struct sockaddr'. */
200 unsigned char sin_zero[sizeof (struct sockaddr) -
201 __SOCKADDR_COMMON_SIZE -
202 sizeof (in_port_t) -
203 sizeof (struct in_addr)];
204 };
205
206 /* Ditto, for IPv6. */
207 struct sockaddr_in6
208 {
209 __SOCKADDR_COMMON (sin6_);
210 in_port_t sin6_port; /* Transport layer port # */
211 uint32_t sin6_flowinfo; /* IPv6 flow information */
212 struct in6_addr sin6_addr; /* IPv6 address */
213 uint32_t sin6_scope_id; /* IPv6 scope-id */
214 };
215
216 /* IPv6 multicast request. */
217 struct ipv6_mreq
218 {
219 /* IPv6 multicast address of group */
220 struct in6_addr ipv6mr_multiaddr;
221
222 /* local interface */
223 unsigned int ipv6mr_interface;
224 };
225
226 /* Get system-specific definitions. */
227 #include <bits/in.h>
228
229 /* Functions to convert between host and network byte order.
230
231 Please note that these functions normally take `unsigned long int' or
232 `unsigned short int' values as arguments and also return them. But
233 this was a short-sighted decision since on different systems the types
234 may have different representations but the values are always the same. */
235
236 extern uint32_t ntohl (uint32_t __netlong) __THROW __attribute__ ((__const__));
237 extern uint16_t ntohs (uint16_t __netshort)
238 __THROW __attribute__ ((__const__));
239 extern uint32_t htonl (uint32_t __hostlong)
240 __THROW __attribute__ ((__const__));
241 extern uint16_t htons (uint16_t __hostshort)
242 __THROW __attribute__ ((__const__));
243
244 #include <endian.h>
245
246 /* Get machine dependent optimized versions of byte swapping functions. */
247 #include <bits/byteswap.h>
248
249 #ifdef __OPTIMIZE__
250 /* We can optimize calls to the conversion functions. Either nothing has
251 to be done or we are using directly the byte-swapping functions which
252 often can be inlined. */
253 # if __BYTE_ORDER == __BIG_ENDIAN
254 /* The host byte order is the same as network byte order,
255 so these functions are all just identity. */
256 # define ntohl(x) (x)
257 # define ntohs(x) (x)
258 # define htonl(x) (x)
259 # define htons(x) (x)
260 # else
261 # if __BYTE_ORDER == __LITTLE_ENDIAN
262 # define ntohl(x) __bswap_32 (x)
263 # define ntohs(x) __bswap_16 (x)
264 # define htonl(x) __bswap_32 (x)
265 # define htons(x) __bswap_16 (x)
266 # endif
267 # endif
268 #endif
269
270 #define IN6_IS_ADDR_UNSPECIFIED(a) \
271 (((__const uint32_t *) (a))[0] == 0 \
272 && ((__const uint32_t *) (a))[1] == 0 \
273 && ((__const uint32_t *) (a))[2] == 0 \
274 && ((__const uint32_t *) (a))[3] == 0)
275
276 #define IN6_IS_ADDR_LOOPBACK(a) \
277 (((__const uint32_t *) (a))[0] == 0 \
278 && ((__const uint32_t *) (a))[1] == 0 \
279 && ((__const uint32_t *) (a))[2] == 0 \
280 && ((__const uint32_t *) (a))[3] == htonl (1))
281
282 #define IN6_IS_ADDR_MULTICAST(a) (((__const uint8_t *) (a))[0] == 0xff)
283
284 #define IN6_IS_ADDR_LINKLOCAL(a) \
285 ((((__const uint32_t *) (a))[0] & htonl (0xffc00000)) \
286 == htonl (0xfe800000))
287
288 #define IN6_IS_ADDR_SITELOCAL(a) \
289 ((((__const uint32_t *) (a))[0] & htonl (0xffc00000)) \
290 == htonl (0xfec00000))
291
292 #define IN6_IS_ADDR_V4MAPPED(a) \
293 ((((__const uint32_t *) (a))[0] == 0) \
294 && (((__const uint32_t *) (a))[1] == 0) \
295 && (((__const uint32_t *) (a))[2] == htonl (0xffff)))
296
297 #define IN6_IS_ADDR_V4COMPAT(a) \
298 ((((__const uint32_t *) (a))[0] == 0) \
299 && (((__const uint32_t *) (a))[1] == 0) \
300 && (((__const uint32_t *) (a))[2] == 0) \
301 && (ntohl (((__const uint32_t *) (a))[3]) > 1))
302
303 #define IN6_ARE_ADDR_EQUAL(a,b) \
304 ((((__const uint32_t *) (a))[0] == ((__const uint32_t *) (b))[0]) \
305 && (((__const uint32_t *) (a))[1] == ((__const uint32_t *) (b))[1]) \
306 && (((__const uint32_t *) (a))[2] == ((__const uint32_t *) (b))[2]) \
307 && (((__const uint32_t *) (a))[3] == ((__const uint32_t *) (b))[3]))
308
309 /* Bind socket to a privileged IP port. */
310 extern int bindresvport (int __sockfd, struct sockaddr_in *__sock_in) __THROW;
311
312 /* The IPv6 version of this function. */
313 extern int bindresvport6 (int __sockfd, struct sockaddr_in6 *__sock_in)
314 __THROW;
315
316
317 #define IN6_IS_ADDR_MC_NODELOCAL(a) \
318 (IN6_IS_ADDR_MULTICAST(a) \
319 && ((((__const uint8_t *) (a))[1] & 0xf) == 0x1))
320
321 #define IN6_IS_ADDR_MC_LINKLOCAL(a) \
322 (IN6_IS_ADDR_MULTICAST(a) \
323 && ((((__const uint8_t *) (a))[1] & 0xf) == 0x2))
324
325 #define IN6_IS_ADDR_MC_SITELOCAL(a) \
326 (IN6_IS_ADDR_MULTICAST(a) \
327 && ((((__const uint8_t *) (a))[1] & 0xf) == 0x5))
328
329 #define IN6_IS_ADDR_MC_ORGLOCAL(a) \
330 (IN6_IS_ADDR_MULTICAST(a) \
331 && ((((__const uint8_t *) (a))[1] & 0xf) == 0x8))
332
333 #define IN6_IS_ADDR_MC_GLOBAL(a) \
334 (IN6_IS_ADDR_MULTICAST(a) \
335 && ((((__const uint8_t *) (a))[1] & 0xf) == 0xe))
336
337 /* IPv6 packet information. */
338 struct in6_pktinfo
339 {
340 struct in6_addr ipi6_addr; /* src/dst IPv6 address */
341 unsigned int ipi6_ifindex; /* send/recv interface index */
342 };
343
344 __END_DECLS
345
346 #endif /* netinet/in.h */