174462Salfred/*	$NetBSD: pmap_rmt.c,v 1.29 2000/07/06 03:10:34 christos Exp $	*/
274462Salfred
3258578Shrs/*-
4258578Shrs * Copyright (c) 2009, Sun Microsystems, Inc.
5258578Shrs * All rights reserved.
68870Srgrimes *
7258578Shrs * Redistribution and use in source and binary forms, with or without
8258578Shrs * modification, are permitted provided that the following conditions are met:
9258578Shrs * - Redistributions of source code must retain the above copyright notice,
10258578Shrs *   this list of conditions and the following disclaimer.
11258578Shrs * - Redistributions in binary form must reproduce the above copyright notice,
12258578Shrs *   this list of conditions and the following disclaimer in the documentation
13258578Shrs *   and/or other materials provided with the distribution.
14258578Shrs * - Neither the name of Sun Microsystems, Inc. nor the names of its
15258578Shrs *   contributors may be used to endorse or promote products derived
16258578Shrs *   from this software without specific prior written permission.
17258578Shrs *
18258578Shrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19258578Shrs * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20258578Shrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21258578Shrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22258578Shrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23258578Shrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24258578Shrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25258578Shrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26258578Shrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27258578Shrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28258578Shrs * POSSIBILITY OF SUCH DAMAGE.
291901Swollman */
301901Swollman
311901Swollman#if defined(LIBC_SCCS) && !defined(lint)
32136581Sobrienstatic char *sccsid2 = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
3392990Sobrienstatic char *sccsid = "@(#)pmap_rmt.c	2.2 88/08/01 4.0 RPCSRC";
341901Swollman#endif
3592990Sobrien#include <sys/cdefs.h>
3692990Sobrien__FBSDID("$FreeBSD$");
371901Swollman
381901Swollman/*
391901Swollman * pmap_rmt.c
401901Swollman * Client interface to pmap rpc service.
411901Swollman * remote call and broadcast service
421901Swollman *
431901Swollman * Copyright (C) 1984, Sun Microsystems, Inc.
441901Swollman */
451901Swollman
4671579Sdeischen#include "namespace.h"
4774462Salfred#include <sys/types.h>
4874462Salfred#include <sys/ioctl.h>
4974462Salfred#include <sys/poll.h>
5074462Salfred#include <sys/socket.h>
5174462Salfred
5274462Salfred#include <net/if.h>
5374462Salfred#include <netinet/in.h>
5474462Salfred#include <arpa/inet.h>
5574462Salfred
5674462Salfred#include <assert.h>
5774462Salfred#include <err.h>
5874462Salfred#include <errno.h>
5974462Salfred#include <stdio.h>
6074462Salfred#include <string.h>
6174462Salfred#include <unistd.h>
6274462Salfred
631901Swollman#include <rpc/rpc.h>
641901Swollman#include <rpc/pmap_prot.h>
651901Swollman#include <rpc/pmap_clnt.h>
661901Swollman#include <rpc/pmap_rmt.h>
6771579Sdeischen#include "un-namespace.h"
6871579Sdeischen
6974462Salfredstatic const struct timeval timeout = { 3, 0 };
701901Swollman
711901Swollman/*
721901Swollman * pmapper remote-call-service interface.
731901Swollman * This routine is used to call the pmapper remote call service
741901Swollman * which will look up a service program in the port maps, and then
751901Swollman * remotely call that routine with the given parameters.  This allows
761901Swollman * programs to do a lookup and call in one step.
771901Swollman*/
781901Swollmanenum clnt_stat
79288113Srodrigcpmap_rmtcall(struct sockaddr_in *addr, u_long prog, u_long vers, u_long proc,
80288113Srodrigc    xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,
81288113Srodrigc    struct timeval tout, u_long *port_ptr)
821901Swollman{
8374462Salfred	int sock = -1;
8474462Salfred	CLIENT *client;
851901Swollman	struct rmtcallargs a;
861901Swollman	struct rmtcallres r;
871901Swollman	enum clnt_stat stat;
881901Swollman
8974462Salfred	assert(addr != NULL);
9074462Salfred	assert(port_ptr != NULL);
9174462Salfred
921901Swollman	addr->sin_port = htons(PMAPPORT);
9374462Salfred	client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
9474462Salfred	if (client != NULL) {
951901Swollman		a.prog = prog;
961901Swollman		a.vers = vers;
971901Swollman		a.proc = proc;
981901Swollman		a.args_ptr = argsp;
991901Swollman		a.xdr_args = xdrargs;
1001901Swollman		r.port_ptr = port_ptr;
1011901Swollman		r.results_ptr = resp;
1021901Swollman		r.xdr_results = xdrres;
10374462Salfred		stat = CLNT_CALL(client, (rpcproc_t)PMAPPROC_CALLIT,
10474462Salfred		    (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
10574462Salfred		    &r, tout);
1061901Swollman		CLNT_DESTROY(client);
1071901Swollman	} else {
1081901Swollman		stat = RPC_FAILED;
1091901Swollman	}
1101901Swollman	addr->sin_port = 0;
1111901Swollman	return (stat);
1121901Swollman}
1131901Swollman
1141901Swollman
1151901Swollman/*
1161901Swollman * XDR remote call arguments
1171901Swollman * written for XDR_ENCODE direction only
1181901Swollman */
1191901Swollmanbool_t
120288113Srodrigcxdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
1211901Swollman{
1221901Swollman	u_int lenposition, argposition, position;
1231901Swollman
12474462Salfred	assert(xdrs != NULL);
12574462Salfred	assert(cap != NULL);
12674462Salfred
1271901Swollman	if (xdr_u_long(xdrs, &(cap->prog)) &&
1281901Swollman	    xdr_u_long(xdrs, &(cap->vers)) &&
1291901Swollman	    xdr_u_long(xdrs, &(cap->proc))) {
1301901Swollman		lenposition = XDR_GETPOS(xdrs);
1311901Swollman		if (! xdr_u_long(xdrs, &(cap->arglen)))
1321901Swollman		    return (FALSE);
1331901Swollman		argposition = XDR_GETPOS(xdrs);
1341901Swollman		if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
1351901Swollman		    return (FALSE);
1361901Swollman		position = XDR_GETPOS(xdrs);
1371901Swollman		cap->arglen = (u_long)position - (u_long)argposition;
1381901Swollman		XDR_SETPOS(xdrs, lenposition);
1391901Swollman		if (! xdr_u_long(xdrs, &(cap->arglen)))
1401901Swollman		    return (FALSE);
1411901Swollman		XDR_SETPOS(xdrs, position);
1421901Swollman		return (TRUE);
1431901Swollman	}
1441901Swollman	return (FALSE);
1451901Swollman}
1461901Swollman
1471901Swollman/*
1481901Swollman * XDR remote call results
1491901Swollman * written for XDR_DECODE direction only
1501901Swollman */
1511901Swollmanbool_t
152288113Srodrigcxdr_rmtcallres(XDR *xdrs, struct rmtcallres *crp)
1531901Swollman{
1541901Swollman	caddr_t port_ptr;
1551901Swollman
15674462Salfred	assert(xdrs != NULL);
15774462Salfred	assert(crp != NULL);
15874462Salfred
15974462Salfred	port_ptr = (caddr_t)(void *)crp->port_ptr;
1601901Swollman	if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
16174462Salfred	    (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
16274462Salfred		crp->port_ptr = (u_long *)(void *)port_ptr;
1631901Swollman		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
1641901Swollman	}
1651901Swollman	return (FALSE);
1661901Swollman}
167