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