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