pmap_rmt.c revision 258578
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: head/lib/libc/rpc/pmap_rmt.c 258578 2013-11-25 19:04:36Z hrs $");
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
7974462Salfredpmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout,
8074462Salfred    port_ptr)
811901Swollman	struct sockaddr_in *addr;
821901Swollman	u_long prog, vers, proc;
831901Swollman	xdrproc_t xdrargs, xdrres;
841901Swollman	caddr_t argsp, resp;
851901Swollman	struct timeval tout;
861901Swollman	u_long *port_ptr;
871901Swollman{
8874462Salfred	int sock = -1;
8974462Salfred	CLIENT *client;
901901Swollman	struct rmtcallargs a;
911901Swollman	struct rmtcallres r;
921901Swollman	enum clnt_stat stat;
931901Swollman
9474462Salfred	assert(addr != NULL);
9574462Salfred	assert(port_ptr != NULL);
9674462Salfred
971901Swollman	addr->sin_port = htons(PMAPPORT);
9874462Salfred	client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
9974462Salfred	if (client != NULL) {
1001901Swollman		a.prog = prog;
1011901Swollman		a.vers = vers;
1021901Swollman		a.proc = proc;
1031901Swollman		a.args_ptr = argsp;
1041901Swollman		a.xdr_args = xdrargs;
1051901Swollman		r.port_ptr = port_ptr;
1061901Swollman		r.results_ptr = resp;
1071901Swollman		r.xdr_results = xdrres;
10874462Salfred		stat = CLNT_CALL(client, (rpcproc_t)PMAPPROC_CALLIT,
10974462Salfred		    (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
11074462Salfred		    &r, tout);
1111901Swollman		CLNT_DESTROY(client);
1121901Swollman	} else {
1131901Swollman		stat = RPC_FAILED;
1141901Swollman	}
1151901Swollman	addr->sin_port = 0;
1161901Swollman	return (stat);
1171901Swollman}
1181901Swollman
1191901Swollman
1201901Swollman/*
1211901Swollman * XDR remote call arguments
1221901Swollman * written for XDR_ENCODE direction only
1231901Swollman */
1241901Swollmanbool_t
1251901Swollmanxdr_rmtcall_args(xdrs, cap)
12674462Salfred	XDR *xdrs;
12774462Salfred	struct rmtcallargs *cap;
1281901Swollman{
1291901Swollman	u_int lenposition, argposition, position;
1301901Swollman
13174462Salfred	assert(xdrs != NULL);
13274462Salfred	assert(cap != NULL);
13374462Salfred
1341901Swollman	if (xdr_u_long(xdrs, &(cap->prog)) &&
1351901Swollman	    xdr_u_long(xdrs, &(cap->vers)) &&
1361901Swollman	    xdr_u_long(xdrs, &(cap->proc))) {
1371901Swollman		lenposition = XDR_GETPOS(xdrs);
1381901Swollman		if (! xdr_u_long(xdrs, &(cap->arglen)))
1391901Swollman		    return (FALSE);
1401901Swollman		argposition = XDR_GETPOS(xdrs);
1411901Swollman		if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
1421901Swollman		    return (FALSE);
1431901Swollman		position = XDR_GETPOS(xdrs);
1441901Swollman		cap->arglen = (u_long)position - (u_long)argposition;
1451901Swollman		XDR_SETPOS(xdrs, lenposition);
1461901Swollman		if (! xdr_u_long(xdrs, &(cap->arglen)))
1471901Swollman		    return (FALSE);
1481901Swollman		XDR_SETPOS(xdrs, position);
1491901Swollman		return (TRUE);
1501901Swollman	}
1511901Swollman	return (FALSE);
1521901Swollman}
1531901Swollman
1541901Swollman/*
1551901Swollman * XDR remote call results
1561901Swollman * written for XDR_DECODE direction only
1571901Swollman */
1581901Swollmanbool_t
1591901Swollmanxdr_rmtcallres(xdrs, crp)
16074462Salfred	XDR *xdrs;
16174462Salfred	struct rmtcallres *crp;
1621901Swollman{
1631901Swollman	caddr_t port_ptr;
1641901Swollman
16574462Salfred	assert(xdrs != NULL);
16674462Salfred	assert(crp != NULL);
16774462Salfred
16874462Salfred	port_ptr = (caddr_t)(void *)crp->port_ptr;
1691901Swollman	if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
17074462Salfred	    (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
17174462Salfred		crp->port_ptr = (u_long *)(void *)port_ptr;
1721901Swollman		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
1731901Swollman	}
1741901Swollman	return (FALSE);
1751901Swollman}
176