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