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);
109 if ((flags & SOCK_CLOEXEC) && have_cloexec <= 0) {
110 fd = __rpc_nconf2fd(nconf);
113 if (flags & SOCK_CLOEXEC) {
115 fcntl(fd, F_SETFD, FD_CLOEXEC);
119 } else if (flags & SOCK_CLOEXEC)
128 if (raddr->sin_port == 0) {
132 mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */
133 proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
134 sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
139 raddr->sin_port = htons(sport);
140 mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */
143 /* Transform sockaddr_in to netbuf */
144 bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in);
145 bindaddr.buf = raddr;
147 bindresvport(fd, NULL);
148 cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
151 if (madefd == TRUE) {
153 * The fd should be closed while destroying the handle.
155 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
158 (void) freenetconfigent(nconf);
159 mutex_unlock(&rpcsoc_lock);
165 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
166 rpc_createerr.cf_error.re_errno = errno;
168 err: if (madefd == TRUE)
170 (void) freenetconfigent(nconf);
171 mutex_unlock(&rpcsoc_lock);
176 __libc_clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz, flags)
177 struct sockaddr_in *raddr;
188 cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
189 sendsz, recvsz, "udp", flags);
193 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
198 clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
199 struct sockaddr_in *raddr;
209 cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
210 sendsz, recvsz, "udp", 0);
214 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
219 clntudp_create(raddr, program, version, wait, sockp)
220 struct sockaddr_in *raddr;
226 return clntudp_bufcreate(raddr, program, version, wait, sockp, UDPMSGSIZE, UDPMSGSIZE);
230 clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
231 struct sockaddr_in *raddr;
238 return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
239 sendsz, recvsz, "tcp", 0);
242 /* IPv6 version of clnt*_*create */
244 #ifdef INET6_NOT_USED
247 clntudp6_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
248 struct sockaddr_in6 *raddr;
258 cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
259 sendsz, recvsz, "udp6", 0);
263 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
268 clntudp6_create(raddr, program, version, wait, sockp)
269 struct sockaddr_in6 *raddr;
275 return clntudp6_bufcreate(raddr, program, version, wait, sockp, UDPMSGSIZE, UDPMSGSIZE);
279 clnttcp6_create(raddr, prog, vers, sockp, sendsz, recvsz)
280 struct sockaddr_in6 *raddr;
287 return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
288 sendsz, recvsz, "tcp6", 0);
294 clntraw_create(prog, vers)
298 return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
302 * A common server create routine
305 svc_com_create(fd, sendsize, recvsize, netid)
311 struct netconfig *nconf;
315 struct sockaddr_in sin;
317 if ((nconf = __rpc_getconfip(netid)) == NULL) {
318 (void) syslog(LOG_ERR, "Could not get %s transport", netid);
321 if (fd == RPC_ANYSOCK) {
322 fd = __rpc_nconf2fd(nconf);
324 (void) freenetconfigent(nconf);
325 (void) syslog(LOG_ERR,
326 "svc%s_create: could not open connection", netid);
332 memset(&sin, 0, sizeof sin);
333 sin.sin_family = AF_INET;
334 bindresvport(fd, &sin);
335 listen(fd, SOMAXCONN);
336 svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
337 (void) freenetconfigent(nconf);
343 port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
344 svc->xp_port = ntohs(port);
349 svctcp_create(fd, sendsize, recvsize)
355 return svc_com_create(fd, sendsize, recvsize, "tcp");
361 svcudp_bufcreate(fd, sendsz, recvsz)
363 u_int sendsz, recvsz;
366 return svc_com_create(fd, sendsz, recvsz, "udp");
372 svcfd_create(fd, sendsize, recvsize)
378 return svc_fd_create(fd, sendsize, recvsize);
387 return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
395 return svc_raw_create();
400 #ifdef INET6_NOT_USED
402 svcudp6_bufcreate(fd, sendsz, recvsz)
404 u_int sendsz, recvsz;
406 return svc_com_create(fd, sendsz, recvsz, "udp6");
411 svctcp6_create(fd, sendsize, recvsize)
416 return svc_com_create(fd, sendsize, recvsize, "tcp6");
424 return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp6");
430 struct sockaddr_in *addr;
433 memset((void *) addr, 0, sizeof(*addr));
434 addr->sin_family = AF_INET;
435 addr->sin_port = htons(PMAPPORT);
436 addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
441 * For connectionless "udp" transport. Obsoleted by rpc_call().
444 callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
446 int prognum, versnum, procnum;
447 xdrproc_t inproc, outproc;
451 return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
452 (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
456 * For connectionless kind of transport. Obsoleted by rpc_reg()
459 registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
460 int prognum, versnum, procnum;
461 char *(*progname)(char [UDPMSGSIZE]);
462 xdrproc_t inproc, outproc;
465 return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
466 (rpcproc_t)procnum, progname, inproc, outproc, "udp");
470 * All the following clnt_broadcast stuff is convulated; it supports
471 * the earlier calling style of the callback function
473 extern thread_key_t clnt_broadcast_key;
476 * Need to translate the netbuf address into sockaddr_in address.
477 * Dont care about netid here.
481 rpc_wrap_bcast(resultp, addr, nconf)
482 char *resultp; /* results of the call */
483 struct netbuf *addr; /* address of the guy who responded */
484 struct netconfig *nconf; /* Netconf of the transport */
486 resultproc_t clnt_broadcast_result;
488 if (strcmp(nconf->nc_netid, "udp"))
490 clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key);
491 return (*clnt_broadcast_result)(resultp,
492 (struct sockaddr_in *)addr->buf);
496 * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
499 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
500 u_long prog; /* program number */
501 u_long vers; /* version number */
502 u_long proc; /* procedure number */
503 xdrproc_t xargs; /* xdr routine for args */
504 void *argsp; /* pointer to args */
505 xdrproc_t xresults; /* xdr routine for results */
506 void *resultsp; /* pointer to results */
507 resultproc_t eachresult; /* call with each result obtained */
509 extern mutex_t tsd_lock;
511 if (clnt_broadcast_key == KEY_INITIALIZER) {
512 mutex_lock(&tsd_lock);
513 if (clnt_broadcast_key == KEY_INITIALIZER)
514 thr_keycreate(&clnt_broadcast_key, free);
515 mutex_unlock(&tsd_lock);
517 thr_setspecific(clnt_broadcast_key, (void *) eachresult);
518 return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
519 (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
520 (resultproc_t) rpc_wrap_bcast, "udp");
524 * Create the client des authentication object. Obsoleted by
525 * authdes_seccreate().
528 authdes_create(servername, window, syncaddr, ckey)
529 char *servername; /* network name of server */
530 u_int window; /* time to live */
531 struct sockaddr *syncaddr; /* optional hostaddr to sync with */
532 des_block *ckey; /* optional conversation key to use */
536 char hostname[NI_MAXHOST];
540 * Change addr to hostname, because that is the way
541 * new interface takes it.
543 if (getnameinfo(syncaddr, sizeof(syncaddr), hostname,
544 sizeof hostname, NULL, 0, 0) != 0)
547 nauth = authdes_seccreate(servername, window, hostname, ckey);
551 dummy = authdes_seccreate(servername, window, NULL, ckey);
556 * Create a client handle for a unix connection. Obsoleted by clnt_vc_create()
559 clntunix_create(raddr, prog, vers, sockp, sendsz, recvsz)
560 struct sockaddr_un *raddr;
567 struct netbuf *svcaddr;
573 if (((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) ||
574 ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) {
577 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
578 rpc_createerr.cf_error.re_errno = errno;
582 *sockp = socket(AF_LOCAL, SOCK_STREAM, 0);
583 len = SUN_LEN(raddr);
584 if ((*sockp < 0) || (connect(*sockp,
585 (struct sockaddr *)raddr, len) < 0)) {
586 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
587 rpc_createerr.cf_error.re_errno = errno;
593 svcaddr->buf = raddr;
594 svcaddr->len = sizeof(raddr);
595 svcaddr->maxlen = sizeof (struct sockaddr_un);
596 cl = clnt_vc_create(*sockp, svcaddr, prog,
597 vers, sendsz, recvsz);
605 * Creates, registers, and returns a (rpc) unix based transporter.
606 * Obsoleted by svc_vc_create().
609 svcunix_create(sock, sendsize, recvsize, path)
615 struct netconfig *nconf;
617 struct sockaddr_un sun;
623 xprt = (SVCXPRT *)NULL;
624 localhandle = setnetconfig();
625 while ((nconf = getnetconfig(localhandle)) != NULL) {
626 if (nconf->nc_protofmly != NULL &&
627 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
633 if ((sock = __rpc_nconf2fd(nconf)) < 0)
636 memset(&sun, 0, sizeof sun);
637 sun.sun_family = AF_LOCAL;
638 strncpy(sun.sun_path, path, sizeof(sun.sun_path));
639 addrlen = sizeof(struct sockaddr_un);
640 sa = (struct sockaddr *)&sun;
642 if (bind(sock, sa, addrlen) < 0)
645 taddr.addr.len = taddr.addr.maxlen = addrlen;
646 taddr.addr.buf = malloc(addrlen);
647 if (taddr.addr.buf == NULL)
649 memcpy(taddr.addr.buf, sa, addrlen);
651 if (nconf->nc_semantics != NC_TPI_CLTS) {
652 if (listen(sock, SOMAXCONN) < 0) {
653 free(taddr.addr.buf);
658 xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize);
661 endnetconfig(localhandle);
666 * Like svunix_create(), except the routine takes any *open* UNIX file
667 * descriptor as its first input. Obsoleted by svc_fd_create();
670 svcunixfd_create(fd, sendsize, recvsize)
675 return (svc_fd_create(fd, sendsize, recvsize));