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