in.h 12 KB

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