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