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