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