xref: /freebsd/lib/libc/rpc/rpc_soc.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
18360efbdSAlfred Perlstein /*	$NetBSD: rpc_soc.c,v 1.6 2000/07/06 03:10:35 christos Exp $	*/
28360efbdSAlfred Perlstein 
32e322d37SHiroki Sato /*-
48a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
58a16b7a1SPedro F. Giffuni  *
62e322d37SHiroki Sato  * Copyright (c) 2009, Sun Microsystems, Inc.
72e322d37SHiroki Sato  * All rights reserved.
88360efbdSAlfred Perlstein  *
92e322d37SHiroki Sato  * Redistribution and use in source and binary forms, with or without
102e322d37SHiroki Sato  * modification, are permitted provided that the following conditions are met:
112e322d37SHiroki Sato  * - Redistributions of source code must retain the above copyright notice,
122e322d37SHiroki Sato  *   this list of conditions and the following disclaimer.
132e322d37SHiroki Sato  * - Redistributions in binary form must reproduce the above copyright notice,
142e322d37SHiroki Sato  *   this list of conditions and the following disclaimer in the documentation
152e322d37SHiroki Sato  *   and/or other materials provided with the distribution.
162e322d37SHiroki Sato  * - Neither the name of Sun Microsystems, Inc. nor the names of its
172e322d37SHiroki Sato  *   contributors may be used to endorse or promote products derived
182e322d37SHiroki Sato  *   from this software without specific prior written permission.
198360efbdSAlfred Perlstein  *
202e322d37SHiroki Sato  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
212e322d37SHiroki Sato  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222e322d37SHiroki Sato  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232e322d37SHiroki Sato  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
242e322d37SHiroki Sato  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
252e322d37SHiroki Sato  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
262e322d37SHiroki Sato  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
272e322d37SHiroki Sato  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
282e322d37SHiroki Sato  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292e322d37SHiroki Sato  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
302e322d37SHiroki Sato  * POSSIBILITY OF SUCH DAMAGE.
318360efbdSAlfred Perlstein  */
328360efbdSAlfred Perlstein 
338360efbdSAlfred Perlstein /*
348360efbdSAlfred Perlstein  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
358360efbdSAlfred Perlstein  * In addition, portions of such source code were derived from Berkeley
368360efbdSAlfred Perlstein  * 4.3 BSD under license from the Regents of the University of
378360efbdSAlfred Perlstein  * California.
388360efbdSAlfred Perlstein  */
398360efbdSAlfred Perlstein 
408360efbdSAlfred Perlstein #ifdef PORTMAP
418360efbdSAlfred Perlstein /*
428360efbdSAlfred Perlstein  * rpc_soc.c
438360efbdSAlfred Perlstein  *
448360efbdSAlfred Perlstein  * The backward compatibility routines for the earlier implementation
458360efbdSAlfred Perlstein  * of RPC, where the only transports supported were tcp/ip and udp/ip.
468360efbdSAlfred Perlstein  * Based on berkeley socket abstraction, now implemented on the top
478360efbdSAlfred Perlstein  * of TLI/Streams
488360efbdSAlfred Perlstein  */
498360efbdSAlfred Perlstein 
508360efbdSAlfred Perlstein #include "namespace.h"
519f5afc13SIan Dowse #include "reentrant.h"
528360efbdSAlfred Perlstein #include <sys/types.h>
538360efbdSAlfred Perlstein #include <sys/socket.h>
548360efbdSAlfred Perlstein #include <stdio.h>
558360efbdSAlfred Perlstein #include <rpc/rpc.h>
568360efbdSAlfred Perlstein #include <rpc/pmap_clnt.h>
578360efbdSAlfred Perlstein #include <rpc/pmap_prot.h>
588360efbdSAlfred Perlstein #include <rpc/nettype.h>
598360efbdSAlfred Perlstein #include <syslog.h>
608360efbdSAlfred Perlstein #include <netinet/in.h>
618360efbdSAlfred Perlstein #include <netdb.h>
628360efbdSAlfred Perlstein #include <errno.h>
638360efbdSAlfred Perlstein #include <syslog.h>
648360efbdSAlfred Perlstein #include <stdlib.h>
658360efbdSAlfred Perlstein #include <string.h>
668360efbdSAlfred Perlstein #include <unistd.h>
678360efbdSAlfred Perlstein #include "un-namespace.h"
688360efbdSAlfred Perlstein 
698360efbdSAlfred Perlstein #include "rpc_com.h"
70235baf26SDaniel Eischen #include "mt_misc.h"
718360efbdSAlfred Perlstein 
721372519bSDavid E. O'Brien static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t,
731372519bSDavid E. O'Brien     int *, u_int, u_int, char *);
74c05ac53bSDavid E. O'Brien static SVCXPRT *svc_com_create(int, u_int, u_int, char *);
75c05ac53bSDavid E. O'Brien static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *);
768360efbdSAlfred Perlstein 
778360efbdSAlfred Perlstein /* XXX */
788360efbdSAlfred Perlstein #define IN4_LOCALHOST_STRING    "127.0.0.1"
798360efbdSAlfred Perlstein #define IN6_LOCALHOST_STRING    "::1"
808360efbdSAlfred Perlstein 
818360efbdSAlfred Perlstein /*
828360efbdSAlfred Perlstein  * A common clnt create routine
838360efbdSAlfred Perlstein  */
848360efbdSAlfred Perlstein static CLIENT *
clnt_com_create(struct sockaddr_in * raddr,rpcprog_t prog,rpcvers_t vers,int * sockp,u_int sendsz,u_int recvsz,char * tp)8568895e38SCraig Rodrigues clnt_com_create(struct sockaddr_in *raddr, rpcprog_t prog, rpcvers_t vers, int *sockp,
8668895e38SCraig Rodrigues     u_int sendsz, u_int recvsz, char *tp)
878360efbdSAlfred Perlstein {
888360efbdSAlfred Perlstein 	CLIENT *cl;
898360efbdSAlfred Perlstein 	int madefd = FALSE;
908360efbdSAlfred Perlstein 	int fd = *sockp;
918360efbdSAlfred Perlstein 	struct netconfig *nconf;
928360efbdSAlfred Perlstein 	struct netbuf bindaddr;
938360efbdSAlfred Perlstein 
948360efbdSAlfred Perlstein 	mutex_lock(&rpcsoc_lock);
958360efbdSAlfred Perlstein 	if ((nconf = __rpc_getconfip(tp)) == NULL) {
968360efbdSAlfred Perlstein 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
978360efbdSAlfred Perlstein 		mutex_unlock(&rpcsoc_lock);
988360efbdSAlfred Perlstein 		return (NULL);
998360efbdSAlfred Perlstein 	}
1008360efbdSAlfred Perlstein 	if (fd == RPC_ANYSOCK) {
1018360efbdSAlfred Perlstein 		fd = __rpc_nconf2fd(nconf);
1028360efbdSAlfred Perlstein 		if (fd == -1)
1038360efbdSAlfred Perlstein 			goto syserror;
1048360efbdSAlfred Perlstein 		madefd = TRUE;
1058360efbdSAlfred Perlstein 	}
1068360efbdSAlfred Perlstein 
1078360efbdSAlfred Perlstein 	if (raddr->sin_port == 0) {
1088360efbdSAlfred Perlstein 		u_int proto;
1098360efbdSAlfred Perlstein 		u_short sport;
1108360efbdSAlfred Perlstein 
1118360efbdSAlfred Perlstein 		mutex_unlock(&rpcsoc_lock);	/* pmap_getport is recursive */
1128360efbdSAlfred Perlstein 		proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
1138360efbdSAlfred Perlstein 		sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
1148360efbdSAlfred Perlstein 		    proto);
115*6d06bc68SKonstantin Belousov 		mutex_lock(&rpcsoc_lock);	/* pmap_getport is recursive */
1168360efbdSAlfred Perlstein 		if (sport == 0) {
1178360efbdSAlfred Perlstein 			goto err;
1188360efbdSAlfred Perlstein 		}
1198360efbdSAlfred Perlstein 		raddr->sin_port = htons(sport);
1208360efbdSAlfred Perlstein 	}
1218360efbdSAlfred Perlstein 
1228360efbdSAlfred Perlstein 	/* Transform sockaddr_in to netbuf */
1238360efbdSAlfred Perlstein 	bindaddr.maxlen = bindaddr.len =  sizeof (struct sockaddr_in);
1248360efbdSAlfred Perlstein 	bindaddr.buf = raddr;
1258360efbdSAlfred Perlstein 
1268360efbdSAlfred Perlstein 	bindresvport(fd, NULL);
1278360efbdSAlfred Perlstein 	cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
1288360efbdSAlfred Perlstein 				sendsz, recvsz);
1298360efbdSAlfred Perlstein 	if (cl) {
1308360efbdSAlfred Perlstein 		if (madefd == TRUE) {
1318360efbdSAlfred Perlstein 			/*
1328360efbdSAlfred Perlstein 			 * The fd should be closed while destroying the handle.
1338360efbdSAlfred Perlstein 			 */
1348360efbdSAlfred Perlstein 			(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
1358360efbdSAlfred Perlstein 			*sockp = fd;
1368360efbdSAlfred Perlstein 		}
1378360efbdSAlfred Perlstein 		(void) freenetconfigent(nconf);
1388360efbdSAlfred Perlstein 		mutex_unlock(&rpcsoc_lock);
1398360efbdSAlfred Perlstein 		return (cl);
1408360efbdSAlfred Perlstein 	}
1418360efbdSAlfred Perlstein 	goto err;
1428360efbdSAlfred Perlstein 
1438360efbdSAlfred Perlstein syserror:
1448360efbdSAlfred Perlstein 	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1458360efbdSAlfred Perlstein 	rpc_createerr.cf_error.re_errno = errno;
1468360efbdSAlfred Perlstein 
1478360efbdSAlfred Perlstein err:	if (madefd == TRUE)
1488360efbdSAlfred Perlstein 		(void)_close(fd);
1498360efbdSAlfred Perlstein 	(void) freenetconfigent(nconf);
1508360efbdSAlfred Perlstein 	mutex_unlock(&rpcsoc_lock);
1518360efbdSAlfred Perlstein 	return (NULL);
1528360efbdSAlfred Perlstein }
1538360efbdSAlfred Perlstein 
1548360efbdSAlfred Perlstein CLIENT *
clntudp_bufcreate(struct sockaddr_in * raddr,u_long prog,u_long vers,struct timeval wait,int * sockp,u_int sendsz,u_int recvsz)15568895e38SCraig Rodrigues clntudp_bufcreate(struct sockaddr_in *raddr, u_long prog, u_long vers,
15668895e38SCraig Rodrigues     struct timeval wait, int *sockp, u_int sendsz, u_int recvsz)
1578360efbdSAlfred Perlstein {
1588360efbdSAlfred Perlstein 	CLIENT *cl;
1598360efbdSAlfred Perlstein 
1608360efbdSAlfred Perlstein 	cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
1618360efbdSAlfred Perlstein 	    sendsz, recvsz, "udp");
1628360efbdSAlfred Perlstein 	if (cl == NULL) {
1638360efbdSAlfred Perlstein 		return (NULL);
1648360efbdSAlfred Perlstein 	}
165f249dbccSDag-Erling Smørgrav 	(void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
1668360efbdSAlfred Perlstein 	return (cl);
1678360efbdSAlfred Perlstein }
1688360efbdSAlfred Perlstein 
1698360efbdSAlfred Perlstein CLIENT *
clntudp_create(struct sockaddr_in * raddr,u_long program,u_long version,struct timeval wait,int * sockp)17068895e38SCraig Rodrigues clntudp_create(struct sockaddr_in *raddr, u_long program, u_long version,
17168895e38SCraig Rodrigues     struct timeval wait, int *sockp)
1728360efbdSAlfred Perlstein {
1738360efbdSAlfred Perlstein 
1748360efbdSAlfred Perlstein 	return clntudp_bufcreate(raddr, program, version, wait, sockp,
1758360efbdSAlfred Perlstein 					UDPMSGSIZE, UDPMSGSIZE);
1768360efbdSAlfred Perlstein }
1778360efbdSAlfred Perlstein 
1788360efbdSAlfred Perlstein CLIENT *
clnttcp_create(struct sockaddr_in * raddr,u_long prog,u_long vers,int * sockp,u_int sendsz,u_int recvsz)17968895e38SCraig Rodrigues clnttcp_create(struct sockaddr_in *raddr, u_long prog, u_long vers, int *sockp,
18068895e38SCraig Rodrigues     u_int sendsz, u_int recvsz)
1818360efbdSAlfred Perlstein {
1828360efbdSAlfred Perlstein 
1838360efbdSAlfred Perlstein 	return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
1848360efbdSAlfred Perlstein 	    sendsz, recvsz, "tcp");
1858360efbdSAlfred Perlstein }
1868360efbdSAlfred Perlstein 
1878360efbdSAlfred Perlstein CLIENT *
clntraw_create(u_long prog,u_long vers)18868895e38SCraig Rodrigues clntraw_create(u_long prog, u_long vers)
1898360efbdSAlfred Perlstein {
1908360efbdSAlfred Perlstein 
1918360efbdSAlfred Perlstein 	return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
1928360efbdSAlfred Perlstein }
1938360efbdSAlfred Perlstein 
1948360efbdSAlfred Perlstein /*
1958360efbdSAlfred Perlstein  * A common server create routine
1968360efbdSAlfred Perlstein  */
1978360efbdSAlfred Perlstein static SVCXPRT *
svc_com_create(int fd,u_int sendsize,u_int recvsize,char * netid)19868895e38SCraig Rodrigues svc_com_create(int fd, u_int sendsize, u_int recvsize, char *netid)
1998360efbdSAlfred Perlstein {
2008360efbdSAlfred Perlstein 	struct netconfig *nconf;
2018360efbdSAlfred Perlstein 	SVCXPRT *svc;
2028360efbdSAlfred Perlstein 	int madefd = FALSE;
2038360efbdSAlfred Perlstein 	int port;
2048360efbdSAlfred Perlstein 	struct sockaddr_in sin;
2058360efbdSAlfred Perlstein 
2068360efbdSAlfred Perlstein 	if ((nconf = __rpc_getconfip(netid)) == NULL) {
2078360efbdSAlfred Perlstein 		(void) syslog(LOG_ERR, "Could not get %s transport", netid);
2088360efbdSAlfred Perlstein 		return (NULL);
2098360efbdSAlfred Perlstein 	}
2108360efbdSAlfred Perlstein 	if (fd == RPC_ANYSOCK) {
2118360efbdSAlfred Perlstein 		fd = __rpc_nconf2fd(nconf);
2128360efbdSAlfred Perlstein 		if (fd == -1) {
2138360efbdSAlfred Perlstein 			(void) freenetconfigent(nconf);
2148360efbdSAlfred Perlstein 			(void) syslog(LOG_ERR,
2158360efbdSAlfred Perlstein 			"svc%s_create: could not open connection", netid);
2168360efbdSAlfred Perlstein 			return (NULL);
2178360efbdSAlfred Perlstein 		}
2188360efbdSAlfred Perlstein 		madefd = TRUE;
2198360efbdSAlfred Perlstein 	}
2208360efbdSAlfred Perlstein 
2218360efbdSAlfred Perlstein 	memset(&sin, 0, sizeof sin);
2228360efbdSAlfred Perlstein 	sin.sin_family = AF_INET;
2238360efbdSAlfred Perlstein 	bindresvport(fd, &sin);
2248360efbdSAlfred Perlstein 	_listen(fd, SOMAXCONN);
2258360efbdSAlfred Perlstein 	svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
2268360efbdSAlfred Perlstein 	(void) freenetconfigent(nconf);
2278360efbdSAlfred Perlstein 	if (svc == NULL) {
2288360efbdSAlfred Perlstein 		if (madefd)
2298360efbdSAlfred Perlstein 			(void)_close(fd);
2308360efbdSAlfred Perlstein 		return (NULL);
2318360efbdSAlfred Perlstein 	}
2328360efbdSAlfred Perlstein 	port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
2338360efbdSAlfred Perlstein 	svc->xp_port = ntohs(port);
2348360efbdSAlfred Perlstein 	return (svc);
2358360efbdSAlfred Perlstein }
2368360efbdSAlfred Perlstein 
2378360efbdSAlfred Perlstein SVCXPRT *
svctcp_create(int fd,u_int sendsize,u_int recvsize)23868895e38SCraig Rodrigues svctcp_create(int fd, u_int sendsize, u_int recvsize)
2398360efbdSAlfred Perlstein {
2408360efbdSAlfred Perlstein 
2418360efbdSAlfred Perlstein 	return svc_com_create(fd, sendsize, recvsize, "tcp");
2428360efbdSAlfred Perlstein }
2438360efbdSAlfred Perlstein 
2448360efbdSAlfred Perlstein SVCXPRT *
svcudp_bufcreate(int fd,u_int sendsz,u_int recvsz)24568895e38SCraig Rodrigues svcudp_bufcreate(int fd, u_int sendsz, u_int recvsz)
2468360efbdSAlfred Perlstein {
2478360efbdSAlfred Perlstein 
2488360efbdSAlfred Perlstein 	return svc_com_create(fd, sendsz, recvsz, "udp");
2498360efbdSAlfred Perlstein }
2508360efbdSAlfred Perlstein 
2518360efbdSAlfred Perlstein SVCXPRT *
svcfd_create(int fd,u_int sendsize,u_int recvsize)25268895e38SCraig Rodrigues svcfd_create(int fd, u_int sendsize, u_int recvsize)
2538360efbdSAlfred Perlstein {
2548360efbdSAlfred Perlstein 
2558360efbdSAlfred Perlstein 	return svc_fd_create(fd, sendsize, recvsize);
2568360efbdSAlfred Perlstein }
2578360efbdSAlfred Perlstein 
2588360efbdSAlfred Perlstein 
2598360efbdSAlfred Perlstein SVCXPRT *
svcudp_create(int fd)26068895e38SCraig Rodrigues svcudp_create(int fd)
2618360efbdSAlfred Perlstein {
2628360efbdSAlfred Perlstein 
2638360efbdSAlfred Perlstein 	return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
2648360efbdSAlfred Perlstein }
2658360efbdSAlfred Perlstein 
2668360efbdSAlfred Perlstein SVCXPRT *
svcraw_create(void)267587cf682SCraig Rodrigues svcraw_create(void)
2688360efbdSAlfred Perlstein {
2698360efbdSAlfred Perlstein 
2708360efbdSAlfred Perlstein 	return svc_raw_create();
2718360efbdSAlfred Perlstein }
2728360efbdSAlfred Perlstein 
2738360efbdSAlfred Perlstein int
get_myaddress(struct sockaddr_in * addr)27468895e38SCraig Rodrigues get_myaddress(struct sockaddr_in *addr)
2758360efbdSAlfred Perlstein {
2768360efbdSAlfred Perlstein 
2778360efbdSAlfred Perlstein 	memset((void *) addr, 0, sizeof(*addr));
2788360efbdSAlfred Perlstein 	addr->sin_family = AF_INET;
2798360efbdSAlfred Perlstein 	addr->sin_port = htons(PMAPPORT);
2808360efbdSAlfred Perlstein 	addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2818360efbdSAlfred Perlstein 	return (0);
2828360efbdSAlfred Perlstein }
2838360efbdSAlfred Perlstein 
2848360efbdSAlfred Perlstein /*
2858360efbdSAlfred Perlstein  * For connectionless "udp" transport. Obsoleted by rpc_call().
2868360efbdSAlfred Perlstein  */
2878360efbdSAlfred Perlstein int
callrpc(const char * host,int prognum,int versnum,int procnum,xdrproc_t inproc,void * in,xdrproc_t outproc,void * out)28868895e38SCraig Rodrigues callrpc(const char *host, int prognum, int versnum, int procnum,
28968895e38SCraig Rodrigues     xdrproc_t inproc, void *in, xdrproc_t outproc, void *out)
2908360efbdSAlfred Perlstein {
2918360efbdSAlfred Perlstein 
2928360efbdSAlfred Perlstein 	return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
2938360efbdSAlfred Perlstein 	    (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
2948360efbdSAlfred Perlstein }
2958360efbdSAlfred Perlstein 
2968360efbdSAlfred Perlstein /*
2978360efbdSAlfred Perlstein  * For connectionless kind of transport. Obsoleted by rpc_reg()
2988360efbdSAlfred Perlstein  */
2998360efbdSAlfred Perlstein int
registerrpc(int prognum,int versnum,int procnum,char * (* progname)(char[UDPMSGSIZE]),xdrproc_t inproc,xdrproc_t outproc)30068895e38SCraig Rodrigues registerrpc(int prognum, int versnum, int procnum,
30168895e38SCraig Rodrigues     char *(*progname)(char [UDPMSGSIZE]),
30268895e38SCraig Rodrigues     xdrproc_t inproc, xdrproc_t outproc)
3038360efbdSAlfred Perlstein {
3048360efbdSAlfred Perlstein 
3058360efbdSAlfred Perlstein 	return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
3068360efbdSAlfred Perlstein 	    (rpcproc_t)procnum, progname, inproc, outproc, "udp");
3078360efbdSAlfred Perlstein }
3088360efbdSAlfred Perlstein 
3098360efbdSAlfred Perlstein /*
3108360efbdSAlfred Perlstein  * All the following clnt_broadcast stuff is convulated; it supports
3118360efbdSAlfred Perlstein  * the earlier calling style of the callback function
3128360efbdSAlfred Perlstein  */
3138360efbdSAlfred Perlstein static thread_key_t	clnt_broadcast_key;
3148360efbdSAlfred Perlstein static resultproc_t	clnt_broadcast_result_main;
3156f88d2a8SJohn Baldwin static once_t		clnt_broadcast_once = ONCE_INITIALIZER;
3166f88d2a8SJohn Baldwin 
3176f88d2a8SJohn Baldwin static void
clnt_broadcast_key_init(void)3186f88d2a8SJohn Baldwin clnt_broadcast_key_init(void)
3196f88d2a8SJohn Baldwin {
3206f88d2a8SJohn Baldwin 
3216f88d2a8SJohn Baldwin 	thr_keycreate(&clnt_broadcast_key, free);
3226f88d2a8SJohn Baldwin }
3238360efbdSAlfred Perlstein 
3248360efbdSAlfred Perlstein /*
3258360efbdSAlfred Perlstein  * Need to translate the netbuf address into sockaddr_in address.
3268360efbdSAlfred Perlstein  * Dont care about netid here.
3278360efbdSAlfred Perlstein  */
3288360efbdSAlfred Perlstein /* ARGSUSED */
3298360efbdSAlfred Perlstein static bool_t
rpc_wrap_bcast(char * resultp,struct netbuf * addr,struct netconfig * nconf)33068895e38SCraig Rodrigues rpc_wrap_bcast(char *resultp, struct netbuf *addr, struct netconfig *nconf)
33168895e38SCraig Rodrigues /*
33268895e38SCraig Rodrigues  *	char *resultp;		// results of the call
33368895e38SCraig Rodrigues  *	struct netbuf *addr;	// address of the guy who responded
33468895e38SCraig Rodrigues  *	struct netconfig *nconf; // Netconf of the transport
33568895e38SCraig Rodrigues  */
3368360efbdSAlfred Perlstein {
3378360efbdSAlfred Perlstein 	resultproc_t clnt_broadcast_result;
3388360efbdSAlfred Perlstein 
3398360efbdSAlfred Perlstein 	if (strcmp(nconf->nc_netid, "udp"))
3408360efbdSAlfred Perlstein 		return (FALSE);
3418360efbdSAlfred Perlstein 	if (thr_main())
3428360efbdSAlfred Perlstein 		clnt_broadcast_result = clnt_broadcast_result_main;
3438360efbdSAlfred Perlstein 	else
3448360efbdSAlfred Perlstein 		clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key);
3458360efbdSAlfred Perlstein 	return (*clnt_broadcast_result)(resultp,
3468360efbdSAlfred Perlstein 				(struct sockaddr_in *)addr->buf);
3478360efbdSAlfred Perlstein }
3488360efbdSAlfred Perlstein 
3498360efbdSAlfred Perlstein /*
3508360efbdSAlfred Perlstein  * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
3518360efbdSAlfred Perlstein  */
3528360efbdSAlfred Perlstein enum clnt_stat
clnt_broadcast(u_long prog,u_long vers,u_long proc,xdrproc_t xargs,void * argsp,xdrproc_t xresults,void * resultsp,resultproc_t eachresult)35368895e38SCraig Rodrigues clnt_broadcast(u_long prog, u_long vers, u_long proc, xdrproc_t xargs,
35468895e38SCraig Rodrigues     void *argsp, xdrproc_t xresults, void *resultsp, resultproc_t eachresult)
35568895e38SCraig Rodrigues /*
35668895e38SCraig Rodrigues  *	u_long		prog;		// program number
35768895e38SCraig Rodrigues  *	u_long		vers;		// version number
35868895e38SCraig Rodrigues  *	u_long		proc;		// procedure number
35968895e38SCraig Rodrigues  *	xdrproc_t	xargs;		// xdr routine for args
36068895e38SCraig Rodrigues  *	void	       *argsp;		// pointer to args
36168895e38SCraig Rodrigues  *	xdrproc_t	xresults;	// xdr routine for results
36268895e38SCraig Rodrigues  *	void	       *resultsp;	// pointer to results
36368895e38SCraig Rodrigues  *	resultproc_t	eachresult;	// call with each result obtained
36468895e38SCraig Rodrigues  */
3658360efbdSAlfred Perlstein {
3668360efbdSAlfred Perlstein 
3678360efbdSAlfred Perlstein 	if (thr_main())
3688360efbdSAlfred Perlstein 		clnt_broadcast_result_main = eachresult;
3698360efbdSAlfred Perlstein 	else {
3706f88d2a8SJohn Baldwin 		thr_once(&clnt_broadcast_once, clnt_broadcast_key_init);
3718360efbdSAlfred Perlstein 		thr_setspecific(clnt_broadcast_key, (void *) eachresult);
3728360efbdSAlfred Perlstein 	}
3738360efbdSAlfred Perlstein 	return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
3748360efbdSAlfred Perlstein 	    (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
3758360efbdSAlfred Perlstein 	    (resultproc_t) rpc_wrap_bcast, "udp");
3768360efbdSAlfred Perlstein }
3778360efbdSAlfred Perlstein 
3788360efbdSAlfred Perlstein /*
3798360efbdSAlfred Perlstein  * Create the client des authentication object. Obsoleted by
3808360efbdSAlfred Perlstein  * authdes_seccreate().
3818360efbdSAlfred Perlstein  */
3828360efbdSAlfred Perlstein AUTH *
authdes_create(char * servername,u_int window,struct sockaddr * syncaddr,des_block * ckey)38368895e38SCraig Rodrigues authdes_create(char *servername, u_int window, struct sockaddr *syncaddr,
38468895e38SCraig Rodrigues     des_block *ckey)
38568895e38SCraig Rodrigues /*
38668895e38SCraig Rodrigues  *	char *servername;		// network name of server
38768895e38SCraig Rodrigues  *	u_int window;			// time to live
38868895e38SCraig Rodrigues  *	struct sockaddr *syncaddr;	// optional hostaddr to sync with
38968895e38SCraig Rodrigues  *	des_block *ckey;		// optional conversation key to use
39068895e38SCraig Rodrigues  */
3918360efbdSAlfred Perlstein {
3928360efbdSAlfred Perlstein 	AUTH *dummy;
3938360efbdSAlfred Perlstein 	AUTH *nauth;
3948360efbdSAlfred Perlstein 	char hostname[NI_MAXHOST];
3958360efbdSAlfred Perlstein 
3968360efbdSAlfred Perlstein 	if (syncaddr) {
3978360efbdSAlfred Perlstein 		/*
3988360efbdSAlfred Perlstein 		 * Change addr to hostname, because that is the way
3998360efbdSAlfred Perlstein 		 * new interface takes it.
4008360efbdSAlfred Perlstein 		 */
4018360efbdSAlfred Perlstein 		if (getnameinfo(syncaddr, syncaddr->sa_len, hostname,
4028360efbdSAlfred Perlstein 		    sizeof hostname, NULL, 0, 0) != 0)
4038360efbdSAlfred Perlstein 			goto fallback;
4048360efbdSAlfred Perlstein 
4058360efbdSAlfred Perlstein 		nauth = authdes_seccreate(servername, window, hostname, ckey);
4068360efbdSAlfred Perlstein 		return (nauth);
4078360efbdSAlfred Perlstein 	}
4088360efbdSAlfred Perlstein fallback:
4098360efbdSAlfred Perlstein 	dummy = authdes_seccreate(servername, window, NULL, ckey);
4108360efbdSAlfred Perlstein 	return (dummy);
4118360efbdSAlfred Perlstein }
4128360efbdSAlfred Perlstein 
413e6f9ad07SBill Paul /*
414e6f9ad07SBill Paul  * Create a client handle for a unix connection. Obsoleted by clnt_vc_create()
415e6f9ad07SBill Paul  */
416e6f9ad07SBill Paul CLIENT *
clntunix_create(struct sockaddr_un * raddr,u_long prog,u_long vers,int * sockp,u_int sendsz,u_int recvsz)41768895e38SCraig Rodrigues clntunix_create(struct sockaddr_un *raddr, u_long prog, u_long vers, int *sockp,
41868895e38SCraig Rodrigues     u_int sendsz, u_int recvsz)
419e6f9ad07SBill Paul {
420e6f9ad07SBill Paul 	struct netbuf *svcaddr;
421e6f9ad07SBill Paul 	CLIENT *cl;
422e6f9ad07SBill Paul 	int len;
423e6f9ad07SBill Paul 
424e6f9ad07SBill Paul 	cl = NULL;
4258d630135SAlfred Perlstein 	svcaddr = NULL;
426e6f9ad07SBill Paul 	if ((raddr->sun_len == 0) ||
427e6f9ad07SBill Paul 	   ((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) ||
428e6f9ad07SBill Paul 	   ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) {
429e6f9ad07SBill Paul 		free(svcaddr);
430e6f9ad07SBill Paul 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
431e6f9ad07SBill Paul 		rpc_createerr.cf_error.re_errno = errno;
432e6f9ad07SBill Paul 		return(cl);
433e6f9ad07SBill Paul 	}
434e6f9ad07SBill Paul 	if (*sockp < 0) {
435e6f9ad07SBill Paul 		*sockp = _socket(AF_LOCAL, SOCK_STREAM, 0);
436e6f9ad07SBill Paul 		len = raddr->sun_len = SUN_LEN(raddr);
437e6f9ad07SBill Paul 		if ((*sockp < 0) || (_connect(*sockp,
438e6f9ad07SBill Paul 		    (struct sockaddr *)raddr, len) < 0)) {
439e6f9ad07SBill Paul 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
440e6f9ad07SBill Paul 			rpc_createerr.cf_error.re_errno = errno;
441e6f9ad07SBill Paul 			if (*sockp != -1)
442e6f9ad07SBill Paul 				(void)_close(*sockp);
443e6f9ad07SBill Paul 			goto done;
444e6f9ad07SBill Paul 		}
445e6f9ad07SBill Paul 	}
446e6f9ad07SBill Paul 	svcaddr->buf = raddr;
447e6f9ad07SBill Paul 	svcaddr->len = raddr->sun_len;
448e6f9ad07SBill Paul 	svcaddr->maxlen = sizeof (struct sockaddr_un);
449e6f9ad07SBill Paul 	cl = clnt_vc_create(*sockp, svcaddr, prog,
450e6f9ad07SBill Paul 	    vers, sendsz, recvsz);
451e6f9ad07SBill Paul done:
452e6f9ad07SBill Paul 	free(svcaddr->buf);
453e6f9ad07SBill Paul 	free(svcaddr);
454e6f9ad07SBill Paul 	return(cl);
455e6f9ad07SBill Paul }
456e6f9ad07SBill Paul 
457e6f9ad07SBill Paul /*
458e6f9ad07SBill Paul  * Creates, registers, and returns a (rpc) unix based transporter.
459e6f9ad07SBill Paul  * Obsoleted by svc_vc_create().
460e6f9ad07SBill Paul  */
461e6f9ad07SBill Paul SVCXPRT *
svcunix_create(int sock,u_int sendsize,u_int recvsize,char * path)46268895e38SCraig Rodrigues svcunix_create(int sock, u_int sendsize, u_int recvsize, char *path)
463e6f9ad07SBill Paul {
464e6f9ad07SBill Paul 	struct netconfig *nconf;
465e6f9ad07SBill Paul 	void *localhandle;
466e6f9ad07SBill Paul 	struct sockaddr_un sun;
467e6f9ad07SBill Paul 	struct sockaddr *sa;
468e6f9ad07SBill Paul 	struct t_bind taddr;
469e6f9ad07SBill Paul 	SVCXPRT *xprt;
470e6f9ad07SBill Paul 	int addrlen;
471e6f9ad07SBill Paul 
472e6f9ad07SBill Paul 	xprt = (SVCXPRT *)NULL;
473e6f9ad07SBill Paul 	localhandle = setnetconfig();
474e6f9ad07SBill Paul 	while ((nconf = getnetconfig(localhandle)) != NULL) {
475e6f9ad07SBill Paul 		if (nconf->nc_protofmly != NULL &&
476e6f9ad07SBill Paul 		    strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
477e6f9ad07SBill Paul 			break;
478e6f9ad07SBill Paul 	}
479e6f9ad07SBill Paul 	if (nconf == NULL)
480d140ec33SEnji Cooper 		goto done;
481e6f9ad07SBill Paul 
482e6f9ad07SBill Paul 	if ((sock = __rpc_nconf2fd(nconf)) < 0)
483e6f9ad07SBill Paul 		goto done;
484e6f9ad07SBill Paul 
485e6f9ad07SBill Paul 	memset(&sun, 0, sizeof sun);
486e6f9ad07SBill Paul 	sun.sun_family = AF_LOCAL;
487d0509082SJacques Vidrine 	if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
488e6f9ad07SBill Paul 	    sizeof(sun.sun_path))
489e6f9ad07SBill Paul 		goto done;
490e6f9ad07SBill Paul 	sun.sun_len = SUN_LEN(&sun);
491e6f9ad07SBill Paul 	addrlen = sizeof (struct sockaddr_un);
492e6f9ad07SBill Paul 	sa = (struct sockaddr *)&sun;
493e6f9ad07SBill Paul 
494e6f9ad07SBill Paul 	if (_bind(sock, sa, addrlen) < 0)
495e6f9ad07SBill Paul 		goto done;
496e6f9ad07SBill Paul 
497e6f9ad07SBill Paul 	taddr.addr.len = taddr.addr.maxlen = addrlen;
498e6f9ad07SBill Paul 	taddr.addr.buf = malloc(addrlen);
499e6f9ad07SBill Paul 	if (taddr.addr.buf == NULL)
500e6f9ad07SBill Paul 		goto done;
501e6f9ad07SBill Paul 	memcpy(taddr.addr.buf, sa, addrlen);
502e6f9ad07SBill Paul 
503e6f9ad07SBill Paul 	if (nconf->nc_semantics != NC_TPI_CLTS) {
504e6f9ad07SBill Paul 		if (_listen(sock, SOMAXCONN) < 0) {
505e6f9ad07SBill Paul 			free(taddr.addr.buf);
506e6f9ad07SBill Paul 			goto done;
507e6f9ad07SBill Paul 		}
508e6f9ad07SBill Paul 	}
509e6f9ad07SBill Paul 
510e6f9ad07SBill Paul 	xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize);
511e6f9ad07SBill Paul 
512e6f9ad07SBill Paul done:
513e6f9ad07SBill Paul 	endnetconfig(localhandle);
514e6f9ad07SBill Paul 	return(xprt);
515e6f9ad07SBill Paul }
516e6f9ad07SBill Paul 
517e6f9ad07SBill Paul /*
518e6f9ad07SBill Paul  * Like svunix_create(), except the routine takes any *open* UNIX file
519e6f9ad07SBill Paul  * descriptor as its first input. Obsoleted by svc_fd_create();
520e6f9ad07SBill Paul  */
521e6f9ad07SBill Paul SVCXPRT *
svcunixfd_create(int fd,u_int sendsize,u_int recvsize)52268895e38SCraig Rodrigues svcunixfd_create(int fd, u_int sendsize, u_int recvsize)
523e6f9ad07SBill Paul {
524e6f9ad07SBill Paul  	return (svc_fd_create(fd, sendsize, recvsize));
525e6f9ad07SBill Paul }
526e6f9ad07SBill Paul 
5278360efbdSAlfred Perlstein #endif /* PORTMAP */
528