1*84d9c625SLionel Sambuc /* $NetBSD: pmap_rmt.c,v 1.34 2013/03/11 20:19:29 tron Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
4*84d9c625SLionel Sambuc * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras *
6*84d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
7*84d9c625SLionel Sambuc * modification, are permitted provided that the following conditions are
8*84d9c625SLionel Sambuc * met:
92fe8fb19SBen Gras *
10*84d9c625SLionel Sambuc * * Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc * * Redistributions in binary form must reproduce the above
13*84d9c625SLionel Sambuc * copyright notice, this list of conditions and the following
14*84d9c625SLionel Sambuc * disclaimer in the documentation and/or other materials
15*84d9c625SLionel Sambuc * provided with the distribution.
16*84d9c625SLionel Sambuc * * Neither the name of the "Oracle America, Inc." nor the names of its
17*84d9c625SLionel Sambuc * contributors may be used to endorse or promote products derived
18*84d9c625SLionel Sambuc * from this software without specific prior written permission.
192fe8fb19SBen Gras *
20*84d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*84d9c625SLionel Sambuc * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*84d9c625SLionel Sambuc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*84d9c625SLionel Sambuc * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*84d9c625SLionel Sambuc * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*84d9c625SLionel Sambuc * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*84d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*84d9c625SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*84d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*84d9c625SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*84d9c625SLionel Sambuc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*84d9c625SLionel Sambuc * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras */
332fe8fb19SBen Gras
342fe8fb19SBen Gras #include <sys/cdefs.h>
352fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
362fe8fb19SBen Gras #if 0
372fe8fb19SBen Gras static char *sccsid = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
382fe8fb19SBen Gras static char *sccsid = "@(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC";
392fe8fb19SBen Gras #else
40*84d9c625SLionel Sambuc __RCSID("$NetBSD: pmap_rmt.c,v 1.34 2013/03/11 20:19:29 tron Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras
442fe8fb19SBen Gras /*
452fe8fb19SBen Gras * pmap_rmt.c
462fe8fb19SBen Gras * Client interface to pmap rpc service.
472fe8fb19SBen Gras * remote call and broadcast service
482fe8fb19SBen Gras *
492fe8fb19SBen Gras * Copyright (C) 1984, Sun Microsystems, Inc.
502fe8fb19SBen Gras */
512fe8fb19SBen Gras
522fe8fb19SBen Gras #include "namespace.h"
532fe8fb19SBen Gras
542fe8fb19SBen Gras #include <sys/types.h>
552fe8fb19SBen Gras #include <sys/ioctl.h>
562fe8fb19SBen Gras #include <sys/poll.h>
572fe8fb19SBen Gras #include <sys/socket.h>
582fe8fb19SBen Gras
592fe8fb19SBen Gras #include <net/if.h>
602fe8fb19SBen Gras #include <netinet/in.h>
612fe8fb19SBen Gras #include <arpa/inet.h>
622fe8fb19SBen Gras
632fe8fb19SBen Gras #include <assert.h>
642fe8fb19SBen Gras #include <err.h>
652fe8fb19SBen Gras #include <errno.h>
662fe8fb19SBen Gras #include <stdio.h>
672fe8fb19SBen Gras #include <string.h>
682fe8fb19SBen Gras #include <unistd.h>
692fe8fb19SBen Gras
702fe8fb19SBen Gras #include <rpc/rpc.h>
712fe8fb19SBen Gras #include <rpc/pmap_prot.h>
722fe8fb19SBen Gras #include <rpc/pmap_clnt.h>
732fe8fb19SBen Gras #include <rpc/pmap_rmt.h>
742fe8fb19SBen Gras
752fe8fb19SBen Gras #ifdef __weak_alias
762fe8fb19SBen Gras __weak_alias(xdr_rmtcall_args,_xdr_rmtcall_args)
772fe8fb19SBen Gras __weak_alias(xdr_rmtcallres,_xdr_rmtcallres)
782fe8fb19SBen Gras #endif
792fe8fb19SBen Gras
802fe8fb19SBen Gras static const struct timeval timeout = { 3, 0 };
812fe8fb19SBen Gras
822fe8fb19SBen Gras /*
832fe8fb19SBen Gras * pmapper remote-call-service interface.
842fe8fb19SBen Gras * This routine is used to call the pmapper remote call service
852fe8fb19SBen Gras * which will look up a service program in the port maps, and then
862fe8fb19SBen Gras * remotely call that routine with the given parameters. This allows
872fe8fb19SBen Gras * programs to do a lookup and call in one step.
882fe8fb19SBen Gras */
892fe8fb19SBen Gras enum clnt_stat
pmap_rmtcall(struct sockaddr_in * addr,u_long prog,u_long vers,u_long proc,xdrproc_t xdrargs,caddr_t argsp,xdrproc_t xdrres,caddr_t resp,struct timeval tout,u_long * port_ptr)90f14fb602SLionel Sambuc pmap_rmtcall(struct sockaddr_in *addr, u_long prog, u_long vers, u_long proc,
91f14fb602SLionel Sambuc xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,
92f14fb602SLionel Sambuc struct timeval tout, u_long *port_ptr)
932fe8fb19SBen Gras {
942fe8fb19SBen Gras int sock = -1;
952fe8fb19SBen Gras CLIENT *client;
962fe8fb19SBen Gras struct rmtcallargs a;
972fe8fb19SBen Gras struct rmtcallres r;
982fe8fb19SBen Gras enum clnt_stat stat;
992fe8fb19SBen Gras
1002fe8fb19SBen Gras _DIAGASSERT(addr != NULL);
1012fe8fb19SBen Gras _DIAGASSERT(port_ptr != NULL);
1022fe8fb19SBen Gras
1032fe8fb19SBen Gras addr->sin_port = htons(PMAPPORT);
1042fe8fb19SBen Gras client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
1052fe8fb19SBen Gras if (client != NULL) {
1062fe8fb19SBen Gras a.prog = prog;
1072fe8fb19SBen Gras a.vers = vers;
1082fe8fb19SBen Gras a.proc = proc;
1092fe8fb19SBen Gras a.args_ptr = argsp;
1102fe8fb19SBen Gras a.xdr_args = xdrargs;
1112fe8fb19SBen Gras r.port_ptr = port_ptr;
1122fe8fb19SBen Gras r.results_ptr = resp;
1132fe8fb19SBen Gras r.xdr_results = xdrres;
1142fe8fb19SBen Gras stat = CLNT_CALL(client, (rpcproc_t)PMAPPROC_CALLIT,
1152fe8fb19SBen Gras (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
1162fe8fb19SBen Gras &r, tout);
1172fe8fb19SBen Gras CLNT_DESTROY(client);
1182fe8fb19SBen Gras } else {
1192fe8fb19SBen Gras stat = RPC_FAILED;
1202fe8fb19SBen Gras }
1212fe8fb19SBen Gras addr->sin_port = 0;
1222fe8fb19SBen Gras return (stat);
1232fe8fb19SBen Gras }
1242fe8fb19SBen Gras
1252fe8fb19SBen Gras
1262fe8fb19SBen Gras /*
1272fe8fb19SBen Gras * XDR remote call arguments
1282fe8fb19SBen Gras * written for XDR_ENCODE direction only
1292fe8fb19SBen Gras */
1302fe8fb19SBen Gras bool_t
xdr_rmtcall_args(XDR * xdrs,struct rmtcallargs * cap)131f14fb602SLionel Sambuc xdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
1322fe8fb19SBen Gras {
1332fe8fb19SBen Gras u_int lenposition, argposition, position;
1342fe8fb19SBen Gras
1352fe8fb19SBen Gras _DIAGASSERT(xdrs != NULL);
1362fe8fb19SBen Gras _DIAGASSERT(cap != NULL);
1372fe8fb19SBen Gras
1382fe8fb19SBen Gras if (xdr_u_long(xdrs, &(cap->prog)) &&
1392fe8fb19SBen Gras xdr_u_long(xdrs, &(cap->vers)) &&
1402fe8fb19SBen Gras xdr_u_long(xdrs, &(cap->proc))) {
1412fe8fb19SBen Gras lenposition = XDR_GETPOS(xdrs);
1422fe8fb19SBen Gras if (! xdr_u_long(xdrs, &(cap->arglen)))
1432fe8fb19SBen Gras return (FALSE);
1442fe8fb19SBen Gras argposition = XDR_GETPOS(xdrs);
1452fe8fb19SBen Gras if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
1462fe8fb19SBen Gras return (FALSE);
1472fe8fb19SBen Gras position = XDR_GETPOS(xdrs);
1482fe8fb19SBen Gras cap->arglen = (u_long)position - (u_long)argposition;
1492fe8fb19SBen Gras XDR_SETPOS(xdrs, lenposition);
1502fe8fb19SBen Gras if (! xdr_u_long(xdrs, &(cap->arglen)))
1512fe8fb19SBen Gras return (FALSE);
1522fe8fb19SBen Gras XDR_SETPOS(xdrs, position);
1532fe8fb19SBen Gras return (TRUE);
1542fe8fb19SBen Gras }
1552fe8fb19SBen Gras return (FALSE);
1562fe8fb19SBen Gras }
1572fe8fb19SBen Gras
1582fe8fb19SBen Gras /*
1592fe8fb19SBen Gras * XDR remote call results
1602fe8fb19SBen Gras * written for XDR_DECODE direction only
1612fe8fb19SBen Gras */
1622fe8fb19SBen Gras bool_t
xdr_rmtcallres(XDR * xdrs,struct rmtcallres * crp)163f14fb602SLionel Sambuc xdr_rmtcallres(XDR *xdrs, struct rmtcallres *crp)
1642fe8fb19SBen Gras {
1652fe8fb19SBen Gras caddr_t port_ptr;
1662fe8fb19SBen Gras
1672fe8fb19SBen Gras _DIAGASSERT(xdrs != NULL);
1682fe8fb19SBen Gras _DIAGASSERT(crp != NULL);
1692fe8fb19SBen Gras
1702fe8fb19SBen Gras port_ptr = (caddr_t)(void *)crp->port_ptr;
171f14fb602SLionel Sambuc if (xdr_reference(xdrs, &port_ptr, (u_int)sizeof(u_long),
1722fe8fb19SBen Gras (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
1732fe8fb19SBen Gras crp->port_ptr = (u_long *)(void *)port_ptr;
1742fe8fb19SBen Gras return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
1752fe8fb19SBen Gras }
1762fe8fb19SBen Gras return (FALSE);
1772fe8fb19SBen Gras }
178