xref: /freebsd/contrib/ntp/libntp/lib/isc/netscope.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert  * Copyright (C) 2004-2007  Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert  * Copyright (C) 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 /*! \file */
19*a466cc55SCy Schubert 
20*a466cc55SCy Schubert #if defined(LIBC_SCCS) && !defined(lint)
21*a466cc55SCy Schubert static char rcsid[] =
22*a466cc55SCy Schubert 	"$Id: netscope.c,v 1.13 2007/06/19 23:47:17 tbox Exp $";
23*a466cc55SCy Schubert #endif /* LIBC_SCCS and not lint */
24*a466cc55SCy Schubert 
25*a466cc55SCy Schubert #include <config.h>
26*a466cc55SCy Schubert 
27*a466cc55SCy Schubert #include <isc/string.h>
28*a466cc55SCy Schubert #include <isc/net.h>
29*a466cc55SCy Schubert #include <isc/netscope.h>
30*a466cc55SCy Schubert #include <isc/result.h>
31*a466cc55SCy Schubert 
32*a466cc55SCy Schubert isc_result_t
isc_netscope_pton(int af,char * scopename,void * addr,isc_uint32_t * zoneid)33*a466cc55SCy Schubert isc_netscope_pton(int af, char *scopename, void *addr, isc_uint32_t *zoneid) {
34*a466cc55SCy Schubert 	char *ep;
35*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX
36*a466cc55SCy Schubert 	unsigned int ifid;
37*a466cc55SCy Schubert #endif
38*a466cc55SCy Schubert 	struct in6_addr *in6;
39*a466cc55SCy Schubert 	isc_uint32_t zone;
40*a466cc55SCy Schubert 	isc_uint64_t llz;
41*a466cc55SCy Schubert 
42*a466cc55SCy Schubert 	/* at this moment, we only support AF_INET6 */
43*a466cc55SCy Schubert 	if (af != AF_INET6)
44*a466cc55SCy Schubert 		return (ISC_R_FAILURE);
45*a466cc55SCy Schubert 
46*a466cc55SCy Schubert 	in6 = (struct in6_addr *)addr;
47*a466cc55SCy Schubert 
48*a466cc55SCy Schubert 	/*
49*a466cc55SCy Schubert 	 * Basically, "names" are more stable than numeric IDs in terms of
50*a466cc55SCy Schubert 	 * renumbering, and are more preferred.  However, since there is no
51*a466cc55SCy Schubert 	 * standard naming convention and APIs to deal with the names.  Thus,
52*a466cc55SCy Schubert 	 * we only handle the case of link-local addresses, for which we use
53*a466cc55SCy Schubert 	 * interface names as link names, assuming one to one mapping between
54*a466cc55SCy Schubert 	 * interfaces and links.
55*a466cc55SCy Schubert 	 */
56*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX
57*a466cc55SCy Schubert 	if (IN6_IS_ADDR_LINKLOCAL(in6) &&
58*a466cc55SCy Schubert 	    (ifid = if_nametoindex((const char *)scopename)) != 0)
59*a466cc55SCy Schubert 		zone = (isc_uint32_t)ifid;
60*a466cc55SCy Schubert 	else {
61*a466cc55SCy Schubert #endif
62*a466cc55SCy Schubert 		llz = isc_string_touint64(scopename, &ep, 10);
63*a466cc55SCy Schubert 		if (ep == scopename)
64*a466cc55SCy Schubert 			return (ISC_R_FAILURE);
65*a466cc55SCy Schubert 
66*a466cc55SCy Schubert 		/* check overflow */
67*a466cc55SCy Schubert 		zone = (isc_uint32_t)(llz & 0xffffffffUL);
68*a466cc55SCy Schubert 		if (zone != llz)
69*a466cc55SCy Schubert 			return (ISC_R_FAILURE);
70*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX
71*a466cc55SCy Schubert 	}
72*a466cc55SCy Schubert #endif
73*a466cc55SCy Schubert 
74*a466cc55SCy Schubert 	*zoneid = zone;
75*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
76*a466cc55SCy Schubert }
77