Libinfo-503.50.4.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.3 2002/02/19 20:36:22 epeyton 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 <stdio.h>
71 #include <stdlib.h>
72 #include <rpc/rpc.h>
73
74 extern bool_t xdr_opaque_auth();
75
76 #define MCALL_MSG_SIZE 24
77
78 /*
79 * This is the "network" we will be moving stuff over.
80 */
81 static struct clntraw_private {
82 CLIENT client_object;
83 XDR xdr_stream;
84 char _raw_buf[UDPMSGSIZE];
85 char mashl_callmsg[MCALL_MSG_SIZE];
86 u_int mcnt;
87 } *clntraw_private;
88
89 static enum clnt_stat clntraw_call();
90 static void clntraw_abort();
91 static void clntraw_geterr();
92 static bool_t clntraw_freeres();
93 static bool_t clntraw_control();
94 static void clntraw_destroy();
95
96 static struct clnt_ops client_ops = {
97 clntraw_call,
98 clntraw_abort,
99 clntraw_geterr,
100 clntraw_freeres,
101 clntraw_destroy,
102 clntraw_control
103 };
104
105 void svc_getreq();
106
107 /*
108 * Create a client handle for memory based rpc.
109 */
110 CLIENT *
111 clntraw_create(prog, vers)
112 #ifdef __LP64__
113 uint32_t prog;
114 uint32_t vers;
115 #else
116 u_long prog;
117 u_long vers;
118 #endif
119 {
120 register struct clntraw_private *clp = clntraw_private;
121 struct rpc_msg call_msg;
122
123 if (clp == 0) {
124 clp = (struct clntraw_private *)calloc(1, sizeof (*clp));
125 if (clp == 0)
126 return (0);
127 clntraw_private = clp;
128 }
129 XDR *xdrs = &clp->xdr_stream;
130 CLIENT *client = &clp->client_object;
131 /*
132 * pre-serialize the staic part of the call msg and stash it away
133 */
134 call_msg.rm_direction = CALL;
135 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
136 call_msg.rm_call.cb_prog = prog;
137 call_msg.rm_call.cb_vers = vers;
138 xdrmem_create(xdrs, clp->mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
139 if (! xdr_callhdr(xdrs, &call_msg)) {
140 perror("clnt_raw.c - Fatal header serialization error.");
141 }
142 clp->mcnt = XDR_GETPOS(xdrs);
143 XDR_DESTROY(xdrs);
144
145 /*
146 * Set xdrmem for client/server shared buffer
147 */
148 xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
149
150 /*
151 * create client handle
152 */
153 client->cl_ops = &client_ops;
154 client->cl_auth = authnone_create();
155 return (client);
156 }
157
158 static enum clnt_stat
159 clntraw_call(h, proc, xargs, argsp, xresults, resultsp, timeout)
160 CLIENT *h;
161 rpc_uint proc;
162 xdrproc_t xargs;
163 caddr_t argsp;
164 xdrproc_t xresults;
165 caddr_t resultsp;
166 struct timeval timeout;
167 {
168 register struct clntraw_private *clp = clntraw_private;
169 register XDR *xdrs = &clp->xdr_stream;
170 struct rpc_msg msg;
171 enum clnt_stat status;
172 struct rpc_err error;
173
174 if (clp == 0)
175 return (RPC_FAILED);
176 call_again:
177 /*
178 * send request
179 */
180 xdrs->x_op = XDR_ENCODE;
181 XDR_SETPOS(xdrs, 0);
182 ((struct rpc_msg *)clp->mashl_callmsg)->rm_xid ++ ;
183 #ifdef __LP64__
184 if ((! XDR_PUTBYTES(xdrs, clp->mashl_callmsg, clp->mcnt)) ||
185 (! XDR_PUTLONG(xdrs, (int *)&proc)) ||
186 (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
187 (! (*xargs)(xdrs, argsp, 0))) return (RPC_CANTENCODEARGS);
188 #else
189 if ((! XDR_PUTBYTES(xdrs, clp->mashl_callmsg, clp->mcnt)) ||
190 (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
191 (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
192 (! (*xargs)(xdrs, argsp, 0))) return (RPC_CANTENCODEARGS);
193 #endif
194 (void)XDR_GETPOS(xdrs); /* called just to cause overhead */
195
196 /*
197 * We have to call server input routine here because this is
198 * all going on in one process. Yuk.
199 */
200 svc_getreq(1);
201
202 /*
203 * get results
204 */
205 xdrs->x_op = XDR_DECODE;
206 XDR_SETPOS(xdrs, 0);
207 msg.acpted_rply.ar_verf = _null_auth;
208 msg.acpted_rply.ar_results.where = resultsp;
209 msg.acpted_rply.ar_results.proc = xresults;
210 if (! xdr_replymsg(xdrs, &msg))
211 return (RPC_CANTDECODERES);
212 _seterr_reply(&msg, &error);
213 status = error.re_status;
214
215 if (status == RPC_SUCCESS) {
216 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
217 status = RPC_AUTHERROR;
218 }
219 } /* end successful completion */
220 else {
221 if (AUTH_REFRESH(h->cl_auth))
222 goto call_again;
223 } /* end of unsuccessful completion */
224
225 if (status == RPC_SUCCESS) {
226 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
227 status = RPC_AUTHERROR;
228 }
229 if (msg.acpted_rply.ar_verf.oa_base != NULL) {
230 xdrs->x_op = XDR_FREE;
231 (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
232 }
233 }
234
235 return (status);
236 }
237
238 static void
239 clntraw_geterr()
240 {
241 }
242
243
244 static bool_t
245 clntraw_freeres(cl, xdr_res, res_ptr)
246 CLIENT *cl;
247 xdrproc_t xdr_res;
248 caddr_t res_ptr;
249 {
250 register struct clntraw_private *clp = clntraw_private;
251 register XDR *xdrs = &clp->xdr_stream;
252 bool_t rval;
253
254 if (clp == 0)
255 {
256 rval = (bool_t) RPC_FAILED;
257 return (rval);
258 }
259 xdrs->x_op = XDR_FREE;
260 return ((*xdr_res)(xdrs, res_ptr, 0));
261 }
262
263 static void
264 clntraw_abort()
265 {
266 }
267
268 static bool_t
269 clntraw_control()
270 {
271 return (FALSE);
272 }
273
274 static void
275 clntraw_destroy()
276 {
277 }