10b6361efSAxel Dörfler /* 2*569791beSAugustin Cavalier * Copyright 2002-2019, Haiku, Inc. All rights reserved. 30b6361efSAxel Dörfler * Distributed under the terms of the MIT License. 40b6361efSAxel Dörfler */ 552a38012Sejakowatz #ifndef _NETINET_IN_H_ 652a38012Sejakowatz #define _NETINET_IN_H_ 752a38012Sejakowatz 80b6361efSAxel Dörfler 94fc67516SDavid Reid #include <net/if.h> 1042415555Sbeveloper #include <endian.h> 115adca30aSAxel Dörfler #include <stdint.h> 120b6361efSAxel Dörfler #include <sys/types.h> 1352a38012Sejakowatz 1475f1f9e0SHugo Santos /* RFC 2553 states that these must be available through <netinet/in.h> */ 1575f1f9e0SHugo Santos #include <netinet6/in6.h> 1675f1f9e0SHugo Santos 170b6361efSAxel Dörfler 187d31ea94SPhilippe Houdoin #ifdef __cplusplus 197d31ea94SPhilippe Houdoin extern "C" { 207d31ea94SPhilippe Houdoin #endif 217d31ea94SPhilippe Houdoin 225adca30aSAxel Dörfler typedef uint16_t in_port_t; 235adca30aSAxel Dörfler typedef uint32_t in_addr_t; 2442415555Sbeveloper 259cb15d72SAugustin Cavalier /* We can't include <ByteOrder.h> since we are a POSIX file, 269cb15d72SAugustin Cavalier * and we are not allowed to import all the BeOS types here. */ 279cb15d72SAugustin Cavalier #if __GNUC__ >= 4 288e309168SAugustin Cavalier # define __net_swap_int32(arg) (uint32_t)__builtin_bswap32(arg) 298e309168SAugustin Cavalier # define __net_swap_int16(arg) (uint16_t)__builtin_bswap16(arg) 303fed1a15SAlex Smith #else 319cb15d72SAugustin Cavalier extern unsigned long __swap_int32(unsigned long); /* private */ 325adca30aSAxel Dörfler extern uint16_t __swap_int16(uint16_t); /* private */ 338e309168SAugustin Cavalier # define __net_swap_int32(arg) __swap_int32(arg) 348e309168SAugustin Cavalier # define __net_swap_int16(arg) __swap_int16(arg) 359cb15d72SAugustin Cavalier #endif 369cb15d72SAugustin Cavalier 379cb15d72SAugustin Cavalier #ifndef htonl 3842415555Sbeveloper # if BYTE_ORDER == LITTLE_ENDIAN 398e309168SAugustin Cavalier # define htonl(x) ((uint32_t)__net_swap_int32(x)) 408e309168SAugustin Cavalier # define ntohl(x) ((uint32_t)__net_swap_int32(x)) 418e309168SAugustin Cavalier # define htons(x) __net_swap_int16(x) 428e309168SAugustin Cavalier # define ntohs(x) __net_swap_int16(x) 4342415555Sbeveloper # elif BYTE_ORDER == BIG_ENDIAN 4442415555Sbeveloper # define htonl(x) (x) 4542415555Sbeveloper # define ntohl(x) (x) 4642415555Sbeveloper # define htons(x) (x) 4742415555Sbeveloper # define ntohs(x) (x) 4842415555Sbeveloper # else 4942415555Sbeveloper # error Unknown byte order. 5042415555Sbeveloper # endif 5142415555Sbeveloper #endif 5242415555Sbeveloper 530b5170a4Sbeveloper 540b6361efSAxel Dörfler /* Protocol definitions */ 556181d22fSFrançois Revol #define IPPROTO_IP 0 /* 0, IPv4 */ 56ca83261dSAxel Dörfler #define IPPROTO_HOPOPTS 0 /* 0, IPv6 hop-by-hop options */ 576181d22fSFrançois Revol #define IPPROTO_ICMP 1 /* 1, ICMP (v4) */ 586181d22fSFrançois Revol #define IPPROTO_IGMP 2 /* 2, IGMP (group management) */ 596181d22fSFrançois Revol #define IPPROTO_TCP 6 /* 6, tcp */ 606181d22fSFrançois Revol #define IPPROTO_UDP 17 /* 17, UDP */ 616181d22fSFrançois Revol #define IPPROTO_IPV6 41 /* 41, IPv6 in IPv6 */ 626181d22fSFrançois Revol #define IPPROTO_ROUTING 43 /* 43, Routing */ 63ca83261dSAxel Dörfler #define IPPROTO_FRAGMENT 44 /* 44, IPv6 fragmentation header */ 64ca83261dSAxel Dörfler #define IPPROTO_ESP 50 /* 50, Encap Sec. Payload */ 65ca83261dSAxel Dörfler #define IPPROTO_AH 51 /* 51, Auth Header */ 666181d22fSFrançois Revol #define IPPROTO_ICMPV6 58 /* 58, IPv6 ICMP */ 67ca83261dSAxel Dörfler #define IPPROTO_NONE 59 /* 59, IPv6 no next header */ 68ca83261dSAxel Dörfler #define IPPROTO_DSTOPTS 60 /* 60, IPv6 destination option */ 696181d22fSFrançois Revol #define IPPROTO_ETHERIP 97 /* 97, Ethernet in IPv4 */ 706181d22fSFrançois Revol #define IPPROTO_RAW 255 /* 255 */ 7152a38012Sejakowatz 7252a38012Sejakowatz #define IPPROTO_MAX 256 7352a38012Sejakowatz 740b6361efSAxel Dörfler 750b6361efSAxel Dörfler /* Port numbers */ 7652a38012Sejakowatz 7752a38012Sejakowatz #define IPPORT_RESERVED 1024 780b6361efSAxel Dörfler /* < IPPORT_RESERVED are privileged and should be accessible only by root */ 7952a38012Sejakowatz #define IPPORT_USERRESERVED 49151 800b6361efSAxel Dörfler /* > IPPORT_USERRESERVED are reserved for servers, though not requiring 810b6361efSAxel Dörfler * privileged status 8252a38012Sejakowatz */ 830b6361efSAxel Dörfler 840b6361efSAxel Dörfler /* IP Version 4 address */ 8552a38012Sejakowatz struct in_addr { 8652a38012Sejakowatz in_addr_t s_addr; 8752a38012Sejakowatz }; 8852a38012Sejakowatz 890b6361efSAxel Dörfler /* IP Version 4 socket address */ 9052a38012Sejakowatz struct sockaddr_in { 915adca30aSAxel Dörfler uint8_t sin_len; 925adca30aSAxel Dörfler uint8_t sin_family; 935adca30aSAxel Dörfler uint16_t sin_port; 9452a38012Sejakowatz struct in_addr sin_addr; 955adca30aSAxel Dörfler int8_t sin_zero[24]; 9652a38012Sejakowatz }; 970b6361efSAxel Dörfler 9852a38012Sejakowatz 9946527f68SHugo Santos /* RFC 3678 - Socket Interface Extensions for Multicast Source Filters */ 10046527f68SHugo Santos 10146527f68SHugo Santos struct ip_mreq { 10246527f68SHugo Santos struct in_addr imr_multiaddr; /* IP address of group */ 10346527f68SHugo Santos struct in_addr imr_interface; /* IP address of interface */ 10446527f68SHugo Santos }; 10546527f68SHugo Santos 10646527f68SHugo Santos struct ip_mreq_source { 10746527f68SHugo Santos struct in_addr imr_multiaddr; /* IP address of group */ 10846527f68SHugo Santos struct in_addr imr_sourceaddr; /* IP address of source */ 10946527f68SHugo Santos struct in_addr imr_interface; /* IP address of interface */ 11046527f68SHugo Santos }; 11146527f68SHugo Santos 11246527f68SHugo Santos struct group_req { 11346527f68SHugo Santos uint32_t gr_interface; /* interface index */ 11446527f68SHugo Santos struct sockaddr_storage gr_group; /* group address */ 11546527f68SHugo Santos }; 11646527f68SHugo Santos 11746527f68SHugo Santos struct group_source_req { 11846527f68SHugo Santos uint32_t gsr_interface; /* interface index */ 11946527f68SHugo Santos struct sockaddr_storage gsr_group; /* group address */ 12046527f68SHugo Santos struct sockaddr_storage gsr_source; /* source address */ 12146527f68SHugo Santos }; 12246527f68SHugo Santos 12352a38012Sejakowatz /* 12452a38012Sejakowatz * Options for use with [gs]etsockopt at the IP level. 12552a38012Sejakowatz * First word of comment is data type; bool is stored in int. 12652a38012Sejakowatz */ 12752a38012Sejakowatz #define IP_OPTIONS 1 /* buf/ip_opts; set/get IP options */ 12852a38012Sejakowatz #define IP_HDRINCL 2 /* int; header is included with data */ 1290b6361efSAxel Dörfler #define IP_TOS 3 1300b6361efSAxel Dörfler /* int; IP type of service and preced. */ 13152a38012Sejakowatz #define IP_TTL 4 /* int; IP time to live */ 13252a38012Sejakowatz #define IP_RECVOPTS 5 /* bool; receive all IP opts w/dgram */ 13352a38012Sejakowatz #define IP_RECVRETOPTS 6 /* bool; receive IP opts for response */ 13452a38012Sejakowatz #define IP_RECVDSTADDR 7 /* bool; receive IP dst addr w/dgram */ 13552a38012Sejakowatz #define IP_RETOPTS 8 /* ip_opts; set/get IP options */ 13652a38012Sejakowatz #define IP_MULTICAST_IF 9 /* in_addr; set/get IP multicast i/f */ 13752a38012Sejakowatz #define IP_MULTICAST_TTL 10 /* u_char; set/get IP multicast ttl */ 1380b6361efSAxel Dörfler #define IP_MULTICAST_LOOP 11 1390b6361efSAxel Dörfler /* u_char; set/get IP multicast loopback */ 1400b6361efSAxel Dörfler #define IP_ADD_MEMBERSHIP 12 1410b6361efSAxel Dörfler /* ip_mreq; add an IP group membership */ 1420b6361efSAxel Dörfler #define IP_DROP_MEMBERSHIP 13 1430b6361efSAxel Dörfler /* ip_mreq; drop an IP group membership */ 14446527f68SHugo Santos #define IP_BLOCK_SOURCE 14 /* ip_mreq_source */ 14546527f68SHugo Santos #define IP_UNBLOCK_SOURCE 15 /* ip_mreq_source */ 14646527f68SHugo Santos #define IP_ADD_SOURCE_MEMBERSHIP 16 /* ip_mreq_source */ 14746527f68SHugo Santos #define IP_DROP_SOURCE_MEMBERSHIP 17 /* ip_mreq_source */ 14846527f68SHugo Santos #define MCAST_JOIN_GROUP 18 /* group_req */ 14946527f68SHugo Santos #define MCAST_BLOCK_SOURCE 19 /* group_source_req */ 15046527f68SHugo Santos #define MCAST_UNBLOCK_SOURCE 20 /* group_source_req */ 15146527f68SHugo Santos #define MCAST_LEAVE_GROUP 21 /* group_req */ 15246527f68SHugo Santos #define MCAST_JOIN_SOURCE_GROUP 22 /* group_source_req */ 15346527f68SHugo Santos #define MCAST_LEAVE_SOURCE_GROUP 23 /* group_source_req */ 15452a38012Sejakowatz 1553d019c5fSAxel Dörfler /* IPPROTO_IPV6 options */ 15675f1f9e0SHugo Santos #define IPV6_MULTICAST_IF 24 /* int */ 15775f1f9e0SHugo Santos #define IPV6_MULTICAST_HOPS 25 /* int */ 15875f1f9e0SHugo Santos #define IPV6_MULTICAST_LOOP 26 /* int */ 15975f1f9e0SHugo Santos 1603d019c5fSAxel Dörfler #define IPV6_UNICAST_HOPS 27 /* int */ 1613d019c5fSAxel Dörfler #define IPV6_JOIN_GROUP 28 /* struct ipv6_mreq */ 1623d019c5fSAxel Dörfler #define IPV6_LEAVE_GROUP 29 /* struct ipv6_mreq */ 1633d019c5fSAxel Dörfler #define IPV6_V6ONLY 30 /* int */ 1643d019c5fSAxel Dörfler 165ca83261dSAxel Dörfler #define IPV6_PKTINFO 31 /* struct ipv6_pktinfo */ 166ca83261dSAxel Dörfler #define IPV6_RECVPKTINFO 32 /* struct ipv6_pktinfo */ 167ca83261dSAxel Dörfler #define IPV6_HOPLIMIT 33 /* int */ 168ca83261dSAxel Dörfler #define IPV6_RECVHOPLIMIT 34 /* int */ 169ca83261dSAxel Dörfler 170ca83261dSAxel Dörfler #define IPV6_HOPOPTS 35 /* struct ip6_hbh */ 171ca83261dSAxel Dörfler #define IPV6_DSTOPTS 36 /* struct ip6_dest */ 172ca83261dSAxel Dörfler #define IPV6_RTHDR 37 /* struct ip6_rthdr */ 173ca83261dSAxel Dörfler 174a8712463SAxel Dörfler #define INADDR_ANY ((in_addr_t)0x00000000) 175a8712463SAxel Dörfler #define INADDR_LOOPBACK ((in_addr_t)0x7f000001) 176a8712463SAxel Dörfler #define INADDR_BROADCAST ((in_addr_t)0xffffffff) /* must be masked */ 17752a38012Sejakowatz 178a8712463SAxel Dörfler #define INADDR_UNSPEC_GROUP ((in_addr_t)0xe0000000) /* 224.0.0.0 */ 179a8712463SAxel Dörfler #define INADDR_ALLHOSTS_GROUP ((in_addr_t)0xe0000001) /* 224.0.0.1 */ 180a8712463SAxel Dörfler #define INADDR_ALLROUTERS_GROUP ((in_addr_t)0xe0000002) /* 224.0.0.2 */ 181a8712463SAxel Dörfler #define INADDR_MAX_LOCAL_GROUP ((in_addr_t)0xe00000ff) /* 224.0.0.255 */ 18252a38012Sejakowatz 183a8712463SAxel Dörfler #define IN_LOOPBACKNET 127 18452a38012Sejakowatz 185a8712463SAxel Dörfler #define INADDR_NONE ((in_addr_t)0xffffffff) 18652a38012Sejakowatz 187a8712463SAxel Dörfler #define IN_CLASSA(i) (((in_addr_t)(i) & 0x80000000) == 0) 188a8712463SAxel Dörfler #define IN_CLASSA_NET 0xff000000 18952a38012Sejakowatz #define IN_CLASSA_NSHIFT 24 190a8712463SAxel Dörfler #define IN_CLASSA_HOST 0x00ffffff 19152a38012Sejakowatz #define IN_CLASSA_MAX 128 19252a38012Sejakowatz 193a8712463SAxel Dörfler #define IN_CLASSB(i) (((in_addr_t)(i) & 0xc0000000) == 0x80000000) 194a8712463SAxel Dörfler #define IN_CLASSB_NET 0xffff0000 19552a38012Sejakowatz #define IN_CLASSB_NSHIFT 16 196a8712463SAxel Dörfler #define IN_CLASSB_HOST 0x0000ffff 19752a38012Sejakowatz #define IN_CLASSB_MAX 65536 19852a38012Sejakowatz 199a8712463SAxel Dörfler #define IN_CLASSC(i) (((in_addr_t)(i) & 0xe0000000) == 0xc0000000) 200a8712463SAxel Dörfler #define IN_CLASSC_NET 0xffffff00 20152a38012Sejakowatz #define IN_CLASSC_NSHIFT 8 202a8712463SAxel Dörfler #define IN_CLASSC_HOST 0x000000ff 20352a38012Sejakowatz 204a8712463SAxel Dörfler #define IN_CLASSD(i) (((in_addr_t)(i) & 0xf0000000) == 0xe0000000) 20552a38012Sejakowatz /* These ones aren't really net and host fields, but routing needn't know. */ 206a8712463SAxel Dörfler #define IN_CLASSD_NET 0xf0000000 20752a38012Sejakowatz #define IN_CLASSD_NSHIFT 28 208a8712463SAxel Dörfler #define IN_CLASSD_HOST 0x0fffffff 20952a38012Sejakowatz 21052a38012Sejakowatz #define IN_MULTICAST(i) IN_CLASSD(i) 21152a38012Sejakowatz 212a8712463SAxel Dörfler #define IN_EXPERIMENTAL(i) (((in_addr_t)(i) & 0xf0000000) == 0xf0000000) 213a8712463SAxel Dörfler #define IN_BADCLASS(i) (((in_addr_t)(i) & 0xf0000000) == 0xf0000000) 21452a38012Sejakowatz 21552a38012Sejakowatz #define IP_MAX_MEMBERSHIPS 20 21652a38012Sejakowatz 217f3917584SIngo Weinhold /* maximal length of the string representation of an IPv4 address */ 218f3917584SIngo Weinhold #define INET_ADDRSTRLEN 16 219f3917584SIngo Weinhold 22052a38012Sejakowatz /* some helpful macro's :) */ 22152a38012Sejakowatz #define in_hosteq(s, t) ((s).s_addr == (t).s_addr) 22252a38012Sejakowatz #define in_nullhost(x) ((x).s_addr == INADDR_ANY) 22352a38012Sejakowatz #define satosin(sa) ((struct sockaddr_in *)(sa)) 22452a38012Sejakowatz #define sintosa(sin) ((struct sockaddr *)(sin)) 22552a38012Sejakowatz 2267d31ea94SPhilippe Houdoin #ifdef __cplusplus 2277d31ea94SPhilippe Houdoin } 228c2c739ceSAxel Dörfler #endif 2297d31ea94SPhilippe Houdoin 2300b6361efSAxel Dörfler #endif /* _NETINET_IN_H_ */ 231