1/*
2 * Copyright 2002-2019, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _NETINET_IN_H_
6#define _NETINET_IN_H_
7
8
9#include <net/if.h>
10#include <endian.h>
11#include <stdint.h>
12#include <sys/types.h>
13
14/* RFC 2553 states that these must be available through <netinet/in.h> */
15#include <netinet6/in6.h>
16
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22typedef uint16_t in_port_t;
23typedef uint32_t in_addr_t;
24
25/* We can't include <ByteOrder.h> since we are a POSIX file,
26 * and we are not allowed to import all the BeOS types here. */
27#if __GNUC__ >= 4
28#	define __net_swap_int32(arg)	(uint32_t)__builtin_bswap32(arg)
29#	define __net_swap_int16(arg)	(uint16_t)__builtin_bswap16(arg)
30#else
31	extern unsigned long __swap_int32(unsigned long); /* private */
32	extern uint16_t __swap_int16(uint16_t);	/* private */
33#	define __net_swap_int32(arg)	__swap_int32(arg)
34#	define __net_swap_int16(arg)	__swap_int16(arg)
35#endif
36
37#ifndef htonl
38#	if BYTE_ORDER == LITTLE_ENDIAN
39#		define htonl(x) ((uint32_t)__net_swap_int32(x))
40#		define ntohl(x) ((uint32_t)__net_swap_int32(x))
41#		define htons(x) __net_swap_int16(x)
42#		define ntohs(x) __net_swap_int16(x)
43#	elif BYTE_ORDER == BIG_ENDIAN
44#		define htonl(x) (x)
45#		define ntohl(x) (x)
46#		define htons(x) (x)
47#		define ntohs(x) (x)
48#	else
49#		error Unknown byte order.
50#	endif
51#endif
52
53
54/* Protocol definitions */
55#define IPPROTO_IP				0	/* 0, IPv4 */
56#define IPPROTO_HOPOPTS			0	/* 0, IPv6 hop-by-hop options */
57#define IPPROTO_ICMP			1	/* 1, ICMP (v4) */
58#define IPPROTO_IGMP			2	/* 2, IGMP (group management) */
59#define IPPROTO_TCP				6	/* 6, tcp */
60#define IPPROTO_UDP				17	/* 17, UDP */
61#define IPPROTO_IPV6			41	/* 41, IPv6 in IPv6 */
62#define IPPROTO_ROUTING			43	/* 43, Routing */
63#define IPPROTO_FRAGMENT		44	/* 44, IPv6 fragmentation header */
64#define IPPROTO_ESP				50	/* 50, Encap Sec. Payload */
65#define IPPROTO_AH				51	/* 51, Auth Header */
66#define IPPROTO_ICMPV6			58	/* 58, IPv6 ICMP */
67#define IPPROTO_NONE			59	/* 59, IPv6 no next header */
68#define IPPROTO_DSTOPTS			60	/* 60, IPv6 destination option */
69#define IPPROTO_ETHERIP			97	/* 97, Ethernet in IPv4 */
70#define IPPROTO_RAW				255	/* 255 */
71
72#define IPPROTO_MAX				256
73
74
75/* Port numbers */
76
77#define IPPORT_RESERVED			1024
78	/* < IPPORT_RESERVED are privileged and should be accessible only by root */
79#define IPPORT_USERRESERVED		49151
80	/* > IPPORT_USERRESERVED are reserved for servers, though not requiring
81	 * privileged status
82	 */
83
84/* IP Version 4 address */
85struct in_addr {
86	in_addr_t s_addr;
87};
88
89/* IP Version 4 socket address */
90struct sockaddr_in {
91	uint8_t		sin_len;
92	uint8_t		sin_family;
93	uint16_t	sin_port;
94	struct in_addr 	sin_addr;
95	int8_t		sin_zero[24];
96};
97
98
99/* RFC 3678 - Socket Interface Extensions for Multicast Source Filters */
100
101struct ip_mreq {
102	struct in_addr imr_multiaddr; /* IP address of group */
103	struct in_addr imr_interface; /* IP address of interface */
104};
105
106struct ip_mreq_source {
107	struct in_addr imr_multiaddr;	/* IP address of group */
108	struct in_addr imr_sourceaddr;	/* IP address of source */
109	struct in_addr imr_interface;	/* IP address of interface */
110};
111
112struct group_req {
113	uint32_t                gr_interface; /* interface index */
114	struct sockaddr_storage gr_group;     /* group address */
115};
116
117struct group_source_req {
118	uint32_t                gsr_interface; /* interface index */
119	struct sockaddr_storage gsr_group;     /* group address */
120	struct sockaddr_storage gsr_source;    /* source address */
121};
122
123/*
124 * Options for use with [gs]etsockopt at the IP level.
125 * First word of comment is data type; bool is stored in int.
126 */
127#define IP_OPTIONS					1	/* buf/ip_opts; set/get IP options */
128#define IP_HDRINCL					2	/* int; header is included with data */
129#define IP_TOS						3
130	/* int; IP type of service and preced. */
131#define IP_TTL						4	/* int; IP time to live */
132#define IP_RECVOPTS					5	/* bool; receive all IP opts w/dgram */
133#define IP_RECVRETOPTS				6	/* bool; receive IP opts for response */
134#define IP_RECVDSTADDR				7	/* bool; receive IP dst addr w/dgram */
135#define IP_RETOPTS					8	/* ip_opts; set/get IP options */
136#define IP_MULTICAST_IF				9	/* in_addr; set/get IP multicast i/f  */
137#define IP_MULTICAST_TTL			10	/* u_char; set/get IP multicast ttl */
138#define IP_MULTICAST_LOOP			11
139	/* u_char; set/get IP multicast loopback */
140#define IP_ADD_MEMBERSHIP			12
141	/* ip_mreq; add an IP group membership */
142#define IP_DROP_MEMBERSHIP			13
143	/* ip_mreq; drop an IP group membership */
144#define IP_BLOCK_SOURCE				14	/* ip_mreq_source */
145#define IP_UNBLOCK_SOURCE			15	/* ip_mreq_source */
146#define IP_ADD_SOURCE_MEMBERSHIP	16	/* ip_mreq_source */
147#define IP_DROP_SOURCE_MEMBERSHIP	17	/* ip_mreq_source */
148#define MCAST_JOIN_GROUP			18	/* group_req */
149#define MCAST_BLOCK_SOURCE			19	/* group_source_req */
150#define MCAST_UNBLOCK_SOURCE		20	/* group_source_req */
151#define MCAST_LEAVE_GROUP			21	/* group_req */
152#define MCAST_JOIN_SOURCE_GROUP		22	/* group_source_req */
153#define MCAST_LEAVE_SOURCE_GROUP	23	/* group_source_req */
154
155/* IPPROTO_IPV6 options */
156#define IPV6_MULTICAST_IF			24	/* int */
157#define IPV6_MULTICAST_HOPS			25	/* int */
158#define IPV6_MULTICAST_LOOP			26	/* int */
159
160#define IPV6_UNICAST_HOPS			27	/* int */
161#define IPV6_JOIN_GROUP				28	/* struct ipv6_mreq */
162#define IPV6_LEAVE_GROUP			29	/* struct ipv6_mreq */
163#define IPV6_V6ONLY					30	/* int */
164
165#define IPV6_PKTINFO				31	/* struct ipv6_pktinfo */
166#define IPV6_RECVPKTINFO			32	/* struct ipv6_pktinfo */
167#define IPV6_HOPLIMIT				33	/* int */
168#define IPV6_RECVHOPLIMIT			34	/* int */
169
170#define IPV6_HOPOPTS				35  /* struct ip6_hbh */
171#define IPV6_DSTOPTS				36  /* struct ip6_dest */
172#define IPV6_RTHDR					37  /* struct ip6_rthdr */
173
174#define INADDR_ANY					((in_addr_t)0x00000000)
175#define INADDR_LOOPBACK				((in_addr_t)0x7f000001)
176#define INADDR_BROADCAST			((in_addr_t)0xffffffff)	/* must be masked */
177
178#define INADDR_UNSPEC_GROUP			((in_addr_t)0xe0000000)	/* 224.0.0.0 */
179#define INADDR_ALLHOSTS_GROUP		((in_addr_t)0xe0000001)	/* 224.0.0.1 */
180#define INADDR_ALLROUTERS_GROUP		((in_addr_t)0xe0000002)	/* 224.0.0.2 */
181#define INADDR_MAX_LOCAL_GROUP		((in_addr_t)0xe00000ff)	/* 224.0.0.255 */
182
183#define IN_LOOPBACKNET				127
184
185#define INADDR_NONE					((in_addr_t)0xffffffff)
186
187#define IN_CLASSA(i)				(((in_addr_t)(i) & 0x80000000) == 0)
188#define IN_CLASSA_NET				0xff000000
189#define IN_CLASSA_NSHIFT			24
190#define IN_CLASSA_HOST				0x00ffffff
191#define IN_CLASSA_MAX				128
192
193#define IN_CLASSB(i)				(((in_addr_t)(i) & 0xc0000000) == 0x80000000)
194#define IN_CLASSB_NET				0xffff0000
195#define IN_CLASSB_NSHIFT			16
196#define IN_CLASSB_HOST				0x0000ffff
197#define IN_CLASSB_MAX				65536
198
199#define IN_CLASSC(i)				(((in_addr_t)(i) & 0xe0000000) == 0xc0000000)
200#define IN_CLASSC_NET				0xffffff00
201#define IN_CLASSC_NSHIFT			8
202#define IN_CLASSC_HOST				0x000000ff
203
204#define IN_CLASSD(i)				(((in_addr_t)(i) & 0xf0000000) == 0xe0000000)
205/* These ones aren't really net and host fields, but routing needn't know. */
206#define IN_CLASSD_NET				0xf0000000
207#define IN_CLASSD_NSHIFT			28
208#define IN_CLASSD_HOST				0x0fffffff
209
210#define IN_MULTICAST(i)				IN_CLASSD(i)
211
212#define IN_EXPERIMENTAL(i)			(((in_addr_t)(i) & 0xf0000000) == 0xf0000000)
213#define IN_BADCLASS(i)				(((in_addr_t)(i) & 0xf0000000) == 0xf0000000)
214
215#define IP_MAX_MEMBERSHIPS			20
216
217/* maximal length of the string representation of an IPv4 address */
218#define INET_ADDRSTRLEN				16
219
220/* some helpful macro's :) */
221#define in_hosteq(s, t)				((s).s_addr == (t).s_addr)
222#define in_nullhost(x)				((x).s_addr == INADDR_ANY)
223#define satosin(sa)					((struct sockaddr_in *)(sa))
224#define sintosa(sin)				((struct sockaddr *)(sin))
225
226#ifdef __cplusplus
227}
228#endif
229
230#endif	/* _NETINET_IN_H_ */
231