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(struct sockaddr_in *raddr, rpcprog_t prog, rpcvers_t vers, int *sockp,
92 u_int sendsz, u_int recvsz, char *tp)
97 struct netconfig *nconf;
98 struct netbuf bindaddr;
100 mutex_lock(&rpcsoc_lock);
101 if ((nconf = __rpc_getconfip(tp)) == NULL) {
102 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
103 mutex_unlock(&rpcsoc_lock);
106 if (fd == RPC_ANYSOCK) {
107 fd = __rpc_nconf2fd(nconf);
113 if (raddr->sin_port == 0) {
117 mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */
118 proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
119 sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
124 raddr->sin_port = htons(sport);
125 mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */
128 /* Transform sockaddr_in to netbuf */
129 bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in);
130 bindaddr.buf = raddr;
132 bindresvport(fd, NULL);
133 cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
136 if (madefd == TRUE) {
138 * The fd should be closed while destroying the handle.
140 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
143 (void) freenetconfigent(nconf);
144 mutex_unlock(&rpcsoc_lock);
150 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
151 rpc_createerr.cf_error.re_errno = errno;
153 err: if (madefd == TRUE)
155 (void) freenetconfigent(nconf);
156 mutex_unlock(&rpcsoc_lock);
161 clntudp_bufcreate(struct sockaddr_in *raddr, u_long prog, u_long vers,
162 struct timeval wait, int *sockp, u_int sendsz, u_int recvsz)
166 cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
167 sendsz, recvsz, "udp");
171 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
176 clntudp_create(struct sockaddr_in *raddr, u_long program, u_long version,
177 struct timeval wait, int *sockp)
180 return clntudp_bufcreate(raddr, program, version, wait, sockp,
181 UDPMSGSIZE, UDPMSGSIZE);
185 clnttcp_create(struct sockaddr_in *raddr, u_long prog, u_long vers, int *sockp,
186 u_int sendsz, u_int recvsz)
189 return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
190 sendsz, recvsz, "tcp");
194 clntraw_create(u_long prog, u_long vers)
197 return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
201 * A common server create routine
204 svc_com_create(int fd, u_int sendsize, u_int recvsize, char *netid)
206 struct netconfig *nconf;
210 struct sockaddr_in sin;
212 if ((nconf = __rpc_getconfip(netid)) == NULL) {
213 (void) syslog(LOG_ERR, "Could not get %s transport", netid);
216 if (fd == RPC_ANYSOCK) {
217 fd = __rpc_nconf2fd(nconf);
219 (void) freenetconfigent(nconf);
220 (void) syslog(LOG_ERR,
221 "svc%s_create: could not open connection", netid);
227 memset(&sin, 0, sizeof sin);
228 sin.sin_family = AF_INET;
229 bindresvport(fd, &sin);
230 _listen(fd, SOMAXCONN);
231 svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
232 (void) freenetconfigent(nconf);
238 port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
239 svc->xp_port = ntohs(port);
244 svctcp_create(int fd, u_int sendsize, u_int recvsize)
247 return svc_com_create(fd, sendsize, recvsize, "tcp");
251 svcudp_bufcreate(int fd, u_int sendsz, u_int recvsz)
254 return svc_com_create(fd, sendsz, recvsz, "udp");
258 svcfd_create(int fd, u_int sendsize, u_int recvsize)
261 return svc_fd_create(fd, sendsize, recvsize);
266 svcudp_create(int fd)
269 return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
276 return svc_raw_create();
280 get_myaddress(struct sockaddr_in *addr)
283 memset((void *) addr, 0, sizeof(*addr));
284 addr->sin_family = AF_INET;
285 addr->sin_port = htons(PMAPPORT);
286 addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
291 * For connectionless "udp" transport. Obsoleted by rpc_call().
294 callrpc(const char *host, int prognum, int versnum, int procnum,
295 xdrproc_t inproc, void *in, xdrproc_t outproc, void *out)
298 return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
299 (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
303 * For connectionless kind of transport. Obsoleted by rpc_reg()
306 registerrpc(int prognum, int versnum, int procnum,
307 char *(*progname)(char [UDPMSGSIZE]),
308 xdrproc_t inproc, xdrproc_t outproc)
311 return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
312 (rpcproc_t)procnum, progname, inproc, outproc, "udp");
316 * All the following clnt_broadcast stuff is convulated; it supports
317 * the earlier calling style of the callback function
319 static thread_key_t clnt_broadcast_key;
320 static resultproc_t clnt_broadcast_result_main;
321 static once_t clnt_broadcast_once = ONCE_INITIALIZER;
324 clnt_broadcast_key_init(void)
327 thr_keycreate(&clnt_broadcast_key, free);
331 * Need to translate the netbuf address into sockaddr_in address.
332 * Dont care about netid here.
336 rpc_wrap_bcast(char *resultp, struct netbuf *addr, struct netconfig *nconf)
338 * char *resultp; // results of the call
339 * struct netbuf *addr; // address of the guy who responded
340 * struct netconfig *nconf; // Netconf of the transport
343 resultproc_t clnt_broadcast_result;
345 if (strcmp(nconf->nc_netid, "udp"))
348 clnt_broadcast_result = clnt_broadcast_result_main;
350 clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key);
351 return (*clnt_broadcast_result)(resultp,
352 (struct sockaddr_in *)addr->buf);
356 * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
359 clnt_broadcast(u_long prog, u_long vers, u_long proc, xdrproc_t xargs,
360 void *argsp, xdrproc_t xresults, void *resultsp, resultproc_t eachresult)
362 * u_long prog; // program number
363 * u_long vers; // version number
364 * u_long proc; // procedure number
365 * xdrproc_t xargs; // xdr routine for args
366 * void *argsp; // pointer to args
367 * xdrproc_t xresults; // xdr routine for results
368 * void *resultsp; // pointer to results
369 * resultproc_t eachresult; // call with each result obtained
374 clnt_broadcast_result_main = eachresult;
376 thr_once(&clnt_broadcast_once, clnt_broadcast_key_init);
377 thr_setspecific(clnt_broadcast_key, (void *) eachresult);
379 return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
380 (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
381 (resultproc_t) rpc_wrap_bcast, "udp");
385 * Create the client des authentication object. Obsoleted by
386 * authdes_seccreate().
389 authdes_create(char *servername, u_int window, struct sockaddr *syncaddr,
392 * char *servername; // network name of server
393 * u_int window; // time to live
394 * struct sockaddr *syncaddr; // optional hostaddr to sync with
395 * des_block *ckey; // optional conversation key to use
400 char hostname[NI_MAXHOST];
404 * Change addr to hostname, because that is the way
405 * new interface takes it.
407 if (getnameinfo(syncaddr, syncaddr->sa_len, hostname,
408 sizeof hostname, NULL, 0, 0) != 0)
411 nauth = authdes_seccreate(servername, window, hostname, ckey);
415 dummy = authdes_seccreate(servername, window, NULL, ckey);
420 * Create a client handle for a unix connection. Obsoleted by clnt_vc_create()
423 clntunix_create(struct sockaddr_un *raddr, u_long prog, u_long vers, int *sockp,
424 u_int sendsz, u_int recvsz)
426 struct netbuf *svcaddr;
432 if ((raddr->sun_len == 0) ||
433 ((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) ||
434 ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) {
436 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
437 rpc_createerr.cf_error.re_errno = errno;
441 *sockp = _socket(AF_LOCAL, SOCK_STREAM, 0);
442 len = raddr->sun_len = SUN_LEN(raddr);
443 if ((*sockp < 0) || (_connect(*sockp,
444 (struct sockaddr *)raddr, len) < 0)) {
445 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
446 rpc_createerr.cf_error.re_errno = errno;
448 (void)_close(*sockp);
452 svcaddr->buf = raddr;
453 svcaddr->len = raddr->sun_len;
454 svcaddr->maxlen = sizeof (struct sockaddr_un);
455 cl = clnt_vc_create(*sockp, svcaddr, prog,
456 vers, sendsz, recvsz);
464 * Creates, registers, and returns a (rpc) unix based transporter.
465 * Obsoleted by svc_vc_create().
468 svcunix_create(int sock, u_int sendsize, u_int recvsize, char *path)
470 struct netconfig *nconf;
472 struct sockaddr_un sun;
478 xprt = (SVCXPRT *)NULL;
479 localhandle = setnetconfig();
480 while ((nconf = getnetconfig(localhandle)) != NULL) {
481 if (nconf->nc_protofmly != NULL &&
482 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
488 if ((sock = __rpc_nconf2fd(nconf)) < 0)
491 memset(&sun, 0, sizeof sun);
492 sun.sun_family = AF_LOCAL;
493 if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
494 sizeof(sun.sun_path))
496 sun.sun_len = SUN_LEN(&sun);
497 addrlen = sizeof (struct sockaddr_un);
498 sa = (struct sockaddr *)&sun;
500 if (_bind(sock, sa, addrlen) < 0)
503 taddr.addr.len = taddr.addr.maxlen = addrlen;
504 taddr.addr.buf = malloc(addrlen);
505 if (taddr.addr.buf == NULL)
507 memcpy(taddr.addr.buf, sa, addrlen);
509 if (nconf->nc_semantics != NC_TPI_CLTS) {
510 if (_listen(sock, SOMAXCONN) < 0) {
511 free(taddr.addr.buf);
516 xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize);
519 endnetconfig(localhandle);
524 * Like svunix_create(), except the routine takes any *open* UNIX file
525 * descriptor as its first input. Obsoleted by svc_fd_create();
528 svcunixfd_create(int fd, u_int sendsize, u_int recvsize)
530 return (svc_fd_create(fd, sendsize, recvsize));