xref: /reactos/dll/3rdparty/libtirpc/src/svc_raw.c (revision c2c66aff)
1*c2c66affSColin Finck 
2*c2c66affSColin Finck /*
3*c2c66affSColin Finck  * Copyright (c) 2009, Sun Microsystems, Inc.
4*c2c66affSColin Finck  * All rights reserved.
5*c2c66affSColin Finck  *
6*c2c66affSColin Finck  * Redistribution and use in source and binary forms, with or without
7*c2c66affSColin Finck  * modification, are permitted provided that the following conditions are met:
8*c2c66affSColin Finck  * - Redistributions of source code must retain the above copyright notice,
9*c2c66affSColin Finck  *   this list of conditions and the following disclaimer.
10*c2c66affSColin Finck  * - Redistributions in binary form must reproduce the above copyright notice,
11*c2c66affSColin Finck  *   this list of conditions and the following disclaimer in the documentation
12*c2c66affSColin Finck  *   and/or other materials provided with the distribution.
13*c2c66affSColin Finck  * - Neither the name of Sun Microsystems, Inc. nor the names of its
14*c2c66affSColin Finck  *   contributors may be used to endorse or promote products derived
15*c2c66affSColin Finck  *   from this software without specific prior written permission.
16*c2c66affSColin Finck  *
17*c2c66affSColin Finck  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*c2c66affSColin Finck  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*c2c66affSColin Finck  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*c2c66affSColin Finck  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*c2c66affSColin Finck  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*c2c66affSColin Finck  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*c2c66affSColin Finck  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*c2c66affSColin Finck  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*c2c66affSColin Finck  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*c2c66affSColin Finck  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*c2c66affSColin Finck  * POSSIBILITY OF SUCH DAMAGE.
28*c2c66affSColin Finck  */
29*c2c66affSColin Finck /*
30*c2c66affSColin Finck  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
31*c2c66affSColin Finck  */
32*c2c66affSColin Finck 
33*c2c66affSColin Finck /*
34*c2c66affSColin Finck  * svc_raw.c,   This a toy for simple testing and timing.
35*c2c66affSColin Finck  * Interface to create an rpc client and server in the same UNIX process.
36*c2c66affSColin Finck  * This lets us similate rpc and get rpc (round trip) overhead, without
37*c2c66affSColin Finck  * any interference from the kernel.
38*c2c66affSColin Finck  *
39*c2c66affSColin Finck  */
40*c2c66affSColin Finck #include <wintirpc.h>
41*c2c66affSColin Finck //#include <pthread.h>
42*c2c66affSColin Finck #include <reentrant.h>
43*c2c66affSColin Finck #include <rpc/rpc.h>
44*c2c66affSColin Finck #include <sys/types.h>
45*c2c66affSColin Finck #include <rpc/raw.h>
46*c2c66affSColin Finck #include <stdlib.h>
47*c2c66affSColin Finck 
48*c2c66affSColin Finck #ifndef UDPMSGSIZE
49*c2c66affSColin Finck #define	UDPMSGSIZE 8800
50*c2c66affSColin Finck #endif
51*c2c66affSColin Finck 
52*c2c66affSColin Finck /*
53*c2c66affSColin Finck  * This is the "network" that we will be moving data over
54*c2c66affSColin Finck  */
55*c2c66affSColin Finck static struct svc_raw_private {
56*c2c66affSColin Finck 	char	*raw_buf;	/* should be shared with the cl handle */
57*c2c66affSColin Finck 	SVCXPRT	server;
58*c2c66affSColin Finck 	XDR	xdr_stream;
59*c2c66affSColin Finck 	char	verf_body[MAX_AUTH_BYTES];
60*c2c66affSColin Finck } *svc_raw_private;
61*c2c66affSColin Finck 
62*c2c66affSColin Finck extern mutex_t	svcraw_lock;
63*c2c66affSColin Finck 
64*c2c66affSColin Finck static enum xprt_stat svc_raw_stat(SVCXPRT *);
65*c2c66affSColin Finck static bool_t svc_raw_recv(SVCXPRT *, struct rpc_msg *);
66*c2c66affSColin Finck static bool_t svc_raw_reply(SVCXPRT *, struct rpc_msg *);
67*c2c66affSColin Finck static bool_t svc_raw_getargs(SVCXPRT *, xdrproc_t, void *);
68*c2c66affSColin Finck static bool_t svc_raw_freeargs(SVCXPRT *, xdrproc_t, void *);
69*c2c66affSColin Finck static void svc_raw_destroy(SVCXPRT *);
70*c2c66affSColin Finck static void svc_raw_ops(SVCXPRT *);
71*c2c66affSColin Finck static bool_t svc_raw_control(SVCXPRT *, const u_int, void *);
72*c2c66affSColin Finck 
73*c2c66affSColin Finck char *__rpc_rawcombuf = NULL;
74*c2c66affSColin Finck 
75*c2c66affSColin Finck SVCXPRT *
svc_raw_create()76*c2c66affSColin Finck svc_raw_create()
77*c2c66affSColin Finck {
78*c2c66affSColin Finck 	struct svc_raw_private *srp;
79*c2c66affSColin Finck /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
80*c2c66affSColin Finck 
81*c2c66affSColin Finck 	mutex_lock(&svcraw_lock);
82*c2c66affSColin Finck 	srp = svc_raw_private;
83*c2c66affSColin Finck 	if (srp == NULL) {
84*c2c66affSColin Finck 		srp = (struct svc_raw_private *)calloc(1, sizeof (*srp));
85*c2c66affSColin Finck 		if (srp == NULL) {
86*c2c66affSColin Finck 			mutex_unlock(&svcraw_lock);
87*c2c66affSColin Finck 			return (NULL);
88*c2c66affSColin Finck 		}
89*c2c66affSColin Finck 		if (__rpc_rawcombuf == NULL)
90*c2c66affSColin Finck 			__rpc_rawcombuf = calloc(UDPMSGSIZE, sizeof (char));
91*c2c66affSColin Finck 		srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */
92*c2c66affSColin Finck 		svc_raw_private = srp;
93*c2c66affSColin Finck 	}
94*c2c66affSColin Finck 	srp->server.xp_fd = FD_SETSIZE;
95*c2c66affSColin Finck 	srp->server.xp_port = 0;
96*c2c66affSColin Finck 	srp->server.xp_p3 = NULL;
97*c2c66affSColin Finck 	svc_raw_ops(&srp->server);
98*c2c66affSColin Finck 	srp->server.xp_verf.oa_base = srp->verf_body;
99*c2c66affSColin Finck 	xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
100*c2c66affSColin Finck 	xprt_register(&srp->server);
101*c2c66affSColin Finck 	mutex_unlock(&svcraw_lock);
102*c2c66affSColin Finck 	return (&srp->server);
103*c2c66affSColin Finck }
104*c2c66affSColin Finck 
105*c2c66affSColin Finck /*ARGSUSED*/
106*c2c66affSColin Finck static enum xprt_stat
svc_raw_stat(xprt)107*c2c66affSColin Finck svc_raw_stat(xprt)
108*c2c66affSColin Finck SVCXPRT *xprt; /* args needed to satisfy ANSI-C typechecking */
109*c2c66affSColin Finck {
110*c2c66affSColin Finck 	return (XPRT_IDLE);
111*c2c66affSColin Finck }
112*c2c66affSColin Finck 
113*c2c66affSColin Finck /*ARGSUSED*/
114*c2c66affSColin Finck static bool_t
svc_raw_recv(xprt,msg)115*c2c66affSColin Finck svc_raw_recv(xprt, msg)
116*c2c66affSColin Finck 	SVCXPRT *xprt;
117*c2c66affSColin Finck 	struct rpc_msg *msg;
118*c2c66affSColin Finck {
119*c2c66affSColin Finck 	struct svc_raw_private *srp;
120*c2c66affSColin Finck 	XDR *xdrs;
121*c2c66affSColin Finck 
122*c2c66affSColin Finck 	mutex_lock(&svcraw_lock);
123*c2c66affSColin Finck 	srp = svc_raw_private;
124*c2c66affSColin Finck 	if (srp == NULL) {
125*c2c66affSColin Finck 		mutex_unlock(&svcraw_lock);
126*c2c66affSColin Finck 		return (FALSE);
127*c2c66affSColin Finck 	}
128*c2c66affSColin Finck 	mutex_unlock(&svcraw_lock);
129*c2c66affSColin Finck 
130*c2c66affSColin Finck 	xdrs = &srp->xdr_stream;
131*c2c66affSColin Finck 	xdrs->x_op = XDR_DECODE;
132*c2c66affSColin Finck 	(void) XDR_SETPOS(xdrs, 0);
133*c2c66affSColin Finck 	if (! xdr_callmsg(xdrs, msg)) {
134*c2c66affSColin Finck 		return (FALSE);
135*c2c66affSColin Finck 	}
136*c2c66affSColin Finck 	return (TRUE);
137*c2c66affSColin Finck }
138*c2c66affSColin Finck 
139*c2c66affSColin Finck /*ARGSUSED*/
140*c2c66affSColin Finck static bool_t
svc_raw_reply(xprt,msg)141*c2c66affSColin Finck svc_raw_reply(xprt, msg)
142*c2c66affSColin Finck 	SVCXPRT *xprt;
143*c2c66affSColin Finck 	struct rpc_msg *msg;
144*c2c66affSColin Finck {
145*c2c66affSColin Finck 	struct svc_raw_private *srp;
146*c2c66affSColin Finck 	XDR *xdrs;
147*c2c66affSColin Finck 
148*c2c66affSColin Finck 	mutex_lock(&svcraw_lock);
149*c2c66affSColin Finck 	srp = svc_raw_private;
150*c2c66affSColin Finck 	if (srp == NULL) {
151*c2c66affSColin Finck 		mutex_unlock(&svcraw_lock);
152*c2c66affSColin Finck 		return (FALSE);
153*c2c66affSColin Finck 	}
154*c2c66affSColin Finck 	mutex_unlock(&svcraw_lock);
155*c2c66affSColin Finck 
156*c2c66affSColin Finck 	xdrs = &srp->xdr_stream;
157*c2c66affSColin Finck 	xdrs->x_op = XDR_ENCODE;
158*c2c66affSColin Finck 	(void) XDR_SETPOS(xdrs, 0);
159*c2c66affSColin Finck 	if (! xdr_replymsg(xdrs, msg)) {
160*c2c66affSColin Finck 		return (FALSE);
161*c2c66affSColin Finck 	}
162*c2c66affSColin Finck 	(void) XDR_GETPOS(xdrs);  /* called just for overhead */
163*c2c66affSColin Finck 	return (TRUE);
164*c2c66affSColin Finck }
165*c2c66affSColin Finck 
166*c2c66affSColin Finck /*ARGSUSED*/
167*c2c66affSColin Finck static bool_t
svc_raw_getargs(xprt,xdr_args,args_ptr)168*c2c66affSColin Finck svc_raw_getargs(xprt, xdr_args, args_ptr)
169*c2c66affSColin Finck 	SVCXPRT *xprt;
170*c2c66affSColin Finck 	xdrproc_t xdr_args;
171*c2c66affSColin Finck 	void *args_ptr;
172*c2c66affSColin Finck {
173*c2c66affSColin Finck 	struct svc_raw_private *srp;
174*c2c66affSColin Finck 
175*c2c66affSColin Finck 	mutex_lock(&svcraw_lock);
176*c2c66affSColin Finck 	srp = svc_raw_private;
177*c2c66affSColin Finck 	if (srp == NULL) {
178*c2c66affSColin Finck 		mutex_unlock(&svcraw_lock);
179*c2c66affSColin Finck 		return (FALSE);
180*c2c66affSColin Finck 	}
181*c2c66affSColin Finck 	mutex_unlock(&svcraw_lock);
182*c2c66affSColin Finck 	return (*xdr_args)(&srp->xdr_stream, args_ptr);
183*c2c66affSColin Finck }
184*c2c66affSColin Finck 
185*c2c66affSColin Finck /*ARGSUSED*/
186*c2c66affSColin Finck static bool_t
svc_raw_freeargs(xprt,xdr_args,args_ptr)187*c2c66affSColin Finck svc_raw_freeargs(xprt, xdr_args, args_ptr)
188*c2c66affSColin Finck 	SVCXPRT *xprt;
189*c2c66affSColin Finck 	xdrproc_t xdr_args;
190*c2c66affSColin Finck 	void *args_ptr;
191*c2c66affSColin Finck {
192*c2c66affSColin Finck 	struct svc_raw_private *srp;
193*c2c66affSColin Finck 	XDR *xdrs;
194*c2c66affSColin Finck 
195*c2c66affSColin Finck 	mutex_lock(&svcraw_lock);
196*c2c66affSColin Finck 	srp = svc_raw_private;
197*c2c66affSColin Finck 	if (srp == NULL) {
198*c2c66affSColin Finck 		mutex_unlock(&svcraw_lock);
199*c2c66affSColin Finck 		return (FALSE);
200*c2c66affSColin Finck 	}
201*c2c66affSColin Finck 	mutex_unlock(&svcraw_lock);
202*c2c66affSColin Finck 
203*c2c66affSColin Finck 	xdrs = &srp->xdr_stream;
204*c2c66affSColin Finck 	xdrs->x_op = XDR_FREE;
205*c2c66affSColin Finck 	return (*xdr_args)(xdrs, args_ptr);
206*c2c66affSColin Finck }
207*c2c66affSColin Finck 
208*c2c66affSColin Finck /*ARGSUSED*/
209*c2c66affSColin Finck static void
svc_raw_destroy(xprt)210*c2c66affSColin Finck svc_raw_destroy(xprt)
211*c2c66affSColin Finck SVCXPRT *xprt;
212*c2c66affSColin Finck {
213*c2c66affSColin Finck }
214*c2c66affSColin Finck 
215*c2c66affSColin Finck /*ARGSUSED*/
216*c2c66affSColin Finck static bool_t
svc_raw_control(xprt,rq,in)217*c2c66affSColin Finck svc_raw_control(xprt, rq, in)
218*c2c66affSColin Finck 	SVCXPRT *xprt;
219*c2c66affSColin Finck 	const u_int	rq;
220*c2c66affSColin Finck 	void		*in;
221*c2c66affSColin Finck {
222*c2c66affSColin Finck 	return (FALSE);
223*c2c66affSColin Finck }
224*c2c66affSColin Finck 
225*c2c66affSColin Finck static void
svc_raw_ops(xprt)226*c2c66affSColin Finck svc_raw_ops(xprt)
227*c2c66affSColin Finck 	SVCXPRT *xprt;
228*c2c66affSColin Finck {
229*c2c66affSColin Finck 	static struct xp_ops ops;
230*c2c66affSColin Finck 	static struct xp_ops2 ops2;
231*c2c66affSColin Finck 	extern mutex_t ops_lock;
232*c2c66affSColin Finck 
233*c2c66affSColin Finck /* VARIABLES PROTECTED BY ops_lock: ops */
234*c2c66affSColin Finck 
235*c2c66affSColin Finck 	mutex_lock(&ops_lock);
236*c2c66affSColin Finck 	if (ops.xp_recv == NULL) {
237*c2c66affSColin Finck 		ops.xp_recv = svc_raw_recv;
238*c2c66affSColin Finck 		ops.xp_stat = svc_raw_stat;
239*c2c66affSColin Finck 		ops.xp_getargs = svc_raw_getargs;
240*c2c66affSColin Finck 		ops.xp_reply = svc_raw_reply;
241*c2c66affSColin Finck 		ops.xp_freeargs = svc_raw_freeargs;
242*c2c66affSColin Finck 		ops.xp_destroy = svc_raw_destroy;
243*c2c66affSColin Finck 		ops2.xp_control = svc_raw_control;
244*c2c66affSColin Finck 	}
245*c2c66affSColin Finck 	xprt->xp_ops = &ops;
246*c2c66affSColin Finck 	xprt->xp_ops2 = &ops2;
247*c2c66affSColin Finck 	mutex_unlock(&ops_lock);
248*c2c66affSColin Finck }
249