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