1 /* $NetBSD: clnt_raw.c,v 1.32 2013/03/11 20:19:29 tron Exp $ */
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
37 static char *sccsid
= "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro";
38 static char *sccsid
= "@(#)clnt_raw.c 2.2 88/08/01 4.0 RPCSRC";
40 __RCSID("$NetBSD: clnt_raw.c,v 1.32 2013/03/11 20:19:29 tron Exp $");
47 * Copyright (C) 1984, Sun Microsystems, Inc.
49 * Memory based rpc for simple testing and timing.
50 * Interface to create an rpc client and server in the same process.
51 * This lets us similate rpc and get round trip overhead, without
52 * any interference from the kernel.
55 #include "namespace.h"
56 #include "reentrant.h"
66 __weak_alias(clntraw_create
,_clntraw_create
)
67 __weak_alias(clnt_raw_create
,_clnt_raw_create
)
71 extern mutex_t clntraw_lock
;
74 #define MCALL_MSG_SIZE 24
77 * This is the "network" we will be moving stuff over.
79 static struct clntraw_private
{
84 struct rpc_msg mashl_rpcmsg
;
85 char mashl_callmsg
[MCALL_MSG_SIZE
];
90 static enum clnt_stat
clnt_raw_call(CLIENT
*, rpcproc_t
, xdrproc_t
,
91 const char *, xdrproc_t
, caddr_t
, struct timeval
);
92 static void clnt_raw_geterr(CLIENT
*, struct rpc_err
*);
93 static bool_t
clnt_raw_freeres(CLIENT
*, xdrproc_t
, caddr_t
);
94 static void clnt_raw_abort(CLIENT
*);
95 static bool_t
clnt_raw_control(CLIENT
*, u_int
, char *);
96 static void clnt_raw_destroy(CLIENT
*);
97 static struct clnt_ops
*clnt_raw_ops(void);
100 * Create a client handle for memory based rpc.
103 clnt_raw_create(rpcprog_t prog
, rpcvers_t vers
)
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
;
110 mutex_lock(&clntraw_lock
);
112 clp
= calloc((size_t)1, sizeof (*clp
));
115 if (__rpc_rawcombuf
== NULL
)
118 if (__rpc_rawcombuf
== NULL
)
120 clp
->_raw_buf
= __rpc_rawcombuf
;
121 clntraw_private
= clp
;
124 * pre-serialize the static part of the call msg and stash it away
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("%s: Fatal header serialization error", __func__
);
134 clp
->mcnt
= XDR_GETPOS(xdrs
);
138 * Set xdrmem for client/server shared buffer
140 xdrmem_create(xdrs
, clp
->_raw_buf
, UDPMSGSIZE
, XDR_FREE
);
143 * create client handle
145 client
->cl_ops
= clnt_raw_ops();
146 client
->cl_auth
= authnone_create();
147 mutex_unlock(&clntraw_lock
);
152 mutex_unlock(&clntraw_lock
);
158 static enum clnt_stat
159 clnt_raw_call(CLIENT
*h
, rpcproc_t proc
, xdrproc_t xargs
, const char *argsp
,
160 xdrproc_t xresults
, caddr_t resultsp
, struct timeval timeout
)
162 struct clntraw_private
*clp
= clntraw_private
;
163 XDR
*xdrs
= &clp
->xdr_stream
;
165 enum clnt_stat status
;
166 struct rpc_err error
;
168 _DIAGASSERT(h
!= NULL
);
170 mutex_lock(&clntraw_lock
);
172 mutex_unlock(&clntraw_lock
);
175 mutex_unlock(&clntraw_lock
);
181 xdrs
->x_op
= XDR_ENCODE
;
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
, __UNCONST(argsp
)))) {
188 return (RPC_CANTENCODEARGS
);
190 (void)XDR_GETPOS(xdrs
); /* called just to cause overhead */
193 * We have to call server input routine here because this is
194 * all going on in one process. Yuk.
196 svc_getreq_common(FD_SETSIZE
);
201 xdrs
->x_op
= XDR_DECODE
;
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
)) {
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.
218 xdrs
->x_op
= XDR_FREE
;
219 xdr_replymsg(xdrs
, &msg
);
221 return (RPC_CANTDECODERES
);
223 _seterr_reply(&msg
, &error
);
224 status
= error
.re_status
;
226 if (status
== RPC_SUCCESS
) {
227 if (! AUTH_VALIDATE(h
->cl_auth
, &msg
.acpted_rply
.ar_verf
)) {
228 status
= RPC_AUTHERROR
;
230 } /* end successful completion */
232 if (AUTH_REFRESH(h
->cl_auth
))
234 } /* end of unsuccessful completion */
236 if (status
== RPC_SUCCESS
) {
237 if (! AUTH_VALIDATE(h
->cl_auth
, &msg
.acpted_rply
.ar_verf
)) {
238 status
= RPC_AUTHERROR
;
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
));
251 clnt_raw_geterr(CLIENT
*cl
, struct rpc_err
*error
)
258 clnt_raw_freeres(CLIENT
*cl
, xdrproc_t xdr_res
, caddr_t res_ptr
)
260 struct clntraw_private
*clp
= clntraw_private
;
261 XDR
*xdrs
= &clp
->xdr_stream
;
264 mutex_lock(&clntraw_lock
);
266 rval
= (bool_t
) RPC_FAILED
;
267 mutex_unlock(&clntraw_lock
);
270 mutex_unlock(&clntraw_lock
);
271 xdrs
->x_op
= XDR_FREE
;
272 return ((*xdr_res
)(xdrs
, res_ptr
));
277 clnt_raw_abort(CLIENT
*cl
)
283 clnt_raw_control(CLIENT
*cl
, u_int ui
, char *str
)
290 clnt_raw_destroy(CLIENT
*cl
)
294 static struct clnt_ops
*
297 static struct clnt_ops ops
;
299 extern mutex_t ops_lock
;
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
);