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