Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/clnt_raw.c

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