xref: /openbsd-src/lib/libc/rpc/clnt_raw.c (revision b725ae7711052a2233e31a66fefb8a752c388d7a)
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 *rcsid = "$OpenBSD: clnt_raw.c,v 1.10 2001/09/15 13:51:00 deraadt Exp $";
32 #endif /* LIBC_SCCS and not lint */
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 similate rpc and get round trip overhead, without
42  * any interference from the kernal.
43  */
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <rpc/rpc.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 	CLIENT	client_object;
56 	XDR	xdr_stream;
57 	char	_raw_buf[UDPMSGSIZE];
58 	char	mashl_callmsg[MCALL_MSG_SIZE];
59 	u_int	mcnt;
60 } *clntraw_private;
61 
62 static enum clnt_stat	clntraw_call(CLIENT *, u_long, xdrproc_t, caddr_t,
63 			    xdrproc_t, caddr_t, struct timeval);
64 static void		clntraw_abort(CLIENT *);
65 static void		clntraw_geterr(CLIENT *, struct rpc_err *);
66 static bool_t		clntraw_freeres(CLIENT *, xdrproc_t, caddr_t);
67 static bool_t		clntraw_control(CLIENT *, u_int, void *);
68 static void		clntraw_destroy(CLIENT *);
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(prog, vers)
86 	u_long prog;
87 	u_long vers;
88 {
89 	struct clntraw_private *clp = clntraw_private;
90 	struct rpc_msg call_msg;
91 	XDR *xdrs = &clp->xdr_stream;
92 	CLIENT	*client = &clp->client_object;
93 
94 	if (clp == NULL) {
95 		clp = (struct clntraw_private *)calloc(1, sizeof (*clp));
96 		if (clp == NULL)
97 			return (NULL);
98 		clntraw_private = clp;
99 	}
100 	/*
101 	 * pre-serialize the staic 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 		perror("clnt_raw.c - Fatal header serialization error.");
110 	}
111 	clp->mcnt = XDR_GETPOS(xdrs);
112 	XDR_DESTROY(xdrs);
113 
114 	/*
115 	 * Set xdrmem for client/server shared buffer
116 	 */
117 	xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE);
118 
119 	/*
120 	 * create client handle
121 	 */
122 	client->cl_ops = &client_ops;
123 	client->cl_auth = authnone_create();
124 	return (client);
125 }
126 
127 /* ARGSUSED */
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 		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 		/* xdr_replymsg() may have left some things allocated */
177 		int op = xdrs->x_op;
178 		xdrs->x_op = XDR_FREE;
179 		xdr_replymsg(xdrs, &msg);
180 		xdrs->x_op = op;
181 		return (RPC_CANTDECODERES);
182 	}
183 	_seterr_reply(&msg, &error);
184 	status = error.re_status;
185 
186 	if (status == RPC_SUCCESS) {
187 		if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
188 			status = RPC_AUTHERROR;
189 		}
190 	}  /* end successful completion */
191 	else {
192 		if (AUTH_REFRESH(h->cl_auth))
193 			goto call_again;
194 	}  /* end of unsuccessful completion */
195 
196 	if (status == RPC_SUCCESS) {
197 		if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) {
198 			status = RPC_AUTHERROR;
199 		}
200 		if (msg.acpted_rply.ar_verf.oa_base != NULL) {
201 			xdrs->x_op = XDR_FREE;
202 			(void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf));
203 		}
204 	}
205 
206 	return (status);
207 }
208 
209 static void
210 clntraw_geterr(CLIENT *clnt, struct rpc_err *err)
211 {
212 }
213 
214 /* ARGSUSED */
215 static bool_t
216 clntraw_freeres(cl, xdr_res, res_ptr)
217 	CLIENT *cl;
218 	xdrproc_t xdr_res;
219 	caddr_t res_ptr;
220 {
221 	struct clntraw_private *clp = clntraw_private;
222 	XDR *xdrs = &clp->xdr_stream;
223 	bool_t rval;
224 
225 	if (clp == NULL) {
226 		rval = (bool_t) RPC_FAILED;
227 		return (rval);
228 	}
229 	xdrs->x_op = XDR_FREE;
230 	return ((*xdr_res)(xdrs, res_ptr));
231 }
232 
233 static void
234 clntraw_abort(CLIENT *clnt)
235 {
236 }
237 
238 static bool_t
239 clntraw_control(CLIENT *clnt, u_int i, void *v)
240 {
241 	return (FALSE);
242 }
243 
244 static void
245 clntraw_destroy(CLIENT *clnt)
246 {
247 }
248