1/*	$NetBSD: pmap_rmt.c,v 1.29 2000/07/06 03:10:34 christos Exp $	*/
2
3/*-
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 *   this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 *   this list of conditions and the following disclaimer in the documentation
15 *   and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 *   contributors may be used to endorse or promote products derived
18 *   from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * pmap_rmt.c
35 * Client interface to pmap rpc service.
36 * remote call and broadcast service
37 *
38 * Copyright (C) 1984, Sun Microsystems, Inc.
39 */
40
41#include "namespace.h"
42#include <sys/types.h>
43#include <sys/ioctl.h>
44#include <sys/poll.h>
45#include <sys/socket.h>
46
47#include <net/if.h>
48#include <netinet/in.h>
49#include <arpa/inet.h>
50
51#include <assert.h>
52#include <err.h>
53#include <errno.h>
54#include <stdio.h>
55#include <string.h>
56#include <unistd.h>
57
58#include <rpc/rpc.h>
59#include <rpc/pmap_prot.h>
60#include <rpc/pmap_clnt.h>
61#include <rpc/pmap_rmt.h>
62#include "un-namespace.h"
63
64static const struct timeval timeout = { 3, 0 };
65
66/*
67 * pmapper remote-call-service interface.
68 * This routine is used to call the pmapper remote call service
69 * which will look up a service program in the port maps, and then
70 * remotely call that routine with the given parameters.  This allows
71 * programs to do a lookup and call in one step.
72*/
73enum clnt_stat
74pmap_rmtcall(struct sockaddr_in *addr, u_long prog, u_long vers, u_long proc,
75    xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,
76    struct timeval tout, u_long *port_ptr)
77{
78	int sock = -1;
79	CLIENT *client;
80	struct rmtcallargs a;
81	struct rmtcallres r;
82	enum clnt_stat stat;
83
84	assert(addr != NULL);
85	assert(port_ptr != NULL);
86
87	addr->sin_port = htons(PMAPPORT);
88	client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
89	if (client != NULL) {
90		a.prog = prog;
91		a.vers = vers;
92		a.proc = proc;
93		a.args_ptr = argsp;
94		a.xdr_args = xdrargs;
95		r.port_ptr = port_ptr;
96		r.results_ptr = resp;
97		r.xdr_results = xdrres;
98		stat = CLNT_CALL(client, (rpcproc_t)PMAPPROC_CALLIT,
99		    (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
100		    &r, tout);
101		CLNT_DESTROY(client);
102	} else {
103		stat = RPC_FAILED;
104	}
105	addr->sin_port = 0;
106	return (stat);
107}
108
109
110/*
111 * XDR remote call arguments
112 * written for XDR_ENCODE direction only
113 */
114bool_t
115xdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
116{
117	u_int lenposition, argposition, position;
118
119	assert(xdrs != NULL);
120	assert(cap != NULL);
121
122	if (xdr_u_long(xdrs, &(cap->prog)) &&
123	    xdr_u_long(xdrs, &(cap->vers)) &&
124	    xdr_u_long(xdrs, &(cap->proc))) {
125		lenposition = XDR_GETPOS(xdrs);
126		if (! xdr_u_long(xdrs, &(cap->arglen)))
127		    return (FALSE);
128		argposition = XDR_GETPOS(xdrs);
129		if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
130		    return (FALSE);
131		position = XDR_GETPOS(xdrs);
132		cap->arglen = (u_long)position - (u_long)argposition;
133		XDR_SETPOS(xdrs, lenposition);
134		if (! xdr_u_long(xdrs, &(cap->arglen)))
135		    return (FALSE);
136		XDR_SETPOS(xdrs, position);
137		return (TRUE);
138	}
139	return (FALSE);
140}
141
142/*
143 * XDR remote call results
144 * written for XDR_DECODE direction only
145 */
146bool_t
147xdr_rmtcallres(XDR *xdrs, struct rmtcallres *crp)
148{
149	caddr_t port_ptr;
150
151	assert(xdrs != NULL);
152	assert(crp != NULL);
153
154	port_ptr = (caddr_t)(void *)crp->port_ptr;
155	if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
156	    (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
157		crp->port_ptr = (u_long *)(void *)port_ptr;
158		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
159	}
160	return (FALSE);
161}
162