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