in.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. typedef uint32_t in_addr_t;
  89. struct in_addr
  90. {
  91. in_addr_t s_addr;
  92. };
  93. /* Definitions of the bits in an Internet address integer.
  94. On subnets, host and network parts are found according to
  95. the subnet mask, not these masks. */
  96. #define IN_CLASSA(a) ((((uint32_t) (a)) & 0x80000000) == 0)
  97. #define IN_CLASSA_NET 0xff000000
  98. #define IN_CLASSA_NSHIFT 24
  99. #define IN_CLASSA_HOST (0xffffffff & ~IN_CLASSA_NET)
  100. #define IN_CLASSA_MAX 128
  101. #define IN_CLASSB(a) ((((uint32_t) (a)) & 0xc0000000) == 0x80000000)
  102. #define IN_CLASSB_NET 0xffff0000
  103. #define IN_CLASSB_NSHIFT 16
  104. #define IN_CLASSB_HOST (0xffffffff & ~IN_CLASSB_NET)
  105. #define IN_CLASSB_MAX 65536
  106. #define IN_CLASSC(a) ((((uint32_t) (a)) & 0xe0000000) == 0xc0000000)
  107. #define IN_CLASSC_NET 0xffffff00
  108. #define IN_CLASSC_NSHIFT 8
  109. #define IN_CLASSC_HOST (0xffffffff & ~IN_CLASSC_NET)
  110. #define IN_CLASSD(a) ((((uint32_t) (a)) & 0xf0000000) == 0xe0000000)
  111. #define IN_MULTICAST(a) IN_CLASSD(a)
  112. #define IN_EXPERIMENTAL(a) ((((uint32_t) (a)) & 0xe0000000) == 0xe0000000)
  113. #define IN_BADCLASS(a) ((((uint32_t) (a)) & 0xf0000000) == 0xf0000000)
  114. /* Address to accept any incoming messages. */
  115. #define INADDR_ANY ((uint32_t) 0x00000000)
  116. /* Address to send to all hosts. */
  117. #define INADDR_BROADCAST ((uint32_t) 0xffffffff)
  118. /* Address indicating an error return. */
  119. #define INADDR_NONE ((uint32_t) 0xffffffff)
  120. /* Network number for local host loopback. */
  121. #define IN_LOOPBACKNET 127
  122. /* Address to loopback in software to local host. */
  123. #ifndef INADDR_LOOPBACK
  124. # define INADDR_LOOPBACK ((uint32_t) 0x7f000001) /* Inet 127.0.0.1. */
  125. #endif
  126. /* Defines for Multicast INADDR. */
  127. #define INADDR_UNSPEC_GROUP ((uint32_t) 0xe0000000) /* 224.0.0.0 */
  128. #define INADDR_ALLHOSTS_GROUP ((uint32_t) 0xe0000001) /* 224.0.0.1 */
  129. #define INADDR_ALLRTRS_GROUP ((uint32_t) 0xe0000002) /* 224.0.0.2 */
  130. #define INADDR_MAX_LOCAL_GROUP ((uint32_t) 0xe00000ff) /* 224.0.0.255 */
  131. /* IPv6 address */
  132. struct in6_addr
  133. {
  134. union
  135. {
  136. uint8_t u6_addr8[16];
  137. uint16_t u6_addr16[8];
  138. uint32_t u6_addr32[4];
  139. #if ULONG_MAX > 0xffffffff
  140. uint64_t u6_addr64[2];
  141. #endif
  142. } in6_u;
  143. #define s6_addr in6_u.u6_addr8
  144. #define s6_addr16 in6_u.u6_addr16
  145. #define s6_addr32 in6_u.u6_addr32
  146. #define s6_addr64 in6_u.u6_addr64
  147. };
  148. extern const struct in6_addr in6addr_any; /* :: */
  149. extern const struct in6_addr in6addr_loopback; /* ::1 */
  150. #define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
  151. #define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
  152. #define INET_ADDRSTRLEN 16
  153. #define INET6_ADDRSTRLEN 46
  154. /* Get the definition of the macro to define the common sockaddr members. */
  155. #include <bits/sockaddr.h>
  156. /* Structure describing an Internet socket address. */
  157. struct sockaddr_in
  158. {
  159. __SOCKADDR_COMMON (sin_);
  160. uint16_t sin_port; /* Port number. */
  161. struct in_addr sin_addr; /* Internet address. */
  162. /* Pad to size of `struct sockaddr'. */
  163. unsigned char sin_zero[sizeof (struct sockaddr) -
  164. __SOCKADDR_COMMON_SIZE -
  165. sizeof (uint16_t) -
  166. sizeof (struct in_addr)];
  167. };
  168. /* Ditto, for IPv6. */
  169. struct sockaddr_in6
  170. {
  171. __SOCKADDR_COMMON (sin6_);
  172. uint16_t sin6_port; /* Transport layer port # */
  173. uint32_t sin6_flowinfo; /* IPv6 flow information */
  174. struct in6_addr sin6_addr; /* IPv6 address */
  175. };
  176. /* IPv6 multicast request. */
  177. struct ipv6_mreq
  178. {
  179. /* IPv6 multicast address of group */
  180. struct in6_addr ipv6mr_multiaddr;
  181. /* local interface */
  182. unsigned int ipv6mr_interface;
  183. };
  184. /* Get system-specific definitions. */
  185. #include <bits/in.h>
  186. /* Functions to convert between host and network byte order.
  187. Please note that these functions normally take `unsigned long int' or
  188. `unsigned short int' values as arguments and also return them. But
  189. this was a short-sighted decision since on different systems the types
  190. may have different representations but the values are always the same. */
  191. extern uint32_t ntohl __P ((uint32_t __netlong));
  192. extern uint16_t ntohs __P ((uint16_t __netshort));
  193. extern uint32_t htonl __P ((uint32_t __hostlong));
  194. extern uint16_t htons __P ((uint16_t __hostshort));
  195. #include <endian.h>
  196. /* Get machine dependent optimized versions of byte swapping functions. */
  197. #include <bits/byteswap.h>
  198. #if __BYTE_ORDER == __BIG_ENDIAN
  199. /* The host byte order is the same as network byte order,
  200. so these functions are all just identity. */
  201. # define ntohl(x) (x)
  202. # define ntohs(x) (x)
  203. # define htonl(x) (x)
  204. # define htons(x) (x)
  205. #else
  206. # if __BYTE_ORDER == __LITTLE_ENDIAN
  207. # define ntohl(x) __bswap_32 (x)
  208. # define ntohs(x) __bswap_16 (x)
  209. # define htonl(x) __bswap_32 (x)
  210. # define htons(x) __bswap_16 (x)
  211. # endif
  212. #endif
  213. #define IN6_IS_ADDR_UNSPECIFIED(a) \
  214. (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
  215. ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
  216. #define IN6_IS_ADDR_LOOPBACK(a) \
  217. (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
  218. ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == htonl (1))
  219. #define IN6_IS_ADDR_MULTICAST(a) (((u_int8_t *) (a))[0] == 0xff)
  220. #define IN6_IS_ADDR_LINKLOCAL(a) \
  221. ((((uint32_t *) (a))[0] & htonl (0xffc00000)) == htonl (0xfe800000))
  222. #define IN6_IS_ADDR_SITELOCAL(a) \
  223. ((((uint32_t *) (a))[0] & htonl (0xffc00000)) == htonl (0xfec00000))
  224. #define IN6_IS_ADDR_V4MAPPED(a) \
  225. ((((uint32_t *) (a))[0] == 0) && (((uint32_t *) (a))[1] == 0) && \
  226. (((uint32_t *) (a))[2] == htonl (0xffff)))
  227. #define IN6_IS_ADDR_V4COMPAT(a) \
  228. ((((uint32_t *) (a))[0] == 0) && (((uint32_t *) (a))[1] == 0) && \
  229. (((uint32_t *) (a))[2] == 0) && (ntohl (((uint32_t *) (a))[3]) > 1))
  230. #define IN6_ARE_ADDR_EQUAL(a,b) \
  231. ((((uint32_t *) (a))[0] == ((uint32_t *) (b))[0]) && \
  232. (((uint32_t *) (a))[1] == ((uint32_t *) (b))[2]) && \
  233. (((uint32_t *) (a))[2] == ((uint32_t *) (b))[1]) && \
  234. (((uint32_t *) (a))[3] == ((uint32_t *) (b))[3]))
  235. /* Bind socket to a privileged IP port. */
  236. extern int bindresvport __P ((int __sockfd, struct sockaddr_in *__sock_in));
  237. #define IN6_IS_ADDR_MC_NODELOCAL(a) \
  238. (IN6_IS_ADDR_MULTICAST(a) && ((((u_int8_t *) (a))[1] & 0xf) == 0x1))
  239. #define IN6_IS_ADDR_MC_LINKLOCAL(a) \
  240. (IN6_IS_ADDR_MULTICAST(a) && ((((u_int8_t *) (a))[1] & 0xf) == 0x2))
  241. #define IN6_IS_ADDR_MC_SITELOCAL(a) \
  242. (IN6_IS_ADDR_MULTICAST(a) && ((((u_int8_t *) (a))[1] & 0xf) == 0x5))
  243. #define IN6_IS_ADDR_MC_ORGLOCAL(a) \
  244. (IN6_IS_ADDR_MULTICAST(a) && ((((u_int8_t *) (a))[1] & 0xf) == 0x8))
  245. #define IN6_IS_ADDR_MC_GLOBAL(a) \
  246. (IN6_IS_ADDR_MULTICAST(a) && ((((u_int8_t *) (a))[1] & 0xf) == 0xe))
  247. /* IPv6 packet information. */
  248. struct in6_pktinfo
  249. {
  250. struct in6_addr ipi6_addr; /* src/dst IPv6 address */
  251. unsigned int ipi6_ifindex; /* send/recv interface index */
  252. };
  253. __END_DECLS
  254. #endif /* netinet/in.h */