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