gethostent_r.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
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 * Copyright 1991-2002 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 *
26 * lib/libnsl/nss/gethostent_r.c
27 *
28 * This file defines and implements the re-entrant enumeration routines for
29 *   hosts: sethostent(), gethostent_r(), and endhostent(). They consult
30 *   the switch policy directly and do not "share" their enumeration state
31 *   nor the stayopen flag with the implentation of the more commonly used
32 *   gethostbyname_r()/gethostbyaddr_r(). The latter follows a tortuous
33 *   route in order to be consistent with netdir_getbyYY() (see
34 *   gethostbyname_r.c and netdir_inet.c).
35 */
36
37#pragma ident	"%Z%%M%	%I%	%E% SMI"
38
39#include <sys/socket.h>
40#include <sys/types.h>
41#include <nss_dbdefs.h>
42#include <rpc/trace.h>
43#include "nss.h"
44
45/*
46 * str2hostent() is now a wrapper to __str2hostent().
47 * __str2hostent() now takes an extra argument to specify the
48 * AF_INET type for address parsing.
49 */
50int
51str2hostent(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
52{
53	return (__str2hostent(AF_INET, instr, lenstr, ent, buffer, buflen));
54}
55
56static int hosts_stayopen;
57/*
58 * Unsynchronized, but it affects only
59 * efficiency, not correctness
60 */
61
62static DEFINE_NSS_DB_ROOT(db_root);
63static DEFINE_NSS_GETENT(context);
64
65void
66_nss_initf_hosts(nss_db_params_t *p)
67{
68	trace1(TR__nss_initf_hosts, 0);
69	p->name	= NSS_DBNAM_HOSTS;
70	p->default_config = NSS_DEFCONF_HOSTS;
71	trace1(TR__nss_initf_hosts, 1);
72}
73
74int
75sethostent(int stay)
76{
77	trace1(TR_sethostent, 0);
78	hosts_stayopen |= stay;
79	nss_setent(&db_root, _nss_initf_hosts, &context);
80	trace1(TR_sethostent, 0);
81	return (0);
82}
83
84int
85endhostent(void)
86{
87	trace1(TR_endhostent, 0);
88	hosts_stayopen = 0;
89	nss_endent(&db_root, _nss_initf_hosts, &context);
90	nss_delete(&db_root);
91	trace1(TR_endhostent, 0);
92	return (0);
93}
94
95struct hostent *
96gethostent_r(struct hostent *result, char *buffer, int buflen, int *h_errnop)
97{
98	nss_XbyY_args_t arg;
99	nss_status_t	res;
100
101	trace2(TR_gethostbyent_r, 0, buflen);
102	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2hostent);
103	res = nss_getent(&db_root, _nss_initf_hosts,
104	    &context, &arg);
105	arg.status = res;
106	*h_errnop = arg.h_errno;
107	trace2(TR_gethostbyent_r, 1, buflen);
108	return ((struct hostent *)NSS_XbyY_FINI(&arg));
109}
110