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