1 /* $NetBSD: clnt_raw.c,v 1.34 2024/01/23 17:24:38 christos Exp $ */
2
3 /*
4 * Copyright (c) 2010, Oracle America, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
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.
19 *
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.
32 */
33
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
36 #if 0
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";
39 #else
40 __RCSID("$NetBSD: clnt_raw.c,v 1.34 2024/01/23 17:24:38 christos Exp $");
41 #endif
42 #endif
43
44 /*
45 * clnt_raw.c
46 *
47 * Copyright (C) 1984, Sun Microsystems, Inc.
48 *
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.
53 */
54
55 #include "namespace.h"
56 #include "reentrant.h"
57 #include <assert.h>
58 #include <err.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61
62 #include <rpc/rpc.h>
63 #include <rpc/raw.h>
64
65 #include "rpc_internal.h"
66
67 #ifdef __weak_alias
68 __weak_alias(clntraw_create,_clntraw_create)
69 __weak_alias(clnt_raw_create,_clnt_raw_create)
70 #endif
71
72 #define MCALL_MSG_SIZE 24
73
74 /*
75 * This is the "network" we will be moving stuff over.
76 */
77 static struct clntraw_private {
78 CLIENT client_object;
79 XDR xdr_stream;
80 char *_raw_buf;
81 union {
82 struct rpc_msg mashl_rpcmsg;
83 char mashl_callmsg[MCALL_MSG_SIZE];
84 } u;
85 u_int mcnt;
86 } *clntraw_private;
87
88 static enum clnt_stat clnt_raw_call(CLIENT *, rpcproc_t, xdrproc_t,
89 const char *, xdrproc_t, caddr_t, struct timeval);
90 static void clnt_raw_geterr(CLIENT *, struct rpc_err *);
91 static bool_t clnt_raw_freeres(CLIENT *, xdrproc_t, caddr_t);
92 static void clnt_raw_abort(CLIENT *);
93 static bool_t clnt_raw_control(CLIENT *, u_int, char *);
94 static void clnt_raw_destroy(CLIENT *);
95 static struct clnt_ops *clnt_raw_ops(void);
96
97 /*
98 * Create a client handle for memory based rpc.
99 */
100 CLIENT *
clnt_raw_create(rpcprog_t prog,rpcvers_t vers)101 clnt_raw_create(rpcprog_t prog, rpcvers_t vers)
102 {
103 struct clntraw_private *clp;
104 struct rpc_msg call_msg;
105 XDR *xdrs;
106 CLIENT *client;
107
108 mutex_lock(&clntraw_lock);
109 if ((clp = clntraw_private) == NULL) {
110 clp = calloc((size_t)1, sizeof (*clp));
111 if (clp == NULL)
112 goto out;
113 if (__rpc_rawcombuf == NULL)
114 __rpc_rawcombuf =
115 malloc(UDPMSGSIZE);
116 if (__rpc_rawcombuf == NULL)
117 goto out;
118 clp->_raw_buf = __rpc_rawcombuf;
119 clntraw_private = clp;
120 }
121
122 xdrs = &clp->xdr_stream;
123 client = &clp->client_object;
124
125 /*
126 * pre-serialize the static part of the call msg and stash it away
127 */
128 call_msg.rm_direction = CALL;
129 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
130 /* XXX: prog and vers have been long historically :-( */
131 call_msg.rm_call.cb_prog = (u_int32_t)prog;
132 call_msg.rm_call.cb_vers = (u_int32_t)vers;
133 xdrmem_create(xdrs, clp->u.mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
134 if (! xdr_callhdr(xdrs, &call_msg))
135 warnx("%s: Fatal header serialization error", __func__);
136 clp->mcnt = XDR_GETPOS(xdrs);
137 XDR_DESTROY(xdrs);
138
139 /*
140 * Set xdrmem for client/server shared buffer
141 */
142 xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
143
144 /*
145 * create client handle
146 */
147 client->cl_ops = clnt_raw_ops();
148 client->cl_auth = authnone_create();
149 mutex_unlock(&clntraw_lock);
150 return (client);
151 out:
152 if (clp)
153 free(clp);
154 mutex_unlock(&clntraw_lock);
155 return NULL;
156
157 }
158
159 /* ARGSUSED */
160 static enum clnt_stat
clnt_raw_call(CLIENT * h,rpcproc_t proc,xdrproc_t xargs,const char * argsp,xdrproc_t xresults,caddr_t resultsp,struct timeval timeout)161 clnt_raw_call(CLIENT *h, rpcproc_t proc, xdrproc_t xargs, const char *argsp,
162 xdrproc_t xresults, caddr_t resultsp, struct timeval timeout)
163 {
164 struct clntraw_private *clp = clntraw_private;
165 XDR *xdrs = &clp->xdr_stream;
166 struct rpc_msg msg;
167 enum clnt_stat status;
168 struct rpc_err error;
169
170 _DIAGASSERT(h != NULL);
171
172 mutex_lock(&clntraw_lock);
173 if (clp == NULL) {
174 mutex_unlock(&clntraw_lock);
175 return (RPC_FAILED);
176 }
177 mutex_unlock(&clntraw_lock);
178
179 call_again:
180 /*
181 * send request
182 */
183 xdrs->x_op = XDR_ENCODE;
184 XDR_SETPOS(xdrs, 0);
185 clp->u.mashl_rpcmsg.rm_xid ++ ;
186 if ((! XDR_PUTBYTES(xdrs, clp->u.mashl_callmsg, clp->mcnt)) ||
187 (! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
188 (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
189 (! (*xargs)(xdrs, __UNCONST(argsp)))) {
190 return (RPC_CANTENCODEARGS);
191 }
192 (void)XDR_GETPOS(xdrs); /* called just to cause overhead */
193
194 /*
195 * We have to call server input routine here because this is
196 * all going on in one process. Yuk.
197 */
198 svc_getreq_common(-1);
199
200 /*
201 * get results
202 */
203 xdrs->x_op = XDR_DECODE;
204 XDR_SETPOS(xdrs, 0);
205 msg.acpted_rply.ar_verf = _null_auth;
206 msg.acpted_rply.ar_results.where = resultsp;
207 msg.acpted_rply.ar_results.proc = xresults;
208 if (! xdr_replymsg(xdrs, &msg)) {
209 /*
210 * It's possible for xdr_replymsg() to fail partway
211 * through its attempt to decode the result from the
212 * server. If this happens, it will leave the reply
213 * structure partially populated with dynamically
214 * allocated memory. (This can happen if someone uses
215 * clntudp_bufcreate() to create a CLIENT handle and
216 * specifies a receive buffer size that is too small.)
217 * This memory must be free()ed to avoid a leak.
218 */
219 int op = xdrs->x_op;
220 xdrs->x_op = XDR_FREE;
221 xdr_replymsg(xdrs, &msg);
222 xdrs->x_op = op;
223 return (RPC_CANTDECODERES);
224 }
225 _seterr_reply(&msg, &error);
226 status = error.re_status;
227
228 if (status == RPC_SUCCESS) {
229 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
230 status = RPC_AUTHERROR;
231 }
232 } /* end successful completion */
233 else {
234 if (AUTH_REFRESH(h->cl_auth))
235 goto call_again;
236 } /* end of unsuccessful completion */
237
238 if (status == RPC_SUCCESS) {
239 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
240 status = RPC_AUTHERROR;
241 }
242 if (msg.acpted_rply.ar_verf.oa_base != NULL) {
243 xdrs->x_op = XDR_FREE;
244 (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
245 }
246 }
247
248 return (status);
249 }
250
251 /*ARGSUSED*/
252 static void
clnt_raw_geterr(CLIENT * cl,struct rpc_err * error)253 clnt_raw_geterr(CLIENT *cl, struct rpc_err *error)
254 {
255 }
256
257
258 /* ARGSUSED */
259 static bool_t
clnt_raw_freeres(CLIENT * cl,xdrproc_t xdr_res,caddr_t res_ptr)260 clnt_raw_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
261 {
262 struct clntraw_private *clp = clntraw_private;
263 XDR *xdrs = &clp->xdr_stream;
264 bool_t rval;
265
266 mutex_lock(&clntraw_lock);
267 if (clp == NULL) {
268 rval = (bool_t) RPC_FAILED;
269 mutex_unlock(&clntraw_lock);
270 return (rval);
271 }
272 mutex_unlock(&clntraw_lock);
273 xdrs->x_op = XDR_FREE;
274 return ((*xdr_res)(xdrs, res_ptr));
275 }
276
277 /*ARGSUSED*/
278 static void
clnt_raw_abort(CLIENT * cl)279 clnt_raw_abort(CLIENT *cl)
280 {
281 }
282
283 /*ARGSUSED*/
284 static bool_t
clnt_raw_control(CLIENT * cl,u_int ui,char * str)285 clnt_raw_control(CLIENT *cl, u_int ui, char *str)
286 {
287 return (FALSE);
288 }
289
290 /*ARGSUSED*/
291 static void
clnt_raw_destroy(CLIENT * cl)292 clnt_raw_destroy(CLIENT *cl)
293 {
294 }
295
296 static struct clnt_ops *
clnt_raw_ops(void)297 clnt_raw_ops(void)
298 {
299 static struct clnt_ops ops;
300
301 /* VARIABLES PROTECTED BY ops_lock: ops */
302
303 mutex_lock(&ops_lock);
304 if (ops.cl_call == NULL) {
305 ops.cl_call = clnt_raw_call;
306 ops.cl_abort = clnt_raw_abort;
307 ops.cl_geterr = clnt_raw_geterr;
308 ops.cl_freeres = clnt_raw_freeres;
309 ops.cl_destroy = clnt_raw_destroy;
310 ops.cl_control = clnt_raw_control;
311 }
312 mutex_unlock(&ops_lock);
313 return (&ops);
314 }
315