xref: /onnv-gate/usr/src/lib/libsocket/inet/getservbyname_r.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * 	Copyright (c) 1991-1994  Sun Microsystems, Inc
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * lib/libsocket/inet/getservbyname_r.c
26*0Sstevel@tonic-gate  *
27*0Sstevel@tonic-gate  * getservbyname_r() is defined in this file. It is implemented on top of
28*0Sstevel@tonic-gate  *   _get_hostserv_inetnetdir_byname() which is also used to implement
29*0Sstevel@tonic-gate  *   netdir_getbyname() for inet family transports.  In turn the common code
30*0Sstevel@tonic-gate  *   uses the name service switch policy for "hosts" and "services" unless
31*0Sstevel@tonic-gate  *   the administrator chooses to bypass the name service switch by
32*0Sstevel@tonic-gate  *   specifying third-party supplied nametoaddr libs for inet transports
33*0Sstevel@tonic-gate  *   in /etc/netconfig.
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * getservbyport_r() is similarly related to _get_hostserv_inetnetdir_byaddr()
36*0Sstevel@tonic-gate  *   and netdir_getbyaddr();
37*0Sstevel@tonic-gate  *
38*0Sstevel@tonic-gate  * The common code lives in lib/libnsl/nss/netdir_inet.c.
39*0Sstevel@tonic-gate  *
40*0Sstevel@tonic-gate  * getservent_r(), setservent() and endservent() are *not* implemented on top
41*0Sstevel@tonic-gate  *   of the common interface;  they go straight to the switch and are
42*0Sstevel@tonic-gate  *   defined in getservent_r.c.
43*0Sstevel@tonic-gate  *
44*0Sstevel@tonic-gate  * There is absolutely no data sharing, not even the stayopen flag or
45*0Sstevel@tonic-gate  *   enumeration state, between getservbyYY_r() and getservent_r();
46*0Sstevel@tonic-gate  */
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #include <netdb.h>
51*0Sstevel@tonic-gate #include <netdir.h>
52*0Sstevel@tonic-gate #include <sys/types.h>
53*0Sstevel@tonic-gate #include <nss_netdir.h>
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate extern int str2servent(const char *, int, void *, char *, int);
56*0Sstevel@tonic-gate extern struct netconfig *__rpc_getconfip();
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate struct servent *
getservbyname_r(const char * name,const char * proto,struct servent * result,char * buffer,int buflen)59*0Sstevel@tonic-gate getservbyname_r(const char *name, const char *proto, struct servent *result,
60*0Sstevel@tonic-gate 	char *buffer, int buflen)
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate 	struct netconfig *nconf;
63*0Sstevel@tonic-gate 	struct	nss_netdirbyname_in nssin;
64*0Sstevel@tonic-gate 	union	nss_netdirbyname_out nssout;
65*0Sstevel@tonic-gate 	int neterr;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
68*0Sstevel@tonic-gate 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
69*0Sstevel@tonic-gate 		return ((struct servent *)NULL);
70*0Sstevel@tonic-gate 	}
71*0Sstevel@tonic-gate 	nssin.op_t = NSS_SERV;
72*0Sstevel@tonic-gate 	nssin.arg.nss.serv.name = name;
73*0Sstevel@tonic-gate 	nssin.arg.nss.serv.proto = proto;
74*0Sstevel@tonic-gate 	nssin.arg.nss.serv.buf = buffer;
75*0Sstevel@tonic-gate 	nssin.arg.nss.serv.buflen = buflen;
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	nssout.nss.serv = result;
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	/*
80*0Sstevel@tonic-gate 	 * We pass in nconf and let the implementation of the long-named func
81*0Sstevel@tonic-gate 	 * decide whether to use the switch based on nc_nlookups.
82*0Sstevel@tonic-gate 	 */
83*0Sstevel@tonic-gate 	neterr = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout);
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	(void) freenetconfigent(nconf);
86*0Sstevel@tonic-gate 	if (neterr != ND_OK) {
87*0Sstevel@tonic-gate 		return ((struct servent *)NULL);
88*0Sstevel@tonic-gate 	}
89*0Sstevel@tonic-gate 	return (nssout.nss.serv);
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate struct servent *
getservbyport_r(int port,const char * proto,struct servent * result,char * buffer,int buflen)93*0Sstevel@tonic-gate getservbyport_r(int port, const char *proto, struct servent *result,
94*0Sstevel@tonic-gate 	char *buffer, int buflen)
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate 	struct netconfig *nconf;
97*0Sstevel@tonic-gate 	struct	nss_netdirbyaddr_in nssin;
98*0Sstevel@tonic-gate 	union	nss_netdirbyaddr_out nssout;
99*0Sstevel@tonic-gate 	int neterr;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
102*0Sstevel@tonic-gate 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
103*0Sstevel@tonic-gate 		return ((struct servent *)NULL);
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate 	nssin.op_t = NSS_SERV;
106*0Sstevel@tonic-gate 	nssin.arg.nss.serv.port = port;
107*0Sstevel@tonic-gate 	nssin.arg.nss.serv.proto = proto;
108*0Sstevel@tonic-gate 	nssin.arg.nss.serv.buf = buffer;
109*0Sstevel@tonic-gate 	nssin.arg.nss.serv.buflen = buflen;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	nssout.nss.serv = result;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	/*
114*0Sstevel@tonic-gate 	 * We pass in nconf and let the implementation of this long-named func
115*0Sstevel@tonic-gate 	 * decide whether to use the switch based on nc_nlookups.
116*0Sstevel@tonic-gate 	 */
117*0Sstevel@tonic-gate 	neterr = _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	(void) freenetconfigent(nconf);
120*0Sstevel@tonic-gate 	if (neterr != ND_OK) {
121*0Sstevel@tonic-gate 		return ((struct servent *)NULL);
122*0Sstevel@tonic-gate 	}
123*0Sstevel@tonic-gate 	return (nssout.nss.serv);
124*0Sstevel@tonic-gate }
125