1*eabc0478Schristos /* $NetBSD: netscope.c,v 1.2 2024/08/18 20:47:14 christos Exp $ */ 2897be3a4Schristos 3897be3a4Schristos /* 4897be3a4Schristos * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC") 5897be3a4Schristos * Copyright (C) 2002 Internet Software Consortium. 6897be3a4Schristos * 7897be3a4Schristos * Permission to use, copy, modify, and/or distribute this software for any 8897be3a4Schristos * purpose with or without fee is hereby granted, provided that the above 9897be3a4Schristos * copyright notice and this permission notice appear in all copies. 10897be3a4Schristos * 11897be3a4Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12897be3a4Schristos * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13897be3a4Schristos * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14897be3a4Schristos * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15897be3a4Schristos * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16897be3a4Schristos * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17897be3a4Schristos * PERFORMANCE OF THIS SOFTWARE. 18897be3a4Schristos */ 19897be3a4Schristos 20897be3a4Schristos /*! \file */ 21897be3a4Schristos 22897be3a4Schristos #if defined(LIBC_SCCS) && !defined(lint) 23897be3a4Schristos static char rcsid[] = 24897be3a4Schristos "Id: netscope.c,v 1.13 2007/06/19 23:47:17 tbox Exp "; 25897be3a4Schristos #endif /* LIBC_SCCS and not lint */ 26897be3a4Schristos 27897be3a4Schristos #include <config.h> 28897be3a4Schristos 29897be3a4Schristos #include <isc/string.h> 30897be3a4Schristos #include <isc/net.h> 31897be3a4Schristos #include <isc/netscope.h> 32897be3a4Schristos #include <isc/result.h> 33897be3a4Schristos 34897be3a4Schristos isc_result_t 35897be3a4Schristos isc_netscope_pton(int af, char *scopename, void *addr, isc_uint32_t *zoneid) { 36897be3a4Schristos char *ep; 37897be3a4Schristos #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX 38897be3a4Schristos unsigned int ifid; 39897be3a4Schristos #endif 40897be3a4Schristos struct in6_addr *in6; 41897be3a4Schristos isc_uint32_t zone; 42897be3a4Schristos isc_uint64_t llz; 43897be3a4Schristos 44897be3a4Schristos /* at this moment, we only support AF_INET6 */ 45897be3a4Schristos if (af != AF_INET6) 46897be3a4Schristos return (ISC_R_FAILURE); 47897be3a4Schristos 48897be3a4Schristos in6 = (struct in6_addr *)addr; 49897be3a4Schristos 50897be3a4Schristos /* 51897be3a4Schristos * Basically, "names" are more stable than numeric IDs in terms of 52897be3a4Schristos * renumbering, and are more preferred. However, since there is no 53897be3a4Schristos * standard naming convention and APIs to deal with the names. Thus, 54897be3a4Schristos * we only handle the case of link-local addresses, for which we use 55897be3a4Schristos * interface names as link names, assuming one to one mapping between 56897be3a4Schristos * interfaces and links. 57897be3a4Schristos */ 58897be3a4Schristos #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX 59897be3a4Schristos if (IN6_IS_ADDR_LINKLOCAL(in6) && 60897be3a4Schristos (ifid = if_nametoindex((const char *)scopename)) != 0) 61897be3a4Schristos zone = (isc_uint32_t)ifid; 62897be3a4Schristos else { 63897be3a4Schristos #endif 64897be3a4Schristos llz = isc_string_touint64(scopename, &ep, 10); 65897be3a4Schristos if (ep == scopename) 66897be3a4Schristos return (ISC_R_FAILURE); 67897be3a4Schristos 68897be3a4Schristos /* check overflow */ 69897be3a4Schristos zone = (isc_uint32_t)(llz & 0xffffffffUL); 70897be3a4Schristos if (zone != llz) 71897be3a4Schristos return (ISC_R_FAILURE); 72897be3a4Schristos #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX 73897be3a4Schristos } 74897be3a4Schristos #endif 75897be3a4Schristos 76897be3a4Schristos *zoneid = zone; 77897be3a4Schristos return (ISC_R_SUCCESS); 78897be3a4Schristos } 79