in.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #ifndef _NETINET_IN_H
  2. #define _NETINET_IN_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include <features.h>
  7. #include <inttypes.h>
  8. #include <sys/socket.h>
  9. typedef uint16_t in_port_t;
  10. typedef uint32_t in_addr_t;
  11. struct in_addr { in_addr_t s_addr; };
  12. struct sockaddr_in {
  13. sa_family_t sin_family;
  14. in_port_t sin_port;
  15. struct in_addr sin_addr;
  16. uint8_t sin_zero[8];
  17. };
  18. struct in6_addr {
  19. union {
  20. uint8_t __s6_addr[16];
  21. uint16_t __s6_addr16[8];
  22. uint32_t __s6_addr32[4];
  23. } __in6_union;
  24. };
  25. #define s6_addr __in6_union.__s6_addr
  26. #define s6_addr16 __in6_union.__s6_addr16
  27. #define s6_addr32 __in6_union.__s6_addr32
  28. struct sockaddr_in6 {
  29. sa_family_t sin6_family;
  30. in_port_t sin6_port;
  31. uint32_t sin6_flowinfo;
  32. struct in6_addr sin6_addr;
  33. uint32_t sin6_scope_id;
  34. };
  35. struct ipv6_mreq {
  36. struct in6_addr ipv6mr_multiaddr;
  37. unsigned ipv6mr_interface;
  38. };
  39. #define INADDR_ANY ((in_addr_t) 0x00000000)
  40. #define INADDR_BROADCAST ((in_addr_t) 0xffffffff)
  41. #define INADDR_NONE ((in_addr_t) 0xffffffff)
  42. #define INADDR_LOOPBACK ((in_addr_t) 0x7f000001)
  43. #define INADDR_UNSPEC_GROUP ((in_addr_t) 0xe0000000)
  44. #define INADDR_ALLHOSTS_GROUP ((in_addr_t) 0xe0000001)
  45. #define INADDR_ALLRTRS_GROUP ((in_addr_t) 0xe0000002)
  46. #define INADDR_MAX_LOCAL_GROUP ((in_addr_t) 0xe00000ff)
  47. #define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
  48. #define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
  49. extern const struct in6_addr in6addr_any, in6addr_loopback;
  50. #undef INET_ADDRSTRLEN
  51. #undef INET6_ADDRSTRLEN
  52. #define INET_ADDRSTRLEN 16
  53. #define INET6_ADDRSTRLEN 46
  54. uint32_t htonl(uint32_t);
  55. uint16_t htons(uint16_t);
  56. uint32_t ntohl(uint32_t);
  57. uint16_t ntohs(uint16_t);
  58. #define IPPROTO_IP 0
  59. #define IPPROTO_HOPOPTS 0
  60. #define IPPROTO_ICMP 1
  61. #define IPPROTO_IGMP 2
  62. #define IPPROTO_IPIP 4
  63. #define IPPROTO_TCP 6
  64. #define IPPROTO_EGP 8
  65. #define IPPROTO_PUP 12
  66. #define IPPROTO_UDP 17
  67. #define IPPROTO_IDP 22
  68. #define IPPROTO_TP 29
  69. #define IPPROTO_DCCP 33
  70. #define IPPROTO_IPV6 41
  71. #define IPPROTO_ROUTING 43
  72. #define IPPROTO_FRAGMENT 44
  73. #define IPPROTO_RSVP 46
  74. #define IPPROTO_GRE 47
  75. #define IPPROTO_ESP 50
  76. #define IPPROTO_AH 51
  77. #define IPPROTO_ICMPV6 58
  78. #define IPPROTO_NONE 59
  79. #define IPPROTO_DSTOPTS 60
  80. #define IPPROTO_MTP 92
  81. #define IPPROTO_BEETPH 94
  82. #define IPPROTO_ENCAP 98
  83. #define IPPROTO_PIM 103
  84. #define IPPROTO_COMP 108
  85. #define IPPROTO_SCTP 132
  86. #define IPPROTO_MH 135
  87. #define IPPROTO_UDPLITE 136
  88. #define IPPROTO_MPLS 137
  89. #define IPPROTO_RAW 255
  90. #define IPPROTO_MAX 256
  91. #define IN6_IS_ADDR_UNSPECIFIED(a) \
  92. (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
  93. ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
  94. #define IN6_IS_ADDR_LOOPBACK(a) \
  95. (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
  96. ((uint32_t *) (a))[2] == 0 && \
  97. ((uint8_t *) (a))[12] == 0 && ((uint8_t *) (a))[13] == 0 && \
  98. ((uint8_t *) (a))[14] == 0 && ((uint8_t *) (a))[15] == 1 )
  99. #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
  100. #define IN6_IS_ADDR_LINKLOCAL(a) \
  101. ((((uint8_t *) (a))[0]) == 0xfe && (((uint8_t *) (a))[1] & 0xc0) == 0x80)
  102. #define IN6_IS_ADDR_SITELOCAL(a) \
  103. ((((uint8_t *) (a))[0]) == 0xfe && (((uint8_t *) (a))[1] & 0xc0) == 0xc0)
  104. #define IN6_IS_ADDR_V4MAPPED(a) \
  105. (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
  106. ((uint8_t *) (a))[8] == 0 && ((uint8_t *) (a))[9] == 0 && \
  107. ((uint8_t *) (a))[10] == 0xff && ((uint8_t *) (a))[11] == 0xff)
  108. #define IN6_IS_ADDR_V4COMPAT(a) \
  109. (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
  110. ((uint32_t *) (a))[2] == 0 && ((uint8_t *) (a))[15] > 1)
  111. #define IN6_IS_ADDR_MC_NODELOCAL(a) \
  112. (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0x1))
  113. #define IN6_IS_ADDR_MC_LINKLOCAL(a) \
  114. (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0x2))
  115. #define IN6_IS_ADDR_MC_SITELOCAL(a) \
  116. (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0x5))
  117. #define IN6_IS_ADDR_MC_ORGLOCAL(a) \
  118. (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0x8))
  119. #define IN6_IS_ADDR_MC_GLOBAL(a) \
  120. (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0xe))
  121. #define __ARE_4_EQUAL(a,b) \
  122. (!( (0[a]-0[b]) | (1[a]-1[b]) | (2[a]-2[b]) | (3[a]-3[b]) ))
  123. #define IN6_ARE_ADDR_EQUAL(a,b) \
  124. __ARE_4_EQUAL((const uint32_t *)(a), (const uint32_t *)(b))
  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. #define IN_LOOPBACKNET 127
  144. #define IP_TOS 1
  145. #define IP_TTL 2
  146. #define IP_HDRINCL 3
  147. #define IP_OPTIONS 4
  148. #define IP_ROUTER_ALERT 5
  149. #define IP_RECVOPTS 6
  150. #define IP_RETOPTS 7
  151. #define IP_PKTINFO 8
  152. #define IP_PKTOPTIONS 9
  153. #define IP_PMTUDISC 10
  154. #define IP_MTU_DISCOVER 10
  155. #define IP_RECVERR 11
  156. #define IP_RECVTTL 12
  157. #define IP_RECVTOS 13
  158. #define IP_MTU 14
  159. #define IP_FREEBIND 15
  160. #define IP_IPSEC_POLICY 16
  161. #define IP_XFRM_POLICY 17
  162. #define IP_PASSSEC 18
  163. #define IP_TRANSPARENT 19
  164. #define IP_ORIGDSTADDR 20
  165. #define IP_RECVORIGDSTADDR IP_ORIGDSTADDR
  166. #define IP_MINTTL 21
  167. #define IP_NODEFRAG 22
  168. #define IP_CHECKSUM 23
  169. #define IP_BIND_ADDRESS_NO_PORT 24
  170. #define IP_MULTICAST_IF 32
  171. #define IP_MULTICAST_TTL 33
  172. #define IP_MULTICAST_LOOP 34
  173. #define IP_ADD_MEMBERSHIP 35
  174. #define IP_DROP_MEMBERSHIP 36
  175. #define IP_UNBLOCK_SOURCE 37
  176. #define IP_BLOCK_SOURCE 38
  177. #define IP_ADD_SOURCE_MEMBERSHIP 39
  178. #define IP_DROP_SOURCE_MEMBERSHIP 40
  179. #define IP_MSFILTER 41
  180. #define IP_MULTICAST_ALL 49
  181. #define IP_UNICAST_IF 50
  182. #define IP_RECVRETOPTS IP_RETOPTS
  183. #define IP_PMTUDISC_DONT 0
  184. #define IP_PMTUDISC_WANT 1
  185. #define IP_PMTUDISC_DO 2
  186. #define IP_PMTUDISC_PROBE 3
  187. #define IP_PMTUDISC_INTERFACE 4
  188. #define IP_PMTUDISC_OMIT 5
  189. #define IP_DEFAULT_MULTICAST_TTL 1
  190. #define IP_DEFAULT_MULTICAST_LOOP 1
  191. #define IP_MAX_MEMBERSHIPS 20
  192. struct ip_opts {
  193. struct in_addr ip_dst;
  194. char ip_opts[40];
  195. };
  196. #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
  197. #define MCAST_JOIN_GROUP 42
  198. #define MCAST_BLOCK_SOURCE 43
  199. #define MCAST_UNBLOCK_SOURCE 44
  200. #define MCAST_LEAVE_GROUP 45
  201. #define MCAST_JOIN_SOURCE_GROUP 46
  202. #define MCAST_LEAVE_SOURCE_GROUP 47
  203. #define MCAST_MSFILTER 48
  204. #define MCAST_EXCLUDE 0
  205. #define MCAST_INCLUDE 1
  206. struct ip_mreq {
  207. struct in_addr imr_multiaddr;
  208. struct in_addr imr_interface;
  209. };
  210. struct ip_mreqn {
  211. struct in_addr imr_multiaddr;
  212. struct in_addr imr_address;
  213. int imr_ifindex;
  214. };
  215. struct ip_mreq_source {
  216. struct in_addr imr_multiaddr;
  217. struct in_addr imr_interface;
  218. struct in_addr imr_sourceaddr;
  219. };
  220. struct ip_msfilter {
  221. struct in_addr imsf_multiaddr;
  222. struct in_addr imsf_interface;
  223. uint32_t imsf_fmode;
  224. uint32_t imsf_numsrc;
  225. struct in_addr imsf_slist[1];
  226. };
  227. #define IP_MSFILTER_SIZE(numsrc) \
  228. (sizeof(struct ip_msfilter) - sizeof(struct in_addr) \
  229. + (numsrc) * sizeof(struct in_addr))
  230. struct group_req {
  231. uint32_t gr_interface;
  232. struct sockaddr_storage gr_group;
  233. };
  234. struct group_source_req {
  235. uint32_t gsr_interface;
  236. struct sockaddr_storage gsr_group;
  237. struct sockaddr_storage gsr_source;
  238. };
  239. struct group_filter {
  240. uint32_t gf_interface;
  241. struct sockaddr_storage gf_group;
  242. uint32_t gf_fmode;
  243. uint32_t gf_numsrc;
  244. struct sockaddr_storage gf_slist[1];
  245. };
  246. #define GROUP_FILTER_SIZE(numsrc) \
  247. (sizeof(struct group_filter) - sizeof(struct sockaddr_storage) \
  248. + (numsrc) * sizeof(struct sockaddr_storage))
  249. struct in_pktinfo {
  250. int ipi_ifindex;
  251. struct in_addr ipi_spec_dst;
  252. struct in_addr ipi_addr;
  253. };
  254. struct in6_pktinfo {
  255. struct in6_addr ipi6_addr;
  256. unsigned ipi6_ifindex;
  257. };
  258. struct ip6_mtuinfo {
  259. struct sockaddr_in6 ip6m_addr;
  260. uint32_t ip6m_mtu;
  261. };
  262. #endif
  263. #define IPV6_ADDRFORM 1
  264. #define IPV6_2292PKTINFO 2
  265. #define IPV6_2292HOPOPTS 3
  266. #define IPV6_2292DSTOPTS 4
  267. #define IPV6_2292RTHDR 5
  268. #define IPV6_2292PKTOPTIONS 6
  269. #define IPV6_CHECKSUM 7
  270. #define IPV6_2292HOPLIMIT 8
  271. #define IPV6_NEXTHOP 9
  272. #define IPV6_AUTHHDR 10
  273. #define IPV6_UNICAST_HOPS 16
  274. #define IPV6_MULTICAST_IF 17
  275. #define IPV6_MULTICAST_HOPS 18
  276. #define IPV6_MULTICAST_LOOP 19
  277. #define IPV6_JOIN_GROUP 20
  278. #define IPV6_LEAVE_GROUP 21
  279. #define IPV6_ROUTER_ALERT 22
  280. #define IPV6_MTU_DISCOVER 23
  281. #define IPV6_MTU 24
  282. #define IPV6_RECVERR 25
  283. #define IPV6_V6ONLY 26
  284. #define IPV6_JOIN_ANYCAST 27
  285. #define IPV6_LEAVE_ANYCAST 28
  286. #define IPV6_IPSEC_POLICY 34
  287. #define IPV6_XFRM_POLICY 35
  288. #define IPV6_HDRINCL 36
  289. #define IPV6_RECVPKTINFO 49
  290. #define IPV6_PKTINFO 50
  291. #define IPV6_RECVHOPLIMIT 51
  292. #define IPV6_HOPLIMIT 52
  293. #define IPV6_RECVHOPOPTS 53
  294. #define IPV6_HOPOPTS 54
  295. #define IPV6_RTHDRDSTOPTS 55
  296. #define IPV6_RECVRTHDR 56
  297. #define IPV6_RTHDR 57
  298. #define IPV6_RECVDSTOPTS 58
  299. #define IPV6_DSTOPTS 59
  300. #define IPV6_RECVPATHMTU 60
  301. #define IPV6_PATHMTU 61
  302. #define IPV6_DONTFRAG 62
  303. #define IPV6_RECVTCLASS 66
  304. #define IPV6_TCLASS 67
  305. #define IPV6_AUTOFLOWLABEL 70
  306. #define IPV6_ADDR_PREFERENCES 72
  307. #define IPV6_MINHOPCOUNT 73
  308. #define IPV6_ORIGDSTADDR 74
  309. #define IPV6_RECVORIGDSTADDR IPV6_ORIGDSTADDR
  310. #define IPV6_TRANSPARENT 75
  311. #define IPV6_UNICAST_IF 76
  312. #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
  313. #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
  314. #define IPV6_RXHOPOPTS IPV6_HOPOPTS
  315. #define IPV6_RXDSTOPTS IPV6_DSTOPTS
  316. #define IPV6_PMTUDISC_DONT 0
  317. #define IPV6_PMTUDISC_WANT 1
  318. #define IPV6_PMTUDISC_DO 2
  319. #define IPV6_PMTUDISC_PROBE 3
  320. #define IPV6_PMTUDISC_INTERFACE 4
  321. #define IPV6_PMTUDISC_OMIT 5
  322. #define IPV6_PREFER_SRC_TMP 0x0001
  323. #define IPV6_PREFER_SRC_PUBLIC 0x0002
  324. #define IPV6_PREFER_SRC_PUBTMP_DEFAULT 0x0100
  325. #define IPV6_PREFER_SRC_COA 0x0004
  326. #define IPV6_PREFER_SRC_HOME 0x0400
  327. #define IPV6_PREFER_SRC_CGA 0x0008
  328. #define IPV6_PREFER_SRC_NONCGA 0x0800
  329. #define IPV6_RTHDR_LOOSE 0
  330. #define IPV6_RTHDR_STRICT 1
  331. #define IPV6_RTHDR_TYPE_0 0
  332. #ifdef __cplusplus
  333. }
  334. #endif
  335. #endif