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