in.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /* Copyright (C) 1991-2001, 2003, 2004 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 <sys/socket.h>
  20. #include <bits/types.h>
  21. __BEGIN_DECLS
  22. /* Standard well-defined IP protocols. */
  23. enum
  24. {
  25. IPPROTO_IP = 0, /* Dummy protocol for TCP. */
  26. #define IPPROTO_IP IPPROTO_IP
  27. IPPROTO_HOPOPTS = 0, /* IPv6 Hop-by-Hop options. */
  28. #define IPPROTO_HOPOPTS IPPROTO_HOPOPTS
  29. IPPROTO_ICMP = 1, /* Internet Control Message Protocol. */
  30. #define IPPROTO_ICMP IPPROTO_ICMP
  31. IPPROTO_IGMP = 2, /* Internet Group Management Protocol. */
  32. #define IPPROTO_IGMP IPPROTO_IGMP
  33. IPPROTO_IPIP = 4, /* IPIP tunnels (older KA9Q tunnels use 94). */
  34. #define IPPROTO_IPIP IPPROTO_IPIP
  35. IPPROTO_TCP = 6, /* Transmission Control Protocol. */
  36. #define IPPROTO_TCP IPPROTO_TCP
  37. IPPROTO_EGP = 8, /* Exterior Gateway Protocol. */
  38. #define IPPROTO_EGP IPPROTO_EGP
  39. IPPROTO_PUP = 12, /* PUP protocol. */
  40. #define IPPROTO_PUP IPPROTO_PUP
  41. IPPROTO_UDP = 17, /* User Datagram Protocol. */
  42. #define IPPROTO_UDP IPPROTO_UDP
  43. IPPROTO_IDP = 22, /* XNS IDP protocol. */
  44. #define IPPROTO_IDP IPPROTO_IDP
  45. IPPROTO_TP = 29, /* SO Transport Protocol Class 4. */
  46. #define IPPROTO_TP IPPROTO_TP
  47. IPPROTO_IPV6 = 41, /* IPv6 header. */
  48. #define IPPROTO_IPV6 IPPROTO_IPV6
  49. IPPROTO_ROUTING = 43, /* IPv6 routing header. */
  50. #define IPPROTO_ROUTING IPPROTO_ROUTING
  51. IPPROTO_FRAGMENT = 44, /* IPv6 fragmentation header. */
  52. #define IPPROTO_FRAGMENT IPPROTO_FRAGMENT
  53. IPPROTO_RSVP = 46, /* Reservation Protocol. */
  54. #define IPPROTO_RSVP IPPROTO_RSVP
  55. IPPROTO_GRE = 47, /* General Routing Encapsulation. */
  56. #define IPPROTO_GRE IPPROTO_GRE
  57. IPPROTO_ESP = 50, /* encapsulating security payload. */
  58. #define IPPROTO_ESP IPPROTO_ESP
  59. IPPROTO_AH = 51, /* authentication header. */
  60. #define IPPROTO_AH IPPROTO_AH
  61. IPPROTO_ICMPV6 = 58, /* ICMPv6. */
  62. #define IPPROTO_ICMPV6 IPPROTO_ICMPV6
  63. IPPROTO_NONE = 59, /* IPv6 no next header. */
  64. #define IPPROTO_NONE IPPROTO_NONE
  65. IPPROTO_DSTOPTS = 60, /* IPv6 destination options. */
  66. #define IPPROTO_DSTOPTS IPPROTO_DSTOPTS
  67. IPPROTO_MTP = 92, /* Multicast Transport Protocol. */
  68. #define IPPROTO_MTP IPPROTO_MTP
  69. IPPROTO_ENCAP = 98, /* Encapsulation Header. */
  70. #define IPPROTO_ENCAP IPPROTO_ENCAP
  71. IPPROTO_PIM = 103, /* Protocol Independent Multicast. */
  72. #define IPPROTO_PIM IPPROTO_PIM
  73. IPPROTO_COMP = 108, /* Compression Header Protocol. */
  74. #define IPPROTO_COMP IPPROTO_COMP
  75. IPPROTO_SCTP = 132, /* Stream Control Transmission Protocol. */
  76. #define IPPROTO_SCTP IPPROTO_SCTP
  77. IPPROTO_RAW = 255, /* Raw IP packets. */
  78. #define IPPROTO_RAW IPPROTO_RAW
  79. IPPROTO_MAX
  80. };
  81. /* Type to represent a port. */
  82. typedef uint16_t in_port_t;
  83. /* Standard well-known ports. */
  84. enum
  85. {
  86. IPPORT_ECHO = 7, /* Echo service. */
  87. IPPORT_DISCARD = 9, /* Discard transmissions service. */
  88. IPPORT_SYSTAT = 11, /* System status service. */
  89. IPPORT_DAYTIME = 13, /* Time of day service. */
  90. IPPORT_NETSTAT = 15, /* Network status service. */
  91. IPPORT_FTP = 21, /* File Transfer Protocol. */
  92. IPPORT_TELNET = 23, /* Telnet protocol. */
  93. IPPORT_SMTP = 25, /* Simple Mail Transfer Protocol. */
  94. IPPORT_TIMESERVER = 37, /* Timeserver service. */
  95. IPPORT_NAMESERVER = 42, /* Domain Name Service. */
  96. IPPORT_WHOIS = 43, /* Internet Whois service. */
  97. IPPORT_MTP = 57,
  98. IPPORT_TFTP = 69, /* Trivial File Transfer Protocol. */
  99. IPPORT_RJE = 77,
  100. IPPORT_FINGER = 79, /* Finger service. */
  101. IPPORT_TTYLINK = 87,
  102. IPPORT_SUPDUP = 95, /* SUPDUP protocol. */
  103. IPPORT_EXECSERVER = 512, /* execd service. */
  104. IPPORT_LOGINSERVER = 513, /* rlogind service. */
  105. IPPORT_CMDSERVER = 514,
  106. IPPORT_EFSSERVER = 520,
  107. /* UDP ports. */
  108. IPPORT_BIFFUDP = 512,
  109. IPPORT_WHOSERVER = 513,
  110. IPPORT_ROUTESERVER = 520,
  111. /* Ports less than this value are reserved for privileged processes. */
  112. IPPORT_RESERVED = 1024,
  113. /* Ports greater this value are reserved for (non-privileged) servers. */
  114. IPPORT_USERRESERVED = 5000
  115. };
  116. /* Internet address. */
  117. typedef uint32_t in_addr_t;
  118. struct in_addr
  119. {
  120. in_addr_t s_addr;
  121. };
  122. /* Definitions of the bits in an Internet address integer.
  123. On subnets, host and network parts are found according to
  124. the subnet mask, not these masks. */
  125. #define IN_CLASSA(a) ((((in_addr_t)(a)) & 0x80000000) == 0)
  126. #define IN_CLASSA_NET 0xff000000
  127. #define IN_CLASSA_NSHIFT 24
  128. #define IN_CLASSA_HOST (0xffffffff & ~IN_CLASSA_NET)
  129. #define IN_CLASSA_MAX 128
  130. #define IN_CLASSB(a) ((((in_addr_t)(a)) & 0xc0000000) == 0x80000000)
  131. #define IN_CLASSB_NET 0xffff0000
  132. #define IN_CLASSB_NSHIFT 16
  133. #define IN_CLASSB_HOST (0xffffffff & ~IN_CLASSB_NET)
  134. #define IN_CLASSB_MAX 65536
  135. #define IN_CLASSC(a) ((((in_addr_t)(a)) & 0xe0000000) == 0xc0000000)
  136. #define IN_CLASSC_NET 0xffffff00
  137. #define IN_CLASSC_NSHIFT 8
  138. #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)
  141. #define IN_EXPERIMENTAL(a) ((((in_addr_t)(a)) & 0xe0000000) == 0xe0000000)
  142. #define IN_BADCLASS(a) ((((in_addr_t)(a)) & 0xf0000000) == 0xf0000000)
  143. /* Address to accept any incoming messages. */
  144. #define INADDR_ANY ((in_addr_t) 0x00000000)
  145. /* Address to send to all hosts. */
  146. #define INADDR_BROADCAST ((in_addr_t) 0xffffffff)
  147. /* Address indicating an error return. */
  148. #define INADDR_NONE ((in_addr_t) 0xffffffff)
  149. /* Network number for local host loopback. */
  150. #define IN_LOOPBACKNET 127
  151. /* Address to loopback in software to local host. */
  152. #ifndef INADDR_LOOPBACK
  153. # define INADDR_LOOPBACK ((in_addr_t) 0x7f000001) /* Inet 127.0.0.1. */
  154. #endif
  155. /* Defines for Multicast INADDR. */
  156. #define INADDR_UNSPEC_GROUP ((in_addr_t) 0xe0000000) /* 224.0.0.0 */
  157. #define INADDR_ALLHOSTS_GROUP ((in_addr_t) 0xe0000001) /* 224.0.0.1 */
  158. #define INADDR_ALLRTRS_GROUP ((in_addr_t) 0xe0000002) /* 224.0.0.2 */
  159. #define INADDR_MAX_LOCAL_GROUP ((in_addr_t) 0xe00000ff) /* 224.0.0.255 */
  160. /* IPv6 address */
  161. struct in6_addr
  162. {
  163. union
  164. {
  165. uint8_t u6_addr8[16];
  166. uint16_t u6_addr16[8];
  167. uint32_t u6_addr32[4];
  168. } in6_u;
  169. #define s6_addr in6_u.u6_addr8
  170. #define s6_addr16 in6_u.u6_addr16
  171. #define s6_addr32 in6_u.u6_addr32
  172. };
  173. extern const struct in6_addr in6addr_any; /* :: */
  174. extern const struct in6_addr in6addr_loopback; /* ::1 */
  175. #define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
  176. #define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
  177. #define INET_ADDRSTRLEN 16
  178. #define INET6_ADDRSTRLEN 46
  179. /* Get the definition of the macro to define the common sockaddr members. */
  180. #include <bits/socket.h>
  181. /* Structure describing an Internet socket address. */
  182. struct sockaddr_in
  183. {
  184. __SOCKADDR_COMMON (sin_);
  185. in_port_t sin_port; /* Port number. */
  186. struct in_addr sin_addr; /* Internet address. */
  187. /* Pad to size of `struct sockaddr'. */
  188. unsigned char sin_zero[sizeof (struct sockaddr) -
  189. __SOCKADDR_COMMON_SIZE -
  190. sizeof (in_port_t) -
  191. sizeof (struct in_addr)];
  192. };
  193. /* Ditto, for IPv6. */
  194. struct sockaddr_in6
  195. {
  196. __SOCKADDR_COMMON (sin6_);
  197. in_port_t sin6_port; /* Transport layer port # */
  198. uint32_t sin6_flowinfo; /* IPv6 flow information */
  199. struct in6_addr sin6_addr; /* IPv6 address */
  200. uint32_t sin6_scope_id; /* IPv6 scope-id */
  201. };
  202. /* IPv4 multicast request. */
  203. struct ip_mreq
  204. {
  205. /* IP multicast address of group. */
  206. struct in_addr imr_multiaddr;
  207. /* Local IP address of interface. */
  208. struct in_addr imr_interface;
  209. };
  210. struct ip_mreq_source
  211. {
  212. /* IP multicast address of group. */
  213. struct in_addr imr_multiaddr;
  214. /* IP address of source. */
  215. struct in_addr imr_interface;
  216. /* IP address of interface. */
  217. struct in_addr imr_sourceaddr;
  218. };
  219. /* Likewise, for IPv6. */
  220. struct ipv6_mreq
  221. {
  222. /* IPv6 multicast address of group */
  223. struct in6_addr ipv6mr_multiaddr;
  224. /* local interface */
  225. unsigned int ipv6mr_interface;
  226. };
  227. /* Multicast group request. */
  228. struct group_req
  229. {
  230. /* Interface index. */
  231. uint32_t gr_interface;
  232. /* Group address. */
  233. struct sockaddr_storage gr_group;
  234. };
  235. struct group_source_req
  236. {
  237. /* Interface index. */
  238. uint32_t gsr_interface;
  239. /* Group address. */
  240. struct sockaddr_storage gsr_group;
  241. /* Source address. */
  242. struct sockaddr_storage gsr_source;
  243. };
  244. /* Full-state filter operations. */
  245. struct ip_msfilter
  246. {
  247. /* IP multicast address of group. */
  248. struct in_addr imsf_multiaddr;
  249. /* Local IP address of interface. */
  250. struct in_addr imsf_interface;
  251. /* Filter mode. */
  252. uint32_t imsf_fmode;
  253. /* Number of source addresses. */
  254. uint32_t imsf_numsrc;
  255. /* Source addresses. */
  256. struct in_addr imsf_slist[1];
  257. };
  258. #define IP_MSFILTER_SIZE(numsrc) (sizeof (struct ip_msfilter) \
  259. - sizeof (struct in_addr) \
  260. + (numsrc) * sizeof (struct in_addr))
  261. struct group_filter
  262. {
  263. /* Interface index. */
  264. uint32_t gf_interface;
  265. /* Group address. */
  266. struct sockaddr_storage gf_group;
  267. /* Filter mode. */
  268. uint32_t gf_fmode;
  269. /* Number of source addresses. */
  270. uint32_t gf_numsrc;
  271. /* Source addresses. */
  272. struct sockaddr_storage gf_slist[1];
  273. };
  274. #define GROUP_FILTER_SIZE(numsrc) (sizeof (struct group_filter) \
  275. - sizeof (struct sockaddr_storage) \
  276. + ((numsrc) \
  277. * sizeof (struct sockaddr_storage)))
  278. /* Get system-specific definitions. */
  279. #include <bits/in.h>
  280. /* Functions to convert between host and network byte order.
  281. Please note that these functions normally take `unsigned long int' or
  282. `unsigned short int' values as arguments and also return them. But
  283. this was a short-sighted decision since on different systems the types
  284. may have different representations but the values are always the same. */
  285. extern uint32_t ntohl (uint32_t __netlong) __THROW __attribute__ ((__const__));
  286. extern uint16_t ntohs (uint16_t __netshort)
  287. __THROW __attribute__ ((__const__));
  288. extern uint32_t htonl (uint32_t __hostlong)
  289. __THROW __attribute__ ((__const__));
  290. extern uint16_t htons (uint16_t __hostshort)
  291. __THROW __attribute__ ((__const__));
  292. #include <endian.h>
  293. /* Get machine dependent optimized versions of byte swapping functions. */
  294. #include <bits/byteswap.h>
  295. #ifdef __OPTIMIZE__
  296. /* We can optimize calls to the conversion functions. Either nothing has
  297. to be done or we are using directly the byte-swapping functions which
  298. often can be inlined. */
  299. # if __BYTE_ORDER == __BIG_ENDIAN
  300. /* The host byte order is the same as network byte order,
  301. so these functions are all just identity. */
  302. # define ntohl(x) (x)
  303. # define ntohs(x) (x)
  304. # define htonl(x) (x)
  305. # define htons(x) (x)
  306. # else
  307. # if __BYTE_ORDER == __LITTLE_ENDIAN
  308. # define ntohl(x) __bswap_32 (x)
  309. # define ntohs(x) __bswap_16 (x)
  310. # define htonl(x) __bswap_32 (x)
  311. # define htons(x) __bswap_16 (x)
  312. # endif
  313. # endif
  314. #endif
  315. #define IN6_IS_ADDR_UNSPECIFIED(a) \
  316. (((__const uint32_t *) (a))[0] == 0 \
  317. && ((__const uint32_t *) (a))[1] == 0 \
  318. && ((__const uint32_t *) (a))[2] == 0 \
  319. && ((__const uint32_t *) (a))[3] == 0)
  320. #define IN6_IS_ADDR_LOOPBACK(a) \
  321. (((__const uint32_t *) (a))[0] == 0 \
  322. && ((__const uint32_t *) (a))[1] == 0 \
  323. && ((__const uint32_t *) (a))[2] == 0 \
  324. && ((__const uint32_t *) (a))[3] == htonl (1))
  325. #define IN6_IS_ADDR_MULTICAST(a) (((__const uint8_t *) (a))[0] == 0xff)
  326. #define IN6_IS_ADDR_LINKLOCAL(a) \
  327. ((((__const uint32_t *) (a))[0] & htonl (0xffc00000)) \
  328. == htonl (0xfe800000))
  329. #define IN6_IS_ADDR_SITELOCAL(a) \
  330. ((((__const uint32_t *) (a))[0] & htonl (0xffc00000)) \
  331. == htonl (0xfec00000))
  332. #define IN6_IS_ADDR_V4MAPPED(a) \
  333. ((((__const uint32_t *) (a))[0] == 0) \
  334. && (((__const uint32_t *) (a))[1] == 0) \
  335. && (((__const uint32_t *) (a))[2] == htonl (0xffff)))
  336. #define IN6_IS_ADDR_V4COMPAT(a) \
  337. ((((__const uint32_t *) (a))[0] == 0) \
  338. && (((__const uint32_t *) (a))[1] == 0) \
  339. && (((__const uint32_t *) (a))[2] == 0) \
  340. && (ntohl (((__const uint32_t *) (a))[3]) > 1))
  341. #define IN6_ARE_ADDR_EQUAL(a,b) \
  342. ((((__const uint32_t *) (a))[0] == ((__const uint32_t *) (b))[0]) \
  343. && (((__const uint32_t *) (a))[1] == ((__const uint32_t *) (b))[1]) \
  344. && (((__const uint32_t *) (a))[2] == ((__const uint32_t *) (b))[2]) \
  345. && (((__const uint32_t *) (a))[3] == ((__const uint32_t *) (b))[3]))
  346. /* Bind socket to a privileged IP port. */
  347. extern int bindresvport (int __sockfd, struct sockaddr_in *__sock_in) __THROW;
  348. /* The IPv6 version of this function. */
  349. extern int bindresvport6 (int __sockfd, struct sockaddr_in6 *__sock_in)
  350. __THROW;
  351. #define IN6_IS_ADDR_MC_NODELOCAL(a) \
  352. (IN6_IS_ADDR_MULTICAST(a) \
  353. && ((((__const uint8_t *) (a))[1] & 0xf) == 0x1))
  354. #define IN6_IS_ADDR_MC_LINKLOCAL(a) \
  355. (IN6_IS_ADDR_MULTICAST(a) \
  356. && ((((__const uint8_t *) (a))[1] & 0xf) == 0x2))
  357. #define IN6_IS_ADDR_MC_SITELOCAL(a) \
  358. (IN6_IS_ADDR_MULTICAST(a) \
  359. && ((((__const uint8_t *) (a))[1] & 0xf) == 0x5))
  360. #define IN6_IS_ADDR_MC_ORGLOCAL(a) \
  361. (IN6_IS_ADDR_MULTICAST(a) \
  362. && ((((__const uint8_t *) (a))[1] & 0xf) == 0x8))
  363. #define IN6_IS_ADDR_MC_GLOBAL(a) \
  364. (IN6_IS_ADDR_MULTICAST(a) \
  365. && ((((__const uint8_t *) (a))[1] & 0xf) == 0xe))
  366. /* IPv6 packet information. */
  367. struct in6_pktinfo
  368. {
  369. struct in6_addr ipi6_addr; /* src/dst IPv6 address */
  370. unsigned int ipi6_ifindex; /* send/recv interface index */
  371. };
  372. #if 0 /*def __USE_GNU*/
  373. /* Hop-by-Hop and Destination Options Processing. */
  374. extern int inet6_option_space (int __nbytes) __THROW;
  375. extern int inet6_option_init (void *__bp, struct cmsghdr **__cmsgp,
  376. int __type) __THROW;
  377. extern int inet6_option_append (struct cmsghdr *__cmsg,
  378. __const uint8_t *__typep, int __multx,
  379. int __plusy) __THROW;
  380. extern uint8_t *inet6_option_alloc (struct cmsghdr *__cmsg, int __datalen,
  381. int __multx, int __plusy) __THROW;
  382. extern int inet6_option_next (__const struct cmsghdr *__cmsg,
  383. uint8_t **__tptrp) __THROW;
  384. extern int inet6_option_find (__const struct cmsghdr *__cmsg,
  385. uint8_t **__tptrp, int __type) __THROW;
  386. /* Multicast source filter support. */
  387. /* Get IPv4 source filter. */
  388. extern int getipv4sourcefilter (int __s, struct in_addr __interface_addr,
  389. struct in_addr __group, uint32_t *__fmode,
  390. uint32_t *__numsrc, struct in_addr *__slist)
  391. __THROW;
  392. /* Set IPv4 source filter. */
  393. extern int setipv4sourcefilter (int __s, struct in_addr __interface_addr,
  394. struct in_addr __group, uint32_t __fmode,
  395. uint32_t __numsrc,
  396. __const struct in_addr *__slist)
  397. __THROW;
  398. /* Get source filter. */
  399. extern int getsourcefilter (int __s, uint32_t __interface_addr,
  400. __const struct sockaddr *__group,
  401. socklen_t __grouplen, uint32_t *__fmode,
  402. uint32_t *__numsrc,
  403. struct sockaddr_storage *__slist) __THROW;
  404. /* Set source filter. */
  405. extern int setsourcefilter (int __s, uint32_t __interface_addr,
  406. __const struct sockaddr *__group,
  407. socklen_t __grouplen, uint32_t __fmode,
  408. uint32_t __numsrc,
  409. __const struct sockaddr_storage *__slist) __THROW;
  410. #endif /* use GNU */
  411. __END_DECLS
  412. #endif /* netinet/in.h */