17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
2261961e0robinson
237c478bdstevel@tonic-gate/*
24e8031f0raf * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bdstevel@tonic-gate * Use is subject to license terms.
26e8031f0raf */
27e8031f0raf
28e8031f0raf/*
297c478bdstevel@tonic-gate * This file defines and implements the re-entrant enumeration routines for
307c478bdstevel@tonic-gate *   hosts: sethostent(), gethostent_r(), and endhostent(). They consult
317c478bdstevel@tonic-gate *   the switch policy directly and do not "share" their enumeration state
327c478bdstevel@tonic-gate *   nor the stayopen flag with the implentation of the more commonly used
337c478bdstevel@tonic-gate *   gethostbyname_r()/gethostbyaddr_r(). The latter follows a tortuous
347c478bdstevel@tonic-gate *   route in order to be consistent with netdir_getbyYY() (see
357c478bdstevel@tonic-gate *   gethostbyname_r.c and netdir_inet.c).
367c478bdstevel@tonic-gate */
377c478bdstevel@tonic-gate
387c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
397c478bdstevel@tonic-gate
40e8031f0raf#include "mt.h"
417c478bdstevel@tonic-gate#include <sys/socket.h>
427c478bdstevel@tonic-gate#include <sys/types.h>
437c478bdstevel@tonic-gate#include <nss_dbdefs.h>
447c478bdstevel@tonic-gate#include "nss.h"
457c478bdstevel@tonic-gate
467c478bdstevel@tonic-gate/*
477c478bdstevel@tonic-gate * str2hostent() is now a wrapper to __str2hostent().
487c478bdstevel@tonic-gate * __str2hostent() now takes an extra argument to specify the
497c478bdstevel@tonic-gate * AF_INET type for address parsing.
507c478bdstevel@tonic-gate */
517c478bdstevel@tonic-gateint
527c478bdstevel@tonic-gatestr2hostent(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
537c478bdstevel@tonic-gate{
547c478bdstevel@tonic-gate	return (__str2hostent(AF_INET, instr, lenstr, ent, buffer, buflen));
557c478bdstevel@tonic-gate}
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gatestatic int hosts_stayopen;
587c478bdstevel@tonic-gate/*
597c478bdstevel@tonic-gate * Unsynchronized, but it affects only
607c478bdstevel@tonic-gate * efficiency, not correctness
617c478bdstevel@tonic-gate */
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gatestatic DEFINE_NSS_DB_ROOT(db_root);
647c478bdstevel@tonic-gatestatic DEFINE_NSS_GETENT(context);
657c478bdstevel@tonic-gate
667c478bdstevel@tonic-gatevoid
677c478bdstevel@tonic-gate_nss_initf_hosts(nss_db_params_t *p)
687c478bdstevel@tonic-gate{
697c478bdstevel@tonic-gate	p->name	= NSS_DBNAM_HOSTS;
707c478bdstevel@tonic-gate	p->default_config = NSS_DEFCONF_HOSTS;
717c478bdstevel@tonic-gate}
727c478bdstevel@tonic-gate
737c478bdstevel@tonic-gateint
747c478bdstevel@tonic-gatesethostent(int stay)
757c478bdstevel@tonic-gate{
767c478bdstevel@tonic-gate	hosts_stayopen |= stay;
777c478bdstevel@tonic-gate	nss_setent(&db_root, _nss_initf_hosts, &context);
787c478bdstevel@tonic-gate	return (0);
797c478bdstevel@tonic-gate}
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gateint
827c478bdstevel@tonic-gateendhostent(void)
837c478bdstevel@tonic-gate{
847c478bdstevel@tonic-gate	hosts_stayopen = 0;
857c478bdstevel@tonic-gate	nss_endent(&db_root, _nss_initf_hosts, &context);
867c478bdstevel@tonic-gate	nss_delete(&db_root);
877c478bdstevel@tonic-gate	return (0);
887c478bdstevel@tonic-gate}
897c478bdstevel@tonic-gate
907c478bdstevel@tonic-gatestruct hostent *
917c478bdstevel@tonic-gategethostent_r(struct hostent *result, char *buffer, int buflen, int *h_errnop)
927c478bdstevel@tonic-gate{
937c478bdstevel@tonic-gate	nss_XbyY_args_t arg;
947c478bdstevel@tonic-gate	nss_status_t	res;
957c478bdstevel@tonic-gate
967c478bdstevel@tonic-gate	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2hostent);
977c478bdstevel@tonic-gate	res = nss_getent(&db_root, _nss_initf_hosts,
987c478bdstevel@tonic-gate	    &context, &arg);
997c478bdstevel@tonic-gate	arg.status = res;
1007c478bdstevel@tonic-gate	*h_errnop = arg.h_errno;
1017c478bdstevel@tonic-gate	return ((struct hostent *)NSS_XbyY_FINI(&arg));
1027c478bdstevel@tonic-gate}
103