1 /* $OpenBSD: clnt_raw.c,v 1.21 2022/02/14 03:38:59 guenther 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 /*
35 * clnt_raw.c
36 *
37 * Memory based rpc for simple testing and timing.
38 * Interface to create an rpc client and server in the same process.
39 * This lets us similate rpc and get round trip overhead, without
40 * any interference from the kernel.
41 */
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <errno.h>
46 #include <rpc/rpc.h>
47
48 #define MCALL_MSG_SIZE 24
49
50 /*
51 * This is the "network" we will be moving stuff over.
52 */
53 static struct clntraw_private {
54 CLIENT client_object;
55 XDR xdr_stream;
56 char _raw_buf[UDPMSGSIZE];
57 char mashl_callmsg[MCALL_MSG_SIZE];
58 u_int mcnt;
59 } *clntraw_private;
60
61 static enum clnt_stat clntraw_call(CLIENT *, u_long, xdrproc_t, caddr_t,
62 xdrproc_t, caddr_t, struct timeval);
63 static void clntraw_abort(CLIENT *);
64 static void clntraw_geterr(CLIENT *, struct rpc_err *);
65 static bool_t clntraw_freeres(CLIENT *, xdrproc_t, caddr_t);
66 static bool_t clntraw_control(CLIENT *, u_int, void *);
67 static void clntraw_destroy(CLIENT *);
68
69 static const struct clnt_ops client_ops = {
70 clntraw_call,
71 clntraw_abort,
72 clntraw_geterr,
73 clntraw_freeres,
74 clntraw_destroy,
75 clntraw_control
76 };
77
78 void svc_getreq(int rdfds);
79
80 /*
81 * Create a client handle for memory based rpc.
82 */
83 CLIENT *
clntraw_create(u_long prog,u_long vers)84 clntraw_create(u_long prog, u_long vers)
85 {
86 struct clntraw_private *clp = clntraw_private;
87 struct rpc_msg call_msg;
88 XDR *xdrs;
89 CLIENT *client;
90
91 if (clp == NULL) {
92 clp = calloc(1, sizeof (*clp));
93 if (clp == NULL)
94 goto fail;
95 clntraw_private = clp;
96 }
97 xdrs = &clp->xdr_stream;
98 client = &clp->client_object;
99 /*
100 * pre-serialize the static part of the call msg and stash it away
101 */
102 call_msg.rm_direction = CALL;
103 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
104 call_msg.rm_call.cb_prog = prog;
105 call_msg.rm_call.cb_vers = vers;
106 xdrmem_create(xdrs, clp->mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
107 if (!xdr_callhdr(xdrs, &call_msg))
108 goto fail;
109 clp->mcnt = XDR_GETPOS(xdrs);
110 XDR_DESTROY(xdrs);
111
112 /*
113 * Set xdrmem for client/server shared buffer
114 */
115 xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
116
117 /*
118 * create client handle
119 */
120 client->cl_ops = &client_ops;
121 client->cl_auth = authnone_create();
122 if (client->cl_auth == NULL)
123 goto fail;
124 return (client);
125
126 fail:
127 mem_free((caddr_t)clntraw_private, sizeof(*clntraw_private));
128 clntraw_private = NULL;
129 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
130 rpc_createerr.cf_error.re_errno = errno;
131 return (NULL);
132 }
133
134 static enum clnt_stat
clntraw_call(CLIENT * h,u_long proc,xdrproc_t xargs,caddr_t argsp,xdrproc_t xresults,caddr_t resultsp,struct timeval timeout)135 clntraw_call(CLIENT *h, u_long proc, xdrproc_t xargs, caddr_t argsp,
136 xdrproc_t xresults, caddr_t resultsp, struct timeval timeout)
137 {
138 struct clntraw_private *clp = clntraw_private;
139 XDR *xdrs;
140 struct rpc_msg msg;
141 enum clnt_stat status;
142 struct rpc_err error;
143
144 if (clp == NULL)
145 return (RPC_FAILED);
146 xdrs = &clp->xdr_stream;
147 call_again:
148 /*
149 * send request
150 */
151 xdrs->x_op = XDR_ENCODE;
152 XDR_SETPOS(xdrs, 0);
153 ((struct rpc_msg *)clp->mashl_callmsg)->rm_xid ++ ;
154 if ((! XDR_PUTBYTES(xdrs, clp->mashl_callmsg, clp->mcnt)) ||
155 (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
156 (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
157 (! (*xargs)(xdrs, argsp))) {
158 return (RPC_CANTENCODEARGS);
159 }
160 (void)XDR_GETPOS(xdrs); /* called just to cause overhead */
161
162 /*
163 * We have to call server input routine here because this is
164 * all going on in one process. Yuk.
165 */
166 svc_getreq(1);
167
168 /*
169 * get results
170 */
171 xdrs->x_op = XDR_DECODE;
172 XDR_SETPOS(xdrs, 0);
173 msg.acpted_rply.ar_verf = _null_auth;
174 msg.acpted_rply.ar_results.where = resultsp;
175 msg.acpted_rply.ar_results.proc = xresults;
176 if (! xdr_replymsg(xdrs, &msg)) {
177 /* xdr_replymsg() may have left some things allocated */
178 int op = xdrs->x_op;
179 xdrs->x_op = XDR_FREE;
180 xdr_replymsg(xdrs, &msg);
181 xdrs->x_op = op;
182 return (RPC_CANTDECODERES);
183 }
184 _seterr_reply(&msg, &error);
185 status = error.re_status;
186
187 if (status == RPC_SUCCESS) {
188 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
189 status = RPC_AUTHERROR;
190 }
191 } /* end successful completion */
192 else {
193 if (AUTH_REFRESH(h->cl_auth))
194 goto call_again;
195 } /* end of unsuccessful completion */
196
197 if (status == RPC_SUCCESS) {
198 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
199 status = RPC_AUTHERROR;
200 }
201 if (msg.acpted_rply.ar_verf.oa_base != NULL) {
202 xdrs->x_op = XDR_FREE;
203 (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
204 }
205 }
206
207 return (status);
208 }
209
210 static void
clntraw_geterr(CLIENT * clnt,struct rpc_err * err)211 clntraw_geterr(CLIENT *clnt, struct rpc_err *err)
212 {
213 }
214
215 static bool_t
clntraw_freeres(CLIENT * cl,xdrproc_t xdr_res,caddr_t res_ptr)216 clntraw_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
217 {
218 struct clntraw_private *clp = clntraw_private;
219 XDR *xdrs;
220 bool_t rval;
221
222 if (clp == NULL) {
223 rval = (bool_t) RPC_FAILED;
224 return (rval);
225 }
226 xdrs = &clp->xdr_stream;
227 xdrs->x_op = XDR_FREE;
228 return ((*xdr_res)(xdrs, res_ptr));
229 }
230
231 static void
clntraw_abort(CLIENT * clnt)232 clntraw_abort(CLIENT *clnt)
233 {
234 }
235
236 static bool_t
clntraw_control(CLIENT * clnt,u_int i,void * v)237 clntraw_control(CLIENT *clnt, u_int i, void *v)
238 {
239 return (FALSE);
240 }
241
242 static void
clntraw_destroy(CLIENT * clnt)243 clntraw_destroy(CLIENT *clnt)
244 {
245 }
246