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, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * 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 * Copyright (c) 1991-1994 Sun Microsystems, Inc
2N/A *
2N/A * lib/libsocket/inet/getservbyname_r.c
2N/A *
2N/A * getservbyname_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 * getservbyport_r() is similarly related to _get_hostserv_inetnetdir_byaddr()
2N/A * and netdir_getbyaddr();
2N/A *
2N/A * The common code lives in lib/libnsl/nss/netdir_inet.c.
2N/A *
2N/A * getservent_r(), setservent() and endservent() are *not* implemented on top
2N/A * of the common interface; they go straight to the switch and are
2N/A * defined in getservent_r.c.
2N/A *
2N/A * There is absolutely no data sharing, not even the stayopen flag or
2N/A * enumeration state, between getservbyYY_r() and getservent_r();
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <netdb.h>
2N/A#include <netdir.h>
2N/A#include <sys/types.h>
2N/A#include <nss_netdir.h>
2N/A
2N/Aextern int str2servent(const char *, int, void *, char *, int);
2N/Aextern struct netconfig *__rpc_getconfip();
2N/A
2N/Astruct servent *
2N/Agetservbyname_r(const char *name, const char *proto, struct servent *result,
2N/A char *buffer, int buflen)
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;
2N/A
2N/A if ((nconf = __rpc_getconfip("udp")) == NULL &&
2N/A (nconf = __rpc_getconfip("tcp")) == NULL) {
2N/A return ((struct servent *)NULL);
2N/A }
2N/A nssin.op_t = NSS_SERV;
2N/A nssin.arg.nss.serv.name = name;
2N/A nssin.arg.nss.serv.proto = proto;
2N/A nssin.arg.nss.serv.buf = buffer;
2N/A nssin.arg.nss.serv.buflen = buflen;
2N/A
2N/A nssout.nss.serv = result;
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 ((struct servent *)NULL);
2N/A }
2N/A return (nssout.nss.serv);
2N/A}
2N/A
2N/Astruct servent *
2N/Agetservbyport_r(int port, const char *proto, struct servent *result,
2N/A char *buffer, int buflen)
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;
2N/A
2N/A if ((nconf = __rpc_getconfip("udp")) == NULL &&
2N/A (nconf = __rpc_getconfip("tcp")) == NULL) {
2N/A return ((struct servent *)NULL);
2N/A }
2N/A nssin.op_t = NSS_SERV;
2N/A nssin.arg.nss.serv.port = port;
2N/A nssin.arg.nss.serv.proto = proto;
2N/A nssin.arg.nss.serv.buf = buffer;
2N/A nssin.arg.nss.serv.buflen = buflen;
2N/A
2N/A nssout.nss.serv = result;
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 ((struct servent *)NULL);
2N/A }
2N/A return (nssout.nss.serv);
2N/A}