xref: /netbsd/lib/libc/rpc/rpc_soc.c (revision bf9ec67e)
1 /*	$NetBSD: rpc_soc.c,v 1.8 2001/11/04 13:57:30 lukem Exp $	*/
2 
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 
32 /* #ident	"@(#)rpc_soc.c	1.17	94/04/24 SMI" */
33 
34 /*
35  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
36  * In addition, portions of such source code were derived from Berkeley
37  * 4.3 BSD under license from the Regents of the University of
38  * California.
39  */
40 
41 #if 0
42 #if !defined(lint) && defined(SCCSIDS)
43 static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro";
44 #endif
45 #endif
46 
47 #ifdef PORTMAP
48 /*
49  * rpc_soc.c
50  *
51  * The backward compatibility routines for the earlier implementation
52  * of RPC, where the only transports supported were tcp/ip and udp/ip.
53  * Based on berkeley socket abstraction, now implemented on the top
54  * of TLI/Streams
55  */
56 
57 #include "namespace.h"
58 #include "reentrant.h"
59 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <stdio.h>
62 #include <rpc/rpc.h>
63 #include <rpc/pmap_clnt.h>
64 #include <rpc/pmap_prot.h>
65 #include <rpc/nettype.h>
66 #include <netinet/in.h>
67 #include <assert.h>
68 #include <errno.h>
69 #include <netdb.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <syslog.h>
73 #include <unistd.h>
74 
75 #include "rpc_com.h"
76 
77 #ifdef __weak_alias
78 __weak_alias(clntudp_bufcreate,_clntudp_bufcreate)
79 __weak_alias(clntudp_create,_clntudp_create)
80 __weak_alias(clnttcp_create,_clnttcp_create)
81 __weak_alias(clntraw_create,_clntraw_create)
82 __weak_alias(get_myaddress,_get_myaddress)
83 __weak_alias(svcfd_create,_svcfd_create)
84 __weak_alias(svcudp_bufcreate,_svcudp_bufcreate)
85 __weak_alias(svcudp_create,_svcudp_create)
86 __weak_alias(svctcp_create,_svctcp_create)
87 __weak_alias(svcraw_create,_svcraw_create)
88 __weak_alias(callrpc,_callrpc)
89 __weak_alias(registerrpc,_registerrpc)
90 __weak_alias(clnt_broadcast,_clnt_broadcast)
91 #endif
92 
93 #ifdef __REENT
94 extern mutex_t	rpcsoc_lock;
95 #endif
96 
97 static CLIENT *clnt_com_create __P((struct sockaddr_in *, rpcprog_t, rpcvers_t,
98 				    int *, u_int, u_int, char *));
99 static SVCXPRT *svc_com_create __P((int, u_int, u_int, char *));
100 static bool_t rpc_wrap_bcast __P((char *, struct netbuf *, struct netconfig *));
101 
102 /*
103  * A common clnt create routine
104  */
105 static CLIENT *
106 clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp)
107 	struct sockaddr_in *raddr;
108 	rpcprog_t prog;
109 	rpcvers_t vers;
110 	int *sockp;
111 	u_int sendsz;
112 	u_int recvsz;
113 	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 	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 *
188 clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
189 	struct sockaddr_in *raddr;
190 	u_long prog;
191 	u_long vers;
192 	struct timeval wait;
193 	int *sockp;
194 	u_int sendsz;
195 	u_int recvsz;
196 {
197 	CLIENT *cl;
198 
199 	_DIAGASSERT(raddr != NULL);
200 	_DIAGASSERT(sockp != NULL);
201 
202 	cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
203 	    sendsz, recvsz, "udp");
204 	if (cl == NULL) {
205 		return (NULL);
206 	}
207 	(void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, (char *)(void *)&wait);
208 	return (cl);
209 }
210 
211 CLIENT *
212 clntudp_create(raddr, program, version, wait, sockp)
213 	struct sockaddr_in *raddr;
214 	u_long program;
215 	u_long version;
216 	struct timeval wait;
217 	int *sockp;
218 {
219 	return clntudp_bufcreate(raddr, program, version, wait, sockp,
220 					UDPMSGSIZE, UDPMSGSIZE);
221 }
222 
223 CLIENT *
224 clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
225 	struct sockaddr_in *raddr;
226 	u_long prog;
227 	u_long vers;
228 	int *sockp;
229 	u_int sendsz;
230 	u_int recvsz;
231 {
232 	return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
233 	    sendsz, recvsz, "tcp");
234 }
235 
236 CLIENT *
237 clntraw_create(prog, vers)
238 	u_long prog;
239 	u_long vers;
240 {
241 	return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
242 }
243 
244 /*
245  * A common server create routine
246  */
247 static SVCXPRT *
248 svc_com_create(fd, sendsize, recvsize, netid)
249 	int fd;
250 	u_int sendsize;
251 	u_int recvsize;
252 	char *netid;
253 {
254 	struct netconfig *nconf;
255 	SVCXPRT *svc;
256 	int madefd = FALSE;
257 	int port;
258 	struct sockaddr_in sccsin;
259 
260 	_DIAGASSERT(netid != NULL);
261 
262 	if ((nconf = __rpc_getconfip(netid)) == NULL) {
263 		(void) syslog(LOG_ERR, "Could not get %s transport", netid);
264 		return (NULL);
265 	}
266 	if (fd == RPC_ANYSOCK) {
267 		fd = __rpc_nconf2fd(nconf);
268 		if (fd == -1) {
269 			(void) freenetconfigent(nconf);
270 			(void) syslog(LOG_ERR,
271 			"svc%s_create: could not open connection", netid);
272 			return (NULL);
273 		}
274 		madefd = TRUE;
275 	}
276 
277 	memset(&sccsin, 0, sizeof sccsin);
278 	sccsin.sin_family = AF_INET;
279 	bindresvport(fd, &sccsin);
280 	listen(fd, SOMAXCONN);
281 	svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
282 	(void) freenetconfigent(nconf);
283 	if (svc == NULL) {
284 		if (madefd)
285 			(void) close(fd);
286 		return (NULL);
287 	}
288 	port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
289 	svc->xp_port = ntohs(port);
290 	return (svc);
291 }
292 
293 SVCXPRT *
294 svctcp_create(fd, sendsize, recvsize)
295 	int fd;
296 	u_int sendsize;
297 	u_int recvsize;
298 {
299 	return svc_com_create(fd, sendsize, recvsize, "tcp");
300 }
301 
302 SVCXPRT *
303 svcudp_bufcreate(fd, sendsz, recvsz)
304 	int fd;
305 	u_int sendsz, recvsz;
306 {
307 	return svc_com_create(fd, sendsz, recvsz, "udp");
308 }
309 
310 SVCXPRT *
311 svcfd_create(fd, sendsize, recvsize)
312 	int fd;
313 	u_int sendsize;
314 	u_int recvsize;
315 {
316 	return svc_fd_create(fd, sendsize, recvsize);
317 }
318 
319 
320 SVCXPRT *
321 svcudp_create(fd)
322 	int fd;
323 {
324 	return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
325 }
326 
327 SVCXPRT *
328 svcraw_create()
329 {
330 	return svc_raw_create();
331 }
332 
333 int
334 get_myaddress(addr)
335 	struct sockaddr_in *addr;
336 {
337 
338 	_DIAGASSERT(addr != NULL);
339 
340 	memset((void *) addr, 0, sizeof(*addr));
341 	addr->sin_family = AF_INET;
342 	addr->sin_port = htons(PMAPPORT);
343 	addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
344 	return (0);
345 }
346 
347 /*
348  * For connectionless "udp" transport. Obsoleted by rpc_call().
349  */
350 int
351 callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
352 	char *host;
353 	int prognum, versnum, procnum;
354 	xdrproc_t inproc, outproc;
355 	char *in, *out;
356 {
357 	return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
358 	    (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
359 }
360 
361 /*
362  * For connectionless kind of transport. Obsoleted by rpc_reg()
363  */
364 int
365 registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
366 	int prognum, versnum, procnum;
367 	char *(*progname) __P((char [UDPMSGSIZE]));
368 	xdrproc_t inproc, outproc;
369 {
370 	return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
371 	    (rpcproc_t)procnum, progname, inproc, outproc, "udp");
372 }
373 
374 /*
375  * All the following clnt_broadcast stuff is convulated; it supports
376  * the earlier calling style of the callback function
377  */
378 #ifdef __REENT
379 static thread_key_t	clnt_broadcast_key;
380 #endif
381 static resultproc_t	clnt_broadcast_result_main;
382 
383 /*
384  * Need to translate the netbuf address into sockaddr_in address.
385  * Dont care about netid here.
386  */
387 /* ARGSUSED */
388 static bool_t
389 rpc_wrap_bcast(resultp, addr, nconf)
390 	char *resultp;		/* results of the call */
391 	struct netbuf *addr;	/* address of the guy who responded */
392 	struct netconfig *nconf; /* Netconf of the transport */
393 {
394 	resultproc_t clnt_broadcast_result;
395 
396 	_DIAGASSERT(resultp != NULL);
397 	_DIAGASSERT(addr != NULL);
398 	_DIAGASSERT(nconf != NULL);
399 
400 	if (strcmp(nconf->nc_netid, "udp"))
401 		return (FALSE);
402 #ifdef __REENT
403 	if (_thr_main())
404 		clnt_broadcast_result = clnt_broadcast_result_main;
405 	else
406 		thr_getspecific(clnt_broadcast_key,
407 			(void **) &clnt_broadcast_result);
408 #else
409 	clnt_broadcast_result = clnt_broadcast_result_main;
410 #endif
411 	return (*clnt_broadcast_result)(resultp,
412 				(struct sockaddr_in *)addr->buf);
413 }
414 
415 /*
416  * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
417  */
418 enum clnt_stat
419 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
420 	u_long		prog;		/* program number */
421 	u_long		vers;		/* version number */
422 	u_long		proc;		/* procedure number */
423 	xdrproc_t	xargs;		/* xdr routine for args */
424 	caddr_t		argsp;		/* pointer to args */
425 	xdrproc_t	xresults;	/* xdr routine for results */
426 	caddr_t		resultsp;	/* pointer to results */
427 	resultproc_t	eachresult;	/* call with each result obtained */
428 {
429 #ifdef __REENT
430 	extern mutex_t tsd_lock;
431 #endif
432 
433 #ifdef __REENT
434 	if (_thr_main())
435 		clnt_broadcast_result_main = eachresult;
436 	else {
437 		if (clnt_broadcast_key == 0) {
438 			mutex_lock(&tsd_lock);
439 			if (clnt_broadcast_key == 0)
440 				thr_keycreate(&clnt_broadcast_key, free);
441 			mutex_unlock(&tsd_lock);
442 		}
443 		thr_setspecific(clnt_broadcast_key, (void *) eachresult);
444 	}
445 #else
446 	clnt_broadcast_result_main = eachresult;
447 #endif
448 	return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
449 	    (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
450 	    (resultproc_t) rpc_wrap_bcast, "udp");
451 }
452 
453 #endif /* PORTMAP */
454