xref: /netbsd-src/lib/libc/rpc/rpc_soc.c (revision 85f5e301f5b0e5c7695c7222d16769f8f55f6aeb)
1*85f5e301Schristos /*	$NetBSD: rpc_soc.c,v 1.24 2024/01/23 17:24:38 christos Exp $	*/
27df0ccbaSfvdl 
37df0ccbaSfvdl /*
447c0e0c3Stron  * Copyright (c) 2010, Oracle America, Inc.
57df0ccbaSfvdl  *
647c0e0c3Stron  * Redistribution and use in source and binary forms, with or without
747c0e0c3Stron  * modification, are permitted provided that the following conditions are
847c0e0c3Stron  * met:
97df0ccbaSfvdl  *
1047c0e0c3Stron  *     * Redistributions of source code must retain the above copyright
1147c0e0c3Stron  *       notice, this list of conditions and the following disclaimer.
1247c0e0c3Stron  *     * Redistributions in binary form must reproduce the above
1347c0e0c3Stron  *       copyright notice, this list of conditions and the following
1447c0e0c3Stron  *       disclaimer in the documentation and/or other materials
1547c0e0c3Stron  *       provided with the distribution.
1647c0e0c3Stron  *     * Neither the name of the "Oracle America, Inc." nor the names of its
1747c0e0c3Stron  *       contributors may be used to endorse or promote products derived
1847c0e0c3Stron  *       from this software without specific prior written permission.
197df0ccbaSfvdl  *
2047c0e0c3Stron  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2147c0e0c3Stron  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2247c0e0c3Stron  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2347c0e0c3Stron  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2447c0e0c3Stron  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2547c0e0c3Stron  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2647c0e0c3Stron  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2747c0e0c3Stron  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2847c0e0c3Stron  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2947c0e0c3Stron  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3047c0e0c3Stron  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3147c0e0c3Stron  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
327df0ccbaSfvdl  */
337df0ccbaSfvdl 
347df0ccbaSfvdl /* #ident	"@(#)rpc_soc.c	1.17	94/04/24 SMI" */
357df0ccbaSfvdl 
367df0ccbaSfvdl /*
377df0ccbaSfvdl  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
387df0ccbaSfvdl  * In addition, portions of such source code were derived from Berkeley
397df0ccbaSfvdl  * 4.3 BSD under license from the Regents of the University of
407df0ccbaSfvdl  * California.
417df0ccbaSfvdl  */
427df0ccbaSfvdl 
435c945215Sitojun #include <sys/cdefs.h>
445c945215Sitojun #if defined(LIBC_SCCS) && !defined(lint)
457df0ccbaSfvdl #if 0
467df0ccbaSfvdl static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro";
475c945215Sitojun #else
48*85f5e301Schristos __RCSID("$NetBSD: rpc_soc.c,v 1.24 2024/01/23 17:24:38 christos Exp $");
497df0ccbaSfvdl #endif
507df0ccbaSfvdl #endif
517df0ccbaSfvdl 
527df0ccbaSfvdl #ifdef PORTMAP
537df0ccbaSfvdl /*
547df0ccbaSfvdl  * rpc_soc.c
557df0ccbaSfvdl  *
567df0ccbaSfvdl  * The backward compatibility routines for the earlier implementation
577df0ccbaSfvdl  * of RPC, where the only transports supported were tcp/ip and udp/ip.
587df0ccbaSfvdl  * Based on berkeley socket abstraction, now implemented on the top
597df0ccbaSfvdl  * of TLI/Streams
607df0ccbaSfvdl  */
617df0ccbaSfvdl 
627df0ccbaSfvdl #include "namespace.h"
637df0ccbaSfvdl #include "reentrant.h"
647df0ccbaSfvdl #include <sys/types.h>
657df0ccbaSfvdl #include <sys/socket.h>
667df0ccbaSfvdl #include <stdio.h>
677df0ccbaSfvdl #include <rpc/rpc.h>
687df0ccbaSfvdl #include <rpc/pmap_clnt.h>
697df0ccbaSfvdl #include <rpc/pmap_prot.h>
70deb154d2Schristos #include <rpc/nettype.h>
717df0ccbaSfvdl #include <netinet/in.h>
720e8cfd8fSlukem #include <assert.h>
737df0ccbaSfvdl #include <errno.h>
740e8cfd8fSlukem #include <netdb.h>
757df0ccbaSfvdl #include <stdlib.h>
76c444e0d6Sthorpej #include <string.h>
770e8cfd8fSlukem #include <syslog.h>
787df0ccbaSfvdl #include <unistd.h>
797df0ccbaSfvdl 
802dec884dSchristos #include "svc_fdset.h"
8179d5b270Sfvdl #include "rpc_internal.h"
827df0ccbaSfvdl 
837df0ccbaSfvdl #ifdef __weak_alias
847df0ccbaSfvdl __weak_alias(clntudp_bufcreate,_clntudp_bufcreate)
857df0ccbaSfvdl __weak_alias(clntudp_create,_clntudp_create)
867df0ccbaSfvdl __weak_alias(clnttcp_create,_clnttcp_create)
877df0ccbaSfvdl __weak_alias(clntraw_create,_clntraw_create)
887df0ccbaSfvdl __weak_alias(get_myaddress,_get_myaddress)
89746a942aSfvdl __weak_alias(svcfd_create,_svcfd_create)
90a4a23df2Sfvdl __weak_alias(svcudp_bufcreate,_svcudp_bufcreate)
917df0ccbaSfvdl __weak_alias(svcudp_create,_svcudp_create)
927df0ccbaSfvdl __weak_alias(svctcp_create,_svctcp_create)
937df0ccbaSfvdl __weak_alias(svcraw_create,_svcraw_create)
947df0ccbaSfvdl __weak_alias(callrpc,_callrpc)
957df0ccbaSfvdl __weak_alias(registerrpc,_registerrpc)
967df0ccbaSfvdl __weak_alias(clnt_broadcast,_clnt_broadcast)
977df0ccbaSfvdl #endif
987df0ccbaSfvdl 
99adb74221Smatt static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t,
100adb74221Smatt 				    int *, u_int, u_int, const char *);
101adb74221Smatt static SVCXPRT *svc_com_create(int, u_int, u_int, const char *);
102adb74221Smatt static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *);
1037df0ccbaSfvdl 
1047df0ccbaSfvdl /*
1057df0ccbaSfvdl  * A common clnt create routine
1067df0ccbaSfvdl  */
1077df0ccbaSfvdl static CLIENT *
clnt_com_create(struct sockaddr_in * raddr,rpcprog_t prog,rpcvers_t vers,int * sockp,u_int sendsz,u_int recvsz,const char * tp)108adb74221Smatt clnt_com_create(struct sockaddr_in *raddr, rpcprog_t prog, rpcvers_t vers,
109adb74221Smatt 	int *sockp, u_int sendsz, u_int recvsz, const char *tp)
1107df0ccbaSfvdl {
1117df0ccbaSfvdl 	CLIENT *cl;
1127df0ccbaSfvdl 	int madefd = FALSE;
1130e8cfd8fSlukem 	int fd;
1147df0ccbaSfvdl 	struct netconfig *nconf;
1157df0ccbaSfvdl 	struct netbuf bindaddr;
1167df0ccbaSfvdl 
1170e8cfd8fSlukem 	_DIAGASSERT(raddr != NULL);
1180e8cfd8fSlukem 	_DIAGASSERT(sockp != NULL);
1190e8cfd8fSlukem 	_DIAGASSERT(tp != NULL);
1200e8cfd8fSlukem 
1210e8cfd8fSlukem 	fd = *sockp;
1220e8cfd8fSlukem 
1237df0ccbaSfvdl 	mutex_lock(&rpcsoc_lock);
1247df0ccbaSfvdl 	if ((nconf = __rpc_getconfip(tp)) == NULL) {
1257df0ccbaSfvdl 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1267df0ccbaSfvdl 		mutex_unlock(&rpcsoc_lock);
127deb154d2Schristos 		return (NULL);
1287df0ccbaSfvdl 	}
1297df0ccbaSfvdl 	if (fd == RPC_ANYSOCK) {
1307df0ccbaSfvdl 		fd = __rpc_nconf2fd(nconf);
1317df0ccbaSfvdl 		if (fd == -1)
1327df0ccbaSfvdl 			goto syserror;
1337df0ccbaSfvdl 		madefd = TRUE;
1347df0ccbaSfvdl 	}
1357df0ccbaSfvdl 
1367df0ccbaSfvdl 	if (raddr->sin_port == 0) {
1377df0ccbaSfvdl 		u_int proto;
1387df0ccbaSfvdl 		u_short sport;
1397df0ccbaSfvdl 
1407df0ccbaSfvdl 		mutex_unlock(&rpcsoc_lock);	/* pmap_getport is recursive */
1417df0ccbaSfvdl 		proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
142deb154d2Schristos 		sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
143deb154d2Schristos 		    proto);
1447df0ccbaSfvdl 		if (sport == 0) {
1457df0ccbaSfvdl 			goto err;
1467df0ccbaSfvdl 		}
1477df0ccbaSfvdl 		raddr->sin_port = htons(sport);
1487df0ccbaSfvdl 		mutex_lock(&rpcsoc_lock);	/* pmap_getport is recursive */
1497df0ccbaSfvdl 	}
1507df0ccbaSfvdl 
1517df0ccbaSfvdl 	/* Transform sockaddr_in to netbuf */
1527df0ccbaSfvdl 	bindaddr.maxlen = bindaddr.len =  sizeof (struct sockaddr_in);
1537df0ccbaSfvdl 	bindaddr.buf = raddr;
1547df0ccbaSfvdl 
155bf67527dSchristos 	(void)bindresvport(fd, NULL);
1567df0ccbaSfvdl 	cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
1577df0ccbaSfvdl 				sendsz, recvsz);
1587df0ccbaSfvdl 	if (cl) {
1597df0ccbaSfvdl 		if (madefd == TRUE) {
1607df0ccbaSfvdl 			/*
1617df0ccbaSfvdl 			 * The fd should be closed while destroying the handle.
1627df0ccbaSfvdl 			 */
163deb154d2Schristos 			(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
1647df0ccbaSfvdl 			*sockp = fd;
1657df0ccbaSfvdl 		}
1667df0ccbaSfvdl 		(void) freenetconfigent(nconf);
1677df0ccbaSfvdl 		mutex_unlock(&rpcsoc_lock);
1687df0ccbaSfvdl 		return (cl);
1697df0ccbaSfvdl 	}
1707df0ccbaSfvdl 	goto err;
1717df0ccbaSfvdl 
1727df0ccbaSfvdl syserror:
1737df0ccbaSfvdl 	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1747df0ccbaSfvdl 	rpc_createerr.cf_error.re_errno = errno;
1757df0ccbaSfvdl 
1767df0ccbaSfvdl err:	if (madefd == TRUE)
1777df0ccbaSfvdl 		(void) close(fd);
1787df0ccbaSfvdl 	(void) freenetconfigent(nconf);
1797df0ccbaSfvdl 	mutex_unlock(&rpcsoc_lock);
180deb154d2Schristos 	return (NULL);
1817df0ccbaSfvdl }
1827df0ccbaSfvdl 
1837df0ccbaSfvdl CLIENT *
clntudp_bufcreate(struct sockaddr_in * raddr,u_long prog,u_long vers,struct timeval wait,int * sockp,u_int sendsz,u_int recvsz)1849e66e6d7Sabs clntudp_bufcreate(struct sockaddr_in *raddr, u_long prog, u_long vers, struct timeval wait, int *sockp, u_int sendsz, u_int recvsz)
1857df0ccbaSfvdl {
1867df0ccbaSfvdl 	CLIENT *cl;
1877df0ccbaSfvdl 
1880e8cfd8fSlukem 	_DIAGASSERT(raddr != NULL);
1890e8cfd8fSlukem 	_DIAGASSERT(sockp != NULL);
1900e8cfd8fSlukem 
191deb154d2Schristos 	cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
192deb154d2Schristos 	    sendsz, recvsz, "udp");
193deb154d2Schristos 	if (cl == NULL) {
194deb154d2Schristos 		return (NULL);
1957df0ccbaSfvdl 	}
196deb154d2Schristos 	(void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, (char *)(void *)&wait);
1977df0ccbaSfvdl 	return (cl);
1987df0ccbaSfvdl }
1997df0ccbaSfvdl 
2007df0ccbaSfvdl CLIENT *
clntudp_create(struct sockaddr_in * raddr,u_long program,u_long version,struct timeval wait,int * sockp)2019e66e6d7Sabs clntudp_create(struct sockaddr_in *raddr, u_long program, u_long version,
2029e66e6d7Sabs     struct timeval wait, int *sockp)
2037df0ccbaSfvdl {
2047df0ccbaSfvdl 	return clntudp_bufcreate(raddr, program, version, wait, sockp,
2057df0ccbaSfvdl 					UDPMSGSIZE, UDPMSGSIZE);
2067df0ccbaSfvdl }
2077df0ccbaSfvdl 
2087df0ccbaSfvdl CLIENT *
clnttcp_create(struct sockaddr_in * raddr,u_long prog,u_long vers,int * sockp,u_int sendsz,u_int recvsz)2099e66e6d7Sabs clnttcp_create(struct sockaddr_in *raddr, u_long prog, u_long vers, int *sockp,
2109e66e6d7Sabs     u_int sendsz, u_int recvsz)
2117df0ccbaSfvdl {
212deb154d2Schristos 	return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
213deb154d2Schristos 	    sendsz, recvsz, "tcp");
2147df0ccbaSfvdl }
2157df0ccbaSfvdl 
2167df0ccbaSfvdl CLIENT *
clntraw_create(u_long prog,u_long vers)217adb74221Smatt clntraw_create(u_long prog, u_long vers)
2187df0ccbaSfvdl {
219deb154d2Schristos 	return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
2207df0ccbaSfvdl }
2217df0ccbaSfvdl 
2227df0ccbaSfvdl /*
2237df0ccbaSfvdl  * A common server create routine
2247df0ccbaSfvdl  */
2257df0ccbaSfvdl static SVCXPRT *
svc_com_create(int fd,u_int sendsize,u_int recvsize,const char * netid)226adb74221Smatt svc_com_create(int fd, u_int sendsize, u_int recvsize, const char *netid)
2277df0ccbaSfvdl {
2287df0ccbaSfvdl 	struct netconfig *nconf;
2297df0ccbaSfvdl 	SVCXPRT *svc;
2307df0ccbaSfvdl 	int madefd = FALSE;
2317df0ccbaSfvdl 	int port;
23250f5afd5Slukem 	struct sockaddr_in sccsin;
2337df0ccbaSfvdl 
2340e8cfd8fSlukem 	_DIAGASSERT(netid != NULL);
2350e8cfd8fSlukem 
2367df0ccbaSfvdl 	if ((nconf = __rpc_getconfip(netid)) == NULL) {
2377df0ccbaSfvdl 		(void) syslog(LOG_ERR, "Could not get %s transport", netid);
238deb154d2Schristos 		return (NULL);
2397df0ccbaSfvdl 	}
2407df0ccbaSfvdl 	if (fd == RPC_ANYSOCK) {
2417df0ccbaSfvdl 		fd = __rpc_nconf2fd(nconf);
2427df0ccbaSfvdl 		if (fd == -1) {
2437df0ccbaSfvdl 			(void) freenetconfigent(nconf);
2447df0ccbaSfvdl 			(void) syslog(LOG_ERR,
2457df0ccbaSfvdl 			"svc%s_create: could not open connection", netid);
246deb154d2Schristos 			return (NULL);
2477df0ccbaSfvdl 		}
2487df0ccbaSfvdl 		madefd = TRUE;
2497df0ccbaSfvdl 	}
2507df0ccbaSfvdl 
25150f5afd5Slukem 	memset(&sccsin, 0, sizeof sccsin);
25250f5afd5Slukem 	sccsin.sin_family = AF_INET;
253bf67527dSchristos 	(void)bindresvport(fd, &sccsin);
25450ccfe1eSchristos 
25550ccfe1eSchristos 	switch (nconf->nc_semantics) {
25650ccfe1eSchristos 	case NC_TPI_COTS:
25750ccfe1eSchristos 	case NC_TPI_COTS_ORD:
25850ccfe1eSchristos 		if (listen(fd, SOMAXCONN) == -1) {
2597655e306Stron 			(void) syslog(LOG_ERR,
2607655e306Stron 			    "svc%s_create: listen(2) failed: %s",
2617655e306Stron 			    netid, strerror(errno));
2627655e306Stron 			(void) freenetconfigent(nconf);
2635d9565aaSchristos 			goto out;
2647655e306Stron 		}
26550ccfe1eSchristos 		break;
26650ccfe1eSchristos 	default:
26750ccfe1eSchristos 		break;
26850ccfe1eSchristos 	}
26950ccfe1eSchristos 
270deb154d2Schristos 	svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
2717df0ccbaSfvdl 	(void) freenetconfigent(nconf);
2725d9565aaSchristos 	if (svc == NULL)
2735d9565aaSchristos 		goto out;
2747df0ccbaSfvdl 	port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
2757df0ccbaSfvdl 	svc->xp_port = ntohs(port);
2765d9565aaSchristos 	return svc;
2775d9565aaSchristos out:
2785d9565aaSchristos 	if (madefd)
2795d9565aaSchristos 		(void) close(fd);
2805d9565aaSchristos 	return NULL;
2817df0ccbaSfvdl }
2827df0ccbaSfvdl 
2837df0ccbaSfvdl SVCXPRT *
svctcp_create(int fd,u_int sendsize,u_int recvsize)2849e66e6d7Sabs svctcp_create(int fd, u_int sendsize, u_int recvsize)
2857df0ccbaSfvdl {
2867df0ccbaSfvdl 	return svc_com_create(fd, sendsize, recvsize, "tcp");
2877df0ccbaSfvdl }
2887df0ccbaSfvdl 
2897df0ccbaSfvdl SVCXPRT *
svcudp_bufcreate(int fd,u_int sendsz,u_int recvsz)2909e66e6d7Sabs svcudp_bufcreate(int fd, u_int sendsz, u_int recvsz)
2917df0ccbaSfvdl {
2927df0ccbaSfvdl 	return svc_com_create(fd, sendsz, recvsz, "udp");
2937df0ccbaSfvdl }
2947df0ccbaSfvdl 
2957df0ccbaSfvdl SVCXPRT *
svcfd_create(int fd,u_int sendsize,u_int recvsize)2969e66e6d7Sabs svcfd_create(int fd, u_int sendsize, u_int recvsize)
2977df0ccbaSfvdl {
2987df0ccbaSfvdl 	return svc_fd_create(fd, sendsize, recvsize);
2997df0ccbaSfvdl }
3007df0ccbaSfvdl 
3017df0ccbaSfvdl 
3027df0ccbaSfvdl SVCXPRT *
svcudp_create(int fd)3039e66e6d7Sabs svcudp_create(int fd)
3047df0ccbaSfvdl {
3057df0ccbaSfvdl 	return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
3067df0ccbaSfvdl }
3077df0ccbaSfvdl 
3087df0ccbaSfvdl SVCXPRT *
svcraw_create(void)3093d365e74Schristos svcraw_create(void)
3107df0ccbaSfvdl {
3117df0ccbaSfvdl 	return svc_raw_create();
3127df0ccbaSfvdl }
3137df0ccbaSfvdl 
3147df0ccbaSfvdl int
get_myaddress(struct sockaddr_in * addr)3159e66e6d7Sabs get_myaddress(struct sockaddr_in *addr)
3167df0ccbaSfvdl {
3170e8cfd8fSlukem 
3180e8cfd8fSlukem 	_DIAGASSERT(addr != NULL);
3190e8cfd8fSlukem 
3207df0ccbaSfvdl 	memset((void *) addr, 0, sizeof(*addr));
3217df0ccbaSfvdl 	addr->sin_family = AF_INET;
3227df0ccbaSfvdl 	addr->sin_port = htons(PMAPPORT);
3237df0ccbaSfvdl 	addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
3247df0ccbaSfvdl 	return (0);
3257df0ccbaSfvdl }
3267df0ccbaSfvdl 
3277df0ccbaSfvdl /*
3287df0ccbaSfvdl  * For connectionless "udp" transport. Obsoleted by rpc_call().
3297df0ccbaSfvdl  */
3307df0ccbaSfvdl int
callrpc(char * host,int prognum,int versnum,int procnum,xdrproc_t inproc,char * in,xdrproc_t outproc,char * out)331adb74221Smatt callrpc(char *host, int prognum, int versnum, int procnum,
332adb74221Smatt 	xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
3337df0ccbaSfvdl {
334deb154d2Schristos 	return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
335deb154d2Schristos 	    (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
3367df0ccbaSfvdl }
3377df0ccbaSfvdl 
3387df0ccbaSfvdl /*
3397df0ccbaSfvdl  * For connectionless kind of transport. Obsoleted by rpc_reg()
3407df0ccbaSfvdl  */
3417df0ccbaSfvdl int
registerrpc(int prognum,int versnum,int procnum,char * (* progname)(char[UDPMSGSIZE]),xdrproc_t inproc,xdrproc_t outproc)342adb74221Smatt registerrpc(int prognum, int versnum, int procnum,
343adb74221Smatt 	char *(*progname)(char [UDPMSGSIZE]),
344adb74221Smatt 	xdrproc_t inproc, xdrproc_t outproc)
3457df0ccbaSfvdl {
346deb154d2Schristos 	return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
34703256c6eSchristos 	    (rpcproc_t)procnum, progname, inproc, outproc, __UNCONST("udp"));
3487df0ccbaSfvdl }
3497df0ccbaSfvdl 
3507df0ccbaSfvdl /*
3517df0ccbaSfvdl  * All the following clnt_broadcast stuff is convulated; it supports
3527df0ccbaSfvdl  * the earlier calling style of the callback function
3537df0ccbaSfvdl  */
3543fdac2b8Sthorpej #ifdef _REENTRANT
3557df0ccbaSfvdl static thread_key_t	clnt_broadcast_key;
3567df0ccbaSfvdl #endif
3577df0ccbaSfvdl static resultproc_t	clnt_broadcast_result_main;
3587df0ccbaSfvdl 
3597df0ccbaSfvdl /*
3607df0ccbaSfvdl  * Need to translate the netbuf address into sockaddr_in address.
3617df0ccbaSfvdl  * Dont care about netid here.
3627df0ccbaSfvdl  */
3637df0ccbaSfvdl /* ARGSUSED */
3647df0ccbaSfvdl static bool_t
rpc_wrap_bcast(char * resultp,struct netbuf * addr,struct netconfig * nconf)365adb74221Smatt rpc_wrap_bcast(
366adb74221Smatt 	char *resultp,		/* results of the call */
367adb74221Smatt 	struct netbuf *addr,	/* address of the guy who responded */
368adb74221Smatt 	struct netconfig *nconf) /* Netconf of the transport */
3697df0ccbaSfvdl {
3707df0ccbaSfvdl 	resultproc_t clnt_broadcast_result;
3717df0ccbaSfvdl 
3720e8cfd8fSlukem 	_DIAGASSERT(resultp != NULL);
3730e8cfd8fSlukem 	_DIAGASSERT(addr != NULL);
3740e8cfd8fSlukem 	_DIAGASSERT(nconf != NULL);
3750e8cfd8fSlukem 
3767df0ccbaSfvdl 	if (strcmp(nconf->nc_netid, "udp"))
3777df0ccbaSfvdl 		return (FALSE);
3783fdac2b8Sthorpej #ifdef _REENTRANT
3793fdac2b8Sthorpej 	if (__isthreaded == 0)
3807df0ccbaSfvdl 		clnt_broadcast_result = clnt_broadcast_result_main;
3817df0ccbaSfvdl 	else
3823fdac2b8Sthorpej 		clnt_broadcast_result = thr_getspecific(clnt_broadcast_key);
3837df0ccbaSfvdl #else
3847df0ccbaSfvdl 	clnt_broadcast_result = clnt_broadcast_result_main;
3857df0ccbaSfvdl #endif
3867df0ccbaSfvdl 	return (*clnt_broadcast_result)(resultp,
3877df0ccbaSfvdl 				(struct sockaddr_in *)addr->buf);
3887df0ccbaSfvdl }
3897df0ccbaSfvdl 
3903fdac2b8Sthorpej #ifdef _REENTRANT
3913fdac2b8Sthorpej static once_t clnt_broadcast_once = ONCE_INITIALIZER;
3923fdac2b8Sthorpej 
3933fdac2b8Sthorpej static void
clnt_broadcast_setup(void)3943fdac2b8Sthorpej clnt_broadcast_setup(void)
3953fdac2b8Sthorpej {
3963fdac2b8Sthorpej 
3973fdac2b8Sthorpej 	thr_keycreate(&clnt_broadcast_key, free);
3983fdac2b8Sthorpej }
3993fdac2b8Sthorpej #endif
4003fdac2b8Sthorpej 
4017df0ccbaSfvdl /*
4027df0ccbaSfvdl  * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
4037df0ccbaSfvdl  */
4047df0ccbaSfvdl enum clnt_stat
clnt_broadcast(u_long prog,u_long vers,u_long proc,xdrproc_t xargs,caddr_t argsp,xdrproc_t xresults,caddr_t resultsp,resultproc_t eachresult)405adb74221Smatt clnt_broadcast(
406adb74221Smatt 	u_long		prog,		/* program number */
407adb74221Smatt 	u_long		vers,		/* version number */
408adb74221Smatt 	u_long		proc,		/* procedure number */
409adb74221Smatt 	xdrproc_t	xargs,		/* xdr routine for args */
410adb74221Smatt 	caddr_t		argsp,		/* pointer to args */
411adb74221Smatt 	xdrproc_t	xresults,	/* xdr routine for results */
412adb74221Smatt 	caddr_t		resultsp,	/* pointer to results */
413adb74221Smatt 	resultproc_t	eachresult)	/* call with each result obtained */
4147df0ccbaSfvdl {
4153fdac2b8Sthorpej #ifdef _REENTRANT
4163fdac2b8Sthorpej 	if (__isthreaded == 0)
4177df0ccbaSfvdl 		clnt_broadcast_result_main = eachresult;
4187df0ccbaSfvdl 	else {
4193fdac2b8Sthorpej 		thr_once(&clnt_broadcast_once, clnt_broadcast_setup);
4207df0ccbaSfvdl 		thr_setspecific(clnt_broadcast_key, (void *) eachresult);
4217df0ccbaSfvdl 	}
4227df0ccbaSfvdl #else
4237df0ccbaSfvdl 	clnt_broadcast_result_main = eachresult;
4247df0ccbaSfvdl #endif
425deb154d2Schristos 	return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
426deb154d2Schristos 	    (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
427deb154d2Schristos 	    (resultproc_t) rpc_wrap_bcast, "udp");
4287df0ccbaSfvdl }
4297df0ccbaSfvdl 
4307df0ccbaSfvdl #endif /* PORTMAP */
431