2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * gethostbyname_r() is defined in this file. It is implemented on top of
2N/A * _get_hostserv_inetnetdir_byname() which is also used to implement
2N/A * netdir_getbyname() for inet family transports. In turn the common code
2N/A * uses the name service switch policy for "hosts" and "services" unless
2N/A * the administrator chooses to bypass the name service switch by
2N/A * specifying third-party supplied nametoaddr libs for inet transports
2N/A * in /etc/netconfig.
2N/A *
2N/A * gethostbyaddr_r() is similarly related to _get_hostserv_inetnetdir_byaddr()
2N/A * and netdir_getbyaddr();
2N/A *
2N/A * The common code lives in netdir_inet.c.
2N/A *
2N/A * gethostent_r(), sethostent() and endhostent() are *not* implemented on top
2N/A * of the common interface; they go straight to the switch and are
2N/A * defined in gethostent_r.c.
2N/A *
2N/A * There is absolutely no data sharing, not even the stayopen flag or
2N/A * enumeration state, between gethostbyYY_r() and gethostent_r();
2N/A */
2N/A
2N/A#include "mt.h"
2N/A#include <netdb.h>
2N/A#include <netdir.h>
2N/A#include <sys/types.h>
2N/A#include <nss_netdir.h>
2N/A#include <string.h>
2N/A
2N/Aextern struct netconfig *__rpc_getconfip();
2N/A
2N/A/*
2N/A * h_errno POLICY: The frontends expect the name service
2N/A * backends to modify the h_errno in "arg"; _switch_gethostbyYY_r()
2N/A * will copy that over onto user's h_errnop pointer. This h_errno is
2N/A * never used for "switching" -- status from nss_search serves
2N/A * the purpose. There is no explicit zeroing in the case of success.
2N/A */
2N/A
2N/Aextern struct hostent *
2N/A_switch_gethostbyname_r(const char *nam, struct hostent *result, char *buffer,
2N/A int buflen, int *h_errnop);
2N/A
2N/Aextern struct hostent *
2N/A_switch_gethostbyaddr_r(const char *addr, int length, int type,
2N/A struct hostent *result, char *buffer, int buflen, int *h_errnop);
2N/A
2N/A#ifdef PIC
2N/Astruct hostent *
2N/A_uncached_gethostbyname_r(const char *nam, struct hostent *result,
2N/A char *buffer, int buflen, int *h_errnop)
2N/A{
2N/A return (_switch_gethostbyname_r(nam, result,
2N/A buffer, buflen, h_errnop));
2N/A}
2N/A
2N/Astruct hostent *
2N/A_uncached_gethostbyaddr_r(const char *addr, int length, int type,
2N/A struct hostent *result, char *buffer, int buflen, int *h_errnop)
2N/A{
2N/A return (_switch_gethostbyaddr_r(addr, length, type,
2N/A result, buffer, buflen, h_errnop));
2N/A}
2N/A
2N/A#endif
2N/A
2N/Aextern struct hostent *
2N/Agethostbyname_r(const char *nam, struct hostent *result, char *buffer,
2N/A int buflen, int *h_errnop);
2N/A
2N/Aextern struct hostent *
2N/Agethostbyaddr_r(const char *addr, int length, int type,
2N/A struct hostent *result, char *buffer, int buflen, int *h_errnop);
2N/A
2N/Astruct hostent *
2N/Agethostbyname_r(const char *nam, struct hostent *result, char *buffer,
2N/A int buflen, int *h_errnop)
2N/A{
2N/A struct netconfig *nconf;
2N/A struct nss_netdirbyname_in nssin;
2N/A union nss_netdirbyname_out nssout;
2N/A int neterr, dummy;
2N/A
2N/A if (h_errnop == NULL)
2N/A h_errnop = &dummy;
2N/A
2N/A if (strlen(nam) == 0) {
2N/A *h_errnop = HOST_NOT_FOUND;
2N/A return (NULL);
2N/A }
2N/A
2N/A if ((nconf = __rpc_getconfip("udp")) == NULL &&
2N/A (nconf = __rpc_getconfip("tcp")) == NULL) {
2N/A *h_errnop = NO_RECOVERY;
2N/A return (NULL);
2N/A }
2N/A
2N/A nssin.op_t = NSS_HOST;
2N/A nssin.arg.nss.host.name = nam;
2N/A nssin.arg.nss.host.buf = buffer;
2N/A nssin.arg.nss.host.buflen = buflen;
2N/A
2N/A nssout.nss.host.hent = result;
2N/A nssout.nss.host.herrno_p = h_errnop;
2N/A
2N/A /*
2N/A * We pass in nconf and let the implementation of the long-named func
2N/A * decide whether to use the switch based on nc_nlookups.
2N/A */
2N/A neterr = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout);
2N/A
2N/A (void) freenetconfigent(nconf);
2N/A if (neterr != ND_OK)
2N/A return (NULL);
2N/A return (nssout.nss.host.hent);
2N/A}
2N/A
2N/Astruct hostent *
2N/Agethostbyaddr_r(const char *addr, int length, int type,
2N/A struct hostent *result, char *buffer, int buflen, int *h_errnop)
2N/A{
2N/A struct netconfig *nconf;
2N/A struct nss_netdirbyaddr_in nssin;
2N/A union nss_netdirbyaddr_out nssout;
2N/A int neterr, dummy;
2N/A
2N/A if (h_errnop == NULL)
2N/A h_errnop = &dummy;
2N/A
2N/A if (type != AF_INET) {
2N/A *h_errnop = HOST_NOT_FOUND;
2N/A return (NULL);
2N/A }
2N/A
2N/A if ((nconf = __rpc_getconfip("udp")) == NULL &&
2N/A (nconf = __rpc_getconfip("tcp")) == NULL) {
2N/A *h_errnop = NO_RECOVERY;
2N/A return (NULL);
2N/A }
2N/A
2N/A nssin.op_t = NSS_HOST;
2N/A nssin.arg.nss.host.addr = addr;
2N/A nssin.arg.nss.host.len = length;
2N/A nssin.arg.nss.host.type = type;
2N/A nssin.arg.nss.host.buf = buffer;
2N/A nssin.arg.nss.host.buflen = buflen;
2N/A
2N/A nssout.nss.host.hent = result;
2N/A nssout.nss.host.herrno_p = h_errnop;
2N/A
2N/A /*
2N/A * We pass in nconf and let the implementation of this long-named func
2N/A * decide whether to use the switch based on nc_nlookups.
2N/A */
2N/A neterr = _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
2N/A
2N/A (void) freenetconfigent(nconf);
2N/A if (neterr != ND_OK)
2N/A return (NULL);
2N/A return (nssout.nss.host.hent);
2N/A}