xref: /netbsd-src/lib/libc/rpc/clnt_raw.c (revision e4d7c2e329d54c97e0c0bd3016bbe74f550c3d5e)
1 /*	$NetBSD: clnt_raw.c,v 1.16 2000/01/22 22:19:17 mycroft 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 = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)clnt_raw.c	2.2 88/08/01 4.0 RPCSRC";
37 #else
38 __RCSID("$NetBSD: clnt_raw.c,v 1.16 2000/01/22 22:19:17 mycroft Exp $");
39 #endif
40 #endif
41 
42 /*
43  * clnt_raw.c
44  *
45  * Copyright (C) 1984, Sun Microsystems, Inc.
46  *
47  * Memory based rpc for simple testing and timing.
48  * Interface to create an rpc client and server in the same process.
49  * This lets us similate rpc and get round trip overhead, without
50  * any interference from the kernal.
51  */
52 
53 #include "namespace.h"
54 
55 #include <assert.h>
56 #include <err.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 
60 #include <rpc/rpc.h>
61 
62 #ifdef __weak_alias
63 __weak_alias(clntraw_create,_clntraw_create)
64 #endif
65 
66 #define MCALL_MSG_SIZE 24
67 
68 /*
69  * This is the "network" we will be moving stuff over.
70  */
71 static struct clntraw_private {
72 	CLIENT	client_object;
73 	XDR	xdr_stream;
74 	char	_raw_buf[UDPMSGSIZE];
75 	union {
76 	    struct rpc_msg	mashl_rpcmsg;
77 	    char 		mashl_callmsg[MCALL_MSG_SIZE];
78 	} u;
79 	u_int	mcnt;
80 } *clntraw_private;
81 
82 
83 
84 static enum clnt_stat clntraw_call __P((CLIENT *, u_long, xdrproc_t,
85     caddr_t, xdrproc_t, caddr_t, struct timeval));
86 static void clntraw_geterr __P((CLIENT *, struct rpc_err *));
87 static bool_t clntraw_freeres __P((CLIENT *, xdrproc_t, caddr_t));
88 static void clntraw_abort __P((CLIENT *));
89 static bool_t clntraw_control __P((CLIENT *, u_int, char *));
90 static void clntraw_destroy __P((CLIENT *));
91 
92 static const struct clnt_ops client_ops = {
93 	clntraw_call,
94 	clntraw_abort,
95 	clntraw_geterr,
96 	clntraw_freeres,
97 	clntraw_destroy,
98 	clntraw_control
99 };
100 
101 /*
102  * Create a client handle for memory based rpc.
103  */
104 CLIENT *
105 clntraw_create(prog, vers)
106 	u_long prog;
107 	u_long vers;
108 {
109 	struct clntraw_private *clp = clntraw_private;
110 	struct rpc_msg call_msg;
111 	XDR *xdrs = &clp->xdr_stream;
112 	CLIENT	*client = &clp->client_object;
113 
114 	if (clp == 0) {
115 		clp = (struct clntraw_private *)calloc(1, sizeof (*clp));
116 		if (clp == 0)
117 			return (0);
118 		clntraw_private = clp;
119 	}
120 	/*
121 	 * pre-serialize the static part of the call msg and stash it away
122 	 */
123 	call_msg.rm_direction = CALL;
124 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
125 	/* XXX: prog and vers have been long historically :-( */
126 	call_msg.rm_call.cb_prog = (u_int32_t)prog;
127 	call_msg.rm_call.cb_vers = (u_int32_t)vers;
128 	xdrmem_create(xdrs, clp->u.mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
129 	if (! xdr_callhdr(xdrs, &call_msg))
130 		warnx("clntraw_create - Fatal header serialization error.");
131 	clp->mcnt = XDR_GETPOS(xdrs);
132 	XDR_DESTROY(xdrs);
133 
134 	/*
135 	 * Set xdrmem for client/server shared buffer
136 	 */
137 	xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
138 
139 	/*
140 	 * create client handle
141 	 */
142 	client->cl_ops = &client_ops;
143 	client->cl_auth = authnone_create();
144 	return (client);
145 }
146 
147 /* ARGSUSED */
148 static enum clnt_stat
149 clntraw_call(h, proc, xargs, argsp, xresults, resultsp, timeout)
150 	CLIENT *h;
151 	u_long proc;
152 	xdrproc_t xargs;
153 	caddr_t argsp;
154 	xdrproc_t xresults;
155 	caddr_t resultsp;
156 	struct timeval timeout;
157 {
158 	struct clntraw_private *clp = clntraw_private;
159 	XDR *xdrs = &clp->xdr_stream;
160 	struct rpc_msg msg;
161 	enum clnt_stat status;
162 	struct rpc_err error;
163 
164 	_DIAGASSERT(h != NULL);
165 
166 	if (clp == 0)
167 		return (RPC_FAILED);
168 call_again:
169 	/*
170 	 * send request
171 	 */
172 	xdrs->x_op = XDR_ENCODE;
173 	XDR_SETPOS(xdrs, 0);
174 	clp->u.mashl_rpcmsg.rm_xid ++ ;
175 	if ((! XDR_PUTBYTES(xdrs, clp->u.mashl_callmsg, clp->mcnt)) ||
176 	    (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
177 	    (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
178 	    (! (*xargs)(xdrs, argsp))) {
179 		return (RPC_CANTENCODEARGS);
180 	}
181 	(void)XDR_GETPOS(xdrs);  /* called just to cause overhead */
182 
183 	/*
184 	 * We have to call server input routine here because this is
185 	 * all going on in one process. Yuk.
186 	 */
187 	svc_getreq(1);
188 
189 	/*
190 	 * get results
191 	 */
192 	xdrs->x_op = XDR_DECODE;
193 	XDR_SETPOS(xdrs, 0);
194 	msg.acpted_rply.ar_verf = _null_auth;
195 	msg.acpted_rply.ar_results.where = resultsp;
196 	msg.acpted_rply.ar_results.proc = xresults;
197 	if (! xdr_replymsg(xdrs, &msg)) {
198 		/*
199 		 * It's possible for xdr_replymsg() to fail partway
200 		 * through its attempt to decode the result from the
201 		 * server. If this happens, it will leave the reply
202 		 * structure partially populated with dynamically
203 		 * allocated memory. (This can happen if someone uses
204 		 * clntudp_bufcreate() to create a CLIENT handle and
205 		 * specifies a receive buffer size that is too small.)
206 		 * This memory must be free()ed to avoid a leak.
207 		 */
208 		int op = xdrs->x_op;
209 		xdrs->x_op = XDR_FREE;
210 		xdr_replymsg(xdrs, &msg);
211 		xdrs->x_op = op;
212 		return (RPC_CANTDECODERES);
213 	}
214 	_seterr_reply(&msg, &error);
215 	status = error.re_status;
216 
217 	if (status == RPC_SUCCESS) {
218 		if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
219 			status = RPC_AUTHERROR;
220 		}
221 	}  /* end successful completion */
222 	else {
223 		if (AUTH_REFRESH(h->cl_auth))
224 			goto call_again;
225 	}  /* end of unsuccessful completion */
226 
227 	if (status == RPC_SUCCESS) {
228 		if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
229 			status = RPC_AUTHERROR;
230 		}
231 		if (msg.acpted_rply.ar_verf.oa_base != NULL) {
232 			xdrs->x_op = XDR_FREE;
233 			(void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
234 		}
235 	}
236 
237 	return (status);
238 }
239 
240 /*ARGSUSED*/
241 static void
242 clntraw_geterr(cl, err)
243 	CLIENT *cl;
244 	struct rpc_err *err;
245 {
246 }
247 
248 
249 /* ARGSUSED */
250 static bool_t
251 clntraw_freeres(cl, xdr_res, res_ptr)
252 	CLIENT *cl;
253 	xdrproc_t xdr_res;
254 	caddr_t res_ptr;
255 {
256 	struct clntraw_private *clp = clntraw_private;
257 	XDR *xdrs = &clp->xdr_stream;
258 	bool_t rval;
259 
260 	if (clp == 0) {
261 		rval = (bool_t) RPC_FAILED;
262 		return (rval);
263 	}
264 	xdrs->x_op = XDR_FREE;
265 	return ((*xdr_res)(xdrs, res_ptr));
266 }
267 
268 /*ARGSUSED*/
269 static void
270 clntraw_abort(cl)
271 	CLIENT *cl;
272 {
273 }
274 
275 /*ARGSUSED*/
276 static bool_t
277 clntraw_control(cl, ui, str)
278 	CLIENT *cl;
279 	u_int ui;
280 	char *str;
281 {
282 	return (FALSE);
283 }
284 
285 /*ARGSUSED*/
286 static void
287 clntraw_destroy(cl)
288 	CLIENT *cl;
289 {
290 }
291