xref: /netbsd-src/lib/libc/rpc/pmap_rmt.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: pmap_rmt.c,v 1.29 2000/07/06 03:10:34 christos 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.29 2000/07/06 03:10:34 christos 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(pmap_rmtcall,_pmap_rmtcall)
75 __weak_alias(xdr_rmtcall_args,_xdr_rmtcall_args)
76 __weak_alias(xdr_rmtcallres,_xdr_rmtcallres)
77 #endif
78 
79 static const struct timeval timeout = { 3, 0 };
80 
81 /*
82  * pmapper remote-call-service interface.
83  * This routine is used to call the pmapper remote call service
84  * which will look up a service program in the port maps, and then
85  * remotely call that routine with the given parameters.  This allows
86  * programs to do a lookup and call in one step.
87 */
88 enum clnt_stat
89 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout,
90     port_ptr)
91 	struct sockaddr_in *addr;
92 	u_long prog, vers, proc;
93 	xdrproc_t xdrargs, xdrres;
94 	caddr_t argsp, resp;
95 	struct timeval tout;
96 	u_long *port_ptr;
97 {
98 	int sock = -1;
99 	CLIENT *client;
100 	struct rmtcallargs a;
101 	struct rmtcallres r;
102 	enum clnt_stat stat;
103 
104 	_DIAGASSERT(addr != NULL);
105 	_DIAGASSERT(port_ptr != NULL);
106 
107 	addr->sin_port = htons(PMAPPORT);
108 	client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
109 	if (client != NULL) {
110 		a.prog = prog;
111 		a.vers = vers;
112 		a.proc = proc;
113 		a.args_ptr = argsp;
114 		a.xdr_args = xdrargs;
115 		r.port_ptr = port_ptr;
116 		r.results_ptr = resp;
117 		r.xdr_results = xdrres;
118 		stat = CLNT_CALL(client, (rpcproc_t)PMAPPROC_CALLIT,
119 		    (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
120 		    &r, tout);
121 		CLNT_DESTROY(client);
122 	} else {
123 		stat = RPC_FAILED;
124 	}
125 	addr->sin_port = 0;
126 	return (stat);
127 }
128 
129 
130 /*
131  * XDR remote call arguments
132  * written for XDR_ENCODE direction only
133  */
134 bool_t
135 xdr_rmtcall_args(xdrs, cap)
136 	XDR *xdrs;
137 	struct rmtcallargs *cap;
138 {
139 	u_int lenposition, argposition, position;
140 
141 	_DIAGASSERT(xdrs != NULL);
142 	_DIAGASSERT(cap != NULL);
143 
144 	if (xdr_u_long(xdrs, &(cap->prog)) &&
145 	    xdr_u_long(xdrs, &(cap->vers)) &&
146 	    xdr_u_long(xdrs, &(cap->proc))) {
147 		lenposition = XDR_GETPOS(xdrs);
148 		if (! xdr_u_long(xdrs, &(cap->arglen)))
149 		    return (FALSE);
150 		argposition = XDR_GETPOS(xdrs);
151 		if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
152 		    return (FALSE);
153 		position = XDR_GETPOS(xdrs);
154 		cap->arglen = (u_long)position - (u_long)argposition;
155 		XDR_SETPOS(xdrs, lenposition);
156 		if (! xdr_u_long(xdrs, &(cap->arglen)))
157 		    return (FALSE);
158 		XDR_SETPOS(xdrs, position);
159 		return (TRUE);
160 	}
161 	return (FALSE);
162 }
163 
164 /*
165  * XDR remote call results
166  * written for XDR_DECODE direction only
167  */
168 bool_t
169 xdr_rmtcallres(xdrs, crp)
170 	XDR *xdrs;
171 	struct rmtcallres *crp;
172 {
173 	caddr_t port_ptr;
174 
175 	_DIAGASSERT(xdrs != NULL);
176 	_DIAGASSERT(crp != NULL);
177 
178 	port_ptr = (caddr_t)(void *)crp->port_ptr;
179 	if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
180 	    (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
181 		crp->port_ptr = (u_long *)(void *)port_ptr;
182 		return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
183 	}
184 	return (FALSE);
185 }
186