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;
96 struct rpc_msg call_msg;
100 mutex_lock(&clntraw_lock);
101 if ((clp = clntraw_private) == NULL) {
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;
113 xdrs = &clp->xdr_stream;
114 client = &clp->client_object;
117 * pre-serialize the static part of the call msg and stash it away
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);
131 * Set xdrmem for client/server shared buffer
133 xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
136 * create client handle
138 client->cl_ops = clnt_raw_ops();
139 client->cl_auth = authnone_create();
140 mutex_unlock(&clntraw_lock);
145 static enum clnt_stat
146 clnt_raw_call(h, proc, xargs, argsp, xresults, resultsp, timeout)
153 struct timeval timeout;
155 struct clntraw_private *clp = clntraw_private;
156 XDR *xdrs = &clp->xdr_stream;
158 enum clnt_stat status;
159 struct rpc_err error;
163 mutex_lock(&clntraw_lock);
165 mutex_unlock(&clntraw_lock);
168 mutex_unlock(&clntraw_lock);
174 xdrs->x_op = XDR_ENCODE;
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);
183 (void)XDR_GETPOS(xdrs); /* called just to cause overhead */
186 * We have to call server input routine here because this is
187 * all going on in one process. Yuk.
189 svc_getreq_common(FD_SETSIZE);
194 xdrs->x_op = XDR_DECODE;
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)) {
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.
211 xdrs->x_op = XDR_FREE;
212 xdr_replymsg(xdrs, &msg);
214 return (RPC_CANTDECODERES);
216 _seterr_reply(&msg, &error);
217 status = error.re_status;
219 if (status == RPC_SUCCESS) {
220 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
221 status = RPC_AUTHERROR;
223 } /* end successful completion */
225 if (AUTH_REFRESH(h->cl_auth, &msg))
227 } /* end of unsuccessful completion */
229 if (status == RPC_SUCCESS) {
230 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
231 status = RPC_AUTHERROR;
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));
244 clnt_raw_geterr(cl, err)
253 clnt_raw_freeres(cl, xdr_res, res_ptr)
258 struct clntraw_private *clp = clntraw_private;
259 XDR *xdrs = &clp->xdr_stream;
262 mutex_lock(&clntraw_lock);
264 rval = (bool_t) RPC_FAILED;
265 mutex_unlock(&clntraw_lock);
268 mutex_unlock(&clntraw_lock);
269 xdrs->x_op = XDR_FREE;
270 return ((*xdr_res)(xdrs, res_ptr));
282 clnt_raw_control(cl, ui, str)
297 static struct clnt_ops *
300 static struct clnt_ops ops;
302 /* VARIABLES PROTECTED BY ops_lock: ops */
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;
313 mutex_unlock(&ops_lock);