xref: /illumos-gate/usr/src/lib/libnsl/nss/gethostent_r.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1991-2002 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  * lib/libnsl/nss/gethostent_r.c
27*7c478bd9Sstevel@tonic-gate  *
28*7c478bd9Sstevel@tonic-gate  * This file defines and implements the re-entrant enumeration routines for
29*7c478bd9Sstevel@tonic-gate  *   hosts: sethostent(), gethostent_r(), and endhostent(). They consult
30*7c478bd9Sstevel@tonic-gate  *   the switch policy directly and do not "share" their enumeration state
31*7c478bd9Sstevel@tonic-gate  *   nor the stayopen flag with the implentation of the more commonly used
32*7c478bd9Sstevel@tonic-gate  *   gethostbyname_r()/gethostbyaddr_r(). The latter follows a tortuous
33*7c478bd9Sstevel@tonic-gate  *   route in order to be consistent with netdir_getbyYY() (see
34*7c478bd9Sstevel@tonic-gate  *   gethostbyname_r.c and netdir_inet.c).
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
41*7c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
42*7c478bd9Sstevel@tonic-gate #include <rpc/trace.h>
43*7c478bd9Sstevel@tonic-gate #include "nss.h"
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /*
46*7c478bd9Sstevel@tonic-gate  * str2hostent() is now a wrapper to __str2hostent().
47*7c478bd9Sstevel@tonic-gate  * __str2hostent() now takes an extra argument to specify the
48*7c478bd9Sstevel@tonic-gate  * AF_INET type for address parsing.
49*7c478bd9Sstevel@tonic-gate  */
50*7c478bd9Sstevel@tonic-gate int
51*7c478bd9Sstevel@tonic-gate str2hostent(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	return (__str2hostent(AF_INET, instr, lenstr, ent, buffer, buflen));
54*7c478bd9Sstevel@tonic-gate }
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate static int hosts_stayopen;
57*7c478bd9Sstevel@tonic-gate /*
58*7c478bd9Sstevel@tonic-gate  * Unsynchronized, but it affects only
59*7c478bd9Sstevel@tonic-gate  * efficiency, not correctness
60*7c478bd9Sstevel@tonic-gate  */
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
63*7c478bd9Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate void
66*7c478bd9Sstevel@tonic-gate _nss_initf_hosts(nss_db_params_t *p)
67*7c478bd9Sstevel@tonic-gate {
68*7c478bd9Sstevel@tonic-gate 	trace1(TR__nss_initf_hosts, 0);
69*7c478bd9Sstevel@tonic-gate 	p->name	= NSS_DBNAM_HOSTS;
70*7c478bd9Sstevel@tonic-gate 	p->default_config = NSS_DEFCONF_HOSTS;
71*7c478bd9Sstevel@tonic-gate 	trace1(TR__nss_initf_hosts, 1);
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate int
75*7c478bd9Sstevel@tonic-gate sethostent(int stay)
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate 	trace1(TR_sethostent, 0);
78*7c478bd9Sstevel@tonic-gate 	hosts_stayopen |= stay;
79*7c478bd9Sstevel@tonic-gate 	nss_setent(&db_root, _nss_initf_hosts, &context);
80*7c478bd9Sstevel@tonic-gate 	trace1(TR_sethostent, 0);
81*7c478bd9Sstevel@tonic-gate 	return (0);
82*7c478bd9Sstevel@tonic-gate }
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate int
85*7c478bd9Sstevel@tonic-gate endhostent(void)
86*7c478bd9Sstevel@tonic-gate {
87*7c478bd9Sstevel@tonic-gate 	trace1(TR_endhostent, 0);
88*7c478bd9Sstevel@tonic-gate 	hosts_stayopen = 0;
89*7c478bd9Sstevel@tonic-gate 	nss_endent(&db_root, _nss_initf_hosts, &context);
90*7c478bd9Sstevel@tonic-gate 	nss_delete(&db_root);
91*7c478bd9Sstevel@tonic-gate 	trace1(TR_endhostent, 0);
92*7c478bd9Sstevel@tonic-gate 	return (0);
93*7c478bd9Sstevel@tonic-gate }
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate struct hostent *
96*7c478bd9Sstevel@tonic-gate gethostent_r(struct hostent *result, char *buffer, int buflen, int *h_errnop)
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
99*7c478bd9Sstevel@tonic-gate 	nss_status_t	res;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	trace2(TR_gethostbyent_r, 0, buflen);
102*7c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2hostent);
103*7c478bd9Sstevel@tonic-gate 	res = nss_getent(&db_root, _nss_initf_hosts,
104*7c478bd9Sstevel@tonic-gate 	    &context, &arg);
105*7c478bd9Sstevel@tonic-gate 	arg.status = res;
106*7c478bd9Sstevel@tonic-gate 	*h_errnop = arg.h_errno;
107*7c478bd9Sstevel@tonic-gate 	trace2(TR_gethostbyent_r, 1, buflen);
108*7c478bd9Sstevel@tonic-gate 	return ((struct hostent *)NSS_XbyY_FINI(&arg));
109*7c478bd9Sstevel@tonic-gate }
110