1*a466cc55SCy Schubert /* 2*a466cc55SCy Schubert * Copyright (C) 2004-2007, 2009 Internet Systems Consortium, Inc. ("ISC") 3*a466cc55SCy Schubert * Copyright (C) 1998-2002 Internet Software Consortium. 4*a466cc55SCy Schubert * 5*a466cc55SCy Schubert * Permission to use, copy, modify, and/or distribute this software for any 6*a466cc55SCy Schubert * purpose with or without fee is hereby granted, provided that the above 7*a466cc55SCy Schubert * copyright notice and this permission notice appear in all copies. 8*a466cc55SCy Schubert * 9*a466cc55SCy Schubert * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10*a466cc55SCy Schubert * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11*a466cc55SCy Schubert * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12*a466cc55SCy Schubert * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13*a466cc55SCy Schubert * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14*a466cc55SCy Schubert * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15*a466cc55SCy Schubert * PERFORMANCE OF THIS SOFTWARE. 16*a466cc55SCy Schubert */ 17*a466cc55SCy Schubert 18*a466cc55SCy Schubert /* $Id: netaddr.h,v 1.37 2009/01/17 23:47:43 tbox Exp $ */ 19*a466cc55SCy Schubert 20*a466cc55SCy Schubert #ifndef ISC_NETADDR_H 21*a466cc55SCy Schubert #define ISC_NETADDR_H 1 22*a466cc55SCy Schubert 23*a466cc55SCy Schubert /*! \file isc/netaddr.h */ 24*a466cc55SCy Schubert 25*a466cc55SCy Schubert #include <isc/lang.h> 26*a466cc55SCy Schubert #include <isc/net.h> 27*a466cc55SCy Schubert #include <isc/types.h> 28*a466cc55SCy Schubert 29*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVESYSUNH 30*a466cc55SCy Schubert #include <sys/types.h> 31*a466cc55SCy Schubert #include <sys/un.h> 32*a466cc55SCy Schubert #endif 33*a466cc55SCy Schubert 34*a466cc55SCy Schubert ISC_LANG_BEGINDECLS 35*a466cc55SCy Schubert 36*a466cc55SCy Schubert struct isc_netaddr { 37*a466cc55SCy Schubert unsigned int family; 38*a466cc55SCy Schubert union { 39*a466cc55SCy Schubert struct in_addr in; 40*a466cc55SCy Schubert struct in6_addr in6; 41*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVESYSUNH 42*a466cc55SCy Schubert char un[sizeof(((struct sockaddr_un *)0)->sun_path)]; 43*a466cc55SCy Schubert #endif 44*a466cc55SCy Schubert } type; 45*a466cc55SCy Schubert isc_uint32_t zone; 46*a466cc55SCy Schubert }; 47*a466cc55SCy Schubert 48*a466cc55SCy Schubert isc_boolean_t 49*a466cc55SCy Schubert isc_netaddr_equal(const isc_netaddr_t *a, const isc_netaddr_t *b); 50*a466cc55SCy Schubert 51*a466cc55SCy Schubert /*%< 52*a466cc55SCy Schubert * Compare network addresses 'a' and 'b'. Return #ISC_TRUE if 53*a466cc55SCy Schubert * they are equal, #ISC_FALSE if not. 54*a466cc55SCy Schubert */ 55*a466cc55SCy Schubert 56*a466cc55SCy Schubert isc_boolean_t 57*a466cc55SCy Schubert isc_netaddr_eqprefix(const isc_netaddr_t *a, const isc_netaddr_t *b, 58*a466cc55SCy Schubert unsigned int prefixlen); 59*a466cc55SCy Schubert /*%< 60*a466cc55SCy Schubert * Compare the 'prefixlen' most significant bits of the network 61*a466cc55SCy Schubert * addresses 'a' and 'b'. If 'b''s scope is zero then 'a''s scope is 62*a466cc55SCy Schubert * ignored. Return #ISC_TRUE if they are equal, #ISC_FALSE if not. 63*a466cc55SCy Schubert */ 64*a466cc55SCy Schubert 65*a466cc55SCy Schubert isc_result_t 66*a466cc55SCy Schubert isc_netaddr_masktoprefixlen(const isc_netaddr_t *s, unsigned int *lenp); 67*a466cc55SCy Schubert /*%< 68*a466cc55SCy Schubert * Convert a netmask in 's' into a prefix length in '*lenp'. 69*a466cc55SCy Schubert * The mask should consist of zero or more '1' bits in the most 70*a466cc55SCy Schubert * most significant part of the address, followed by '0' bits. 71*a466cc55SCy Schubert * If this is not the case, #ISC_R_MASKNONCONTIG is returned. 72*a466cc55SCy Schubert * 73*a466cc55SCy Schubert * Returns: 74*a466cc55SCy Schubert *\li #ISC_R_SUCCESS 75*a466cc55SCy Schubert *\li #ISC_R_MASKNONCONTIG 76*a466cc55SCy Schubert */ 77*a466cc55SCy Schubert 78*a466cc55SCy Schubert isc_result_t 79*a466cc55SCy Schubert isc_netaddr_totext(const isc_netaddr_t *netaddr, isc_buffer_t *target); 80*a466cc55SCy Schubert /*%< 81*a466cc55SCy Schubert * Append a text representation of 'sockaddr' to the buffer 'target'. 82*a466cc55SCy Schubert * The text is NOT null terminated. Handles IPv4 and IPv6 addresses. 83*a466cc55SCy Schubert * 84*a466cc55SCy Schubert * Returns: 85*a466cc55SCy Schubert *\li #ISC_R_SUCCESS 86*a466cc55SCy Schubert *\li #ISC_R_NOSPACE The text or the null termination did not fit. 87*a466cc55SCy Schubert *\li #ISC_R_FAILURE Unspecified failure 88*a466cc55SCy Schubert */ 89*a466cc55SCy Schubert 90*a466cc55SCy Schubert void 91*a466cc55SCy Schubert isc_netaddr_format(const isc_netaddr_t *na, char *array, unsigned int size); 92*a466cc55SCy Schubert /*%< 93*a466cc55SCy Schubert * Format a human-readable representation of the network address '*na' 94*a466cc55SCy Schubert * into the character array 'array', which is of size 'size'. 95*a466cc55SCy Schubert * The resulting string is guaranteed to be null-terminated. 96*a466cc55SCy Schubert */ 97*a466cc55SCy Schubert 98*a466cc55SCy Schubert #define ISC_NETADDR_FORMATSIZE \ 99*a466cc55SCy Schubert sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:XXX.XXX.XXX.XXX%SSSSSSSSSS") 100*a466cc55SCy Schubert /*%< 101*a466cc55SCy Schubert * Minimum size of array to pass to isc_netaddr_format(). 102*a466cc55SCy Schubert */ 103*a466cc55SCy Schubert 104*a466cc55SCy Schubert void 105*a466cc55SCy Schubert isc_netaddr_fromsockaddr(isc_netaddr_t *netaddr, const isc_sockaddr_t *source); 106*a466cc55SCy Schubert 107*a466cc55SCy Schubert void 108*a466cc55SCy Schubert isc_netaddr_fromin(isc_netaddr_t *netaddr, const struct in_addr *ina); 109*a466cc55SCy Schubert 110*a466cc55SCy Schubert void 111*a466cc55SCy Schubert isc_netaddr_fromin6(isc_netaddr_t *netaddr, const struct in6_addr *ina6); 112*a466cc55SCy Schubert 113*a466cc55SCy Schubert isc_result_t 114*a466cc55SCy Schubert isc_netaddr_frompath(isc_netaddr_t *netaddr, const char *path); 115*a466cc55SCy Schubert 116*a466cc55SCy Schubert void 117*a466cc55SCy Schubert isc_netaddr_setzone(isc_netaddr_t *netaddr, isc_uint32_t zone); 118*a466cc55SCy Schubert 119*a466cc55SCy Schubert isc_uint32_t 120*a466cc55SCy Schubert isc_netaddr_getzone(const isc_netaddr_t *netaddr); 121*a466cc55SCy Schubert 122*a466cc55SCy Schubert void 123*a466cc55SCy Schubert isc_netaddr_any(isc_netaddr_t *netaddr); 124*a466cc55SCy Schubert /*%< 125*a466cc55SCy Schubert * Return the IPv4 wildcard address. 126*a466cc55SCy Schubert */ 127*a466cc55SCy Schubert 128*a466cc55SCy Schubert void 129*a466cc55SCy Schubert isc_netaddr_any6(isc_netaddr_t *netaddr); 130*a466cc55SCy Schubert /*%< 131*a466cc55SCy Schubert * Return the IPv6 wildcard address. 132*a466cc55SCy Schubert */ 133*a466cc55SCy Schubert 134*a466cc55SCy Schubert isc_boolean_t 135*a466cc55SCy Schubert isc_netaddr_ismulticast(isc_netaddr_t *na); 136*a466cc55SCy Schubert /*%< 137*a466cc55SCy Schubert * Returns ISC_TRUE if the address is a multicast address. 138*a466cc55SCy Schubert */ 139*a466cc55SCy Schubert 140*a466cc55SCy Schubert isc_boolean_t 141*a466cc55SCy Schubert isc_netaddr_isexperimental(isc_netaddr_t *na); 142*a466cc55SCy Schubert /*%< 143*a466cc55SCy Schubert * Returns ISC_TRUE if the address is a experimental (CLASS E) address. 144*a466cc55SCy Schubert */ 145*a466cc55SCy Schubert 146*a466cc55SCy Schubert isc_boolean_t 147*a466cc55SCy Schubert isc_netaddr_islinklocal(isc_netaddr_t *na); 148*a466cc55SCy Schubert /*%< 149*a466cc55SCy Schubert * Returns #ISC_TRUE if the address is a link local address. 150*a466cc55SCy Schubert */ 151*a466cc55SCy Schubert 152*a466cc55SCy Schubert isc_boolean_t 153*a466cc55SCy Schubert isc_netaddr_issitelocal(isc_netaddr_t *na); 154*a466cc55SCy Schubert /*%< 155*a466cc55SCy Schubert * Returns #ISC_TRUE if the address is a site local address. 156*a466cc55SCy Schubert */ 157*a466cc55SCy Schubert 158*a466cc55SCy Schubert void 159*a466cc55SCy Schubert isc_netaddr_fromv4mapped(isc_netaddr_t *t, const isc_netaddr_t *s); 160*a466cc55SCy Schubert /*%< 161*a466cc55SCy Schubert * Convert an IPv6 v4mapped address into an IPv4 address. 162*a466cc55SCy Schubert */ 163*a466cc55SCy Schubert 164*a466cc55SCy Schubert isc_result_t 165*a466cc55SCy Schubert isc_netaddr_prefixok(const isc_netaddr_t *na, unsigned int prefixlen); 166*a466cc55SCy Schubert /* 167*a466cc55SCy Schubert * Test whether the netaddr 'na' and 'prefixlen' are consistant. 168*a466cc55SCy Schubert * e.g. prefixlen within range. 169*a466cc55SCy Schubert * na does not have bits set which are not covered by the prefixlen. 170*a466cc55SCy Schubert * 171*a466cc55SCy Schubert * Returns: 172*a466cc55SCy Schubert * ISC_R_SUCCESS 173*a466cc55SCy Schubert * ISC_R_RANGE prefixlen out of range 174*a466cc55SCy Schubert * ISC_R_NOTIMPLEMENTED unsupported family 175*a466cc55SCy Schubert * ISC_R_FAILURE extra bits. 176*a466cc55SCy Schubert */ 177*a466cc55SCy Schubert 178*a466cc55SCy Schubert ISC_LANG_ENDDECLS 179*a466cc55SCy Schubert 180*a466cc55SCy Schubert #endif /* ISC_NETADDR_H */ 181