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