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