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