Merge from vendor branch OPENSSH:
[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  * @(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro
30  * @(#)clnt_raw.c       2.2 88/08/01 4.0 RPCSRC
31  * $FreeBSD: src/lib/libc/rpc/clnt_raw.c,v 1.10 1999/08/28 00:00:36 peter Exp $
32  * $DragonFly: src/lib/libc/rpc/clnt_raw.c,v 1.4 2005/11/13 12:27:04 swildner Exp $
33  */
34
35 /*
36  * clnt_raw.c
37  *
38  * Copyright (C) 1984, Sun Microsystems, Inc.
39  *
40  * Memory based rpc for simple testing and timing.
41  * Interface to create an rpc client and server in the same process.
42  * This lets us similate rpc and get round trip overhead, without
43  * any interference from the kernal.
44  */
45
46 #include <rpc/rpc.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49
50 #define MCALL_MSG_SIZE 24
51
52 /*
53  * This is the "network" we will be moving stuff over.
54  */
55 static struct clntraw_private {
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 } *clntraw_private;
62
63 static enum clnt_stat   clntraw_call();
64 static void             clntraw_abort();
65 static void             clntraw_geterr();
66 static bool_t           clntraw_freeres();
67 static bool_t           clntraw_control();
68 static void             clntraw_destroy();
69
70 static struct clnt_ops client_ops = {
71         clntraw_call,
72         clntraw_abort,
73         clntraw_geterr,
74         clntraw_freeres,
75         clntraw_destroy,
76         clntraw_control
77 };
78
79 void    svc_getreq();
80
81 /*
82  * Create a client handle for memory based rpc.
83  */
84 CLIENT *
85 clntraw_create(u_long prog, u_long vers)
86 {
87         struct clntraw_private *clp = clntraw_private;
88         struct rpc_msg call_msg;
89         XDR *xdrs = &clp->xdr_stream;
90         CLIENT  *client = &clp->client_object;
91
92         if (clp == 0) {
93                 clp = (struct clntraw_private *)calloc(1, sizeof (*clp));
94                 if (clp == 0)
95                         return (0);
96                 clntraw_private = clp;
97         }
98         /*
99          * pre-serialize the static part of the call msg and stash it away
100          */
101         call_msg.rm_direction = CALL;
102         call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
103         call_msg.rm_call.cb_prog = prog;
104         call_msg.rm_call.cb_vers = vers;
105         xdrmem_create(xdrs, clp->mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE);
106         if (! xdr_callhdr(xdrs, &call_msg)) {
107                 perror("clnt_raw.c - Fatal header serialization error.");
108         }
109         clp->mcnt = XDR_GETPOS(xdrs);
110         XDR_DESTROY(xdrs);
111
112         /*
113          * Set xdrmem for client/server shared buffer
114          */
115         xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
116
117         /*
118          * create client handle
119          */
120         client->cl_ops = &client_ops;
121         client->cl_auth = authnone_create();
122         return (client);
123 }
124
125 static enum clnt_stat
126 clntraw_call(CLIENT *h, u_long proc, xdrproc_t xargs, caddr_t argsp,
127              xdrproc_t xresults, caddr_t resultsp, struct timeval timeout)
128 {
129         struct clntraw_private *clp = clntraw_private;
130         XDR *xdrs = &clp->xdr_stream;
131         struct rpc_msg msg;
132         enum clnt_stat status;
133         struct rpc_err error;
134
135         if (clp == 0)
136                 return (RPC_FAILED);
137 call_again:
138         /*
139          * send request
140          */
141         xdrs->x_op = XDR_ENCODE;
142         XDR_SETPOS(xdrs, 0);
143         ((struct rpc_msg *)clp->mashl_callmsg)->rm_xid ++ ;
144         if ((! XDR_PUTBYTES(xdrs, clp->mashl_callmsg, clp->mcnt)) ||
145             (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
146             (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
147             (! (*xargs)(xdrs, argsp))) {
148                 return (RPC_CANTENCODEARGS);
149         }
150         XDR_GETPOS(xdrs);  /* called just to cause overhead */
151
152         /*
153          * We have to call server input routine here because this is
154          * all going on in one process. Yuk.
155          */
156         svc_getreq(1);
157
158         /*
159          * get results
160          */
161         xdrs->x_op = XDR_DECODE;
162         XDR_SETPOS(xdrs, 0);
163         msg.acpted_rply.ar_verf = _null_auth;
164         msg.acpted_rply.ar_results.where = resultsp;
165         msg.acpted_rply.ar_results.proc = xresults;
166         if (! xdr_replymsg(xdrs, &msg))
167                 return (RPC_CANTDECODERES);
168         _seterr_reply(&msg, &error);
169         status = error.re_status;
170
171         if (status == RPC_SUCCESS) {
172                 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
173                         status = RPC_AUTHERROR;
174                 }
175         }  /* end successful completion */
176         else {
177                 if (AUTH_REFRESH(h->cl_auth))
178                         goto call_again;
179         }  /* end of unsuccessful completion */
180
181         if (status == RPC_SUCCESS) {
182                 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
183                         status = RPC_AUTHERROR;
184                 }
185                 if (msg.acpted_rply.ar_verf.oa_base != NULL) {
186                         xdrs->x_op = XDR_FREE;
187                         xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
188                 }
189         }
190
191         return (status);
192 }
193
194 static void
195 clntraw_geterr(void)
196 {
197 }
198
199
200 static bool_t
201 clntraw_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
202 {
203         struct clntraw_private *clp = clntraw_private;
204         XDR *xdrs = &clp->xdr_stream;
205         bool_t rval;
206
207         if (clp == 0)
208         {
209                 rval = (bool_t) RPC_FAILED;
210                 return (rval);
211         }
212         xdrs->x_op = XDR_FREE;
213         return ((*xdr_res)(xdrs, res_ptr));
214 }
215
216 static void
217 clntraw_abort(void)
218 {
219 }
220
221 static bool_t
222 clntraw_control(void)
223 {
224         return (FALSE);
225 }
226
227 static void
228 clntraw_destroy(void)
229 {
230 }