1 /* $NetBSD: rpc_soc.c,v 1.6 2000/07/06 03:10:35 christos Exp $ */
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.
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.
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.
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.
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.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 /* #ident "@(#)rpc_soc.c 1.17 94/04/24 SMI" */
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
41 #if defined(LIBC_SCCS) && !defined(lint)
42 static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
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
57 #include "namespace.h"
58 #include "reentrant.h"
59 #include <sys/types.h>
60 #include <sys/socket.h>
63 #include <rpc/pmap_clnt.h>
64 #include <rpc/pmap_prot.h>
65 #include <rpc/nettype.h>
67 #include <netinet/in.h>
74 #include "un-namespace.h"
79 static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t,
80 int *, u_int, u_int, char *);
81 static SVCXPRT *svc_com_create(int, u_int, u_int, char *);
82 static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *);
85 #define IN4_LOCALHOST_STRING "127.0.0.1"
86 #define IN6_LOCALHOST_STRING "::1"
89 * A common clnt create routine
92 clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp)
93 struct sockaddr_in *raddr;
104 struct netconfig *nconf;
105 struct netbuf bindaddr;
107 mutex_lock(&rpcsoc_lock);
108 if ((nconf = __rpc_getconfip(tp)) == NULL) {
109 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
110 mutex_unlock(&rpcsoc_lock);
113 if (fd == RPC_ANYSOCK) {
114 fd = __rpc_nconf2fd(nconf);
120 if (raddr->sin_port == 0) {
124 mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */
125 proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
126 sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
131 raddr->sin_port = htons(sport);
132 mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */
135 /* Transform sockaddr_in to netbuf */
136 bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in);
137 bindaddr.buf = raddr;
139 bindresvport(fd, NULL);
140 cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
143 if (madefd == TRUE) {
145 * The fd should be closed while destroying the handle.
147 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
150 (void) freenetconfigent(nconf);
151 mutex_unlock(&rpcsoc_lock);
157 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
158 rpc_createerr.cf_error.re_errno = errno;
160 err: if (madefd == TRUE)
162 (void) freenetconfigent(nconf);
163 mutex_unlock(&rpcsoc_lock);
168 clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
169 struct sockaddr_in *raddr;
179 cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
180 sendsz, recvsz, "udp");
184 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
189 clntudp_create(raddr, program, version, wait, sockp)
190 struct sockaddr_in *raddr;
197 return clntudp_bufcreate(raddr, program, version, wait, sockp,
198 UDPMSGSIZE, UDPMSGSIZE);
202 clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
203 struct sockaddr_in *raddr;
211 return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
212 sendsz, recvsz, "tcp");
216 clntraw_create(prog, vers)
221 return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
225 * A common server create routine
228 svc_com_create(fd, sendsize, recvsize, netid)
234 struct netconfig *nconf;
238 struct sockaddr_in sin;
240 if ((nconf = __rpc_getconfip(netid)) == NULL) {
241 (void) syslog(LOG_ERR, "Could not get %s transport", netid);
244 if (fd == RPC_ANYSOCK) {
245 fd = __rpc_nconf2fd(nconf);
247 (void) freenetconfigent(nconf);
248 (void) syslog(LOG_ERR,
249 "svc%s_create: could not open connection", netid);
255 memset(&sin, 0, sizeof sin);
256 sin.sin_family = AF_INET;
257 bindresvport(fd, &sin);
258 _listen(fd, SOMAXCONN);
259 svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
260 (void) freenetconfigent(nconf);
266 port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
267 svc->xp_port = ntohs(port);
272 svctcp_create(fd, sendsize, recvsize)
278 return svc_com_create(fd, sendsize, recvsize, "tcp");
282 svcudp_bufcreate(fd, sendsz, recvsz)
284 u_int sendsz, recvsz;
287 return svc_com_create(fd, sendsz, recvsz, "udp");
291 svcfd_create(fd, sendsize, recvsize)
297 return svc_fd_create(fd, sendsize, recvsize);
306 return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
313 return svc_raw_create();
318 struct sockaddr_in *addr;
321 memset((void *) addr, 0, sizeof(*addr));
322 addr->sin_family = AF_INET;
323 addr->sin_port = htons(PMAPPORT);
324 addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
329 * For connectionless "udp" transport. Obsoleted by rpc_call().
332 callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
334 int prognum, versnum, procnum;
335 xdrproc_t inproc, outproc;
339 return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
340 (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
344 * For connectionless kind of transport. Obsoleted by rpc_reg()
347 registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
348 int prognum, versnum, procnum;
349 char *(*progname)(char [UDPMSGSIZE]);
350 xdrproc_t inproc, outproc;
353 return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
354 (rpcproc_t)procnum, progname, inproc, outproc, "udp");
358 * All the following clnt_broadcast stuff is convulated; it supports
359 * the earlier calling style of the callback function
361 static thread_key_t clnt_broadcast_key;
362 static resultproc_t clnt_broadcast_result_main;
363 static once_t clnt_broadcast_once = ONCE_INITIALIZER;
366 clnt_broadcast_key_init(void)
369 thr_keycreate(&clnt_broadcast_key, free);
373 * Need to translate the netbuf address into sockaddr_in address.
374 * Dont care about netid here.
378 rpc_wrap_bcast(resultp, addr, nconf)
379 char *resultp; /* results of the call */
380 struct netbuf *addr; /* address of the guy who responded */
381 struct netconfig *nconf; /* Netconf of the transport */
383 resultproc_t clnt_broadcast_result;
385 if (strcmp(nconf->nc_netid, "udp"))
388 clnt_broadcast_result = clnt_broadcast_result_main;
390 clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key);
391 return (*clnt_broadcast_result)(resultp,
392 (struct sockaddr_in *)addr->buf);
396 * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
399 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
400 u_long prog; /* program number */
401 u_long vers; /* version number */
402 u_long proc; /* procedure number */
403 xdrproc_t xargs; /* xdr routine for args */
404 void *argsp; /* pointer to args */
405 xdrproc_t xresults; /* xdr routine for results */
406 void *resultsp; /* pointer to results */
407 resultproc_t eachresult; /* call with each result obtained */
411 clnt_broadcast_result_main = eachresult;
413 thr_once(&clnt_broadcast_once, clnt_broadcast_key_init);
414 thr_setspecific(clnt_broadcast_key, (void *) eachresult);
416 return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
417 (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
418 (resultproc_t) rpc_wrap_bcast, "udp");
422 * Create the client des authentication object. Obsoleted by
423 * authdes_seccreate().
426 authdes_create(servername, window, syncaddr, ckey)
427 char *servername; /* network name of server */
428 u_int window; /* time to live */
429 struct sockaddr *syncaddr; /* optional hostaddr to sync with */
430 des_block *ckey; /* optional conversation key to use */
434 char hostname[NI_MAXHOST];
438 * Change addr to hostname, because that is the way
439 * new interface takes it.
441 if (getnameinfo(syncaddr, syncaddr->sa_len, hostname,
442 sizeof hostname, NULL, 0, 0) != 0)
445 nauth = authdes_seccreate(servername, window, hostname, ckey);
449 dummy = authdes_seccreate(servername, window, NULL, ckey);
454 * Create a client handle for a unix connection. Obsoleted by clnt_vc_create()
457 clntunix_create(raddr, prog, vers, sockp, sendsz, recvsz)
458 struct sockaddr_un *raddr;
465 struct netbuf *svcaddr;
466 struct netconfig *nconf;
473 if ((raddr->sun_len == 0) ||
474 ((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) ||
475 ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) {
478 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
479 rpc_createerr.cf_error.re_errno = errno;
483 *sockp = _socket(AF_LOCAL, SOCK_STREAM, 0);
484 len = raddr->sun_len = SUN_LEN(raddr);
485 if ((*sockp < 0) || (_connect(*sockp,
486 (struct sockaddr *)raddr, len) < 0)) {
487 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
488 rpc_createerr.cf_error.re_errno = errno;
490 (void)_close(*sockp);
494 svcaddr->buf = raddr;
495 svcaddr->len = raddr->sun_len;
496 svcaddr->maxlen = sizeof (struct sockaddr_un);
497 cl = clnt_vc_create(*sockp, svcaddr, prog,
498 vers, sendsz, recvsz);
506 * Creates, registers, and returns a (rpc) unix based transporter.
507 * Obsoleted by svc_vc_create().
510 svcunix_create(sock, sendsize, recvsize, path)
516 struct netconfig *nconf;
518 struct sockaddr_un sun;
524 xprt = (SVCXPRT *)NULL;
525 localhandle = setnetconfig();
526 while ((nconf = getnetconfig(localhandle)) != NULL) {
527 if (nconf->nc_protofmly != NULL &&
528 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
534 if ((sock = __rpc_nconf2fd(nconf)) < 0)
537 memset(&sun, 0, sizeof sun);
538 sun.sun_family = AF_LOCAL;
539 if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
540 sizeof(sun.sun_path))
542 sun.sun_len = SUN_LEN(&sun);
543 addrlen = sizeof (struct sockaddr_un);
544 sa = (struct sockaddr *)&sun;
546 if (_bind(sock, sa, addrlen) < 0)
549 taddr.addr.len = taddr.addr.maxlen = addrlen;
550 taddr.addr.buf = malloc(addrlen);
551 if (taddr.addr.buf == NULL)
553 memcpy(taddr.addr.buf, sa, addrlen);
555 if (nconf->nc_semantics != NC_TPI_CLTS) {
556 if (_listen(sock, SOMAXCONN) < 0) {
557 free(taddr.addr.buf);
562 xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize);
565 endnetconfig(localhandle);
570 * Like svunix_create(), except the routine takes any *open* UNIX file
571 * descriptor as its first input. Obsoleted by svc_fd_create();
574 svcunixfd_create(fd, sendsize, recvsize)
579 return (svc_fd_create(fd, sendsize, recvsize));