in.h 11 KB

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