1 /* $NetBSD: svc_raw.c,v 1.14 2000/07/06 03:10:35 christos Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
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.
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.
33 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
36 /* #ident "@(#)svc_raw.c 1.16 94/04/24 SMI" */
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)svc_raw.c 1.25 89/01/31 Copyr 1984 Sun Micro";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
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.
52 #include "namespace.h"
53 #include "reentrant.h"
55 #include <sys/types.h>
58 #include "un-namespace.h"
62 #define UDPMSGSIZE 8800
66 * This is the "network" that we will be moving data over
68 static struct svc_raw_private {
69 char *raw_buf; /* should be shared with the cl handle */
72 char verf_body[MAX_AUTH_BYTES];
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 *);
84 char *__rpc_rawcombuf = NULL;
89 struct svc_raw_private *srp;
90 /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
92 mutex_lock(&svcraw_lock);
93 srp = svc_raw_private;
95 srp = (struct svc_raw_private *)calloc(1, sizeof (*srp));
97 mutex_unlock(&svcraw_lock);
100 if (__rpc_rawcombuf == NULL) {
101 __rpc_rawcombuf = calloc(UDPMSGSIZE, sizeof (char));
102 if (__rpc_rawcombuf == NULL) {
104 mutex_unlock(&svcraw_lock);
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);
113 mutex_unlock(&svcraw_lock);
116 svc_raw_private = srp;
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);
129 static enum xprt_stat
130 svc_raw_stat(SVCXPRT *xprt)
137 svc_raw_recv(SVCXPRT *xprt, struct rpc_msg *msg)
139 struct svc_raw_private *srp;
142 mutex_lock(&svcraw_lock);
143 srp = svc_raw_private;
145 mutex_unlock(&svcraw_lock);
148 mutex_unlock(&svcraw_lock);
150 xdrs = &srp->xdr_stream;
151 xdrs->x_op = XDR_DECODE;
152 (void) XDR_SETPOS(xdrs, 0);
153 if (! xdr_callmsg(xdrs, msg)) {
161 svc_raw_reply(SVCXPRT *xprt, struct rpc_msg *msg)
163 struct svc_raw_private *srp;
169 mutex_lock(&svcraw_lock);
170 srp = svc_raw_private;
172 mutex_unlock(&svcraw_lock);
175 mutex_unlock(&svcraw_lock);
177 xdrs = &srp->xdr_stream;
178 xdrs->x_op = XDR_ENCODE;
179 (void) XDR_SETPOS(xdrs, 0);
180 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
181 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
182 xdr_proc = msg->acpted_rply.ar_results.proc;
183 xdr_where = msg->acpted_rply.ar_results.where;
184 msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
185 msg->acpted_rply.ar_results.where = NULL;
187 stat = xdr_replymsg(xdrs, msg) &&
188 SVCAUTH_WRAP(&SVC_AUTH(xprt), xdrs, xdr_proc, xdr_where);
190 stat = xdr_replymsg(xdrs, msg);
195 (void) XDR_GETPOS(xdrs); /* called just for overhead */
201 svc_raw_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
203 struct svc_raw_private *srp;
205 mutex_lock(&svcraw_lock);
206 srp = svc_raw_private;
208 mutex_unlock(&svcraw_lock);
211 mutex_unlock(&svcraw_lock);
213 return (SVCAUTH_UNWRAP(&SVC_AUTH(xprt), &srp->xdr_stream,
214 xdr_args, args_ptr));
219 svc_raw_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
221 struct svc_raw_private *srp;
224 mutex_lock(&svcraw_lock);
225 srp = svc_raw_private;
227 mutex_unlock(&svcraw_lock);
230 mutex_unlock(&svcraw_lock);
232 xdrs = &srp->xdr_stream;
233 xdrs->x_op = XDR_FREE;
234 return (*xdr_args)(xdrs, args_ptr);
239 svc_raw_destroy(SVCXPRT *xprt)
245 svc_raw_control(SVCXPRT *xprt, const u_int rq, void *in)
251 svc_raw_ops(SVCXPRT *xprt)
253 static struct xp_ops ops;
254 static struct xp_ops2 ops2;
256 /* VARIABLES PROTECTED BY ops_lock: ops */
258 mutex_lock(&ops_lock);
259 if (ops.xp_recv == NULL) {
260 ops.xp_recv = svc_raw_recv;
261 ops.xp_stat = svc_raw_stat;
262 ops.xp_getargs = svc_raw_getargs;
263 ops.xp_reply = svc_raw_reply;
264 ops.xp_freeargs = svc_raw_freeargs;
265 ops.xp_destroy = svc_raw_destroy;
266 ops2.xp_control = svc_raw_control;
269 xprt->xp_ops2 = &ops2;
270 mutex_unlock(&ops_lock);