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