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