netscope.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004-2007  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 2002  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /*! \file */
00019 
00020 #if defined(LIBC_SCCS) && !defined(lint)
00021 static char rcsid[] =
00022         "$Id: netscope.c,v 1.13 2007/06/19 23:47:17 tbox Exp $";
00023 #endif /* LIBC_SCCS and not lint */
00024 
00025 #include <config.h>
00026 
00027 #include <isc/string.h>
00028 #include <isc/net.h>
00029 #include <isc/netscope.h>
00030 #include <isc/result.h>
00031 
00032 isc_result_t
00033 isc_netscope_pton(int af, char *scopename, void *addr, isc_uint32_t *zoneid) {
00034         char *ep;
00035 #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX
00036         unsigned int ifid;
00037 #endif
00038         struct in6_addr *in6;
00039         isc_uint32_t zone;
00040         isc_uint64_t llz;
00041 
00042         /* at this moment, we only support AF_INET6 */
00043         if (af != AF_INET6)
00044                 return (ISC_R_FAILURE);
00045 
00046         in6 = (struct in6_addr *)addr;
00047 
00048         /*
00049          * Basically, "names" are more stable than numeric IDs in terms of
00050          * renumbering, and are more preferred.  However, since there is no
00051          * standard naming convention and APIs to deal with the names.  Thus,
00052          * we only handle the case of link-local addresses, for which we use
00053          * interface names as link names, assuming one to one mapping between
00054          * interfaces and links.
00055          */
00056 #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX
00057         if (IN6_IS_ADDR_LINKLOCAL(in6) &&
00058             (ifid = if_nametoindex((const char *)scopename)) != 0)
00059                 zone = (isc_uint32_t)ifid;
00060         else {
00061 #endif
00062                 llz = isc_string_touint64(scopename, &ep, 10);
00063                 if (ep == scopename)
00064                         return (ISC_R_FAILURE);
00065 
00066                 /* check overflow */
00067                 zone = (isc_uint32_t)(llz & 0xffffffffUL);
00068                 if (zone != llz)
00069                         return (ISC_R_FAILURE);
00070 #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX
00071         }
00072 #endif
00073 
00074         *zoneid = zone;
00075         return (ISC_R_SUCCESS);
00076 }

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha