3 * Copyright (c) 2009, Sun Microsystems, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * - Neither the name of Sun Microsystems, Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
31 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
32 * In addition, portions of such source code were derived from Berkeley
33 * 4.3 BSD under license from the Regents of the University of
41 * The backward compatibility routines for the earlier implementation
42 * of RPC, where the only transports supported were tcp/ip and udp/ip.
43 * Based on berkeley socket abstraction, now implemented on the top
47 #include <reentrant.h>
48 #include <sys/types.h>
49 #include <sys/socket.h>
52 #include <rpc/pmap_clnt.h>
53 #include <rpc/pmap_prot.h>
54 #include <rpc/nettype.h>
56 #include <netinet/in.h>
67 extern mutex_t rpcsoc_lock;
69 static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t,
70 int *, u_int, u_int, char *, int);
71 static SVCXPRT *svc_com_create(int, u_int, u_int, char *);
72 static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *);
75 #define IN4_LOCALHOST_STRING "127.0.0.1"
76 #define IN6_LOCALHOST_STRING "::1"
79 * A common clnt create routine
82 clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp, flags)
83 struct sockaddr_in *raddr;
95 struct netconfig *nconf;
96 struct netbuf bindaddr;
98 mutex_lock(&rpcsoc_lock);
99 if ((nconf = __rpc_getconfip(tp)) == NULL) {
100 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
101 mutex_unlock(&rpcsoc_lock);
104 if (fd == RPC_ANYSOCK) {
105 static int have_cloexec;
106 fd = __rpc_nconf2fd_flags(nconf, flags);
108 if ((flags & SOCK_CLOEXEC) && have_cloexec <= 0) {
109 fd = __rpc_nconf2fd(nconf);
112 if (flags & SOCK_CLOEXEC) {
114 fcntl(fd, F_SETFD, FD_CLOEXEC);
118 } else if (flags & SOCK_CLOEXEC)
123 if (raddr->sin_port == 0) {
127 mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */
128 proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
129 sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
134 raddr->sin_port = htons(sport);
135 mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */
138 /* Transform sockaddr_in to netbuf */
139 bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in);
140 bindaddr.buf = raddr;
142 bindresvport(fd, NULL);
143 cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
146 if (madefd == TRUE) {
148 * The fd should be closed while destroying the handle.
150 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
153 (void) freenetconfigent(nconf);
154 mutex_unlock(&rpcsoc_lock);
160 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
161 rpc_createerr.cf_error.re_errno = errno;
163 err: if (madefd == TRUE)
165 (void) freenetconfigent(nconf);
166 mutex_unlock(&rpcsoc_lock);
171 __libc_clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz, flags)
172 struct sockaddr_in *raddr;
183 cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
184 sendsz, recvsz, "udp", flags);
188 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
193 clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
194 struct sockaddr_in *raddr;
204 cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
205 sendsz, recvsz, "udp", 0);
209 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
214 clntudp_create(raddr, program, version, wait, sockp)
215 struct sockaddr_in *raddr;
221 return clntudp_bufcreate(raddr, program, version, wait, sockp, UDPMSGSIZE, UDPMSGSIZE);
225 clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
226 struct sockaddr_in *raddr;
233 return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
234 sendsz, recvsz, "tcp", 0);
237 /* IPv6 version of clnt*_*create */
239 #ifdef INET6_NOT_USED
242 clntudp6_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
243 struct sockaddr_in6 *raddr;
253 cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
254 sendsz, recvsz, "udp6", 0);
258 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
263 clntudp6_create(raddr, program, version, wait, sockp)
264 struct sockaddr_in6 *raddr;
270 return clntudp6_bufcreate(raddr, program, version, wait, sockp, UDPMSGSIZE, UDPMSGSIZE);
274 clnttcp6_create(raddr, prog, vers, sockp, sendsz, recvsz)
275 struct sockaddr_in6 *raddr;
282 return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
283 sendsz, recvsz, "tcp6", 0);
289 clntraw_create(prog, vers)
293 return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
297 * A common server create routine
300 svc_com_create(fd, sendsize, recvsize, netid)
306 struct netconfig *nconf;
310 struct sockaddr_in sin;
312 if ((nconf = __rpc_getconfip(netid)) == NULL) {
313 (void) syslog(LOG_ERR, "Could not get %s transport", netid);
316 if (fd == RPC_ANYSOCK) {
317 fd = __rpc_nconf2fd(nconf);
319 (void) freenetconfigent(nconf);
320 (void) syslog(LOG_ERR,
321 "svc%s_create: could not open connection", netid);
327 memset(&sin, 0, sizeof sin);
328 sin.sin_family = AF_INET;
329 bindresvport(fd, &sin);
330 listen(fd, SOMAXCONN);
331 svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
332 (void) freenetconfigent(nconf);
338 port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
339 svc->xp_port = ntohs(port);
344 svctcp_create(fd, sendsize, recvsize)
350 return svc_com_create(fd, sendsize, recvsize, "tcp");
356 svcudp_bufcreate(fd, sendsz, recvsz)
358 u_int sendsz, recvsz;
361 return svc_com_create(fd, sendsz, recvsz, "udp");
367 svcfd_create(fd, sendsize, recvsize)
373 return svc_fd_create(fd, sendsize, recvsize);
382 return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
390 return svc_raw_create();
395 #ifdef INET6_NOT_USED
397 svcudp6_bufcreate(fd, sendsz, recvsz)
399 u_int sendsz, recvsz;
401 return svc_com_create(fd, sendsz, recvsz, "udp6");
406 svctcp6_create(fd, sendsize, recvsize)
411 return svc_com_create(fd, sendsize, recvsize, "tcp6");
419 return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp6");
425 struct sockaddr_in *addr;
428 memset((void *) addr, 0, sizeof(*addr));
429 addr->sin_family = AF_INET;
430 addr->sin_port = htons(PMAPPORT);
431 addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
436 * For connectionless "udp" transport. Obsoleted by rpc_call().
439 callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
441 int prognum, versnum, procnum;
442 xdrproc_t inproc, outproc;
446 return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
447 (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
451 * For connectionless kind of transport. Obsoleted by rpc_reg()
454 registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
455 int prognum, versnum, procnum;
456 char *(*progname)(char [UDPMSGSIZE]);
457 xdrproc_t inproc, outproc;
460 return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
461 (rpcproc_t)procnum, progname, inproc, outproc, "udp");
465 * All the following clnt_broadcast stuff is convulated; it supports
466 * the earlier calling style of the callback function
468 extern thread_key_t clnt_broadcast_key;
471 * Need to translate the netbuf address into sockaddr_in address.
472 * Dont care about netid here.
476 rpc_wrap_bcast(resultp, addr, nconf)
477 char *resultp; /* results of the call */
478 struct netbuf *addr; /* address of the guy who responded */
479 struct netconfig *nconf; /* Netconf of the transport */
481 resultproc_t clnt_broadcast_result;
483 if (strcmp(nconf->nc_netid, "udp"))
485 clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key);
486 return (*clnt_broadcast_result)(resultp,
487 (struct sockaddr_in *)addr->buf);
491 * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
494 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
495 u_long prog; /* program number */
496 u_long vers; /* version number */
497 u_long proc; /* procedure number */
498 xdrproc_t xargs; /* xdr routine for args */
499 void *argsp; /* pointer to args */
500 xdrproc_t xresults; /* xdr routine for results */
501 void *resultsp; /* pointer to results */
502 resultproc_t eachresult; /* call with each result obtained */
504 extern mutex_t tsd_lock;
506 if (clnt_broadcast_key == -1) {
507 mutex_lock(&tsd_lock);
508 if (clnt_broadcast_key == -1)
509 thr_keycreate(&clnt_broadcast_key, free);
510 mutex_unlock(&tsd_lock);
512 thr_setspecific(clnt_broadcast_key, (void *) eachresult);
513 return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
514 (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
515 (resultproc_t) rpc_wrap_bcast, "udp");
519 * Create the client des authentication object. Obsoleted by
520 * authdes_seccreate().
523 authdes_create(servername, window, syncaddr, ckey)
524 char *servername; /* network name of server */
525 u_int window; /* time to live */
526 struct sockaddr *syncaddr; /* optional hostaddr to sync with */
527 des_block *ckey; /* optional conversation key to use */
531 char hostname[NI_MAXHOST];
535 * Change addr to hostname, because that is the way
536 * new interface takes it.
538 if (getnameinfo(syncaddr, sizeof(syncaddr), hostname,
539 sizeof hostname, NULL, 0, 0) != 0)
542 nauth = authdes_seccreate(servername, window, hostname, ckey);
546 dummy = authdes_seccreate(servername, window, NULL, ckey);
551 * Create a client handle for a unix connection. Obsoleted by clnt_vc_create()
554 clntunix_create(raddr, prog, vers, sockp, sendsz, recvsz)
555 struct sockaddr_un *raddr;
562 struct netbuf *svcaddr;
563 struct netconfig *nconf;
570 if (((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) ||
571 ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) {
574 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
575 rpc_createerr.cf_error.re_errno = errno;
579 *sockp = socket(AF_LOCAL, SOCK_STREAM, 0);
580 len = SUN_LEN(raddr);
581 if ((*sockp < 0) || (connect(*sockp,
582 (struct sockaddr *)raddr, len) < 0)) {
583 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
584 rpc_createerr.cf_error.re_errno = errno;
590 svcaddr->buf = raddr;
591 svcaddr->len = sizeof(raddr);
592 svcaddr->maxlen = sizeof (struct sockaddr_un);
593 cl = clnt_vc_create(*sockp, svcaddr, prog,
594 vers, sendsz, recvsz);
602 * Creates, registers, and returns a (rpc) unix based transporter.
603 * Obsoleted by svc_vc_create().
606 svcunix_create(sock, sendsize, recvsize, path)
612 struct netconfig *nconf;
614 struct sockaddr_un sun;
620 xprt = (SVCXPRT *)NULL;
621 localhandle = setnetconfig();
622 while ((nconf = getnetconfig(localhandle)) != NULL) {
623 if (nconf->nc_protofmly != NULL &&
624 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
630 if ((sock = __rpc_nconf2fd(nconf)) < 0)
633 memset(&sun, 0, sizeof sun);
634 sun.sun_family = AF_LOCAL;
635 strncpy(sun.sun_path, path, sizeof(sun.sun_path));
636 addrlen = sizeof(struct sockaddr_un);
637 sa = (struct sockaddr *)&sun;
639 if (bind(sock, sa, addrlen) < 0)
642 taddr.addr.len = taddr.addr.maxlen = addrlen;
643 taddr.addr.buf = malloc(addrlen);
644 if (taddr.addr.buf == NULL)
646 memcpy(taddr.addr.buf, sa, addrlen);
648 if (nconf->nc_semantics != NC_TPI_CLTS) {
649 if (listen(sock, SOMAXCONN) < 0) {
650 free(taddr.addr.buf);
655 xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize);
658 endnetconfig(localhandle);
663 * Like svunix_create(), except the routine takes any *open* UNIX file
664 * descriptor as its first input. Obsoleted by svc_fd_create();
667 svcunixfd_create(fd, sendsize, recvsize)
672 return (svc_fd_create(fd, sendsize, recvsize));