1*0a6a1f1dSLionel Sambuc /* $NetBSD: rpc_soc.c,v 1.19 2014/05/28 14:45:57 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
484d9c625SLionel Sambuc * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras *
684d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
784d9c625SLionel Sambuc * modification, are permitted provided that the following conditions are
884d9c625SLionel Sambuc * met:
92fe8fb19SBen Gras *
1084d9c625SLionel Sambuc * * Redistributions of source code must retain the above copyright
1184d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1284d9c625SLionel Sambuc * * Redistributions in binary form must reproduce the above
1384d9c625SLionel Sambuc * copyright notice, this list of conditions and the following
1484d9c625SLionel Sambuc * disclaimer in the documentation and/or other materials
1584d9c625SLionel Sambuc * provided with the distribution.
1684d9c625SLionel Sambuc * * Neither the name of the "Oracle America, Inc." nor the names of its
1784d9c625SLionel Sambuc * contributors may be used to endorse or promote products derived
1884d9c625SLionel Sambuc * from this software without specific prior written permission.
192fe8fb19SBen Gras *
2084d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2184d9c625SLionel Sambuc * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2284d9c625SLionel Sambuc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2384d9c625SLionel Sambuc * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2484d9c625SLionel Sambuc * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2584d9c625SLionel Sambuc * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2684d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2784d9c625SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2884d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2984d9c625SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3084d9c625SLionel Sambuc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3184d9c625SLionel Sambuc * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras */
332fe8fb19SBen Gras
342fe8fb19SBen Gras /* #ident "@(#)rpc_soc.c 1.17 94/04/24 SMI" */
352fe8fb19SBen Gras
362fe8fb19SBen Gras /*
372fe8fb19SBen Gras * Copyright (c) 1986-1991 by Sun Microsystems Inc.
382fe8fb19SBen Gras * In addition, portions of such source code were derived from Berkeley
392fe8fb19SBen Gras * 4.3 BSD under license from the Regents of the University of
402fe8fb19SBen Gras * California.
412fe8fb19SBen Gras */
422fe8fb19SBen Gras
432fe8fb19SBen Gras #include <sys/cdefs.h>
442fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
452fe8fb19SBen Gras #if 0
462fe8fb19SBen Gras static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro";
472fe8fb19SBen Gras #else
48*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: rpc_soc.c,v 1.19 2014/05/28 14:45:57 christos Exp $");
492fe8fb19SBen Gras #endif
502fe8fb19SBen Gras #endif
512fe8fb19SBen Gras
522fe8fb19SBen Gras #ifdef PORTMAP
532fe8fb19SBen Gras /*
542fe8fb19SBen Gras * rpc_soc.c
552fe8fb19SBen Gras *
562fe8fb19SBen Gras * The backward compatibility routines for the earlier implementation
572fe8fb19SBen Gras * of RPC, where the only transports supported were tcp/ip and udp/ip.
582fe8fb19SBen Gras * Based on berkeley socket abstraction, now implemented on the top
592fe8fb19SBen Gras * of TLI/Streams
602fe8fb19SBen Gras */
612fe8fb19SBen Gras
622fe8fb19SBen Gras #include "namespace.h"
632fe8fb19SBen Gras #include "reentrant.h"
642fe8fb19SBen Gras #include <sys/types.h>
652fe8fb19SBen Gras #include <sys/socket.h>
662fe8fb19SBen Gras #include <stdio.h>
672fe8fb19SBen Gras #include <rpc/rpc.h>
682fe8fb19SBen Gras #include <rpc/pmap_clnt.h>
692fe8fb19SBen Gras #include <rpc/pmap_prot.h>
702fe8fb19SBen Gras #include <rpc/nettype.h>
712fe8fb19SBen Gras #include <netinet/in.h>
722fe8fb19SBen Gras #include <assert.h>
732fe8fb19SBen Gras #include <errno.h>
742fe8fb19SBen Gras #include <netdb.h>
752fe8fb19SBen Gras #include <stdlib.h>
762fe8fb19SBen Gras #include <string.h>
772fe8fb19SBen Gras #include <syslog.h>
782fe8fb19SBen Gras #include <unistd.h>
792fe8fb19SBen Gras
8084d9c625SLionel Sambuc #include "svc_fdset.h"
812fe8fb19SBen Gras #include "rpc_internal.h"
822fe8fb19SBen Gras
832fe8fb19SBen Gras #ifdef __weak_alias
842fe8fb19SBen Gras __weak_alias(clntudp_bufcreate,_clntudp_bufcreate)
852fe8fb19SBen Gras __weak_alias(clntudp_create,_clntudp_create)
862fe8fb19SBen Gras __weak_alias(clnttcp_create,_clnttcp_create)
872fe8fb19SBen Gras __weak_alias(clntraw_create,_clntraw_create)
882fe8fb19SBen Gras __weak_alias(get_myaddress,_get_myaddress)
892fe8fb19SBen Gras __weak_alias(svcfd_create,_svcfd_create)
902fe8fb19SBen Gras __weak_alias(svcudp_bufcreate,_svcudp_bufcreate)
912fe8fb19SBen Gras __weak_alias(svcudp_create,_svcudp_create)
922fe8fb19SBen Gras __weak_alias(svctcp_create,_svctcp_create)
932fe8fb19SBen Gras __weak_alias(svcraw_create,_svcraw_create)
942fe8fb19SBen Gras __weak_alias(callrpc,_callrpc)
952fe8fb19SBen Gras __weak_alias(registerrpc,_registerrpc)
962fe8fb19SBen Gras __weak_alias(clnt_broadcast,_clnt_broadcast)
972fe8fb19SBen Gras #endif
982fe8fb19SBen Gras
992fe8fb19SBen Gras #ifdef _REENTRANT
1002fe8fb19SBen Gras extern mutex_t rpcsoc_lock;
1012fe8fb19SBen Gras #endif
1022fe8fb19SBen Gras
103f14fb602SLionel Sambuc static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t,
104f14fb602SLionel Sambuc int *, u_int, u_int, const char *);
105f14fb602SLionel Sambuc static SVCXPRT *svc_com_create(int, u_int, u_int, const char *);
106f14fb602SLionel Sambuc static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *);
1072fe8fb19SBen Gras
1082fe8fb19SBen Gras /*
1092fe8fb19SBen Gras * A common clnt create routine
1102fe8fb19SBen Gras */
1112fe8fb19SBen Gras 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)112f14fb602SLionel Sambuc clnt_com_create(struct sockaddr_in *raddr, rpcprog_t prog, rpcvers_t vers,
113f14fb602SLionel Sambuc int *sockp, u_int sendsz, u_int recvsz, const char *tp)
1142fe8fb19SBen Gras {
1152fe8fb19SBen Gras CLIENT *cl;
1162fe8fb19SBen Gras int madefd = FALSE;
1172fe8fb19SBen Gras int fd;
1182fe8fb19SBen Gras struct netconfig *nconf;
1192fe8fb19SBen Gras struct netbuf bindaddr;
1202fe8fb19SBen Gras
1212fe8fb19SBen Gras _DIAGASSERT(raddr != NULL);
1222fe8fb19SBen Gras _DIAGASSERT(sockp != NULL);
1232fe8fb19SBen Gras _DIAGASSERT(tp != NULL);
1242fe8fb19SBen Gras
1252fe8fb19SBen Gras fd = *sockp;
1262fe8fb19SBen Gras
1272fe8fb19SBen Gras mutex_lock(&rpcsoc_lock);
1282fe8fb19SBen Gras if ((nconf = __rpc_getconfip(tp)) == NULL) {
1292fe8fb19SBen Gras rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1302fe8fb19SBen Gras mutex_unlock(&rpcsoc_lock);
1312fe8fb19SBen Gras return (NULL);
1322fe8fb19SBen Gras }
1332fe8fb19SBen Gras if (fd == RPC_ANYSOCK) {
1342fe8fb19SBen Gras fd = __rpc_nconf2fd(nconf);
1352fe8fb19SBen Gras if (fd == -1)
1362fe8fb19SBen Gras goto syserror;
1372fe8fb19SBen Gras madefd = TRUE;
1382fe8fb19SBen Gras }
1392fe8fb19SBen Gras
1402fe8fb19SBen Gras if (raddr->sin_port == 0) {
1412fe8fb19SBen Gras u_int proto;
1422fe8fb19SBen Gras u_short sport;
1432fe8fb19SBen Gras
1442fe8fb19SBen Gras mutex_unlock(&rpcsoc_lock); /* pmap_getport is recursive */
1452fe8fb19SBen Gras proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
1462fe8fb19SBen Gras sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
1472fe8fb19SBen Gras proto);
1482fe8fb19SBen Gras if (sport == 0) {
1492fe8fb19SBen Gras goto err;
1502fe8fb19SBen Gras }
1512fe8fb19SBen Gras raddr->sin_port = htons(sport);
1522fe8fb19SBen Gras mutex_lock(&rpcsoc_lock); /* pmap_getport is recursive */
1532fe8fb19SBen Gras }
1542fe8fb19SBen Gras
1552fe8fb19SBen Gras /* Transform sockaddr_in to netbuf */
1562fe8fb19SBen Gras bindaddr.maxlen = bindaddr.len = sizeof (struct sockaddr_in);
1572fe8fb19SBen Gras bindaddr.buf = raddr;
1582fe8fb19SBen Gras
159*0a6a1f1dSLionel Sambuc (void)bindresvport(fd, NULL);
1602fe8fb19SBen Gras cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
1612fe8fb19SBen Gras sendsz, recvsz);
1622fe8fb19SBen Gras if (cl) {
1632fe8fb19SBen Gras if (madefd == TRUE) {
1642fe8fb19SBen Gras /*
1652fe8fb19SBen Gras * The fd should be closed while destroying the handle.
1662fe8fb19SBen Gras */
1672fe8fb19SBen Gras (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
1682fe8fb19SBen Gras *sockp = fd;
1692fe8fb19SBen Gras }
1702fe8fb19SBen Gras (void) freenetconfigent(nconf);
1712fe8fb19SBen Gras mutex_unlock(&rpcsoc_lock);
1722fe8fb19SBen Gras return (cl);
1732fe8fb19SBen Gras }
1742fe8fb19SBen Gras goto err;
1752fe8fb19SBen Gras
1762fe8fb19SBen Gras syserror:
1772fe8fb19SBen Gras rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1782fe8fb19SBen Gras rpc_createerr.cf_error.re_errno = errno;
1792fe8fb19SBen Gras
1802fe8fb19SBen Gras err: if (madefd == TRUE)
1812fe8fb19SBen Gras (void) close(fd);
1822fe8fb19SBen Gras (void) freenetconfigent(nconf);
1832fe8fb19SBen Gras mutex_unlock(&rpcsoc_lock);
1842fe8fb19SBen Gras return (NULL);
1852fe8fb19SBen Gras }
1862fe8fb19SBen Gras
1872fe8fb19SBen Gras CLIENT *
clntudp_bufcreate(struct sockaddr_in * raddr,u_long prog,u_long vers,struct timeval wait,int * sockp,u_int sendsz,u_int recvsz)188f14fb602SLionel Sambuc clntudp_bufcreate(struct sockaddr_in *raddr, u_long prog, u_long vers, struct timeval wait, int *sockp, u_int sendsz, u_int recvsz)
1892fe8fb19SBen Gras {
1902fe8fb19SBen Gras CLIENT *cl;
1912fe8fb19SBen Gras
1922fe8fb19SBen Gras _DIAGASSERT(raddr != NULL);
1932fe8fb19SBen Gras _DIAGASSERT(sockp != NULL);
1942fe8fb19SBen Gras
1952fe8fb19SBen Gras cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
1962fe8fb19SBen Gras sendsz, recvsz, "udp");
1972fe8fb19SBen Gras if (cl == NULL) {
1982fe8fb19SBen Gras return (NULL);
1992fe8fb19SBen Gras }
2002fe8fb19SBen Gras (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, (char *)(void *)&wait);
2012fe8fb19SBen Gras return (cl);
2022fe8fb19SBen Gras }
2032fe8fb19SBen Gras
2042fe8fb19SBen Gras CLIENT *
clntudp_create(struct sockaddr_in * raddr,u_long program,u_long version,struct timeval wait,int * sockp)205f14fb602SLionel Sambuc clntudp_create(struct sockaddr_in *raddr, u_long program, u_long version,
206f14fb602SLionel Sambuc struct timeval wait, int *sockp)
2072fe8fb19SBen Gras {
2082fe8fb19SBen Gras return clntudp_bufcreate(raddr, program, version, wait, sockp,
2092fe8fb19SBen Gras UDPMSGSIZE, UDPMSGSIZE);
2102fe8fb19SBen Gras }
2112fe8fb19SBen Gras
2122fe8fb19SBen Gras CLIENT *
clnttcp_create(struct sockaddr_in * raddr,u_long prog,u_long vers,int * sockp,u_int sendsz,u_int recvsz)213f14fb602SLionel Sambuc clnttcp_create(struct sockaddr_in *raddr, u_long prog, u_long vers, int *sockp,
214f14fb602SLionel Sambuc u_int sendsz, u_int recvsz)
2152fe8fb19SBen Gras {
2162fe8fb19SBen Gras return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
2172fe8fb19SBen Gras sendsz, recvsz, "tcp");
2182fe8fb19SBen Gras }
2192fe8fb19SBen Gras
2202fe8fb19SBen Gras CLIENT *
clntraw_create(u_long prog,u_long vers)221f14fb602SLionel Sambuc clntraw_create(u_long prog, u_long vers)
2222fe8fb19SBen Gras {
2232fe8fb19SBen Gras return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
2242fe8fb19SBen Gras }
2252fe8fb19SBen Gras
2262fe8fb19SBen Gras /*
2272fe8fb19SBen Gras * A common server create routine
2282fe8fb19SBen Gras */
2292fe8fb19SBen Gras static SVCXPRT *
svc_com_create(int fd,u_int sendsize,u_int recvsize,const char * netid)230f14fb602SLionel Sambuc svc_com_create(int fd, u_int sendsize, u_int recvsize, const char *netid)
2312fe8fb19SBen Gras {
2322fe8fb19SBen Gras struct netconfig *nconf;
2332fe8fb19SBen Gras SVCXPRT *svc;
2342fe8fb19SBen Gras int madefd = FALSE;
2352fe8fb19SBen Gras int port;
2362fe8fb19SBen Gras struct sockaddr_in sccsin;
2372fe8fb19SBen Gras
2382fe8fb19SBen Gras _DIAGASSERT(netid != NULL);
2392fe8fb19SBen Gras
2402fe8fb19SBen Gras if ((nconf = __rpc_getconfip(netid)) == NULL) {
2412fe8fb19SBen Gras (void) syslog(LOG_ERR, "Could not get %s transport", netid);
2422fe8fb19SBen Gras return (NULL);
2432fe8fb19SBen Gras }
2442fe8fb19SBen Gras if (fd == RPC_ANYSOCK) {
2452fe8fb19SBen Gras fd = __rpc_nconf2fd(nconf);
2462fe8fb19SBen Gras if (fd == -1) {
2472fe8fb19SBen Gras (void) freenetconfigent(nconf);
2482fe8fb19SBen Gras (void) syslog(LOG_ERR,
2492fe8fb19SBen Gras "svc%s_create: could not open connection", netid);
2502fe8fb19SBen Gras return (NULL);
2512fe8fb19SBen Gras }
2522fe8fb19SBen Gras madefd = TRUE;
2532fe8fb19SBen Gras }
2542fe8fb19SBen Gras
2552fe8fb19SBen Gras memset(&sccsin, 0, sizeof sccsin);
2562fe8fb19SBen Gras sccsin.sin_family = AF_INET;
257*0a6a1f1dSLionel Sambuc (void)bindresvport(fd, &sccsin);
2582fe8fb19SBen Gras listen(fd, SOMAXCONN);
2592fe8fb19SBen Gras svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
2602fe8fb19SBen Gras (void) freenetconfigent(nconf);
2612fe8fb19SBen Gras if (svc == NULL) {
2622fe8fb19SBen Gras if (madefd)
2632fe8fb19SBen Gras (void) close(fd);
2642fe8fb19SBen Gras return (NULL);
2652fe8fb19SBen Gras }
2662fe8fb19SBen Gras port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
2672fe8fb19SBen Gras svc->xp_port = ntohs(port);
2682fe8fb19SBen Gras return (svc);
2692fe8fb19SBen Gras }
2702fe8fb19SBen Gras
2712fe8fb19SBen Gras SVCXPRT *
svctcp_create(int fd,u_int sendsize,u_int recvsize)272f14fb602SLionel Sambuc svctcp_create(int fd, u_int sendsize, u_int recvsize)
2732fe8fb19SBen Gras {
2742fe8fb19SBen Gras return svc_com_create(fd, sendsize, recvsize, "tcp");
2752fe8fb19SBen Gras }
2762fe8fb19SBen Gras
2772fe8fb19SBen Gras SVCXPRT *
svcudp_bufcreate(int fd,u_int sendsz,u_int recvsz)278f14fb602SLionel Sambuc svcudp_bufcreate(int fd, u_int sendsz, u_int recvsz)
2792fe8fb19SBen Gras {
2802fe8fb19SBen Gras return svc_com_create(fd, sendsz, recvsz, "udp");
2812fe8fb19SBen Gras }
2822fe8fb19SBen Gras
2832fe8fb19SBen Gras SVCXPRT *
svcfd_create(int fd,u_int sendsize,u_int recvsize)284f14fb602SLionel Sambuc svcfd_create(int fd, u_int sendsize, u_int recvsize)
2852fe8fb19SBen Gras {
2862fe8fb19SBen Gras return svc_fd_create(fd, sendsize, recvsize);
2872fe8fb19SBen Gras }
2882fe8fb19SBen Gras
2892fe8fb19SBen Gras
2902fe8fb19SBen Gras SVCXPRT *
svcudp_create(int fd)291f14fb602SLionel Sambuc svcudp_create(int fd)
2922fe8fb19SBen Gras {
2932fe8fb19SBen Gras return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
2942fe8fb19SBen Gras }
2952fe8fb19SBen Gras
2962fe8fb19SBen Gras SVCXPRT *
svcraw_create(void)297f14fb602SLionel Sambuc svcraw_create(void)
2982fe8fb19SBen Gras {
2992fe8fb19SBen Gras return svc_raw_create();
3002fe8fb19SBen Gras }
3012fe8fb19SBen Gras
3022fe8fb19SBen Gras int
get_myaddress(struct sockaddr_in * addr)303f14fb602SLionel Sambuc get_myaddress(struct sockaddr_in *addr)
3042fe8fb19SBen Gras {
3052fe8fb19SBen Gras
3062fe8fb19SBen Gras _DIAGASSERT(addr != NULL);
3072fe8fb19SBen Gras
3082fe8fb19SBen Gras memset((void *) addr, 0, sizeof(*addr));
3092fe8fb19SBen Gras addr->sin_family = AF_INET;
3102fe8fb19SBen Gras addr->sin_port = htons(PMAPPORT);
3112fe8fb19SBen Gras addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
3122fe8fb19SBen Gras return (0);
3132fe8fb19SBen Gras }
3142fe8fb19SBen Gras
3152fe8fb19SBen Gras /*
3162fe8fb19SBen Gras * For connectionless "udp" transport. Obsoleted by rpc_call().
3172fe8fb19SBen Gras */
3182fe8fb19SBen Gras int
callrpc(char * host,int prognum,int versnum,int procnum,xdrproc_t inproc,char * in,xdrproc_t outproc,char * out)319f14fb602SLionel Sambuc callrpc(char *host, int prognum, int versnum, int procnum,
320f14fb602SLionel Sambuc xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
3212fe8fb19SBen Gras {
3222fe8fb19SBen Gras return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
3232fe8fb19SBen Gras (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
3242fe8fb19SBen Gras }
3252fe8fb19SBen Gras
3262fe8fb19SBen Gras /*
3272fe8fb19SBen Gras * For connectionless kind of transport. Obsoleted by rpc_reg()
3282fe8fb19SBen Gras */
3292fe8fb19SBen Gras int
registerrpc(int prognum,int versnum,int procnum,char * (* progname)(char[UDPMSGSIZE]),xdrproc_t inproc,xdrproc_t outproc)330f14fb602SLionel Sambuc registerrpc(int prognum, int versnum, int procnum,
331f14fb602SLionel Sambuc char *(*progname)(char [UDPMSGSIZE]),
332f14fb602SLionel Sambuc xdrproc_t inproc, xdrproc_t outproc)
3332fe8fb19SBen Gras {
3342fe8fb19SBen Gras return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
3352fe8fb19SBen Gras (rpcproc_t)procnum, progname, inproc, outproc, __UNCONST("udp"));
3362fe8fb19SBen Gras }
3372fe8fb19SBen Gras
3382fe8fb19SBen Gras /*
3392fe8fb19SBen Gras * All the following clnt_broadcast stuff is convulated; it supports
3402fe8fb19SBen Gras * the earlier calling style of the callback function
3412fe8fb19SBen Gras */
3422fe8fb19SBen Gras #ifdef _REENTRANT
3432fe8fb19SBen Gras static thread_key_t clnt_broadcast_key;
3442fe8fb19SBen Gras #endif
3452fe8fb19SBen Gras static resultproc_t clnt_broadcast_result_main;
3462fe8fb19SBen Gras
3472fe8fb19SBen Gras /*
3482fe8fb19SBen Gras * Need to translate the netbuf address into sockaddr_in address.
3492fe8fb19SBen Gras * Dont care about netid here.
3502fe8fb19SBen Gras */
3512fe8fb19SBen Gras /* ARGSUSED */
3522fe8fb19SBen Gras static bool_t
rpc_wrap_bcast(char * resultp,struct netbuf * addr,struct netconfig * nconf)353f14fb602SLionel Sambuc rpc_wrap_bcast(
354f14fb602SLionel Sambuc char *resultp, /* results of the call */
355f14fb602SLionel Sambuc struct netbuf *addr, /* address of the guy who responded */
356f14fb602SLionel Sambuc struct netconfig *nconf) /* Netconf of the transport */
3572fe8fb19SBen Gras {
3582fe8fb19SBen Gras resultproc_t clnt_broadcast_result;
3592fe8fb19SBen Gras
3602fe8fb19SBen Gras _DIAGASSERT(resultp != NULL);
3612fe8fb19SBen Gras _DIAGASSERT(addr != NULL);
3622fe8fb19SBen Gras _DIAGASSERT(nconf != NULL);
3632fe8fb19SBen Gras
3642fe8fb19SBen Gras if (strcmp(nconf->nc_netid, "udp"))
3652fe8fb19SBen Gras return (FALSE);
3662fe8fb19SBen Gras #ifdef _REENTRANT
3672fe8fb19SBen Gras if (__isthreaded == 0)
3682fe8fb19SBen Gras clnt_broadcast_result = clnt_broadcast_result_main;
3692fe8fb19SBen Gras else
3702fe8fb19SBen Gras clnt_broadcast_result = thr_getspecific(clnt_broadcast_key);
3712fe8fb19SBen Gras #else
3722fe8fb19SBen Gras clnt_broadcast_result = clnt_broadcast_result_main;
3732fe8fb19SBen Gras #endif
3742fe8fb19SBen Gras return (*clnt_broadcast_result)(resultp,
3752fe8fb19SBen Gras (struct sockaddr_in *)addr->buf);
3762fe8fb19SBen Gras }
3772fe8fb19SBen Gras
3782fe8fb19SBen Gras #ifdef _REENTRANT
3792fe8fb19SBen Gras static once_t clnt_broadcast_once = ONCE_INITIALIZER;
3802fe8fb19SBen Gras
3812fe8fb19SBen Gras static void
clnt_broadcast_setup(void)3822fe8fb19SBen Gras clnt_broadcast_setup(void)
3832fe8fb19SBen Gras {
3842fe8fb19SBen Gras
3852fe8fb19SBen Gras thr_keycreate(&clnt_broadcast_key, free);
3862fe8fb19SBen Gras }
3872fe8fb19SBen Gras #endif
3882fe8fb19SBen Gras
3892fe8fb19SBen Gras /*
3902fe8fb19SBen Gras * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
3912fe8fb19SBen Gras */
3922fe8fb19SBen Gras 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)393f14fb602SLionel Sambuc clnt_broadcast(
394f14fb602SLionel Sambuc u_long prog, /* program number */
395f14fb602SLionel Sambuc u_long vers, /* version number */
396f14fb602SLionel Sambuc u_long proc, /* procedure number */
397f14fb602SLionel Sambuc xdrproc_t xargs, /* xdr routine for args */
398f14fb602SLionel Sambuc caddr_t argsp, /* pointer to args */
399f14fb602SLionel Sambuc xdrproc_t xresults, /* xdr routine for results */
400f14fb602SLionel Sambuc caddr_t resultsp, /* pointer to results */
401f14fb602SLionel Sambuc resultproc_t eachresult) /* call with each result obtained */
4022fe8fb19SBen Gras {
4032fe8fb19SBen Gras #ifdef _REENTRANT
4042fe8fb19SBen Gras if (__isthreaded == 0)
4052fe8fb19SBen Gras clnt_broadcast_result_main = eachresult;
4062fe8fb19SBen Gras else {
4072fe8fb19SBen Gras thr_once(&clnt_broadcast_once, clnt_broadcast_setup);
4082fe8fb19SBen Gras thr_setspecific(clnt_broadcast_key, (void *) eachresult);
4092fe8fb19SBen Gras }
4102fe8fb19SBen Gras #else
4112fe8fb19SBen Gras clnt_broadcast_result_main = eachresult;
4122fe8fb19SBen Gras #endif
4132fe8fb19SBen Gras return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
4142fe8fb19SBen Gras (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
4152fe8fb19SBen Gras (resultproc_t) rpc_wrap_bcast, "udp");
4162fe8fb19SBen Gras }
4172fe8fb19SBen Gras
4182fe8fb19SBen Gras #endif /* PORTMAP */
419