1 /* $NetBSD: clnt_raw.c,v 1.20 2000/12/10 04:12:03 christos Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, 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.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *sccsid2 = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro";
35 static char *sccsid = "@(#)clnt_raw.c 2.2 88/08/01 4.0 RPCSRC";
36 #endif
37 /*
38 * clnt_raw.c
39 *
40 * Copyright (C) 1984, Sun Microsystems, Inc.
41 *
42 * Memory based rpc for simple testing and timing.
43 * Interface to create an rpc client and server in the same process.
44 * This lets us similate rpc and get round trip overhead, without
45 * any interference from the kernel.
46 */
47
48 #include "namespace.h"
49 #include "reentrant.h"
50 #include <assert.h>
51 #include <err.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54
55 #include <rpc/rpc.h>
56 #include <rpc/raw.h>
57 #include "un-namespace.h"
58 #include "mt_misc.h"
59
60 #define MCALL_MSG_SIZE 24
61
62 /*
63 * This is the "network" we will be moving stuff over.
64 */
65 static struct clntraw_private {
66 CLIENT client_object;
67 XDR xdr_stream;
68 char *_raw_buf;
69 union {
70 struct rpc_msg mashl_rpcmsg;
71 char mashl_callmsg[MCALL_MSG_SIZE];
72 } u;
73 u_int mcnt;
74 } *clntraw_private;
75
76 static enum clnt_stat clnt_raw_call(CLIENT *, rpcproc_t, xdrproc_t, void *,
77 xdrproc_t, void *, struct timeval);
78 static void clnt_raw_geterr(CLIENT *, struct rpc_err *);
79 static bool_t clnt_raw_freeres(CLIENT *, xdrproc_t, void *);
80 static void clnt_raw_abort(CLIENT *);
81 static bool_t clnt_raw_control(CLIENT *, u_int, void *);
82 static void clnt_raw_destroy(CLIENT *);
83 static struct clnt_ops *clnt_raw_ops(void);
84
85 /*
86 * Create a client handle for memory based rpc.
87 */
88 CLIENT *
clnt_raw_create(rpcprog_t prog,rpcvers_t vers)89 clnt_raw_create(rpcprog_t prog, rpcvers_t vers)
90 {
91 struct clntraw_private *clp;
92 struct rpc_msg call_msg;
93 XDR *xdrs;
94 CLIENT *client;
95
96 mutex_lock(&clntraw_lock);
97 if ((clp = clntraw_private) == NULL) {
98 clp = (struct clntraw_private *)calloc(1, sizeof (*clp));
99 if (clp == NULL) {
100 mutex_unlock(&clntraw_lock);
101 return NULL;
102 }
103 if (__rpc_rawcombuf == NULL)
104 __rpc_rawcombuf =
105 (char *)calloc(UDPMSGSIZE, sizeof (char));
106 clp->_raw_buf = __rpc_rawcombuf;
107 clntraw_private = clp;
108 }
109 xdrs = &clp->xdr_stream;
110 client = &clp->client_object;
111
112 /*
113 * pre-serialize the static part of the call msg and stash it away
114 */
115 call_msg.rm_direction = CALL;
116 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
117 /* XXX: prog and vers have been long historically :-( */
118 call_msg.rm_call.cb_prog = (u_int32_t)prog;
119 call_msg.rm_call.cb_vers = (u_int32_t)vers;
120 xdrmem_create(xdrs, clp->u.mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
121 if (! xdr_callhdr(xdrs, &call_msg))
122 warnx("clntraw_create - Fatal header serialization error.");
123 clp->mcnt = XDR_GETPOS(xdrs);
124 XDR_DESTROY(xdrs);
125
126 /*
127 * Set xdrmem for client/server shared buffer
128 */
129 xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
130
131 /*
132 * create client handle
133 */
134 client->cl_ops = clnt_raw_ops();
135 client->cl_auth = authnone_create();
136 mutex_unlock(&clntraw_lock);
137 return (client);
138 }
139
140 /* ARGSUSED */
141 static enum clnt_stat
clnt_raw_call(CLIENT * h,rpcproc_t proc,xdrproc_t xargs,void * argsp,xdrproc_t xresults,void * resultsp,struct timeval timeout)142 clnt_raw_call(CLIENT *h, rpcproc_t proc, xdrproc_t xargs, void *argsp,
143 xdrproc_t xresults, void *resultsp, struct timeval timeout)
144 {
145 struct clntraw_private *clp = clntraw_private;
146 XDR *xdrs = &clp->xdr_stream;
147 struct rpc_msg msg;
148 enum clnt_stat status;
149 struct rpc_err error;
150
151 assert(h != NULL);
152
153 mutex_lock(&clntraw_lock);
154 if (clp == NULL) {
155 mutex_unlock(&clntraw_lock);
156 return (RPC_FAILED);
157 }
158 mutex_unlock(&clntraw_lock);
159
160 call_again:
161 /*
162 * send request
163 */
164 xdrs->x_op = XDR_ENCODE;
165 XDR_SETPOS(xdrs, 0);
166 clp->u.mashl_rpcmsg.rm_xid ++ ;
167 if ((! XDR_PUTBYTES(xdrs, clp->u.mashl_callmsg, clp->mcnt)) ||
168 (! XDR_PUTINT32(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_common(FD_SETSIZE);
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, &msg))
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
clnt_raw_geterr(CLIENT * cl,struct rpc_err * err)234 clnt_raw_geterr(CLIENT *cl, struct rpc_err *err)
235 {
236 }
237
238
239 /* ARGSUSED */
240 static bool_t
clnt_raw_freeres(CLIENT * cl,xdrproc_t xdr_res,void * res_ptr)241 clnt_raw_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
242 {
243 struct clntraw_private *clp = clntraw_private;
244 XDR *xdrs = &clp->xdr_stream;
245 bool_t rval;
246
247 mutex_lock(&clntraw_lock);
248 if (clp == NULL) {
249 rval = (bool_t) RPC_FAILED;
250 mutex_unlock(&clntraw_lock);
251 return (rval);
252 }
253 mutex_unlock(&clntraw_lock);
254 xdrs->x_op = XDR_FREE;
255 return ((*xdr_res)(xdrs, res_ptr));
256 }
257
258 /*ARGSUSED*/
259 static void
clnt_raw_abort(CLIENT * cl)260 clnt_raw_abort(CLIENT *cl)
261 {
262 }
263
264 /*ARGSUSED*/
265 static bool_t
clnt_raw_control(CLIENT * cl,u_int ui,void * str)266 clnt_raw_control(CLIENT *cl, u_int ui, void *str)
267 {
268 return (FALSE);
269 }
270
271 /*ARGSUSED*/
272 static void
clnt_raw_destroy(CLIENT * cl)273 clnt_raw_destroy(CLIENT *cl)
274 {
275 }
276
277 static struct clnt_ops *
clnt_raw_ops(void)278 clnt_raw_ops(void)
279 {
280 static struct clnt_ops ops;
281
282 /* VARIABLES PROTECTED BY ops_lock: ops */
283
284 mutex_lock(&ops_lock);
285 if (ops.cl_call == NULL) {
286 ops.cl_call = clnt_raw_call;
287 ops.cl_abort = clnt_raw_abort;
288 ops.cl_geterr = clnt_raw_geterr;
289 ops.cl_freeres = clnt_raw_freeres;
290 ops.cl_destroy = clnt_raw_destroy;
291 ops.cl_control = clnt_raw_control;
292 }
293 mutex_unlock(&ops_lock);
294 return (&ops);
295 }
296