pmap_rmt.c revision 74462
174462Salfred/*	$NetBSD: pmap_rmt.c,v 1.29 2000/07/06 03:10:34 christos Exp $	*/
274462Salfred
31901Swollman/*
41901Swollman * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
51901Swollman * unrestricted use provided that this legend is included on all tape
61901Swollman * media and as a part of the software program in whole or part.  Users
71901Swollman * may copy or modify Sun RPC without charge, but are not authorized
81901Swollman * to license or distribute it to anyone else except as part of a product or
91901Swollman * program developed by the user.
108870Srgrimes *
111901Swollman * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
121901Swollman * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
131901Swollman * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
148870Srgrimes *
151901Swollman * Sun RPC is provided with no support and without any obligation on the
161901Swollman * part of Sun Microsystems, Inc. to assist in its use, correction,
171901Swollman * modification or enhancement.
188870Srgrimes *
191901Swollman * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
201901Swollman * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
211901Swollman * OR ANY PART THEREOF.
228870Srgrimes *
231901Swollman * In no event will Sun Microsystems, Inc. be liable for any lost revenue
241901Swollman * or profits or other special, indirect and consequential damages, even if
251901Swollman * Sun has been advised of the possibility of such damages.
268870Srgrimes *
271901Swollman * Sun Microsystems, Inc.
281901Swollman * 2550 Garcia Avenue
291901Swollman * Mountain View, California  94043
301901Swollman */
311901Swollman
3274462Salfred#include <sys/cdefs.h>
331901Swollman#if defined(LIBC_SCCS) && !defined(lint)
341901Swollman/*static char *sccsid = "from: @(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";*/
351901Swollman/*static char *sccsid = "from: @(#)pmap_rmt.c	2.2 88/08/01 4.0 RPCSRC";*/
3650476Speterstatic char *rcsid = "$FreeBSD: head/lib/libc/rpc/pmap_rmt.c 74462 2001-03-19 12:50:13Z alfred $";
371901Swollman#endif
381901Swollman
391901Swollman/*
401901Swollman * pmap_rmt.c
411901Swollman * Client interface to pmap rpc service.
421901Swollman * remote call and broadcast service
431901Swollman *
441901Swollman * Copyright (C) 1984, Sun Microsystems, Inc.
451901Swollman */
461901Swollman
4771579Sdeischen#include "namespace.h"
4874462Salfred#include <sys/types.h>
4974462Salfred#include <sys/ioctl.h>
5074462Salfred#include <sys/poll.h>
5174462Salfred#include <sys/socket.h>
5274462Salfred
5374462Salfred#include <net/if.h>
5474462Salfred#include <netinet/in.h>
5574462Salfred#include <arpa/inet.h>
5674462Salfred
5774462Salfred#include <assert.h>
5874462Salfred#include <err.h>
5974462Salfred#include <errno.h>
6074462Salfred#include <stdio.h>
6174462Salfred#include <string.h>
6274462Salfred#include <unistd.h>
6374462Salfred
641901Swollman#include <rpc/rpc.h>
651901Swollman#include <rpc/pmap_prot.h>
661901Swollman#include <rpc/pmap_clnt.h>
671901Swollman#include <rpc/pmap_rmt.h>
6871579Sdeischen#include "un-namespace.h"
6971579Sdeischen
7074462Salfredstatic const struct timeval timeout = { 3, 0 };
711901Swollman
721901Swollman/*
731901Swollman * pmapper remote-call-service interface.
741901Swollman * This routine is used to call the pmapper remote call service
751901Swollman * which will look up a service program in the port maps, and then
761901Swollman * remotely call that routine with the given parameters.  This allows
771901Swollman * programs to do a lookup and call in one step.
781901Swollman*/
791901Swollmanenum clnt_stat
8074462Salfredpmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout,
8174462Salfred    port_ptr)
821901Swollman	struct sockaddr_in *addr;
831901Swollman	u_long prog, vers, proc;
841901Swollman	xdrproc_t xdrargs, xdrres;
851901Swollman	caddr_t argsp, resp;
861901Swollman	struct timeval tout;
871901Swollman	u_long *port_ptr;
881901Swollman{
8974462Salfred	int sock = -1;
9074462Salfred	CLIENT *client;
911901Swollman	struct rmtcallargs a;
921901Swollman	struct rmtcallres r;
931901Swollman	enum clnt_stat stat;
941901Swollman
9574462Salfred	assert(addr != NULL);
9674462Salfred	assert(port_ptr != NULL);
9774462Salfred
981901Swollman	addr->sin_port = htons(PMAPPORT);
9974462Salfred	client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
10074462Salfred	if (client != NULL) {
1011901Swollman		a.prog = prog;
1021901Swollman		a.vers = vers;
1031901Swollman		a.proc = proc;
1041901Swollman		a.args_ptr = argsp;
1051901Swollman		a.xdr_args = xdrargs;
1061901Swollman		r.port_ptr = port_ptr;
1071901Swollman		r.results_ptr = resp;
1081901Swollman		r.xdr_results = xdrres;
10974462Salfred		stat = CLNT_CALL(client, (rpcproc_t)PMAPPROC_CALLIT,
11074462Salfred		    (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
11174462Salfred		    &r, tout);
1121901Swollman		CLNT_DESTROY(client);
1131901Swollman	} else {
1141901Swollman		stat = RPC_FAILED;
1151901Swollman	}
1161901Swollman	addr->sin_port = 0;
1171901Swollman	return (stat);
1181901Swollman}
1191901Swollman
1201901Swollman
1211901Swollman/*
1221901Swollman * XDR remote call arguments
1231901Swollman * written for XDR_ENCODE direction only
1241901Swollman */
1251901Swollmanbool_t
1261901Swollmanxdr_rmtcall_args(xdrs, cap)
12774462Salfred	XDR *xdrs;
12874462Salfred	struct rmtcallargs *cap;
1291901Swollman{
1301901Swollman	u_int lenposition, argposition, position;
1311901Swollman
13274462Salfred	assert(xdrs != NULL);
13374462Salfred	assert(cap != NULL);
13474462Salfred
1351901Swollman	if (xdr_u_long(xdrs, &(cap->prog)) &&
1361901Swollman	    xdr_u_long(xdrs, &(cap->vers)) &&
1371901Swollman	    xdr_u_long(xdrs, &(cap->proc))) {
1381901Swollman		lenposition = XDR_GETPOS(xdrs);
1391901Swollman		if (! xdr_u_long(xdrs, &(cap->arglen)))
1401901Swollman		    return (FALSE);
1411901Swollman		argposition = XDR_GETPOS(xdrs);
1421901Swollman		if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
1431901Swollman		    return (FALSE);
1441901Swollman		position = XDR_GETPOS(xdrs);
1451901Swollman		cap->arglen = (u_long)position - (u_long)argposition;
1461901Swollman		XDR_SETPOS(xdrs, lenposition);
1471901Swollman		if (! xdr_u_long(xdrs, &(cap->arglen)))
1481901Swollman		    return (FALSE);
1491901Swollman		XDR_SETPOS(xdrs, position);
1501901Swollman		return (TRUE);
1511901Swollman	}
1521901Swollman	return (FALSE);
1531901Swollman}
1541901Swollman
1551901Swollman/*
1561901Swollman * XDR remote call results
1571901Swollman * written for XDR_DECODE direction only
1581901Swollman */
1591901Swollmanbool_t
1601901Swollmanxdr_rmtcallres(xdrs, crp)
16174462Salfred	XDR *xdrs;
16274462Salfred	struct rmtcallres *crp;
1631901Swollman{
1641901Swollman	caddr_t port_ptr;
1651901Swollman
16674462Salfred	assert(xdrs != NULL);
16774462Salfred	assert(crp != NULL);
16874462Salfred
16974462Salfred	port_ptr = (caddr_t)(void *)crp->port_ptr;
1701901Swollman	if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
17174462Salfred	    (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
17274462Salfred		crp->port_ptr = (u_long *)(void *)port_ptr;
1731901Swollman		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
1741901Swollman	}
1751901Swollman	return (FALSE);
1761901Swollman}
177