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