OSDN Git Service

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