1 /* $NetBSD: clnt_raw.c,v 1.20 2000/12/10 04:12:03 christos Exp $ */
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.
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.
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.
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.
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.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
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";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
42 * Copyright (C) 1984, Sun Microsystems, Inc.
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.
50 #include "namespace.h"
51 #include "reentrant.h"
59 #include "un-namespace.h"
62 #define MCALL_MSG_SIZE 24
65 * This is the "network" we will be moving stuff over.
67 static struct clntraw_private {
72 struct rpc_msg mashl_rpcmsg;
73 char mashl_callmsg[MCALL_MSG_SIZE];
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);
88 * Create a client handle for memory based rpc.
91 clnt_raw_create(prog, vers)
95 struct clntraw_private *clp = clntraw_private;
96 struct rpc_msg call_msg;
97 XDR *xdrs = &clp->xdr_stream;
98 CLIENT *client = &clp->client_object;
100 mutex_lock(&clntraw_lock);
102 clp = (struct clntraw_private *)calloc(1, sizeof (*clp));
104 mutex_unlock(&clntraw_lock);
107 if (__rpc_rawcombuf == NULL)
109 (char *)calloc(UDPMSGSIZE, sizeof (char));
110 clp->_raw_buf = __rpc_rawcombuf;
111 clntraw_private = clp;
114 * pre-serialize the static part of the call msg and stash it away
116 call_msg.rm_direction = CALL;
117 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
118 /* XXX: prog and vers have been long historically :-( */
119 call_msg.rm_call.cb_prog = (u_int32_t)prog;
120 call_msg.rm_call.cb_vers = (u_int32_t)vers;
121 xdrmem_create(xdrs, clp->u.mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
122 if (! xdr_callhdr(xdrs, &call_msg))
123 warnx("clntraw_create - Fatal header serialization error.");
124 clp->mcnt = XDR_GETPOS(xdrs);
128 * Set xdrmem for client/server shared buffer
130 xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
133 * create client handle
135 client->cl_ops = clnt_raw_ops();
136 client->cl_auth = authnone_create();
137 mutex_unlock(&clntraw_lock);
142 static enum clnt_stat
143 clnt_raw_call(h, proc, xargs, argsp, xresults, resultsp, timeout)
150 struct timeval timeout;
152 struct clntraw_private *clp = clntraw_private;
153 XDR *xdrs = &clp->xdr_stream;
155 enum clnt_stat status;
156 struct rpc_err error;
160 mutex_lock(&clntraw_lock);
162 mutex_unlock(&clntraw_lock);
165 mutex_unlock(&clntraw_lock);
171 xdrs->x_op = XDR_ENCODE;
173 clp->u.mashl_rpcmsg.rm_xid ++ ;
174 if ((! XDR_PUTBYTES(xdrs, clp->u.mashl_callmsg, clp->mcnt)) ||
175 (! XDR_PUTINT32(xdrs, &proc)) ||
176 (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
177 (! (*xargs)(xdrs, argsp))) {
178 return (RPC_CANTENCODEARGS);
180 (void)XDR_GETPOS(xdrs); /* called just to cause overhead */
183 * We have to call server input routine here because this is
184 * all going on in one process. Yuk.
186 svc_getreq_common(FD_SETSIZE);
191 xdrs->x_op = XDR_DECODE;
193 msg.acpted_rply.ar_verf = _null_auth;
194 msg.acpted_rply.ar_results.where = resultsp;
195 msg.acpted_rply.ar_results.proc = xresults;
196 if (! xdr_replymsg(xdrs, &msg)) {
198 * It's possible for xdr_replymsg() to fail partway
199 * through its attempt to decode the result from the
200 * server. If this happens, it will leave the reply
201 * structure partially populated with dynamically
202 * allocated memory. (This can happen if someone uses
203 * clntudp_bufcreate() to create a CLIENT handle and
204 * specifies a receive buffer size that is too small.)
205 * This memory must be free()ed to avoid a leak.
208 xdrs->x_op = XDR_FREE;
209 xdr_replymsg(xdrs, &msg);
211 return (RPC_CANTDECODERES);
213 _seterr_reply(&msg, &error);
214 status = error.re_status;
216 if (status == RPC_SUCCESS) {
217 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
218 status = RPC_AUTHERROR;
220 } /* end successful completion */
222 if (AUTH_REFRESH(h->cl_auth, &msg))
224 } /* end of unsuccessful completion */
226 if (status == RPC_SUCCESS) {
227 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
228 status = RPC_AUTHERROR;
230 if (msg.acpted_rply.ar_verf.oa_base != NULL) {
231 xdrs->x_op = XDR_FREE;
232 (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
241 clnt_raw_geterr(cl, err)
250 clnt_raw_freeres(cl, xdr_res, res_ptr)
255 struct clntraw_private *clp = clntraw_private;
256 XDR *xdrs = &clp->xdr_stream;
259 mutex_lock(&clntraw_lock);
261 rval = (bool_t) RPC_FAILED;
262 mutex_unlock(&clntraw_lock);
265 mutex_unlock(&clntraw_lock);
266 xdrs->x_op = XDR_FREE;
267 return ((*xdr_res)(xdrs, res_ptr));
279 clnt_raw_control(cl, ui, str)
294 static struct clnt_ops *
297 static struct clnt_ops ops;
299 /* VARIABLES PROTECTED BY ops_lock: ops */
301 mutex_lock(&ops_lock);
302 if (ops.cl_call == NULL) {
303 ops.cl_call = clnt_raw_call;
304 ops.cl_abort = clnt_raw_abort;
305 ops.cl_geterr = clnt_raw_geterr;
306 ops.cl_freeres = clnt_raw_freeres;
307 ops.cl_destroy = clnt_raw_destroy;
308 ops.cl_control = clnt_raw_control;
310 mutex_unlock(&ops_lock);