clnt_raw.c revision 1.29.8.2 1 1.29.8.2 christos /* $NetBSD: clnt_raw.c,v 1.29.8.2 2008/04/25 17:44:45 christos Exp $ */
2 1.29.8.2 christos
3 1.29.8.2 christos /*
4 1.29.8.2 christos * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 1.29.8.2 christos * unrestricted use provided that this legend is included on all tape
6 1.29.8.2 christos * media and as a part of the software program in whole or part. Users
7 1.29.8.2 christos * may copy or modify Sun RPC without charge, but are not authorized
8 1.29.8.2 christos * to license or distribute it to anyone else except as part of a product or
9 1.29.8.2 christos * program developed by the user.
10 1.29.8.2 christos *
11 1.29.8.2 christos * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 1.29.8.2 christos * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 1.29.8.2 christos * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 1.29.8.2 christos *
15 1.29.8.2 christos * Sun RPC is provided with no support and without any obligation on the
16 1.29.8.2 christos * part of Sun Microsystems, Inc. to assist in its use, correction,
17 1.29.8.2 christos * modification or enhancement.
18 1.29.8.2 christos *
19 1.29.8.2 christos * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 1.29.8.2 christos * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 1.29.8.2 christos * OR ANY PART THEREOF.
22 1.29.8.2 christos *
23 1.29.8.2 christos * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 1.29.8.2 christos * or profits or other special, indirect and consequential damages, even if
25 1.29.8.2 christos * Sun has been advised of the possibility of such damages.
26 1.29.8.2 christos *
27 1.29.8.2 christos * Sun Microsystems, Inc.
28 1.29.8.2 christos * 2550 Garcia Avenue
29 1.29.8.2 christos * Mountain View, California 94043
30 1.29.8.2 christos */
31 1.29.8.2 christos
32 1.29.8.2 christos #include <sys/cdefs.h>
33 1.29.8.2 christos #if defined(LIBC_SCCS) && !defined(lint)
34 1.29.8.2 christos #if 0
35 1.29.8.2 christos static char *sccsid = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro";
36 1.29.8.2 christos static char *sccsid = "@(#)clnt_raw.c 2.2 88/08/01 4.0 RPCSRC";
37 1.29.8.2 christos #else
38 1.29.8.2 christos __RCSID("$NetBSD: clnt_raw.c,v 1.29.8.2 2008/04/25 17:44:45 christos Exp $");
39 1.29.8.2 christos #endif
40 1.29.8.2 christos #endif
41 1.29.8.2 christos
42 1.29.8.2 christos /*
43 1.29.8.2 christos * clnt_raw.c
44 1.29.8.2 christos *
45 1.29.8.2 christos * Copyright (C) 1984, Sun Microsystems, Inc.
46 1.29.8.2 christos *
47 1.29.8.2 christos * Memory based rpc for simple testing and timing.
48 1.29.8.2 christos * Interface to create an rpc client and server in the same process.
49 1.29.8.2 christos * This lets us similate rpc and get round trip overhead, without
50 1.29.8.2 christos * any interference from the kernel.
51 1.29.8.2 christos */
52 1.29.8.2 christos
53 1.29.8.2 christos #include "namespace.h"
54 1.29.8.2 christos #include "reentrant.h"
55 1.29.8.2 christos #include <assert.h>
56 1.29.8.2 christos #include <err.h>
57 1.29.8.2 christos #include <stdio.h>
58 1.29.8.2 christos #include <stdlib.h>
59 1.29.8.2 christos
60 1.29.8.2 christos #include <rpc/rpc.h>
61 1.29.8.2 christos #include <rpc/raw.h>
62 1.29.8.2 christos
63 1.29.8.2 christos #ifdef __weak_alias
64 1.29.8.2 christos __weak_alias(clntraw_create,_clntraw_create)
65 1.29.8.2 christos __weak_alias(clnt_raw_create,_clnt_raw_create)
66 1.29.8.2 christos #endif
67 1.29.8.2 christos
68 1.29.8.2 christos #ifdef _REENTRANT
69 1.29.8.2 christos extern mutex_t clntraw_lock;
70 1.29.8.2 christos #endif
71 1.29.8.2 christos
72 1.29.8.2 christos #define MCALL_MSG_SIZE 24
73 1.29.8.2 christos
74 1.29.8.2 christos /*
75 1.29.8.2 christos * This is the "network" we will be moving stuff over.
76 1.29.8.2 christos */
77 1.29.8.2 christos static struct clntraw_private {
78 1.29.8.2 christos CLIENT client_object;
79 1.29.8.2 christos XDR xdr_stream;
80 1.29.8.2 christos char *_raw_buf;
81 1.29.8.2 christos union {
82 1.29.8.2 christos struct rpc_msg mashl_rpcmsg;
83 1.29.8.2 christos char mashl_callmsg[MCALL_MSG_SIZE];
84 1.29.8.2 christos } u;
85 1.29.8.2 christos u_int mcnt;
86 1.29.8.2 christos } *clntraw_private;
87 1.29.8.2 christos
88 1.29.8.2 christos static enum clnt_stat clnt_raw_call __P((CLIENT *, rpcproc_t, xdrproc_t,
89 1.29.8.2 christos const char *, xdrproc_t, caddr_t, struct timeval));
90 1.29.8.2 christos static void clnt_raw_geterr __P((CLIENT *, struct rpc_err *));
91 1.29.8.2 christos static bool_t clnt_raw_freeres __P((CLIENT *, xdrproc_t, caddr_t));
92 1.29.8.2 christos static void clnt_raw_abort __P((CLIENT *));
93 1.29.8.2 christos static bool_t clnt_raw_control __P((CLIENT *, u_int, char *));
94 1.29.8.2 christos static void clnt_raw_destroy __P((CLIENT *));
95 1.29.8.2 christos static struct clnt_ops *clnt_raw_ops __P((void));
96 1.29.8.2 christos
97 1.29.8.2 christos /*
98 1.29.8.2 christos * Create a client handle for memory based rpc.
99 1.29.8.2 christos */
100 1.29.8.2 christos CLIENT *
101 1.29.8.2 christos clnt_raw_create(prog, vers)
102 1.29.8.2 christos rpcprog_t prog;
103 1.29.8.2 christos rpcvers_t vers;
104 1.29.8.2 christos {
105 1.29.8.2 christos struct clntraw_private *clp = clntraw_private;
106 1.29.8.2 christos struct rpc_msg call_msg;
107 1.29.8.2 christos XDR *xdrs = &clp->xdr_stream;
108 1.29.8.2 christos CLIENT *client = &clp->client_object;
109 1.29.8.2 christos
110 1.29.8.2 christos mutex_lock(&clntraw_lock);
111 1.29.8.2 christos if (clp == NULL) {
112 1.29.8.2 christos clp = calloc((size_t)1, sizeof (*clp));
113 1.29.8.2 christos if (clp == NULL)
114 1.29.8.2 christos goto out;
115 1.29.8.2 christos if (__rpc_rawcombuf == NULL)
116 1.29.8.2 christos __rpc_rawcombuf =
117 1.29.8.2 christos malloc(UDPMSGSIZE);
118 1.29.8.2 christos if (__rpc_rawcombuf == NULL)
119 1.29.8.2 christos goto out;
120 1.29.8.2 christos clp->_raw_buf = __rpc_rawcombuf;
121 1.29.8.2 christos clntraw_private = clp;
122 1.29.8.2 christos }
123 1.29.8.2 christos /*
124 1.29.8.2 christos * pre-serialize the static part of the call msg and stash it away
125 1.29.8.2 christos */
126 1.29.8.2 christos call_msg.rm_direction = CALL;
127 1.29.8.2 christos call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
128 1.29.8.2 christos /* XXX: prog and vers have been long historically :-( */
129 1.29.8.2 christos call_msg.rm_call.cb_prog = (u_int32_t)prog;
130 1.29.8.2 christos call_msg.rm_call.cb_vers = (u_int32_t)vers;
131 1.29.8.2 christos xdrmem_create(xdrs, clp->u.mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
132 1.29.8.2 christos if (! xdr_callhdr(xdrs, &call_msg))
133 1.29.8.2 christos warnx("clntraw_create - Fatal header serialization error.");
134 1.29.8.2 christos clp->mcnt = XDR_GETPOS(xdrs);
135 1.29.8.2 christos XDR_DESTROY(xdrs);
136 1.29.8.2 christos
137 1.29.8.2 christos /*
138 1.29.8.2 christos * Set xdrmem for client/server shared buffer
139 1.29.8.2 christos */
140 1.29.8.2 christos xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
141 1.29.8.2 christos
142 1.29.8.2 christos /*
143 1.29.8.2 christos * create client handle
144 1.29.8.2 christos */
145 1.29.8.2 christos client->cl_ops = clnt_raw_ops();
146 1.29.8.2 christos client->cl_auth = authnone_create();
147 1.29.8.2 christos mutex_unlock(&clntraw_lock);
148 1.29.8.2 christos return (client);
149 1.29.8.2 christos out:
150 1.29.8.2 christos if (clp)
151 1.29.8.2 christos free(clp);
152 1.29.8.2 christos mutex_unlock(&clntraw_lock);
153 1.29.8.2 christos return NULL;
154 1.29.8.2 christos
155 1.29.8.2 christos }
156 1.29.8.2 christos
157 1.29.8.2 christos /* ARGSUSED */
158 1.29.8.2 christos static enum clnt_stat
159 1.29.8.2 christos clnt_raw_call(h, proc, xargs, argsp, xresults, resultsp, timeout)
160 1.29.8.2 christos CLIENT *h;
161 1.29.8.2 christos rpcproc_t proc;
162 1.29.8.2 christos xdrproc_t xargs;
163 1.29.8.2 christos const char *argsp;
164 1.29.8.2 christos xdrproc_t xresults;
165 1.29.8.2 christos caddr_t resultsp;
166 1.29.8.2 christos struct timeval timeout;
167 1.29.8.2 christos {
168 1.29.8.2 christos struct clntraw_private *clp = clntraw_private;
169 1.29.8.2 christos XDR *xdrs = &clp->xdr_stream;
170 1.29.8.2 christos struct rpc_msg msg;
171 1.29.8.2 christos enum clnt_stat status;
172 1.29.8.2 christos struct rpc_err error;
173 1.29.8.2 christos
174 1.29.8.2 christos _DIAGASSERT(h != NULL);
175 1.29.8.2 christos
176 1.29.8.2 christos mutex_lock(&clntraw_lock);
177 1.29.8.2 christos if (clp == NULL) {
178 1.29.8.2 christos mutex_unlock(&clntraw_lock);
179 1.29.8.2 christos return (RPC_FAILED);
180 1.29.8.2 christos }
181 1.29.8.2 christos mutex_unlock(&clntraw_lock);
182 1.29.8.2 christos
183 1.29.8.2 christos call_again:
184 1.29.8.2 christos /*
185 1.29.8.2 christos * send request
186 1.29.8.2 christos */
187 1.29.8.2 christos xdrs->x_op = XDR_ENCODE;
188 1.29.8.2 christos XDR_SETPOS(xdrs, 0);
189 1.29.8.2 christos clp->u.mashl_rpcmsg.rm_xid ++ ;
190 1.29.8.2 christos if ((! XDR_PUTBYTES(xdrs, clp->u.mashl_callmsg, clp->mcnt)) ||
191 1.29.8.2 christos (! XDR_PUTINT32(xdrs, (int32_t *)&proc)) ||
192 1.29.8.2 christos (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
193 1.29.8.2 christos (! (*xargs)(xdrs, __UNCONST(argsp)))) {
194 1.29.8.2 christos return (RPC_CANTENCODEARGS);
195 1.29.8.2 christos }
196 1.29.8.2 christos (void)XDR_GETPOS(xdrs); /* called just to cause overhead */
197 1.29.8.2 christos
198 1.29.8.2 christos /*
199 1.29.8.2 christos * We have to call server input routine here because this is
200 1.29.8.2 christos * all going on in one process. Yuk.
201 1.29.8.2 christos */
202 1.29.8.2 christos svc_getreq_common(FD_SETSIZE);
203 1.29.8.2 christos
204 1.29.8.2 christos /*
205 1.29.8.2 christos * get results
206 1.29.8.2 christos */
207 1.29.8.2 christos xdrs->x_op = XDR_DECODE;
208 1.29.8.2 christos XDR_SETPOS(xdrs, 0);
209 1.29.8.2 christos msg.acpted_rply.ar_verf = _null_auth;
210 1.29.8.2 christos msg.acpted_rply.ar_results.where = resultsp;
211 1.29.8.2 christos msg.acpted_rply.ar_results.proc = xresults;
212 1.29.8.2 christos if (! xdr_replymsg(xdrs, &msg)) {
213 1.29.8.2 christos /*
214 1.29.8.2 christos * It's possible for xdr_replymsg() to fail partway
215 1.29.8.2 christos * through its attempt to decode the result from the
216 1.29.8.2 christos * server. If this happens, it will leave the reply
217 1.29.8.2 christos * structure partially populated with dynamically
218 1.29.8.2 christos * allocated memory. (This can happen if someone uses
219 1.29.8.2 christos * clntudp_bufcreate() to create a CLIENT handle and
220 1.29.8.2 christos * specifies a receive buffer size that is too small.)
221 1.29.8.2 christos * This memory must be free()ed to avoid a leak.
222 1.29.8.2 christos */
223 1.29.8.2 christos int op = xdrs->x_op;
224 1.29.8.2 christos xdrs->x_op = XDR_FREE;
225 1.29.8.2 christos xdr_replymsg(xdrs, &msg);
226 1.29.8.2 christos xdrs->x_op = op;
227 1.29.8.2 christos return (RPC_CANTDECODERES);
228 1.29.8.2 christos }
229 1.29.8.2 christos _seterr_reply(&msg, &error);
230 1.29.8.2 christos status = error.re_status;
231 1.29.8.2 christos
232 1.29.8.2 christos if (status == RPC_SUCCESS) {
233 1.29.8.2 christos if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
234 1.29.8.2 christos status = RPC_AUTHERROR;
235 1.29.8.2 christos }
236 1.29.8.2 christos } /* end successful completion */
237 1.29.8.2 christos else {
238 1.29.8.2 christos if (AUTH_REFRESH(h->cl_auth))
239 1.29.8.2 christos goto call_again;
240 1.29.8.2 christos } /* end of unsuccessful completion */
241 1.29.8.2 christos
242 1.29.8.2 christos if (status == RPC_SUCCESS) {
243 1.29.8.2 christos if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
244 1.29.8.2 christos status = RPC_AUTHERROR;
245 1.29.8.2 christos }
246 1.29.8.2 christos if (msg.acpted_rply.ar_verf.oa_base != NULL) {
247 1.29.8.2 christos xdrs->x_op = XDR_FREE;
248 1.29.8.2 christos (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
249 1.29.8.2 christos }
250 1.29.8.2 christos }
251 1.29.8.2 christos
252 1.29.8.2 christos return (status);
253 1.29.8.2 christos }
254 1.29.8.2 christos
255 1.29.8.2 christos /*ARGSUSED*/
256 1.29.8.2 christos static void
257 1.29.8.2 christos clnt_raw_geterr(cl, error)
258 1.29.8.2 christos CLIENT *cl;
259 1.29.8.2 christos struct rpc_err *error;
260 1.29.8.2 christos {
261 1.29.8.2 christos }
262 1.29.8.2 christos
263 1.29.8.2 christos
264 1.29.8.2 christos /* ARGSUSED */
265 1.29.8.2 christos static bool_t
266 1.29.8.2 christos clnt_raw_freeres(cl, xdr_res, res_ptr)
267 1.29.8.2 christos CLIENT *cl;
268 1.29.8.2 christos xdrproc_t xdr_res;
269 1.29.8.2 christos caddr_t res_ptr;
270 1.29.8.2 christos {
271 1.29.8.2 christos struct clntraw_private *clp = clntraw_private;
272 1.29.8.2 christos XDR *xdrs = &clp->xdr_stream;
273 1.29.8.2 christos bool_t rval;
274 1.29.8.2 christos
275 1.29.8.2 christos mutex_lock(&clntraw_lock);
276 1.29.8.2 christos if (clp == NULL) {
277 1.29.8.2 christos rval = (bool_t) RPC_FAILED;
278 1.29.8.2 christos mutex_unlock(&clntraw_lock);
279 1.29.8.2 christos return (rval);
280 1.29.8.2 christos }
281 1.29.8.2 christos mutex_unlock(&clntraw_lock);
282 1.29.8.2 christos xdrs->x_op = XDR_FREE;
283 1.29.8.2 christos return ((*xdr_res)(xdrs, res_ptr));
284 1.29.8.2 christos }
285 1.29.8.2 christos
286 1.29.8.2 christos /*ARGSUSED*/
287 1.29.8.2 christos static void
288 1.29.8.2 christos clnt_raw_abort(cl)
289 1.29.8.2 christos CLIENT *cl;
290 1.29.8.2 christos {
291 1.29.8.2 christos }
292 1.29.8.2 christos
293 1.29.8.2 christos /*ARGSUSED*/
294 1.29.8.2 christos static bool_t
295 1.29.8.2 christos clnt_raw_control(cl, ui, str)
296 1.29.8.2 christos CLIENT *cl;
297 1.29.8.2 christos u_int ui;
298 1.29.8.2 christos char *str;
299 1.29.8.2 christos {
300 1.29.8.2 christos return (FALSE);
301 1.29.8.2 christos }
302 1.29.8.2 christos
303 1.29.8.2 christos /*ARGSUSED*/
304 1.29.8.2 christos static void
305 1.29.8.2 christos clnt_raw_destroy(cl)
306 1.29.8.2 christos CLIENT *cl;
307 1.29.8.2 christos {
308 1.29.8.2 christos }
309 1.29.8.2 christos
310 1.29.8.2 christos static struct clnt_ops *
311 1.29.8.2 christos clnt_raw_ops()
312 1.29.8.2 christos {
313 1.29.8.2 christos static struct clnt_ops ops;
314 1.29.8.2 christos #ifdef _REENTRANT
315 1.29.8.2 christos extern mutex_t ops_lock;
316 1.29.8.2 christos #endif
317 1.29.8.2 christos
318 1.29.8.2 christos /* VARIABLES PROTECTED BY ops_lock: ops */
319 1.29.8.2 christos
320 1.29.8.2 christos mutex_lock(&ops_lock);
321 1.29.8.2 christos if (ops.cl_call == NULL) {
322 1.29.8.2 christos ops.cl_call = clnt_raw_call;
323 1.29.8.2 christos ops.cl_abort = clnt_raw_abort;
324 1.29.8.2 christos ops.cl_geterr = clnt_raw_geterr;
325 1.29.8.2 christos ops.cl_freeres = clnt_raw_freeres;
326 1.29.8.2 christos ops.cl_destroy = clnt_raw_destroy;
327 1.29.8.2 christos ops.cl_control = clnt_raw_control;
328 1.29.8.2 christos }
329 1.29.8.2 christos mutex_unlock(&ops_lock);
330 1.29.8.2 christos return (&ops);
331 1.29.8.2 christos }
332