1 /* $NetBSD: rpc_soc.c,v 1.6 2000/07/06 03:10:35 christos Exp $ */
4 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 /* #ident "@(#)rpc_soc.c 1.17 94/04/24 SMI" */
34 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 * In addition, portions of such source code were derived from Berkeley
36 * 4.3 BSD under license from the Regents of the University of
40 #if defined(LIBC_SCCS) && !defined(lint)
41 static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
50 * The backward compatibility routines for the earlier implementation
51 * of RPC, where the only transports supported were tcp/ip and udp/ip.
52 * Based on berkeley socket abstraction, now implemented on the top
56 #include "namespace.h"
57 #include "reentrant.h"
58 #include <sys/types.h>
59 #include <sys/socket.h>
62 #include <rpc/pmap_clnt.h>
63 #include <rpc/pmap_prot.h>
64 #include <rpc/nettype.h>
66 #include <netinet/in.h>
73 #include "un-namespace.h"
78 static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t,
79 int *, u_int, u_int, char *);
80 static SVCXPRT *svc_com_create(int, u_int, u_int, char *);
81 static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *);
84 #define IN4_LOCALHOST_STRING "127.0.0.1"
85 #define IN6_LOCALHOST_STRING "::1"
88 * A common clnt create routine
91 clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp)
92 struct sockaddr_in *raddr;
103 struct netconfig *nconf;
104 struct netbuf bindaddr;
106 mutex_lock(&rpcsoc_lock);
107 if ((nconf = __rpc_getconfip(tp)) == NULL) {
108 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
109 mutex_unlock(&rpcsoc_lock);
112 if (fd == RPC_ANYSOCK) {
113 fd = __rpc_nconf2fd(nconf);
119 if (raddr->sin_port == 0) {
123 mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */
124 proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
125 sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
130 raddr->sin_port = htons(sport);
131 mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */
134 /* Transform sockaddr_in to netbuf */
135 bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in);
136 bindaddr.buf = raddr;
138 bindresvport(fd, NULL);
139 cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
142 if (madefd == TRUE) {
144 * The fd should be closed while destroying the handle.
146 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
149 (void) freenetconfigent(nconf);
150 mutex_unlock(&rpcsoc_lock);
156 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
157 rpc_createerr.cf_error.re_errno = errno;
159 err: if (madefd == TRUE)
161 (void) freenetconfigent(nconf);
162 mutex_unlock(&rpcsoc_lock);
167 clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
168 struct sockaddr_in *raddr;
178 cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
179 sendsz, recvsz, "udp");
183 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
188 clntudp_create(raddr, program, version, wait, sockp)
189 struct sockaddr_in *raddr;
196 return clntudp_bufcreate(raddr, program, version, wait, sockp,
197 UDPMSGSIZE, UDPMSGSIZE);
201 clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
202 struct sockaddr_in *raddr;
210 return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
211 sendsz, recvsz, "tcp");
215 clntraw_create(prog, vers)
220 return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
224 * A common server create routine
227 svc_com_create(fd, sendsize, recvsize, netid)
233 struct netconfig *nconf;
237 struct sockaddr_in sin;
239 if ((nconf = __rpc_getconfip(netid)) == NULL) {
240 (void) syslog(LOG_ERR, "Could not get %s transport", netid);
243 if (fd == RPC_ANYSOCK) {
244 fd = __rpc_nconf2fd(nconf);
246 (void) freenetconfigent(nconf);
247 (void) syslog(LOG_ERR,
248 "svc%s_create: could not open connection", netid);
254 memset(&sin, 0, sizeof sin);
255 sin.sin_family = AF_INET;
256 bindresvport(fd, &sin);
257 _listen(fd, SOMAXCONN);
258 svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
259 (void) freenetconfigent(nconf);
265 port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
266 svc->xp_port = ntohs(port);
271 svctcp_create(fd, sendsize, recvsize)
277 return svc_com_create(fd, sendsize, recvsize, "tcp");
281 svcudp_bufcreate(fd, sendsz, recvsz)
283 u_int sendsz, recvsz;
286 return svc_com_create(fd, sendsz, recvsz, "udp");
290 svcfd_create(fd, sendsize, recvsize)
296 return svc_fd_create(fd, sendsize, recvsize);
305 return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
312 return svc_raw_create();
317 struct sockaddr_in *addr;
320 memset((void *) addr, 0, sizeof(*addr));
321 addr->sin_family = AF_INET;
322 addr->sin_port = htons(PMAPPORT);
323 addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
328 * For connectionless "udp" transport. Obsoleted by rpc_call().
331 callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
333 int prognum, versnum, procnum;
334 xdrproc_t inproc, outproc;
338 return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
339 (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
343 * For connectionless kind of transport. Obsoleted by rpc_reg()
346 registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
347 int prognum, versnum, procnum;
348 char *(*progname)(char [UDPMSGSIZE]);
349 xdrproc_t inproc, outproc;
352 return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
353 (rpcproc_t)procnum, progname, inproc, outproc, "udp");
357 * All the following clnt_broadcast stuff is convulated; it supports
358 * the earlier calling style of the callback function
360 static thread_key_t clnt_broadcast_key;
361 static resultproc_t clnt_broadcast_result_main;
362 static once_t clnt_broadcast_once = ONCE_INITIALIZER;
365 clnt_broadcast_key_init(void)
368 thr_keycreate(&clnt_broadcast_key, free);
372 * Need to translate the netbuf address into sockaddr_in address.
373 * Dont care about netid here.
377 rpc_wrap_bcast(resultp, addr, nconf)
378 char *resultp; /* results of the call */
379 struct netbuf *addr; /* address of the guy who responded */
380 struct netconfig *nconf; /* Netconf of the transport */
382 resultproc_t clnt_broadcast_result;
384 if (strcmp(nconf->nc_netid, "udp"))
387 clnt_broadcast_result = clnt_broadcast_result_main;
389 clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key);
390 return (*clnt_broadcast_result)(resultp,
391 (struct sockaddr_in *)addr->buf);
395 * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
398 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
399 u_long prog; /* program number */
400 u_long vers; /* version number */
401 u_long proc; /* procedure number */
402 xdrproc_t xargs; /* xdr routine for args */
403 void *argsp; /* pointer to args */
404 xdrproc_t xresults; /* xdr routine for results */
405 void *resultsp; /* pointer to results */
406 resultproc_t eachresult; /* call with each result obtained */
410 clnt_broadcast_result_main = eachresult;
412 thr_once(&clnt_broadcast_once, clnt_broadcast_key_init);
413 thr_setspecific(clnt_broadcast_key, (void *) eachresult);
415 return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
416 (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
417 (resultproc_t) rpc_wrap_bcast, "udp");
421 * Create the client des authentication object. Obsoleted by
422 * authdes_seccreate().
425 authdes_create(servername, window, syncaddr, ckey)
426 char *servername; /* network name of server */
427 u_int window; /* time to live */
428 struct sockaddr *syncaddr; /* optional hostaddr to sync with */
429 des_block *ckey; /* optional conversation key to use */
433 char hostname[NI_MAXHOST];
437 * Change addr to hostname, because that is the way
438 * new interface takes it.
440 if (getnameinfo(syncaddr, syncaddr->sa_len, hostname,
441 sizeof hostname, NULL, 0, 0) != 0)
444 nauth = authdes_seccreate(servername, window, hostname, ckey);
448 dummy = authdes_seccreate(servername, window, NULL, ckey);
453 * Create a client handle for a unix connection. Obsoleted by clnt_vc_create()
456 clntunix_create(raddr, prog, vers, sockp, sendsz, recvsz)
457 struct sockaddr_un *raddr;
464 struct netbuf *svcaddr;
465 struct netconfig *nconf;
472 if ((raddr->sun_len == 0) ||
473 ((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) ||
474 ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) {
477 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
478 rpc_createerr.cf_error.re_errno = errno;
482 *sockp = _socket(AF_LOCAL, SOCK_STREAM, 0);
483 len = raddr->sun_len = SUN_LEN(raddr);
484 if ((*sockp < 0) || (_connect(*sockp,
485 (struct sockaddr *)raddr, len) < 0)) {
486 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
487 rpc_createerr.cf_error.re_errno = errno;
489 (void)_close(*sockp);
493 svcaddr->buf = raddr;
494 svcaddr->len = raddr->sun_len;
495 svcaddr->maxlen = sizeof (struct sockaddr_un);
496 cl = clnt_vc_create(*sockp, svcaddr, prog,
497 vers, sendsz, recvsz);
505 * Creates, registers, and returns a (rpc) unix based transporter.
506 * Obsoleted by svc_vc_create().
509 svcunix_create(sock, sendsize, recvsize, path)
515 struct netconfig *nconf;
517 struct sockaddr_un sun;
523 xprt = (SVCXPRT *)NULL;
524 localhandle = setnetconfig();
525 while ((nconf = getnetconfig(localhandle)) != NULL) {
526 if (nconf->nc_protofmly != NULL &&
527 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
533 if ((sock = __rpc_nconf2fd(nconf)) < 0)
536 memset(&sun, 0, sizeof sun);
537 sun.sun_family = AF_LOCAL;
538 if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
539 sizeof(sun.sun_path))
541 sun.sun_len = SUN_LEN(&sun);
542 addrlen = sizeof (struct sockaddr_un);
543 sa = (struct sockaddr *)&sun;
545 if (_bind(sock, sa, addrlen) < 0)
548 taddr.addr.len = taddr.addr.maxlen = addrlen;
549 taddr.addr.buf = malloc(addrlen);
550 if (taddr.addr.buf == NULL)
552 memcpy(taddr.addr.buf, sa, addrlen);
554 if (nconf->nc_semantics != NC_TPI_CLTS) {
555 if (_listen(sock, SOMAXCONN) < 0) {
556 free(taddr.addr.buf);
561 xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize);
564 endnetconfig(localhandle);
569 * Like svunix_create(), except the routine takes any *open* UNIX file
570 * descriptor as its first input. Obsoleted by svc_fd_create();
573 svcunixfd_create(fd, sendsize, recvsize)
578 return (svc_fd_create(fd, sendsize, recvsize));