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