xref: /netbsd-src/lib/libc/rpc/pmap_rmt.c (revision 47c0e0c312cd964546204bd0febab9a56fa11df9)
1*47c0e0c3Stron /*	$NetBSD: pmap_rmt.c,v 1.34 2013/03/11 20:19:29 tron Exp $	*/
29e15c989Scgd 
363d7b677Scgd /*
4*47c0e0c3Stron  * Copyright (c) 2010, Oracle America, Inc.
563d7b677Scgd  *
6*47c0e0c3Stron  * Redistribution and use in source and binary forms, with or without
7*47c0e0c3Stron  * modification, are permitted provided that the following conditions are
8*47c0e0c3Stron  * met:
963d7b677Scgd  *
10*47c0e0c3Stron  *     * Redistributions of source code must retain the above copyright
11*47c0e0c3Stron  *       notice, this list of conditions and the following disclaimer.
12*47c0e0c3Stron  *     * Redistributions in binary form must reproduce the above
13*47c0e0c3Stron  *       copyright notice, this list of conditions and the following
14*47c0e0c3Stron  *       disclaimer in the documentation and/or other materials
15*47c0e0c3Stron  *       provided with the distribution.
16*47c0e0c3Stron  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17*47c0e0c3Stron  *       contributors may be used to endorse or promote products derived
18*47c0e0c3Stron  *       from this software without specific prior written permission.
1963d7b677Scgd  *
20*47c0e0c3Stron  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*47c0e0c3Stron  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*47c0e0c3Stron  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*47c0e0c3Stron  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*47c0e0c3Stron  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*47c0e0c3Stron  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*47c0e0c3Stron  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*47c0e0c3Stron  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*47c0e0c3Stron  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*47c0e0c3Stron  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*47c0e0c3Stron  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*47c0e0c3Stron  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3263d7b677Scgd  */
3363d7b677Scgd 
34c63c52b2Schristos #include <sys/cdefs.h>
3563d7b677Scgd #if defined(LIBC_SCCS) && !defined(lint)
36c63c52b2Schristos #if 0
37c63c52b2Schristos static char *sccsid = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
38c63c52b2Schristos static char *sccsid = "@(#)pmap_rmt.c	2.2 88/08/01 4.0 RPCSRC";
39c63c52b2Schristos #else
40*47c0e0c3Stron __RCSID("$NetBSD: pmap_rmt.c,v 1.34 2013/03/11 20:19:29 tron Exp $");
41c63c52b2Schristos #endif
4263d7b677Scgd #endif
4363d7b677Scgd 
4463d7b677Scgd /*
4563d7b677Scgd  * pmap_rmt.c
4663d7b677Scgd  * Client interface to pmap rpc service.
4763d7b677Scgd  * remote call and broadcast service
4863d7b677Scgd  *
4963d7b677Scgd  * Copyright (C) 1984, Sun Microsystems, Inc.
5063d7b677Scgd  */
5163d7b677Scgd 
5243fa6fe3Sjtc #include "namespace.h"
5346e6c5e8Slukem 
54f6c91ed9Smrg #include <sys/types.h>
5546e6c5e8Slukem #include <sys/ioctl.h>
56f6c91ed9Smrg #include <sys/poll.h>
5746e6c5e8Slukem #include <sys/socket.h>
5846e6c5e8Slukem 
5946e6c5e8Slukem #include <net/if.h>
6046e6c5e8Slukem #include <netinet/in.h>
6146e6c5e8Slukem #include <arpa/inet.h>
6246e6c5e8Slukem 
63b48252f3Slukem #include <assert.h>
6446e6c5e8Slukem #include <err.h>
6546e6c5e8Slukem #include <errno.h>
6646e6c5e8Slukem #include <stdio.h>
6746e6c5e8Slukem #include <string.h>
6846e6c5e8Slukem #include <unistd.h>
69f6c91ed9Smrg 
7063d7b677Scgd #include <rpc/rpc.h>
7163d7b677Scgd #include <rpc/pmap_prot.h>
7263d7b677Scgd #include <rpc/pmap_clnt.h>
7363d7b677Scgd #include <rpc/pmap_rmt.h>
7443fa6fe3Sjtc 
7543fa6fe3Sjtc #ifdef __weak_alias
7660549036Smycroft __weak_alias(xdr_rmtcall_args,_xdr_rmtcall_args)
7760549036Smycroft __weak_alias(xdr_rmtcallres,_xdr_rmtcallres)
7843fa6fe3Sjtc #endif
7943fa6fe3Sjtc 
80982e9200Smycroft static const struct timeval timeout = { 3, 0 };
8163d7b677Scgd 
8263d7b677Scgd /*
8363d7b677Scgd  * pmapper remote-call-service interface.
8463d7b677Scgd  * This routine is used to call the pmapper remote call service
8563d7b677Scgd  * which will look up a service program in the port maps, and then
8663d7b677Scgd  * remotely call that routine with the given parameters.  This allows
8763d7b677Scgd  * programs to do a lookup and call in one step.
8863d7b677Scgd */
8963d7b677Scgd 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)909e66e6d7Sabs pmap_rmtcall(struct sockaddr_in *addr, u_long prog, u_long vers, u_long proc,
919e66e6d7Sabs     xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,
929e66e6d7Sabs     struct timeval tout, u_long *port_ptr)
9363d7b677Scgd {
941325a26dSchristos 	int sock = -1;
9546e6c5e8Slukem 	CLIENT *client;
9663d7b677Scgd 	struct rmtcallargs a;
9763d7b677Scgd 	struct rmtcallres r;
9863d7b677Scgd 	enum clnt_stat stat;
9963d7b677Scgd 
100b48252f3Slukem 	_DIAGASSERT(addr != NULL);
101b48252f3Slukem 	_DIAGASSERT(port_ptr != NULL);
102b48252f3Slukem 
10363d7b677Scgd 	addr->sin_port = htons(PMAPPORT);
1041325a26dSchristos 	client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
105deb154d2Schristos 	if (client != NULL) {
10663d7b677Scgd 		a.prog = prog;
10763d7b677Scgd 		a.vers = vers;
10863d7b677Scgd 		a.proc = proc;
10963d7b677Scgd 		a.args_ptr = argsp;
11063d7b677Scgd 		a.xdr_args = xdrargs;
11163d7b677Scgd 		r.port_ptr = port_ptr;
11263d7b677Scgd 		r.results_ptr = resp;
11363d7b677Scgd 		r.xdr_results = xdrres;
114deb154d2Schristos 		stat = CLNT_CALL(client, (rpcproc_t)PMAPPROC_CALLIT,
115caaf1528Schristos 		    (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
116caaf1528Schristos 		    &r, tout);
11763d7b677Scgd 		CLNT_DESTROY(client);
11863d7b677Scgd 	} else {
11963d7b677Scgd 		stat = RPC_FAILED;
12063d7b677Scgd 	}
12163d7b677Scgd 	addr->sin_port = 0;
12263d7b677Scgd 	return (stat);
12363d7b677Scgd }
12463d7b677Scgd 
12563d7b677Scgd 
12663d7b677Scgd /*
12763d7b677Scgd  * XDR remote call arguments
12863d7b677Scgd  * written for XDR_ENCODE direction only
12963d7b677Scgd  */
13063d7b677Scgd bool_t
xdr_rmtcall_args(XDR * xdrs,struct rmtcallargs * cap)131adb74221Smatt xdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
13263d7b677Scgd {
133ce147c1cSlukem 	u_int lenposition, argposition, position;
13463d7b677Scgd 
135b48252f3Slukem 	_DIAGASSERT(xdrs != NULL);
136b48252f3Slukem 	_DIAGASSERT(cap != NULL);
137b48252f3Slukem 
138ce147c1cSlukem 	if (xdr_u_long(xdrs, &(cap->prog)) &&
139ce147c1cSlukem 	    xdr_u_long(xdrs, &(cap->vers)) &&
140ce147c1cSlukem 	    xdr_u_long(xdrs, &(cap->proc))) {
14163d7b677Scgd 		lenposition = XDR_GETPOS(xdrs);
142ce147c1cSlukem 		if (! xdr_u_long(xdrs, &(cap->arglen)))
14363d7b677Scgd 		    return (FALSE);
14463d7b677Scgd 		argposition = XDR_GETPOS(xdrs);
14563d7b677Scgd 		if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
14663d7b677Scgd 		    return (FALSE);
14763d7b677Scgd 		position = XDR_GETPOS(xdrs);
148ce147c1cSlukem 		cap->arglen = (u_long)position - (u_long)argposition;
14963d7b677Scgd 		XDR_SETPOS(xdrs, lenposition);
150ce147c1cSlukem 		if (! xdr_u_long(xdrs, &(cap->arglen)))
15163d7b677Scgd 		    return (FALSE);
15263d7b677Scgd 		XDR_SETPOS(xdrs, position);
15363d7b677Scgd 		return (TRUE);
15463d7b677Scgd 	}
15563d7b677Scgd 	return (FALSE);
15663d7b677Scgd }
15763d7b677Scgd 
15863d7b677Scgd /*
15963d7b677Scgd  * XDR remote call results
16063d7b677Scgd  * written for XDR_DECODE direction only
16163d7b677Scgd  */
16263d7b677Scgd bool_t
xdr_rmtcallres(XDR * xdrs,struct rmtcallres * crp)163adb74221Smatt xdr_rmtcallres(XDR *xdrs, struct rmtcallres *crp)
16463d7b677Scgd {
16563d7b677Scgd 	caddr_t port_ptr;
16663d7b677Scgd 
167b48252f3Slukem 	_DIAGASSERT(xdrs != NULL);
168b48252f3Slukem 	_DIAGASSERT(crp != NULL);
169b48252f3Slukem 
1701325a26dSchristos 	port_ptr = (caddr_t)(void *)crp->port_ptr;
171c5e820caSchristos 	if (xdr_reference(xdrs, &port_ptr, (u_int)sizeof(u_long),
172caaf1528Schristos 	    (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
1731325a26dSchristos 		crp->port_ptr = (u_long *)(void *)port_ptr;
17463d7b677Scgd 		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
17563d7b677Scgd 	}
17663d7b677Scgd 	return (FALSE);
17763d7b677Scgd }
178