2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 * @(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro
29 * @(#)clnt_raw.c 2.2 88/08/01 4.0 RPCSRC
30 * $NetBSD: clnt_raw.c,v 1.20 2000/12/10 04:12:03 christos Exp $
31 * $FreeBSD: src/lib/libc/rpc/clnt_raw.c,v 1.20 2006/02/27 22:10:58 deischen Exp $
37 * Copyright (C) 1984, Sun Microsystems, Inc.
39 * Memory based rpc for simple testing and timing.
40 * Interface to create an rpc client and server in the same process.
41 * This lets us similate rpc and get round trip overhead, without
42 * any interference from the kernel.
45 #include "namespace.h"
46 #include "reentrant.h"
54 #include "un-namespace.h"
57 #define MCALL_MSG_SIZE 24
60 * This is the "network" we will be moving stuff over.
62 static struct clntraw_private {
67 struct rpc_msg mashl_rpcmsg;
68 char mashl_callmsg[MCALL_MSG_SIZE];
73 static void clnt_raw_abort(CLIENT *);
74 static enum clnt_stat clnt_raw_call(CLIENT *, rpcproc_t, xdrproc_t, void *,
75 xdrproc_t, void *, struct timeval);
76 static bool_t clnt_raw_control(CLIENT *, u_int, void *);
77 static void clnt_raw_destroy(CLIENT *);
78 static bool_t clnt_raw_freeres(CLIENT *, xdrproc_t, void *);
79 static void clnt_raw_geterr(CLIENT *, struct rpc_err *);
80 static struct clnt_ops *clnt_raw_ops(void);
83 * Create a client handle for memory based rpc.
86 clnt_raw_create(rpcprog_t prog, rpcvers_t vers)
88 struct clntraw_private *clp = clntraw_private;
89 struct rpc_msg call_msg;
90 XDR *xdrs = &clp->xdr_stream;
91 CLIENT *client = &clp->client_object;
93 mutex_lock(&clntraw_lock);
95 clp = (struct clntraw_private *)calloc(1, sizeof (*clp));
97 mutex_unlock(&clntraw_lock);
100 if (__rpc_rawcombuf == NULL)
102 (char *)calloc(UDPMSGSIZE, sizeof (char));
103 clp->_raw_buf = __rpc_rawcombuf;
104 clntraw_private = clp;
107 * pre-serialize the static part of the call msg and stash it away
109 call_msg.rm_direction = CALL;
110 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
111 /* XXX: prog and vers have been long historically :-( */
112 call_msg.rm_call.cb_prog = (u_int32_t)prog;
113 call_msg.rm_call.cb_vers = (u_int32_t)vers;
114 xdrmem_create(xdrs, clp->u.mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
115 if (! xdr_callhdr(xdrs, &call_msg))
116 warnx("clntraw_create - Fatal header serialization error.");
117 clp->mcnt = XDR_GETPOS(xdrs);
121 * Set xdrmem for client/server shared buffer
123 xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
126 * create client handle
128 client->cl_ops = clnt_raw_ops();
129 client->cl_auth = authnone_create();
130 mutex_unlock(&clntraw_lock);
135 static enum clnt_stat
136 clnt_raw_call(CLIENT *h, rpcproc_t proc, xdrproc_t xargs, void *argsp,
137 xdrproc_t xresults, void *resultsp,
138 struct timeval timeout __unused)
140 struct clntraw_private *clp = clntraw_private;
141 XDR *xdrs = &clp->xdr_stream;
143 enum clnt_stat status;
144 struct rpc_err error;
148 mutex_lock(&clntraw_lock);
150 mutex_unlock(&clntraw_lock);
153 mutex_unlock(&clntraw_lock);
159 xdrs->x_op = XDR_ENCODE;
161 clp->u.mashl_rpcmsg.rm_xid ++ ;
162 if ((! XDR_PUTBYTES(xdrs, clp->u.mashl_callmsg, clp->mcnt)) ||
163 (! XDR_PUTINT32(xdrs, &proc)) ||
164 (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
165 (! (*xargs)(xdrs, argsp))) {
166 return (RPC_CANTENCODEARGS);
168 XDR_GETPOS(xdrs); /* called just to cause overhead */
171 * We have to call server input routine here because this is
172 * all going on in one process. Yuk.
174 svc_getreq_common(FD_SETSIZE);
179 xdrs->x_op = XDR_DECODE;
181 msg.acpted_rply.ar_verf = _null_auth;
182 msg.acpted_rply.ar_results.where = resultsp;
183 msg.acpted_rply.ar_results.proc = xresults;
184 if (! xdr_replymsg(xdrs, &msg)) {
186 * It's possible for xdr_replymsg() to fail partway
187 * through its attempt to decode the result from the
188 * server. If this happens, it will leave the reply
189 * structure partially populated with dynamically
190 * allocated memory. (This can happen if someone uses
191 * clntudp_bufcreate() to create a CLIENT handle and
192 * specifies a receive buffer size that is too small.)
193 * This memory must be free()ed to avoid a leak.
196 xdrs->x_op = XDR_FREE;
197 xdr_replymsg(xdrs, &msg);
199 return (RPC_CANTDECODERES);
201 _seterr_reply(&msg, &error);
202 status = error.re_status;
204 if (status == RPC_SUCCESS) {
205 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
206 status = RPC_AUTHERROR;
208 } /* end successful completion */
210 if (AUTH_REFRESH(h->cl_auth, &msg))
212 } /* end of unsuccessful completion */
214 if (status == RPC_SUCCESS) {
215 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
216 status = RPC_AUTHERROR;
218 if (msg.acpted_rply.ar_verf.oa_base != NULL) {
219 xdrs->x_op = XDR_FREE;
220 xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
229 clnt_raw_geterr(CLIENT *cl __unused, struct rpc_err *err __unused)
236 clnt_raw_freeres(CLIENT *cl __unused, xdrproc_t xdr_res, void *res_ptr)
238 struct clntraw_private *clp = clntraw_private;
239 XDR *xdrs = &clp->xdr_stream;
242 mutex_lock(&clntraw_lock);
244 rval = (bool_t) RPC_FAILED;
245 mutex_unlock(&clntraw_lock);
248 mutex_unlock(&clntraw_lock);
249 xdrs->x_op = XDR_FREE;
250 return ((*xdr_res)(xdrs, res_ptr));
255 clnt_raw_abort(CLIENT *cl __unused)
261 clnt_raw_control(CLIENT *cl __unused, u_int ui __unused, void *str __unused)
268 clnt_raw_destroy(CLIENT *cl __unused)
272 static struct clnt_ops *
275 static struct clnt_ops ops;
277 /* VARIABLES PROTECTED BY ops_lock: ops */
279 mutex_lock(&ops_lock);
280 if (ops.cl_call == NULL) {
281 ops.cl_call = clnt_raw_call;
282 ops.cl_abort = clnt_raw_abort;
283 ops.cl_geterr = clnt_raw_geterr;
284 ops.cl_freeres = clnt_raw_freeres;
285 ops.cl_destroy = clnt_raw_destroy;
286 ops.cl_control = clnt_raw_control;
288 mutex_unlock(&ops_lock);