Libinfo-78.tar.gz
[apple/libinfo.git] / rpc.subproj / clnt_raw.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
26 * unrestricted use provided that this legend is included on all tape
27 * media and as a part of the software program in whole or part. Users
28 * may copy or modify Sun RPC without charge, but are not authorized
29 * to license or distribute it to anyone else except as part of a product or
30 * program developed by the user.
31 *
32 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
33 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
35 *
36 * Sun RPC is provided with no support and without any obligation on the
37 * part of Sun Microsystems, Inc. to assist in its use, correction,
38 * modification or enhancement.
39 *
40 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
41 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
42 * OR ANY PART THEREOF.
43 *
44 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
45 * or profits or other special, indirect and consequential damages, even if
46 * Sun has been advised of the possibility of such damages.
47 *
48 * Sun Microsystems, Inc.
49 * 2550 Garcia Avenue
50 * Mountain View, California 94043
51 */
52
53 #if defined(LIBC_SCCS) && !defined(lint)
54 /*static char *sccsid = "from: @(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro";*/
55 /*static char *sccsid = "from: @(#)clnt_raw.c 2.2 88/08/01 4.0 RPCSRC";*/
56 static char *rcsid = "$Id: clnt_raw.c,v 1.2 1999/10/14 21:56:53 wsanchez Exp $";
57 #endif
58
59 /*
60 * clnt_raw.c
61 *
62 * Copyright (C) 1984, Sun Microsystems, Inc.
63 *
64 * Memory based rpc for simple testing and timing.
65 * Interface to create an rpc client and server in the same process.
66 * This lets us similate rpc and get round trip overhead, without
67 * any interference from the kernal.
68 */
69
70 #include <rpc/rpc.h>
71
72 #define MCALL_MSG_SIZE 24
73
74 /*
75 * This is the "network" we will be moving stuff over.
76 */
77 static struct clntraw_private {
78 CLIENT client_object;
79 XDR xdr_stream;
80 char _raw_buf[UDPMSGSIZE];
81 char mashl_callmsg[MCALL_MSG_SIZE];
82 u_int mcnt;
83 } *clntraw_private;
84
85 static enum clnt_stat clntraw_call();
86 static void clntraw_abort();
87 static void clntraw_geterr();
88 static bool_t clntraw_freeres();
89 static bool_t clntraw_control();
90 static void clntraw_destroy();
91
92 static struct clnt_ops client_ops = {
93 clntraw_call,
94 clntraw_abort,
95 clntraw_geterr,
96 clntraw_freeres,
97 clntraw_destroy,
98 clntraw_control
99 };
100
101 void svc_getreq();
102
103 /*
104 * Create a client handle for memory based rpc.
105 */
106 CLIENT *
107 clntraw_create(prog, vers)
108 u_long prog;
109 u_long vers;
110 {
111 register struct clntraw_private *clp = clntraw_private;
112 struct rpc_msg call_msg;
113 XDR *xdrs = &clp->xdr_stream;
114 CLIENT *client = &clp->client_object;
115
116 if (clp == 0) {
117 clp = (struct clntraw_private *)calloc(1, sizeof (*clp));
118 if (clp == 0)
119 return (0);
120 clntraw_private = clp;
121 }
122 /*
123 * pre-serialize the staic part of the call msg and stash it away
124 */
125 call_msg.rm_direction = CALL;
126 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
127 call_msg.rm_call.cb_prog = prog;
128 call_msg.rm_call.cb_vers = vers;
129 xdrmem_create(xdrs, clp->mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
130 if (! xdr_callhdr(xdrs, &call_msg)) {
131 perror("clnt_raw.c - Fatal header serialization error.");
132 }
133 clp->mcnt = XDR_GETPOS(xdrs);
134 XDR_DESTROY(xdrs);
135
136 /*
137 * Set xdrmem for client/server shared buffer
138 */
139 xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
140
141 /*
142 * create client handle
143 */
144 client->cl_ops = &client_ops;
145 client->cl_auth = authnone_create();
146 return (client);
147 }
148
149 static enum clnt_stat
150 clntraw_call(h, proc, xargs, argsp, xresults, resultsp, timeout)
151 CLIENT *h;
152 u_long proc;
153 xdrproc_t xargs;
154 caddr_t argsp;
155 xdrproc_t xresults;
156 caddr_t resultsp;
157 struct timeval timeout;
158 {
159 register struct clntraw_private *clp = clntraw_private;
160 register XDR *xdrs = &clp->xdr_stream;
161 struct rpc_msg msg;
162 enum clnt_stat status;
163 struct rpc_err error;
164
165 if (clp == 0)
166 return (RPC_FAILED);
167 call_again:
168 /*
169 * send request
170 */
171 xdrs->x_op = XDR_ENCODE;
172 XDR_SETPOS(xdrs, 0);
173 ((struct rpc_msg *)clp->mashl_callmsg)->rm_xid ++ ;
174 if ((! XDR_PUTBYTES(xdrs, clp->mashl_callmsg, clp->mcnt)) ||
175 (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
176 (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
177 (! (*xargs)(xdrs, argsp))) {
178 return (RPC_CANTENCODEARGS);
179 }
180 (void)XDR_GETPOS(xdrs); /* called just to cause overhead */
181
182 /*
183 * We have to call server input routine here because this is
184 * all going on in one process. Yuk.
185 */
186 svc_getreq(1);
187
188 /*
189 * get results
190 */
191 xdrs->x_op = XDR_DECODE;
192 XDR_SETPOS(xdrs, 0);
193 msg.acpted_rply.ar_verf = _null_auth;
194 msg.acpted_rply.ar_results.where = resultsp;
195 msg.acpted_rply.ar_results.proc = xresults;
196 if (! xdr_replymsg(xdrs, &msg))
197 return (RPC_CANTDECODERES);
198 _seterr_reply(&msg, &error);
199 status = error.re_status;
200
201 if (status == RPC_SUCCESS) {
202 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
203 status = RPC_AUTHERROR;
204 }
205 } /* end successful completion */
206 else {
207 if (AUTH_REFRESH(h->cl_auth))
208 goto call_again;
209 } /* end of unsuccessful completion */
210
211 if (status == RPC_SUCCESS) {
212 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
213 status = RPC_AUTHERROR;
214 }
215 if (msg.acpted_rply.ar_verf.oa_base != NULL) {
216 xdrs->x_op = XDR_FREE;
217 (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
218 }
219 }
220
221 return (status);
222 }
223
224 static void
225 clntraw_geterr()
226 {
227 }
228
229
230 static bool_t
231 clntraw_freeres(cl, xdr_res, res_ptr)
232 CLIENT *cl;
233 xdrproc_t xdr_res;
234 caddr_t res_ptr;
235 {
236 register struct clntraw_private *clp = clntraw_private;
237 register XDR *xdrs = &clp->xdr_stream;
238 bool_t rval;
239
240 if (clp == 0)
241 {
242 rval = (bool_t) RPC_FAILED;
243 return (rval);
244 }
245 xdrs->x_op = XDR_FREE;
246 return ((*xdr_res)(xdrs, res_ptr));
247 }
248
249 static void
250 clntraw_abort()
251 {
252 }
253
254 static bool_t
255 clntraw_control()
256 {
257 return (FALSE);
258 }
259
260 static void
261 clntraw_destroy()
262 {
263 }