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