net.h revision 290001
1243166Sadrian/* 2243166Sadrian * Copyright (C) 2004, 2005, 2007, 2008, 2012 Internet Systems Consortium, Inc. ("ISC") 3243166Sadrian * Copyright (C) 1999-2003 Internet Software Consortium. 4243166Sadrian * 5243166Sadrian * Permission to use, copy, modify, and/or distribute this software for any 6243166Sadrian * purpose with or without fee is hereby granted, provided that the above 7243166Sadrian * copyright notice and this permission notice appear in all copies. 8243166Sadrian * 9243166Sadrian * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10243166Sadrian * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11243166Sadrian * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12243166Sadrian * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13243166Sadrian * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14243166Sadrian * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15243166Sadrian * PERFORMANCE OF THIS SOFTWARE. 16243166Sadrian */ 17243166Sadrian 18243166Sadrian/* $Id$ */ 19243166Sadrian 20243166Sadrian#ifndef ISC_NET_H 21243166Sadrian#define ISC_NET_H 1 22243166Sadrian 23243166Sadrian/***** 24243166Sadrian ***** Module Info 25243166Sadrian *****/ 26243166Sadrian 27243166Sadrian/*! \file 28243166Sadrian * \brief 29243166Sadrian * Basic Networking Types 30243166Sadrian * 31243166Sadrian * This module is responsible for defining the following basic networking 32243166Sadrian * types: 33250266Sadrian * 34243166Sadrian *\li struct in_addr 35243166Sadrian *\li struct in6_addr 36243171Sadrian *\li struct in6_pktinfo 37243170Sadrian *\li struct sockaddr 38243167Sadrian *\li struct sockaddr_in 39243166Sadrian *\li struct sockaddr_in6 40243166Sadrian *\li in_port_t 41246935Sadrian * 42246935Sadrian * It ensures that the AF_ and PF_ macros are defined. 43243166Sadrian * 44243166Sadrian * It declares ntoh[sl]() and hton[sl](). 45243166Sadrian * 46243166Sadrian * It declares inet_aton(), inet_ntop(), and inet_pton(). 47243166Sadrian * 48243166Sadrian * It ensures that #INADDR_LOOPBACK, #INADDR_ANY, #IN6ADDR_ANY_INIT, 49243166Sadrian * in6addr_any, and in6addr_loopback are available. 50243166Sadrian * 51243166Sadrian * It ensures that IN_MULTICAST() is available to check for multicast 52243166Sadrian * addresses. 53243166Sadrian * 54243166Sadrian * MP: 55243166Sadrian *\li No impact. 56243166Sadrian * 57243166Sadrian * Reliability: 58243166Sadrian *\li No anticipated impact. 59243166Sadrian * 60243166Sadrian * Resources: 61243166Sadrian *\li N/A. 62243166Sadrian * 63246935Sadrian * Security: 64246935Sadrian *\li No anticipated impact. 65246935Sadrian * 66246935Sadrian * Standards: 67246935Sadrian *\li BSD Socket API 68246935Sadrian *\li RFC2553 69246935Sadrian */ 70246935Sadrian 71246935Sadrian/*** 72246935Sadrian *** Imports. 73246935Sadrian ***/ 74246935Sadrian#include <isc/platform.h> 75246935Sadrian 76246935Sadrian#include <sys/types.h> 77246935Sadrian#include <sys/socket.h> /* Contractual promise. */ 78250899Sadrian 79250899Sadrian#include <net/if.h> 80250899Sadrian 81250899Sadrian#include <netinet/in.h> /* Contractual promise. */ 82250899Sadrian#include <arpa/inet.h> /* Contractual promise. */ 83250899Sadrian#ifdef ISC_PLATFORM_NEEDNETINETIN6H 84250899Sadrian#include <netinet/in6.h> /* Required on UnixWare. */ 85250899Sadrian#endif 86250899Sadrian#ifdef ISC_PLATFORM_NEEDNETINET6IN6H 87250899Sadrian#include <netinet6/in6.h> /* Required on BSD/OS for in6_pktinfo. */ 88250899Sadrian#endif 89250899Sadrian 90250899Sadrian#ifndef ISC_PLATFORM_HAVEIPV6 91250899Sadrian#include <isc/ipv6.h> /* Contractual promise. */ 92250899Sadrian#endif 93250899Sadrian 94250899Sadrian#include <isc/lang.h> 95250899Sadrian#include <isc/types.h> 96250899Sadrian 97250899Sadrian#ifdef ISC_PLATFORM_HAVEINADDR6 98250899Sadrian#define in6_addr in_addr6 /*%< Required for pre RFC2133 implementations. */ 99250899Sadrian#endif 100250899Sadrian 101250899Sadrian#ifdef ISC_PLATFORM_HAVEIPV6 102250899Sadrian#ifndef IN6ADDR_ANY_INIT 103250899Sadrian#ifdef s6_addr 104250899Sadrian/*% 105250899Sadrian * Required for some pre RFC2133 implementations. 106250899Sadrian * IN6ADDR_ANY_INIT and IN6ADDR_LOOPBACK_INIT were added in 107250899Sadrian * draft-ietf-ipngwg-bsd-api-04.txt or draft-ietf-ipngwg-bsd-api-05.txt. 108243166Sadrian * If 's6_addr' is defined then assume that there is a union and three 109243166Sadrian * levels otherwise assume two levels required. 110243166Sadrian */ 111243166Sadrian#define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } } 112243166Sadrian#else 113243166Sadrian#define IN6ADDR_ANY_INIT { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } 114243166Sadrian#endif 115243166Sadrian#endif 116243166Sadrian 117243166Sadrian#ifndef IN6ADDR_LOOPBACK_INIT 118243166Sadrian#ifdef s6_addr 119243166Sadrian/*% IPv6 address loopback init */ 120243166Sadrian#define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } } 121243166Sadrian#else 122243166Sadrian#define IN6ADDR_LOOPBACK_INIT { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } 123243166Sadrian#endif 124243166Sadrian#endif 125243166Sadrian 126243166Sadrian#ifndef IN6_IS_ADDR_V4MAPPED 127243166Sadrian/*% Is IPv6 address V4 mapped? */ 128243166Sadrian#define IN6_IS_ADDR_V4MAPPED(x) \ 129243166Sadrian (memcmp((x)->s6_addr, in6addr_any.s6_addr, 10) == 0 && \ 130243166Sadrian (x)->s6_addr[10] == 0xff && (x)->s6_addr[11] == 0xff) 131243166Sadrian#endif 132243166Sadrian 133243166Sadrian#ifndef IN6_IS_ADDR_V4COMPAT 134243166Sadrian/*% Is IPv6 address V4 compatible? */ 135243166Sadrian#define IN6_IS_ADDR_V4COMPAT(x) \ 136243166Sadrian (memcmp((x)->s6_addr, in6addr_any.s6_addr, 12) == 0 && \ 137243166Sadrian ((x)->s6_addr[12] != 0 || (x)->s6_addr[13] != 0 || \ 138243166Sadrian (x)->s6_addr[14] != 0 || \ 139243166Sadrian ((x)->s6_addr[15] != 0 && (x)->s6_addr[15] != 1))) 140243166Sadrian#endif 141243166Sadrian 142243166Sadrian#ifndef IN6_IS_ADDR_MULTICAST 143243166Sadrian/*% Is IPv6 address multicast? */ 144243166Sadrian#define IN6_IS_ADDR_MULTICAST(a) ((a)->s6_addr[0] == 0xff) 145243166Sadrian#endif 146243166Sadrian 147243166Sadrian#ifndef IN6_IS_ADDR_LINKLOCAL 148243166Sadrian/*% Is IPv6 address linklocal? */ 149243166Sadrian#define IN6_IS_ADDR_LINKLOCAL(a) \ 150243166Sadrian (((a)->s6_addr[0] == 0xfe) && (((a)->s6_addr[1] & 0xc0) == 0x80)) 151243166Sadrian#endif 152243166Sadrian 153243166Sadrian#ifndef IN6_IS_ADDR_SITELOCAL 154243166Sadrian/*% is IPv6 address sitelocal? */ 155243166Sadrian#define IN6_IS_ADDR_SITELOCAL(a) \ 156243166Sadrian (((a)->s6_addr[0] == 0xfe) && (((a)->s6_addr[1] & 0xc0) == 0xc0)) 157243166Sadrian#endif 158243166Sadrian 159243166Sadrian 160243166Sadrian#ifndef IN6_IS_ADDR_LOOPBACK 161243166Sadrian/*% is IPv6 address loopback? */ 162246935Sadrian#define IN6_IS_ADDR_LOOPBACK(x) \ 163246935Sadrian (memcmp((x)->s6_addr, in6addr_loopback.s6_addr, 16) == 0) 164246935Sadrian#endif 165246935Sadrian#endif 166246935Sadrian 167246935Sadrian#ifndef AF_INET6 168246935Sadrian/*% IPv6 */ 169246935Sadrian#define AF_INET6 99 170246935Sadrian#endif 171246935Sadrian 172246935Sadrian#ifndef PF_INET6 173246935Sadrian/*% IPv6 */ 174246935Sadrian#define PF_INET6 AF_INET6 175246935Sadrian#endif 176246935Sadrian 177246935Sadrian#ifndef INADDR_LOOPBACK 178246935Sadrian/*% inaddr loopback */ 179246935Sadrian#define INADDR_LOOPBACK 0x7f000001UL 180250899Sadrian#endif 181250899Sadrian 182250899Sadrian#ifndef ISC_PLATFORM_HAVEIN6PKTINFO 183250899Sadrian/*% IPv6 packet info */ 184250899Sadrianstruct in6_pktinfo { 185250899Sadrian struct in6_addr ipi6_addr; /*%< src/dst IPv6 address */ 186250899Sadrian unsigned int ipi6_ifindex; /*%< send/recv interface index */ 187250899Sadrian}; 188250899Sadrian#endif 189243166Sadrian 190243171Sadrian#if defined(ISC_PLATFORM_NEEDIN6ADDRANY) 191243171Sadrianextern const struct in6_addr isc_net_in6addrany; 192243171Sadrian/*% 193243170Sadrian * Cope with a missing in6addr_any and in6addr_loopback. 194243170Sadrian */ 195243167Sadrian#define in6addr_any isc_net_in6addrany 196243167Sadrian#endif 197243167Sadrian 198250266Sadrian#if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRLOOPBACK) 199243167Sadrianextern const struct in6_addr isc_net_in6addrloop; 200243166Sadrian#define in6addr_loopback isc_net_in6addrloop 201243166Sadrian#endif 202243166Sadrian 203246649Sadrian#ifdef ISC_PLATFORM_FIXIN6ISADDR 204246649Sadrian#undef IN6_IS_ADDR_GEOGRAPHIC 205246649Sadrian/*! 206243166Sadrian * \brief 207243166Sadrian * Fix UnixWare 7.1.1's broken IN6_IS_ADDR_* definitions. 208243166Sadrian */ 209243166Sadrian#define IN6_IS_ADDR_GEOGRAPHIC(a) (((a)->S6_un.S6_l[0] & 0xE0) == 0x80) 210243166Sadrian#undef IN6_IS_ADDR_IPX 211243166Sadrian#define IN6_IS_ADDR_IPX(a) (((a)->S6_un.S6_l[0] & 0xFE) == 0x04) 212243166Sadrian#undef IN6_IS_ADDR_LINKLOCAL 213243166Sadrian#define IN6_IS_ADDR_LINKLOCAL(a) (((a)->S6_un.S6_l[0] & 0xC0FF) == 0x80FE) 214243166Sadrian#undef IN6_IS_ADDR_MULTICAST 215243166Sadrian#define IN6_IS_ADDR_MULTICAST(a) (((a)->S6_un.S6_l[0] & 0xFF) == 0xFF) 216243166Sadrian#undef IN6_IS_ADDR_NSAP 217243166Sadrian#define IN6_IS_ADDR_NSAP(a) (((a)->S6_un.S6_l[0] & 0xFE) == 0x02) 218243166Sadrian#undef IN6_IS_ADDR_PROVIDER 219243166Sadrian#define IN6_IS_ADDR_PROVIDER(a) (((a)->S6_un.S6_l[0] & 0xE0) == 0x40) 220243166Sadrian#undef IN6_IS_ADDR_SITELOCAL 221243166Sadrian#define IN6_IS_ADDR_SITELOCAL(a) (((a)->S6_un.S6_l[0] & 0xC0FF) == 0xC0FE) 222243166Sadrian#endif /* ISC_PLATFORM_FIXIN6ISADDR */ 223243166Sadrian 224#ifdef ISC_PLATFORM_NEEDPORTT 225/*% 226 * Ensure type in_port_t is defined. 227 */ 228typedef isc_uint16_t in_port_t; 229#endif 230 231#ifndef MSG_TRUNC 232/*% 233 * If this system does not have MSG_TRUNC (as returned from recvmsg()) 234 * ISC_PLATFORM_RECVOVERFLOW will be defined. This will enable the MSG_TRUNC 235 * faking code in socket.c. 236 */ 237#define ISC_PLATFORM_RECVOVERFLOW 238#endif 239 240/*% IP address. */ 241#define ISC__IPADDR(x) ((isc_uint32_t)htonl((isc_uint32_t)(x))) 242 243/*% Is IP address multicast? */ 244#define ISC_IPADDR_ISMULTICAST(i) \ 245 (((isc_uint32_t)(i) & ISC__IPADDR(0xf0000000)) \ 246 == ISC__IPADDR(0xe0000000)) 247 248#define ISC_IPADDR_ISEXPERIMENTAL(i) \ 249 (((isc_uint32_t)(i) & ISC__IPADDR(0xf0000000)) \ 250 == ISC__IPADDR(0xf0000000)) 251 252/*** 253 *** Functions. 254 ***/ 255 256ISC_LANG_BEGINDECLS 257 258isc_result_t 259isc_net_probeipv4(void); 260/*%< 261 * Check if the system's kernel supports IPv4. 262 * 263 * Returns: 264 * 265 *\li #ISC_R_SUCCESS IPv4 is supported. 266 *\li #ISC_R_NOTFOUND IPv4 is not supported. 267 *\li #ISC_R_DISABLED IPv4 is disabled. 268 *\li #ISC_R_UNEXPECTED 269 */ 270 271isc_result_t 272isc_net_probeipv6(void); 273/*%< 274 * Check if the system's kernel supports IPv6. 275 * 276 * Returns: 277 * 278 *\li #ISC_R_SUCCESS IPv6 is supported. 279 *\li #ISC_R_NOTFOUND IPv6 is not supported. 280 *\li #ISC_R_DISABLED IPv6 is disabled. 281 *\li #ISC_R_UNEXPECTED 282 */ 283 284isc_result_t 285isc_net_probe_ipv6only(void); 286/*%< 287 * Check if the system's kernel supports the IPV6_V6ONLY socket option. 288 * 289 * Returns: 290 * 291 *\li #ISC_R_SUCCESS the option is supported for both TCP and UDP. 292 *\li #ISC_R_NOTFOUND IPv6 itself or the option is not supported. 293 *\li #ISC_R_UNEXPECTED 294 */ 295 296isc_result_t 297isc_net_probe_ipv6pktinfo(void); 298/* 299 * Check if the system's kernel supports the IPV6_(RECV)PKTINFO socket option 300 * for UDP sockets. 301 * 302 * Returns: 303 * 304 * \li #ISC_R_SUCCESS the option is supported. 305 * \li #ISC_R_NOTFOUND IPv6 itself or the option is not supported. 306 * \li #ISC_R_UNEXPECTED 307 */ 308 309void 310isc_net_disableipv4(void); 311 312void 313isc_net_disableipv6(void); 314 315void 316isc_net_enableipv4(void); 317 318void 319isc_net_enableipv6(void); 320 321isc_result_t 322isc_net_probeunix(void); 323/* 324 * Returns whether UNIX domain sockets are supported. 325 */ 326 327isc_result_t 328isc_net_getudpportrange(int af, in_port_t *low, in_port_t *high); 329/*%< 330 * Returns system's default range of ephemeral UDP ports, if defined. 331 * If the range is not available or unknown, ISC_NET_PORTRANGELOW and 332 * ISC_NET_PORTRANGEHIGH will be returned. 333 * 334 * Requires: 335 * 336 *\li 'low' and 'high' must be non NULL. 337 * 338 * Returns: 339 * 340 *\li *low and *high will be the ports specifying the low and high ends of 341 * the range. 342 */ 343 344#ifdef ISC_PLATFORM_NEEDNTOP 345const char * 346isc_net_ntop(int af, const void *src, char *dst, size_t size); 347#define inet_ntop isc_net_ntop 348#endif 349 350#ifdef ISC_PLATFORM_NEEDPTON 351int 352isc_net_pton(int af, const char *src, void *dst); 353#undef inet_pton 354#define inet_pton isc_net_pton 355#endif 356 357int 358isc_net_aton(const char *cp, struct in_addr *addr); 359#undef inet_aton 360#define inet_aton isc_net_aton 361 362ISC_LANG_ENDDECLS 363 364#endif /* ISC_NET_H */ 365