xref: /minix3/lib/libc/rpc/rpc_soc.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 /*	$NetBSD: rpc_soc.c,v 1.19 2014/05/28 14:45:57 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2010, Oracle America, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  *       copyright notice, this list of conditions and the following
14  *       disclaimer in the documentation and/or other materials
15  *       provided with the distribution.
16  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /* #ident	"@(#)rpc_soc.c	1.17	94/04/24 SMI" */
35 
36 /*
37  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
38  * In addition, portions of such source code were derived from Berkeley
39  * 4.3 BSD under license from the Regents of the University of
40  * California.
41  */
42 
43 #include <sys/cdefs.h>
44 #if defined(LIBC_SCCS) && !defined(lint)
45 #if 0
46 static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro";
47 #else
48 __RCSID("$NetBSD: rpc_soc.c,v 1.19 2014/05/28 14:45:57 christos Exp $");
49 #endif
50 #endif
51 
52 #ifdef PORTMAP
53 /*
54  * rpc_soc.c
55  *
56  * The backward compatibility routines for the earlier implementation
57  * of RPC, where the only transports supported were tcp/ip and udp/ip.
58  * Based on berkeley socket abstraction, now implemented on the top
59  * of TLI/Streams
60  */
61 
62 #include "namespace.h"
63 #include "reentrant.h"
64 #include <sys/types.h>
65 #include <sys/socket.h>
66 #include <stdio.h>
67 #include <rpc/rpc.h>
68 #include <rpc/pmap_clnt.h>
69 #include <rpc/pmap_prot.h>
70 #include <rpc/nettype.h>
71 #include <netinet/in.h>
72 #include <assert.h>
73 #include <errno.h>
74 #include <netdb.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <syslog.h>
78 #include <unistd.h>
79 
80 #include "svc_fdset.h"
81 #include "rpc_internal.h"
82 
83 #ifdef __weak_alias
84 __weak_alias(clntudp_bufcreate,_clntudp_bufcreate)
85 __weak_alias(clntudp_create,_clntudp_create)
86 __weak_alias(clnttcp_create,_clnttcp_create)
87 __weak_alias(clntraw_create,_clntraw_create)
88 __weak_alias(get_myaddress,_get_myaddress)
89 __weak_alias(svcfd_create,_svcfd_create)
90 __weak_alias(svcudp_bufcreate,_svcudp_bufcreate)
91 __weak_alias(svcudp_create,_svcudp_create)
92 __weak_alias(svctcp_create,_svctcp_create)
93 __weak_alias(svcraw_create,_svcraw_create)
94 __weak_alias(callrpc,_callrpc)
95 __weak_alias(registerrpc,_registerrpc)
96 __weak_alias(clnt_broadcast,_clnt_broadcast)
97 #endif
98 
99 #ifdef _REENTRANT
100 extern mutex_t	rpcsoc_lock;
101 #endif
102 
103 static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t,
104 				    int *, u_int, u_int, const char *);
105 static SVCXPRT *svc_com_create(int, u_int, u_int, const char *);
106 static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *);
107 
108 /*
109  * A common clnt create routine
110  */
111 static CLIENT *
clnt_com_create(struct sockaddr_in * raddr,rpcprog_t prog,rpcvers_t vers,int * sockp,u_int sendsz,u_int recvsz,const char * tp)112 clnt_com_create(struct sockaddr_in *raddr, rpcprog_t prog, rpcvers_t vers,
113 	int *sockp, u_int sendsz, u_int recvsz, const char *tp)
114 {
115 	CLIENT *cl;
116 	int madefd = FALSE;
117 	int fd;
118 	struct netconfig *nconf;
119 	struct netbuf bindaddr;
120 
121 	_DIAGASSERT(raddr != NULL);
122 	_DIAGASSERT(sockp != NULL);
123 	_DIAGASSERT(tp != NULL);
124 
125 	fd = *sockp;
126 
127 	mutex_lock(&rpcsoc_lock);
128 	if ((nconf = __rpc_getconfip(tp)) == NULL) {
129 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
130 		mutex_unlock(&rpcsoc_lock);
131 		return (NULL);
132 	}
133 	if (fd == RPC_ANYSOCK) {
134 		fd = __rpc_nconf2fd(nconf);
135 		if (fd == -1)
136 			goto syserror;
137 		madefd = TRUE;
138 	}
139 
140 	if (raddr->sin_port == 0) {
141 		u_int proto;
142 		u_short sport;
143 
144 		mutex_unlock(&rpcsoc_lock);	/* pmap_getport is recursive */
145 		proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
146 		sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
147 		    proto);
148 		if (sport == 0) {
149 			goto err;
150 		}
151 		raddr->sin_port = htons(sport);
152 		mutex_lock(&rpcsoc_lock);	/* pmap_getport is recursive */
153 	}
154 
155 	/* Transform sockaddr_in to netbuf */
156 	bindaddr.maxlen = bindaddr.len =  sizeof (struct sockaddr_in);
157 	bindaddr.buf = raddr;
158 
159 	(void)bindresvport(fd, NULL);
160 	cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
161 				sendsz, recvsz);
162 	if (cl) {
163 		if (madefd == TRUE) {
164 			/*
165 			 * The fd should be closed while destroying the handle.
166 			 */
167 			(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
168 			*sockp = fd;
169 		}
170 		(void) freenetconfigent(nconf);
171 		mutex_unlock(&rpcsoc_lock);
172 		return (cl);
173 	}
174 	goto err;
175 
176 syserror:
177 	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
178 	rpc_createerr.cf_error.re_errno = errno;
179 
180 err:	if (madefd == TRUE)
181 		(void) close(fd);
182 	(void) freenetconfigent(nconf);
183 	mutex_unlock(&rpcsoc_lock);
184 	return (NULL);
185 }
186 
187 CLIENT *
clntudp_bufcreate(struct sockaddr_in * raddr,u_long prog,u_long vers,struct timeval wait,int * sockp,u_int sendsz,u_int recvsz)188 clntudp_bufcreate(struct sockaddr_in *raddr, u_long prog, u_long vers, struct timeval wait, int *sockp, u_int sendsz, u_int recvsz)
189 {
190 	CLIENT *cl;
191 
192 	_DIAGASSERT(raddr != NULL);
193 	_DIAGASSERT(sockp != NULL);
194 
195 	cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
196 	    sendsz, recvsz, "udp");
197 	if (cl == NULL) {
198 		return (NULL);
199 	}
200 	(void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, (char *)(void *)&wait);
201 	return (cl);
202 }
203 
204 CLIENT *
clntudp_create(struct sockaddr_in * raddr,u_long program,u_long version,struct timeval wait,int * sockp)205 clntudp_create(struct sockaddr_in *raddr, u_long program, u_long version,
206     struct timeval wait, int *sockp)
207 {
208 	return clntudp_bufcreate(raddr, program, version, wait, sockp,
209 					UDPMSGSIZE, UDPMSGSIZE);
210 }
211 
212 CLIENT *
clnttcp_create(struct sockaddr_in * raddr,u_long prog,u_long vers,int * sockp,u_int sendsz,u_int recvsz)213 clnttcp_create(struct sockaddr_in *raddr, u_long prog, u_long vers, int *sockp,
214     u_int sendsz, u_int recvsz)
215 {
216 	return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
217 	    sendsz, recvsz, "tcp");
218 }
219 
220 CLIENT *
clntraw_create(u_long prog,u_long vers)221 clntraw_create(u_long prog, u_long vers)
222 {
223 	return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
224 }
225 
226 /*
227  * A common server create routine
228  */
229 static SVCXPRT *
svc_com_create(int fd,u_int sendsize,u_int recvsize,const char * netid)230 svc_com_create(int fd, u_int sendsize, u_int recvsize, const char *netid)
231 {
232 	struct netconfig *nconf;
233 	SVCXPRT *svc;
234 	int madefd = FALSE;
235 	int port;
236 	struct sockaddr_in sccsin;
237 
238 	_DIAGASSERT(netid != NULL);
239 
240 	if ((nconf = __rpc_getconfip(netid)) == NULL) {
241 		(void) syslog(LOG_ERR, "Could not get %s transport", netid);
242 		return (NULL);
243 	}
244 	if (fd == RPC_ANYSOCK) {
245 		fd = __rpc_nconf2fd(nconf);
246 		if (fd == -1) {
247 			(void) freenetconfigent(nconf);
248 			(void) syslog(LOG_ERR,
249 			"svc%s_create: could not open connection", netid);
250 			return (NULL);
251 		}
252 		madefd = TRUE;
253 	}
254 
255 	memset(&sccsin, 0, sizeof sccsin);
256 	sccsin.sin_family = AF_INET;
257 	(void)bindresvport(fd, &sccsin);
258 	listen(fd, SOMAXCONN);
259 	svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
260 	(void) freenetconfigent(nconf);
261 	if (svc == NULL) {
262 		if (madefd)
263 			(void) close(fd);
264 		return (NULL);
265 	}
266 	port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
267 	svc->xp_port = ntohs(port);
268 	return (svc);
269 }
270 
271 SVCXPRT *
svctcp_create(int fd,u_int sendsize,u_int recvsize)272 svctcp_create(int fd, u_int sendsize, u_int recvsize)
273 {
274 	return svc_com_create(fd, sendsize, recvsize, "tcp");
275 }
276 
277 SVCXPRT *
svcudp_bufcreate(int fd,u_int sendsz,u_int recvsz)278 svcudp_bufcreate(int fd, u_int sendsz, u_int recvsz)
279 {
280 	return svc_com_create(fd, sendsz, recvsz, "udp");
281 }
282 
283 SVCXPRT *
svcfd_create(int fd,u_int sendsize,u_int recvsize)284 svcfd_create(int fd, u_int sendsize, u_int recvsize)
285 {
286 	return svc_fd_create(fd, sendsize, recvsize);
287 }
288 
289 
290 SVCXPRT *
svcudp_create(int fd)291 svcudp_create(int fd)
292 {
293 	return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
294 }
295 
296 SVCXPRT *
svcraw_create(void)297 svcraw_create(void)
298 {
299 	return svc_raw_create();
300 }
301 
302 int
get_myaddress(struct sockaddr_in * addr)303 get_myaddress(struct sockaddr_in *addr)
304 {
305 
306 	_DIAGASSERT(addr != NULL);
307 
308 	memset((void *) addr, 0, sizeof(*addr));
309 	addr->sin_family = AF_INET;
310 	addr->sin_port = htons(PMAPPORT);
311 	addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
312 	return (0);
313 }
314 
315 /*
316  * For connectionless "udp" transport. Obsoleted by rpc_call().
317  */
318 int
callrpc(char * host,int prognum,int versnum,int procnum,xdrproc_t inproc,char * in,xdrproc_t outproc,char * out)319 callrpc(char *host, int prognum, int versnum, int procnum,
320 	xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
321 {
322 	return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
323 	    (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
324 }
325 
326 /*
327  * For connectionless kind of transport. Obsoleted by rpc_reg()
328  */
329 int
registerrpc(int prognum,int versnum,int procnum,char * (* progname)(char[UDPMSGSIZE]),xdrproc_t inproc,xdrproc_t outproc)330 registerrpc(int prognum, int versnum, int procnum,
331 	char *(*progname)(char [UDPMSGSIZE]),
332 	xdrproc_t inproc, xdrproc_t outproc)
333 {
334 	return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
335 	    (rpcproc_t)procnum, progname, inproc, outproc, __UNCONST("udp"));
336 }
337 
338 /*
339  * All the following clnt_broadcast stuff is convulated; it supports
340  * the earlier calling style of the callback function
341  */
342 #ifdef _REENTRANT
343 static thread_key_t	clnt_broadcast_key;
344 #endif
345 static resultproc_t	clnt_broadcast_result_main;
346 
347 /*
348  * Need to translate the netbuf address into sockaddr_in address.
349  * Dont care about netid here.
350  */
351 /* ARGSUSED */
352 static bool_t
rpc_wrap_bcast(char * resultp,struct netbuf * addr,struct netconfig * nconf)353 rpc_wrap_bcast(
354 	char *resultp,		/* results of the call */
355 	struct netbuf *addr,	/* address of the guy who responded */
356 	struct netconfig *nconf) /* Netconf of the transport */
357 {
358 	resultproc_t clnt_broadcast_result;
359 
360 	_DIAGASSERT(resultp != NULL);
361 	_DIAGASSERT(addr != NULL);
362 	_DIAGASSERT(nconf != NULL);
363 
364 	if (strcmp(nconf->nc_netid, "udp"))
365 		return (FALSE);
366 #ifdef _REENTRANT
367 	if (__isthreaded == 0)
368 		clnt_broadcast_result = clnt_broadcast_result_main;
369 	else
370 		clnt_broadcast_result = thr_getspecific(clnt_broadcast_key);
371 #else
372 	clnt_broadcast_result = clnt_broadcast_result_main;
373 #endif
374 	return (*clnt_broadcast_result)(resultp,
375 				(struct sockaddr_in *)addr->buf);
376 }
377 
378 #ifdef _REENTRANT
379 static once_t clnt_broadcast_once = ONCE_INITIALIZER;
380 
381 static void
clnt_broadcast_setup(void)382 clnt_broadcast_setup(void)
383 {
384 
385 	thr_keycreate(&clnt_broadcast_key, free);
386 }
387 #endif
388 
389 /*
390  * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
391  */
392 enum clnt_stat
clnt_broadcast(u_long prog,u_long vers,u_long proc,xdrproc_t xargs,caddr_t argsp,xdrproc_t xresults,caddr_t resultsp,resultproc_t eachresult)393 clnt_broadcast(
394 	u_long		prog,		/* program number */
395 	u_long		vers,		/* version number */
396 	u_long		proc,		/* procedure number */
397 	xdrproc_t	xargs,		/* xdr routine for args */
398 	caddr_t		argsp,		/* pointer to args */
399 	xdrproc_t	xresults,	/* xdr routine for results */
400 	caddr_t		resultsp,	/* pointer to results */
401 	resultproc_t	eachresult)	/* call with each result obtained */
402 {
403 #ifdef _REENTRANT
404 	if (__isthreaded == 0)
405 		clnt_broadcast_result_main = eachresult;
406 	else {
407 		thr_once(&clnt_broadcast_once, clnt_broadcast_setup);
408 		thr_setspecific(clnt_broadcast_key, (void *) eachresult);
409 	}
410 #else
411 	clnt_broadcast_result_main = eachresult;
412 #endif
413 	return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
414 	    (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
415 	    (resultproc_t) rpc_wrap_bcast, "udp");
416 }
417 
418 #endif /* PORTMAP */
419