1*4afad4b7Schristos /* $NetBSD: netscope.c,v 1.1 2024/02/18 20:57:49 christos Exp $ */
2*4afad4b7Schristos
3*4afad4b7Schristos /*
4*4afad4b7Schristos * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5*4afad4b7Schristos *
6*4afad4b7Schristos * SPDX-License-Identifier: MPL-2.0
7*4afad4b7Schristos *
8*4afad4b7Schristos * This Source Code Form is subject to the terms of the Mozilla Public
9*4afad4b7Schristos * License, v. 2.0. If a copy of the MPL was not distributed with this
10*4afad4b7Schristos * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11*4afad4b7Schristos *
12*4afad4b7Schristos * See the COPYRIGHT file distributed with this work for additional
13*4afad4b7Schristos * information regarding copyright ownership.
14*4afad4b7Schristos */
15*4afad4b7Schristos
16*4afad4b7Schristos /*! \file */
17*4afad4b7Schristos
18*4afad4b7Schristos #include <inttypes.h>
19*4afad4b7Schristos #include <stdlib.h>
20*4afad4b7Schristos
21*4afad4b7Schristos #include <isc/net.h>
22*4afad4b7Schristos #include <isc/netscope.h>
23*4afad4b7Schristos #include <isc/result.h>
24*4afad4b7Schristos #include <isc/string.h>
25*4afad4b7Schristos #include <isc/util.h>
26*4afad4b7Schristos
27*4afad4b7Schristos isc_result_t
isc_netscope_pton(int af,char * scopename,void * addr,uint32_t * zoneid)28*4afad4b7Schristos isc_netscope_pton(int af, char *scopename, void *addr, uint32_t *zoneid) {
29*4afad4b7Schristos char *ep;
30*4afad4b7Schristos #ifdef HAVE_IF_NAMETOINDEX
31*4afad4b7Schristos unsigned int ifid;
32*4afad4b7Schristos struct in6_addr *in6;
33*4afad4b7Schristos #endif /* ifdef HAVE_IF_NAMETOINDEX */
34*4afad4b7Schristos uint32_t zone = 0;
35*4afad4b7Schristos uint64_t llz;
36*4afad4b7Schristos
37*4afad4b7Schristos #ifndef HAVE_IF_NAMETOINDEX
38*4afad4b7Schristos UNUSED(addr);
39*4afad4b7Schristos #endif
40*4afad4b7Schristos
41*4afad4b7Schristos /* at this moment, we only support AF_INET6 */
42*4afad4b7Schristos if (af != AF_INET6) {
43*4afad4b7Schristos return (ISC_R_FAILURE);
44*4afad4b7Schristos }
45*4afad4b7Schristos
46*4afad4b7Schristos /*
47*4afad4b7Schristos * Basically, "names" are more stable than numeric IDs in terms
48*4afad4b7Schristos * of renumbering, and are more preferred. However, since there
49*4afad4b7Schristos * is no standard naming convention and APIs to deal with the
50*4afad4b7Schristos * names. Thus, we only handle the case of link-local
51*4afad4b7Schristos * addresses, for which we use interface names as link names,
52*4afad4b7Schristos * assuming one to one mapping between interfaces and links.
53*4afad4b7Schristos */
54*4afad4b7Schristos #ifdef HAVE_IF_NAMETOINDEX
55*4afad4b7Schristos in6 = (struct in6_addr *)addr;
56*4afad4b7Schristos if (IN6_IS_ADDR_LINKLOCAL(in6) &&
57*4afad4b7Schristos (ifid = if_nametoindex((const char *)scopename)) != 0)
58*4afad4b7Schristos {
59*4afad4b7Schristos zone = (uint32_t)ifid;
60*4afad4b7Schristos } else {
61*4afad4b7Schristos #endif /* ifdef HAVE_IF_NAMETOINDEX */
62*4afad4b7Schristos llz = strtoull(scopename, &ep, 10);
63*4afad4b7Schristos if (ep == scopename) {
64*4afad4b7Schristos return (ISC_R_FAILURE);
65*4afad4b7Schristos }
66*4afad4b7Schristos
67*4afad4b7Schristos /* check overflow */
68*4afad4b7Schristos zone = (uint32_t)(llz & 0xffffffffUL);
69*4afad4b7Schristos if (zone != llz) {
70*4afad4b7Schristos return (ISC_R_FAILURE);
71*4afad4b7Schristos }
72*4afad4b7Schristos #ifdef HAVE_IF_NAMETOINDEX
73*4afad4b7Schristos }
74*4afad4b7Schristos #endif /* ifdef HAVE_IF_NAMETOINDEX */
75*4afad4b7Schristos
76*4afad4b7Schristos *zoneid = zone;
77*4afad4b7Schristos return (ISC_R_SUCCESS);
78*4afad4b7Schristos }
79