1 /* $NetBSD: rpc_soc.c,v 1.6 2000/07/06 03:10:35 christos Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 /* #ident "@(#)rpc_soc.c 1.17 94/04/24 SMI" */
36 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
37 * In addition, portions of such source code were derived from Berkeley
38 * 4.3 BSD under license from the Regents of the University of
42 #if defined(LIBC_SCCS) && !defined(lint)
43 static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
52 * The backward compatibility routines for the earlier implementation
53 * of RPC, where the only transports supported were tcp/ip and udp/ip.
54 * Based on berkeley socket abstraction, now implemented on the top
58 #include "namespace.h"
59 #include "reentrant.h"
60 #include <sys/types.h>
61 #include <sys/socket.h>
64 #include <rpc/pmap_clnt.h>
65 #include <rpc/pmap_prot.h>
66 #include <rpc/nettype.h>
68 #include <netinet/in.h>
75 #include "un-namespace.h"
80 static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t,
81 int *, u_int, u_int, char *);
82 static SVCXPRT *svc_com_create(int, u_int, u_int, char *);
83 static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *);
86 #define IN4_LOCALHOST_STRING "127.0.0.1"
87 #define IN6_LOCALHOST_STRING "::1"
90 * A common clnt create routine
93 clnt_com_create(struct sockaddr_in *raddr, rpcprog_t prog, rpcvers_t vers, int *sockp,
94 u_int sendsz, u_int recvsz, char *tp)
99 struct netconfig *nconf;
100 struct netbuf bindaddr;
102 mutex_lock(&rpcsoc_lock);
103 if ((nconf = __rpc_getconfip(tp)) == NULL) {
104 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
105 mutex_unlock(&rpcsoc_lock);
108 if (fd == RPC_ANYSOCK) {
109 fd = __rpc_nconf2fd(nconf);
115 if (raddr->sin_port == 0) {
119 mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */
120 proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
121 sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
126 raddr->sin_port = htons(sport);
127 mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */
130 /* Transform sockaddr_in to netbuf */
131 bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in);
132 bindaddr.buf = raddr;
134 bindresvport(fd, NULL);
135 cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
138 if (madefd == TRUE) {
140 * The fd should be closed while destroying the handle.
142 (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
145 (void) freenetconfigent(nconf);
146 mutex_unlock(&rpcsoc_lock);
152 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
153 rpc_createerr.cf_error.re_errno = errno;
155 err: if (madefd == TRUE)
157 (void) freenetconfigent(nconf);
158 mutex_unlock(&rpcsoc_lock);
163 clntudp_bufcreate(struct sockaddr_in *raddr, u_long prog, u_long vers,
164 struct timeval wait, int *sockp, u_int sendsz, u_int recvsz)
168 cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
169 sendsz, recvsz, "udp");
173 (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
178 clntudp_create(struct sockaddr_in *raddr, u_long program, u_long version,
179 struct timeval wait, int *sockp)
182 return clntudp_bufcreate(raddr, program, version, wait, sockp,
183 UDPMSGSIZE, UDPMSGSIZE);
187 clnttcp_create(struct sockaddr_in *raddr, u_long prog, u_long vers, int *sockp,
188 u_int sendsz, u_int recvsz)
191 return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
192 sendsz, recvsz, "tcp");
196 clntraw_create(u_long prog, u_long vers)
199 return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
203 * A common server create routine
206 svc_com_create(int fd, u_int sendsize, u_int recvsize, char *netid)
208 struct netconfig *nconf;
212 struct sockaddr_in sin;
214 if ((nconf = __rpc_getconfip(netid)) == NULL) {
215 (void) syslog(LOG_ERR, "Could not get %s transport", netid);
218 if (fd == RPC_ANYSOCK) {
219 fd = __rpc_nconf2fd(nconf);
221 (void) freenetconfigent(nconf);
222 (void) syslog(LOG_ERR,
223 "svc%s_create: could not open connection", netid);
229 memset(&sin, 0, sizeof sin);
230 sin.sin_family = AF_INET;
231 bindresvport(fd, &sin);
232 _listen(fd, SOMAXCONN);
233 svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
234 (void) freenetconfigent(nconf);
240 port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
241 svc->xp_port = ntohs(port);
246 svctcp_create(int fd, u_int sendsize, u_int recvsize)
249 return svc_com_create(fd, sendsize, recvsize, "tcp");
253 svcudp_bufcreate(int fd, u_int sendsz, u_int recvsz)
256 return svc_com_create(fd, sendsz, recvsz, "udp");
260 svcfd_create(int fd, u_int sendsize, u_int recvsize)
263 return svc_fd_create(fd, sendsize, recvsize);
268 svcudp_create(int fd)
271 return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
278 return svc_raw_create();
282 get_myaddress(struct sockaddr_in *addr)
285 memset((void *) addr, 0, sizeof(*addr));
286 addr->sin_family = AF_INET;
287 addr->sin_port = htons(PMAPPORT);
288 addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
293 * For connectionless "udp" transport. Obsoleted by rpc_call().
296 callrpc(const char *host, int prognum, int versnum, int procnum,
297 xdrproc_t inproc, void *in, xdrproc_t outproc, void *out)
300 return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
301 (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
305 * For connectionless kind of transport. Obsoleted by rpc_reg()
308 registerrpc(int prognum, int versnum, int procnum,
309 char *(*progname)(char [UDPMSGSIZE]),
310 xdrproc_t inproc, xdrproc_t outproc)
313 return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
314 (rpcproc_t)procnum, progname, inproc, outproc, "udp");
318 * All the following clnt_broadcast stuff is convulated; it supports
319 * the earlier calling style of the callback function
321 static thread_key_t clnt_broadcast_key;
322 static resultproc_t clnt_broadcast_result_main;
323 static once_t clnt_broadcast_once = ONCE_INITIALIZER;
326 clnt_broadcast_key_init(void)
329 thr_keycreate(&clnt_broadcast_key, free);
333 * Need to translate the netbuf address into sockaddr_in address.
334 * Dont care about netid here.
338 rpc_wrap_bcast(char *resultp, struct netbuf *addr, struct netconfig *nconf)
340 * char *resultp; // results of the call
341 * struct netbuf *addr; // address of the guy who responded
342 * struct netconfig *nconf; // Netconf of the transport
345 resultproc_t clnt_broadcast_result;
347 if (strcmp(nconf->nc_netid, "udp"))
350 clnt_broadcast_result = clnt_broadcast_result_main;
352 clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key);
353 return (*clnt_broadcast_result)(resultp,
354 (struct sockaddr_in *)addr->buf);
358 * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
361 clnt_broadcast(u_long prog, u_long vers, u_long proc, xdrproc_t xargs,
362 void *argsp, xdrproc_t xresults, void *resultsp, resultproc_t eachresult)
364 * u_long prog; // program number
365 * u_long vers; // version number
366 * u_long proc; // procedure number
367 * xdrproc_t xargs; // xdr routine for args
368 * void *argsp; // pointer to args
369 * xdrproc_t xresults; // xdr routine for results
370 * void *resultsp; // pointer to results
371 * resultproc_t eachresult; // call with each result obtained
376 clnt_broadcast_result_main = eachresult;
378 thr_once(&clnt_broadcast_once, clnt_broadcast_key_init);
379 thr_setspecific(clnt_broadcast_key, (void *) eachresult);
381 return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
382 (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
383 (resultproc_t) rpc_wrap_bcast, "udp");
387 * Create the client des authentication object. Obsoleted by
388 * authdes_seccreate().
391 authdes_create(char *servername, u_int window, struct sockaddr *syncaddr,
394 * char *servername; // network name of server
395 * u_int window; // time to live
396 * struct sockaddr *syncaddr; // optional hostaddr to sync with
397 * des_block *ckey; // optional conversation key to use
402 char hostname[NI_MAXHOST];
406 * Change addr to hostname, because that is the way
407 * new interface takes it.
409 if (getnameinfo(syncaddr, syncaddr->sa_len, hostname,
410 sizeof hostname, NULL, 0, 0) != 0)
413 nauth = authdes_seccreate(servername, window, hostname, ckey);
417 dummy = authdes_seccreate(servername, window, NULL, ckey);
422 * Create a client handle for a unix connection. Obsoleted by clnt_vc_create()
425 clntunix_create(struct sockaddr_un *raddr, u_long prog, u_long vers, int *sockp,
426 u_int sendsz, u_int recvsz)
428 struct netbuf *svcaddr;
434 if ((raddr->sun_len == 0) ||
435 ((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) ||
436 ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) {
438 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
439 rpc_createerr.cf_error.re_errno = errno;
443 *sockp = _socket(AF_LOCAL, SOCK_STREAM, 0);
444 len = raddr->sun_len = SUN_LEN(raddr);
445 if ((*sockp < 0) || (_connect(*sockp,
446 (struct sockaddr *)raddr, len) < 0)) {
447 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
448 rpc_createerr.cf_error.re_errno = errno;
450 (void)_close(*sockp);
454 svcaddr->buf = raddr;
455 svcaddr->len = raddr->sun_len;
456 svcaddr->maxlen = sizeof (struct sockaddr_un);
457 cl = clnt_vc_create(*sockp, svcaddr, prog,
458 vers, sendsz, recvsz);
466 * Creates, registers, and returns a (rpc) unix based transporter.
467 * Obsoleted by svc_vc_create().
470 svcunix_create(int sock, u_int sendsize, u_int recvsize, char *path)
472 struct netconfig *nconf;
474 struct sockaddr_un sun;
480 xprt = (SVCXPRT *)NULL;
481 localhandle = setnetconfig();
482 while ((nconf = getnetconfig(localhandle)) != NULL) {
483 if (nconf->nc_protofmly != NULL &&
484 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
490 if ((sock = __rpc_nconf2fd(nconf)) < 0)
493 memset(&sun, 0, sizeof sun);
494 sun.sun_family = AF_LOCAL;
495 if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
496 sizeof(sun.sun_path))
498 sun.sun_len = SUN_LEN(&sun);
499 addrlen = sizeof (struct sockaddr_un);
500 sa = (struct sockaddr *)&sun;
502 if (_bind(sock, sa, addrlen) < 0)
505 taddr.addr.len = taddr.addr.maxlen = addrlen;
506 taddr.addr.buf = malloc(addrlen);
507 if (taddr.addr.buf == NULL)
509 memcpy(taddr.addr.buf, sa, addrlen);
511 if (nconf->nc_semantics != NC_TPI_CLTS) {
512 if (_listen(sock, SOMAXCONN) < 0) {
513 free(taddr.addr.buf);
518 xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize);
521 endnetconfig(localhandle);
526 * Like svunix_create(), except the routine takes any *open* UNIX file
527 * descriptor as its first input. Obsoleted by svc_fd_create();
530 svcunixfd_create(int fd, u_int sendsize, u_int recvsize)
532 return (svc_fd_create(fd, sendsize, recvsize));