xref: /illumos-gate/usr/src/lib/libsocket/inet/getservbyname_r.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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 #include <netdb.h>
49 #include <netdir.h>
50 #include <sys/types.h>
51 #include <nss_netdir.h>
52 
53 extern int str2servent(const char *, int, void *, char *, int);
54 extern struct netconfig *__rpc_getconfip();
55 
56 struct servent *
getservbyname_r(const char * name,const char * proto,struct servent * result,char * buffer,int buflen)57 getservbyname_r(const char *name, const char *proto, struct servent *result,
58 	char *buffer, int buflen)
59 {
60 	struct netconfig *nconf;
61 	struct	nss_netdirbyname_in nssin;
62 	union	nss_netdirbyname_out nssout;
63 	int neterr;
64 
65 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
66 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
67 		return ((struct servent *)NULL);
68 	}
69 	nssin.op_t = NSS_SERV;
70 	nssin.arg.nss.serv.name = name;
71 	nssin.arg.nss.serv.proto = proto;
72 	nssin.arg.nss.serv.buf = buffer;
73 	nssin.arg.nss.serv.buflen = buflen;
74 
75 	nssout.nss.serv = result;
76 
77 	/*
78 	 * We pass in nconf and let the implementation of the long-named func
79 	 * decide whether to use the switch based on nc_nlookups.
80 	 */
81 	neterr = _get_hostserv_inetnetdir_byname(nconf, &nssin, &nssout);
82 
83 	(void) freenetconfigent(nconf);
84 	if (neterr != ND_OK) {
85 		return ((struct servent *)NULL);
86 	}
87 	return (nssout.nss.serv);
88 }
89 
90 struct servent *
getservbyport_r(int port,const char * proto,struct servent * result,char * buffer,int buflen)91 getservbyport_r(int port, const char *proto, struct servent *result,
92 	char *buffer, int buflen)
93 {
94 	struct netconfig *nconf;
95 	struct	nss_netdirbyaddr_in nssin;
96 	union	nss_netdirbyaddr_out nssout;
97 	int neterr;
98 
99 	if ((nconf = __rpc_getconfip("udp")) == NULL &&
100 	    (nconf = __rpc_getconfip("tcp")) == NULL) {
101 		return ((struct servent *)NULL);
102 	}
103 	nssin.op_t = NSS_SERV;
104 	nssin.arg.nss.serv.port = port;
105 	nssin.arg.nss.serv.proto = proto;
106 	nssin.arg.nss.serv.buf = buffer;
107 	nssin.arg.nss.serv.buflen = buflen;
108 
109 	nssout.nss.serv = result;
110 
111 	/*
112 	 * We pass in nconf and let the implementation of this long-named func
113 	 * decide whether to use the switch based on nc_nlookups.
114 	 */
115 	neterr = _get_hostserv_inetnetdir_byaddr(nconf, &nssin, &nssout);
116 
117 	(void) freenetconfigent(nconf);
118 	if (neterr != ND_OK) {
119 		return ((struct servent *)NULL);
120 	}
121 	return (nssout.nss.serv);
122 }
123