clnt_raw.c revision 1.8 1 /* $NetBSD: clnt_raw.c,v 1.8 1998/02/11 07:50:07 lukem Exp $ */
2
3 /*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char *sccsid = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)clnt_raw.c 2.2 88/08/01 4.0 RPCSRC";
37 #else
38 __RCSID("$NetBSD: clnt_raw.c,v 1.8 1998/02/11 07:50:07 lukem Exp $");
39 #endif
40 #endif
41
42 /*
43 * clnt_raw.c
44 *
45 * Copyright (C) 1984, Sun Microsystems, Inc.
46 *
47 * Memory based rpc for simple testing and timing.
48 * Interface to create an rpc client and server in the same process.
49 * This lets us similate rpc and get round trip overhead, without
50 * any interference from the kernal.
51 */
52
53 #include "namespace.h"
54
55 #include <stdio.h>
56 #include <stdlib.h>
57
58 #include <rpc/rpc.h>
59
60 #ifdef __weak_alias
61 __weak_alias(clntraw_create,_clntraw_create);
62 #endif
63
64 #define MCALL_MSG_SIZE 24
65
66 /*
67 * This is the "network" we will be moving stuff over.
68 */
69 static struct clntraw_private {
70 CLIENT client_object;
71 XDR xdr_stream;
72 char _raw_buf[UDPMSGSIZE];
73 char mashl_callmsg[MCALL_MSG_SIZE];
74 size_t mcnt;
75 } *clntraw_private;
76
77
78
79 static enum clnt_stat clntraw_call __P((CLIENT *, u_int32_t, xdrproc_t,
80 caddr_t, xdrproc_t, caddr_t, struct timeval));
81 static void clntraw_geterr __P((CLIENT *, struct rpc_err *));
82 static bool_t clntraw_freeres __P((CLIENT *, xdrproc_t, caddr_t));
83 static void clntraw_abort __P((CLIENT *));
84 static bool_t clntraw_control __P((CLIENT *, u_int, caddr_t));
85 static void clntraw_destroy __P((CLIENT *));
86
87 static struct clnt_ops client_ops = {
88 clntraw_call,
89 clntraw_abort,
90 clntraw_geterr,
91 clntraw_freeres,
92 clntraw_destroy,
93 clntraw_control
94 };
95
96 /*
97 * Create a client handle for memory based rpc.
98 */
99 CLIENT *
100 clntraw_create(prog, vers)
101 u_int32_t prog;
102 u_int32_t vers;
103 {
104 struct clntraw_private *clp = clntraw_private;
105 struct rpc_msg call_msg;
106 XDR *xdrs = &clp->xdr_stream;
107 CLIENT *client = &clp->client_object;
108
109 if (clp == NULL) {
110 clp = (struct clntraw_private *)calloc(1, sizeof (*clp));
111 if (clp == NULL)
112 return (NULL);
113 clntraw_private = clp;
114 }
115 /*
116 * pre-serialize the staic part of the call msg and stash it away
117 */
118 call_msg.rm_direction = CALL;
119 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
120 call_msg.rm_call.cb_prog = prog;
121 call_msg.rm_call.cb_vers = vers;
122 xdrmem_create(xdrs, clp->mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
123 if (! xdr_callhdr(xdrs, &call_msg)) {
124 perror("clnt_raw.c - Fatal header serialization error.");
125 }
126 clp->mcnt = XDR_GETPOS(xdrs);
127 XDR_DESTROY(xdrs);
128
129 /*
130 * Set xdrmem for client/server shared buffer
131 */
132 xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
133
134 /*
135 * create client handle
136 */
137 client->cl_ops = &client_ops;
138 client->cl_auth = authnone_create();
139 return (client);
140 }
141
142 static enum clnt_stat
143 clntraw_call(h, proc, xargs, argsp, xresults, resultsp, timeout)
144 CLIENT *h;
145 u_int32_t proc;
146 xdrproc_t xargs;
147 caddr_t argsp;
148 xdrproc_t xresults;
149 caddr_t resultsp;
150 struct timeval timeout;
151 {
152 struct clntraw_private *clp = clntraw_private;
153 XDR *xdrs = &clp->xdr_stream;
154 struct rpc_msg msg;
155 enum clnt_stat status;
156 struct rpc_err error;
157
158 if (clp == NULL)
159 return (RPC_FAILED);
160 call_again:
161 /*
162 * send request
163 */
164 xdrs->x_op = XDR_ENCODE;
165 XDR_SETPOS(xdrs, 0);
166 ((struct rpc_msg *)clp->mashl_callmsg)->rm_xid ++ ;
167 if ((! XDR_PUTBYTES(xdrs, clp->mashl_callmsg, clp->mcnt)) ||
168 (! XDR_PUTLONG(xdrs, &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(1);
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))
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 clntraw_geterr(cl, err)
235 CLIENT *cl;
236 struct rpc_err *err;
237 {
238 }
239
240
241 static bool_t
242 clntraw_freeres(cl, xdr_res, res_ptr)
243 CLIENT *cl;
244 xdrproc_t xdr_res;
245 caddr_t res_ptr;
246 {
247 struct clntraw_private *clp = clntraw_private;
248 XDR *xdrs = &clp->xdr_stream;
249 bool_t rval;
250
251 if (clp == NULL) {
252 rval = (bool_t) RPC_FAILED;
253 return (rval);
254 }
255 xdrs->x_op = XDR_FREE;
256 return ((*xdr_res)(xdrs, res_ptr));
257 }
258
259 /*ARGSUSED*/
260 static void
261 clntraw_abort(cl)
262 CLIENT *cl;
263 {
264 }
265
266 /*ARGSUSED*/
267 static bool_t
268 clntraw_control(cl, ui, str)
269 CLIENT *cl;
270 u_int ui;
271 caddr_t str;
272 {
273 return (FALSE);
274 }
275
276 /*ARGSUSED*/
277 static void
278 clntraw_destroy(cl)
279 CLIENT *cl;
280 {
281 }
282