xref: /illumos-gate/usr/src/lib/nsswitch/mdns/common/gethostent.c (revision d17be682a2c70b4505d43c830bbd2603da11918d)
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 #include "mdns_common.h"
28 
29 /*
30  * gethostby* functions for the hosts database. The hosts
31  * database stores IPv4 addresses only.
32  * mDNS query functions to perform the host lookup
33  * are in mdns/common/mdns_common.c file.
34  * _nss_mdns_hosts_constr is called to initialize
35  * the nsswitch backend data structures.
36  */
37 
38 static nss_status_t
39 getbyname(be, a)
40 	mdns_backend_ptr_t	be;
41 	void			*a;
42 {
43 	struct mdns_querydata   qdata;
44 	char			*hname;
45 
46 	(void) memset(&qdata, 0, sizeof (struct mdns_querydata));
47 
48 	qdata.argp = (nss_XbyY_args_t *)a;
49 	hname = (char *)qdata.argp->key.name;
50 
51 	_nss_mdns_updatecfg(be);
52 	return (_nss_mdns_querybyname(be, hname, AF_INET, &qdata));
53 }
54 
55 /*ARGSUSED*/
56 static nss_status_t
57 getbyaddr(be, a)
58 	mdns_backend_ptr_t	be;
59 	void			*a;
60 {
61 	nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
62 	struct in_addr addr;
63 	struct mdns_querydata  qdata;
64 	char buffer[sizeof ("255.255.255.255.in-addr.arpa.")];
65 	uint8_t *p;
66 
67 	(void) memset(&qdata, 0, sizeof (struct mdns_querydata));
68 	qdata.argp = argp;
69 
70 	argp->h_errno = 0;
71 	if ((argp->key.hostaddr.type != AF_INET) ||
72 	    (argp->key.hostaddr.len != sizeof (addr)))
73 		return (NSS_NOTFOUND);
74 
75 	(void) memcpy(&addr, argp->key.hostaddr.addr, sizeof (addr));
76 
77 	if (inet_ntop(AF_INET, (void *) &addr.s_addr,
78 		(void *)qdata.paddrbuf,
79 		sizeof (qdata.paddrbuf)) == NULL)
80 			return (NSS_NOTFOUND);
81 
82 	qdata.af = AF_INET;
83 	p = (uint8_t *)&addr.s_addr;
84 	(void) snprintf(buffer, sizeof (buffer),
85 		"%u.%u.%u.%u.in-addr.arpa.", p[3], p[2], p[1], p[0]);
86 
87 	_nss_mdns_updatecfg(be);
88 	return (_nss_mdns_querybyaddr(be, buffer, qdata.af, &qdata));
89 }
90 
91 /*ARGSUSED*/
92 static nss_status_t
93 _nss_mdns_getent(be, args)
94 	mdns_backend_ptr_t	be;
95 	void			*args;
96 {
97 	return (NSS_UNAVAIL);
98 }
99 
100 /*ARGSUSED*/
101 static nss_status_t
102 _nss_mdns_setent(be, dummy)
103 	mdns_backend_ptr_t	be;
104 	void			*dummy;
105 {
106 	return (NSS_UNAVAIL);
107 }
108 
109 /*ARGSUSED*/
110 static nss_status_t
111 _nss_mdns_endent(be, dummy)
112 	mdns_backend_ptr_t	be;
113 	void			*dummy;
114 {
115 	return (NSS_UNAVAIL);
116 }
117 
118 /*ARGSUSED*/
119 static nss_status_t
120 _nss_mdns_hosts_destr(be, dummy)
121 	mdns_backend_ptr_t	be;
122 	void			*dummy;
123 {
124 	_nss_mdns_destr(be);
125 	return (NSS_SUCCESS);
126 }
127 
128 static mdns_backend_op_t host_ops[] = {
129 	_nss_mdns_hosts_destr,
130 	_nss_mdns_endent,
131 	_nss_mdns_setent,
132 	_nss_mdns_getent,
133 	getbyname,
134 	getbyaddr,
135 };
136 
137 /*ARGSUSED*/
138 nss_backend_t *
139 _nss_mdns_hosts_constr(dummy1, dummy2, dummy3)
140 	const char	*dummy1, *dummy2, *dummy3;
141 {
142 	return (_nss_mdns_constr(host_ops,
143 		sizeof (host_ops) / sizeof (host_ops[0])));
144 }
145 
146 /*ARGSUSED*/
147 nss_status_t
148 _nss_get_mdns_hosts_name(mdns_backend_ptr_t *be, void **bufp, size_t *sizep)
149 {
150 	return (_nss_mdns_gethost_withttl(*bufp, *sizep, 0));
151 }
152