1 1.1 christos /* $NetBSD: netscope.c,v 1.2 2024/08/18 20:47:14 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC") 5 1.1 christos * Copyright (C) 2002 Internet Software Consortium. 6 1.1 christos * 7 1.1 christos * Permission to use, copy, modify, and/or distribute this software for any 8 1.1 christos * purpose with or without fee is hereby granted, provided that the above 9 1.1 christos * copyright notice and this permission notice appear in all copies. 10 1.1 christos * 11 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12 1.1 christos * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13 1.1 christos * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14 1.1 christos * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15 1.1 christos * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16 1.1 christos * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 1.1 christos * PERFORMANCE OF THIS SOFTWARE. 18 1.1 christos */ 19 1.1 christos 20 1.1 christos /*! \file */ 21 1.1 christos 22 1.1 christos #if defined(LIBC_SCCS) && !defined(lint) 23 1.1 christos static char rcsid[] = 24 1.1 christos "Id: netscope.c,v 1.13 2007/06/19 23:47:17 tbox Exp "; 25 1.1 christos #endif /* LIBC_SCCS and not lint */ 26 1.1 christos 27 1.1 christos #include <config.h> 28 1.1 christos 29 1.1 christos #include <isc/string.h> 30 1.1 christos #include <isc/net.h> 31 1.1 christos #include <isc/netscope.h> 32 1.1 christos #include <isc/result.h> 33 1.1 christos 34 1.1 christos isc_result_t 35 1.1 christos isc_netscope_pton(int af, char *scopename, void *addr, isc_uint32_t *zoneid) { 36 1.1 christos char *ep; 37 1.1 christos #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX 38 1.1 christos unsigned int ifid; 39 1.1 christos #endif 40 1.1 christos struct in6_addr *in6; 41 1.1 christos isc_uint32_t zone; 42 1.1 christos isc_uint64_t llz; 43 1.1 christos 44 1.1 christos /* at this moment, we only support AF_INET6 */ 45 1.1 christos if (af != AF_INET6) 46 1.1 christos return (ISC_R_FAILURE); 47 1.1 christos 48 1.1 christos in6 = (struct in6_addr *)addr; 49 1.1 christos 50 1.1 christos /* 51 1.1 christos * Basically, "names" are more stable than numeric IDs in terms of 52 1.1 christos * renumbering, and are more preferred. However, since there is no 53 1.1 christos * standard naming convention and APIs to deal with the names. Thus, 54 1.1 christos * we only handle the case of link-local addresses, for which we use 55 1.1 christos * interface names as link names, assuming one to one mapping between 56 1.1 christos * interfaces and links. 57 1.1 christos */ 58 1.1 christos #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX 59 1.1 christos if (IN6_IS_ADDR_LINKLOCAL(in6) && 60 1.1 christos (ifid = if_nametoindex((const char *)scopename)) != 0) 61 1.1 christos zone = (isc_uint32_t)ifid; 62 1.1 christos else { 63 1.1 christos #endif 64 1.1 christos llz = isc_string_touint64(scopename, &ep, 10); 65 1.1 christos if (ep == scopename) 66 1.1 christos return (ISC_R_FAILURE); 67 1.1 christos 68 1.1 christos /* check overflow */ 69 1.1 christos zone = (isc_uint32_t)(llz & 0xffffffffUL); 70 1.1 christos if (zone != llz) 71 1.1 christos return (ISC_R_FAILURE); 72 1.1 christos #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX 73 1.1 christos } 74 1.1 christos #endif 75 1.1 christos 76 1.1 christos *zoneid = zone; 77 1.1 christos return (ISC_R_SUCCESS); 78 1.1 christos } 79