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