1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "mdns_common.h"
30 
31 /*
32  * gethostby* functions for the ipnodes database. The ipnodes
33  * database stores both IPv4 and IPv6 address information.
34  * mDNS query functions to perform the host lookup are
35  * in mdns/common/mdns_common.c file.
36  * _nss_mdns_ipnodes_constr is called to initialize
37  * the nsswitch backend data structures.
38  */
39 
40 static nss_status_t
41 getbyname(be, a)
42 	mdns_backend_ptr_t	be;
43 	void			*a;
44 {
45 	nss_XbyY_args_t 	*argp = (nss_XbyY_args_t *)a;
46 	int			af = argp->key.ipnode.af_family;
47 	char			*hname = (char *)argp->key.ipnode.name;
48 	struct mdns_querydata   qdata;
49 
50 	(void) memset(&qdata, 0, sizeof (struct mdns_querydata));
51 	qdata.argp = argp;
52 
53 	_nss_mdns_updatecfg(be);
54 	return (_nss_mdns_querybyname(be, hname, af, &qdata));
55 }
56 
57 static nss_status_t
58 getbyaddr(be, a)
59 	mdns_backend_ptr_t	be;
60 	void			*a;
61 {
62 	int i;
63 	char ch;
64 	char *chptr;
65 	uint8_t *p;
66 	struct in6_addr *addr;
67 	struct mdns_querydata qdata;
68 	char addrqryname[ sizeof ("f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f") + \
69 		sizeof (".f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.ip6.arpa.")];
70 	nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
71 
72 	(void) memset(&qdata, 0, sizeof (struct mdns_querydata));
73 	qdata.argp = argp;
74 	argp->h_errno = 0;
75 
76 	if ((argp->key.hostaddr.type != AF_INET6) ||
77 	    (argp->key.hostaddr.len != sizeof (*addr)))
78 		return (NSS_NOTFOUND);
79 
80 	/* LINTED E_BAD_PTR_CAST_ALIGN */
81 	addr = (struct in6_addr *)(argp->key.hostaddr.addr);
82 
83 	if (IN6_IS_ADDR_V4MAPPED(addr)) {
84 		struct in_addr ipv4addr;
85 
86 		IN6_V4MAPPED_TO_INADDR(addr, &ipv4addr);
87 		if (inet_ntop(AF_INET, (void *) &ipv4addr.s_addr,
88 		(void *)qdata.paddrbuf, sizeof (qdata.paddrbuf)) == NULL)
89 				return (NSS_NOTFOUND);
90 		qdata.af = AF_INET;
91 		p = (uint8_t *)&ipv4addr.s_addr;
92 		(void) snprintf(addrqryname, sizeof (addrqryname),
93 			"%u.%u.%u.%u.in-addr.arpa.", p[3], p[2], p[1], p[0]);
94 	} else {
95 		if (inet_ntop(AF_INET6, (void *)addr,
96 			(void *)qdata.paddrbuf,
97 			sizeof (qdata.paddrbuf)) == NULL)
98 			return (NSS_NOTFOUND);
99 		qdata.af = AF_INET6;
100 		chptr = addrqryname;
101 		for (i = 0; i < 16; i++)
102 		{
103 			ch = ((char *)addr)[15-i];
104 			chptr += snprintf(chptr, sizeof (addrqryname)-i*4,
105 					"%X.%X.", ch&0x0f, (ch>>4)&0x0f);
106 		}
107 		(void) strlcpy(chptr, "ip6.arpa.", sizeof (addrqryname)-64);
108 	}
109 
110 	_nss_mdns_updatecfg(be);
111 	return (_nss_mdns_querybyaddr(be, addrqryname, qdata.af, &qdata));
112 }
113 
114 /*ARGSUSED*/
115 static nss_status_t
116 _nss_mdns_getent(be, args)
117 	mdns_backend_ptr_t	be;
118 	void			*args;
119 {
120 	return (NSS_UNAVAIL);
121 }
122 
123 /*ARGSUSED*/
124 static nss_status_t
125 _nss_mdns_setent(be, dummy)
126 	mdns_backend_ptr_t	be;
127 	void			*dummy;
128 {
129 	return (NSS_UNAVAIL);
130 }
131 
132 /*ARGSUSED*/
133 static nss_status_t
134 _nss_mdns_endent(be, dummy)
135 	mdns_backend_ptr_t	be;
136 	void			*dummy;
137 {
138 	return (NSS_UNAVAIL);
139 }
140 
141 /*ARGSUSED*/
142 static nss_status_t
143 _nss_mdns_ipnodes_destr(be, dummy)
144 	mdns_backend_ptr_t	be;
145 	void			*dummy;
146 {
147 	_nss_mdns_destr(be);
148 	return (NSS_SUCCESS);
149 }
150 
151 static mdns_backend_op_t ipnodes_ops[] = {
152 	_nss_mdns_ipnodes_destr,
153 	_nss_mdns_endent,
154 	_nss_mdns_setent,
155 	_nss_mdns_getent,
156 	getbyname,
157 	getbyaddr,
158 };
159 
160 /*ARGSUSED*/
161 nss_backend_t *
162 _nss_mdns_ipnodes_constr(dummy1, dummy2, dummy3)
163 	const char	*dummy1, *dummy2, *dummy3;
164 {
165 	return (_nss_mdns_constr(ipnodes_ops,
166 		sizeof (ipnodes_ops) / sizeof (ipnodes_ops[0])));
167 }
168 
169 /*ARGSUSED*/
170 nss_status_t
171 _nss_get_mdns_ipnodes_name(mdns_backend_ptr_t *be, void **bufp, size_t *sizep)
172 {
173 	return (_nss_mdns_gethost_withttl(*bufp, *sizep, 1));
174 }
175