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