xref: /netbsd-src/lib/libc/rpc/pmap_rmt.c (revision a536ee5124e62c9a0051a252f7833dc8f50f44c9)
1 /*	$NetBSD: pmap_rmt.c,v 1.33 2012/06/25 22:32:45 abs Exp $	*/
2 
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char *sccsid = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)pmap_rmt.c	2.2 88/08/01 4.0 RPCSRC";
37 #else
38 __RCSID("$NetBSD: pmap_rmt.c,v 1.33 2012/06/25 22:32:45 abs Exp $");
39 #endif
40 #endif
41 
42 /*
43  * pmap_rmt.c
44  * Client interface to pmap rpc service.
45  * remote call and broadcast service
46  *
47  * Copyright (C) 1984, Sun Microsystems, Inc.
48  */
49 
50 #include "namespace.h"
51 
52 #include <sys/types.h>
53 #include <sys/ioctl.h>
54 #include <sys/poll.h>
55 #include <sys/socket.h>
56 
57 #include <net/if.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
60 
61 #include <assert.h>
62 #include <err.h>
63 #include <errno.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <unistd.h>
67 
68 #include <rpc/rpc.h>
69 #include <rpc/pmap_prot.h>
70 #include <rpc/pmap_clnt.h>
71 #include <rpc/pmap_rmt.h>
72 
73 #ifdef __weak_alias
74 __weak_alias(xdr_rmtcall_args,_xdr_rmtcall_args)
75 __weak_alias(xdr_rmtcallres,_xdr_rmtcallres)
76 #endif
77 
78 static const struct timeval timeout = { 3, 0 };
79 
80 /*
81  * pmapper remote-call-service interface.
82  * This routine is used to call the pmapper remote call service
83  * which will look up a service program in the port maps, and then
84  * remotely call that routine with the given parameters.  This allows
85  * programs to do a lookup and call in one step.
86 */
87 enum clnt_stat
88 pmap_rmtcall(struct sockaddr_in *addr, u_long prog, u_long vers, u_long proc,
89     xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,
90     struct timeval tout, u_long *port_ptr)
91 {
92 	int sock = -1;
93 	CLIENT *client;
94 	struct rmtcallargs a;
95 	struct rmtcallres r;
96 	enum clnt_stat stat;
97 
98 	_DIAGASSERT(addr != NULL);
99 	_DIAGASSERT(port_ptr != NULL);
100 
101 	addr->sin_port = htons(PMAPPORT);
102 	client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
103 	if (client != NULL) {
104 		a.prog = prog;
105 		a.vers = vers;
106 		a.proc = proc;
107 		a.args_ptr = argsp;
108 		a.xdr_args = xdrargs;
109 		r.port_ptr = port_ptr;
110 		r.results_ptr = resp;
111 		r.xdr_results = xdrres;
112 		stat = CLNT_CALL(client, (rpcproc_t)PMAPPROC_CALLIT,
113 		    (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
114 		    &r, tout);
115 		CLNT_DESTROY(client);
116 	} else {
117 		stat = RPC_FAILED;
118 	}
119 	addr->sin_port = 0;
120 	return (stat);
121 }
122 
123 
124 /*
125  * XDR remote call arguments
126  * written for XDR_ENCODE direction only
127  */
128 bool_t
129 xdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
130 {
131 	u_int lenposition, argposition, position;
132 
133 	_DIAGASSERT(xdrs != NULL);
134 	_DIAGASSERT(cap != NULL);
135 
136 	if (xdr_u_long(xdrs, &(cap->prog)) &&
137 	    xdr_u_long(xdrs, &(cap->vers)) &&
138 	    xdr_u_long(xdrs, &(cap->proc))) {
139 		lenposition = XDR_GETPOS(xdrs);
140 		if (! xdr_u_long(xdrs, &(cap->arglen)))
141 		    return (FALSE);
142 		argposition = XDR_GETPOS(xdrs);
143 		if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
144 		    return (FALSE);
145 		position = XDR_GETPOS(xdrs);
146 		cap->arglen = (u_long)position - (u_long)argposition;
147 		XDR_SETPOS(xdrs, lenposition);
148 		if (! xdr_u_long(xdrs, &(cap->arglen)))
149 		    return (FALSE);
150 		XDR_SETPOS(xdrs, position);
151 		return (TRUE);
152 	}
153 	return (FALSE);
154 }
155 
156 /*
157  * XDR remote call results
158  * written for XDR_DECODE direction only
159  */
160 bool_t
161 xdr_rmtcallres(XDR *xdrs, struct rmtcallres *crp)
162 {
163 	caddr_t port_ptr;
164 
165 	_DIAGASSERT(xdrs != NULL);
166 	_DIAGASSERT(crp != NULL);
167 
168 	port_ptr = (caddr_t)(void *)crp->port_ptr;
169 	if (xdr_reference(xdrs, &port_ptr, (u_int)sizeof(u_long),
170 	    (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
171 		crp->port_ptr = (u_long *)(void *)port_ptr;
172 		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
173 	}
174 	return (FALSE);
175 }
176