1 /* $NetBSD: svc_raw.c,v 1.14 2000/07/06 03:10:35 christos Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-3-Clause
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 /*
42 * svc_raw.c, This a toy for simple testing and timing.
43 * Interface to create an rpc client and server in the same UNIX process.
44 * This lets us similate rpc and get rpc (round trip) overhead, without
45 * any interference from the kernel.
46 *
47 */
48
49 #include "namespace.h"
50 #include "reentrant.h"
51 #include <rpc/rpc.h>
52 #include <sys/types.h>
53 #include <rpc/raw.h>
54 #include <stdlib.h>
55 #include "un-namespace.h"
56 #include "mt_misc.h"
57
58 #ifndef UDPMSGSIZE
59 #define UDPMSGSIZE 8800
60 #endif
61
62 /*
63 * This is the "network" that we will be moving data over
64 */
65 static struct svc_raw_private {
66 char *raw_buf; /* should be shared with the cl handle */
67 SVCXPRT *server;
68 XDR xdr_stream;
69 char verf_body[MAX_AUTH_BYTES];
70 } *svc_raw_private;
71
72 static enum xprt_stat svc_raw_stat(SVCXPRT *);
73 static bool_t svc_raw_recv(SVCXPRT *, struct rpc_msg *);
74 static bool_t svc_raw_reply(SVCXPRT *, struct rpc_msg *);
75 static bool_t svc_raw_getargs(SVCXPRT *, xdrproc_t, void *);
76 static bool_t svc_raw_freeargs(SVCXPRT *, xdrproc_t, void *);
77 static void svc_raw_destroy(SVCXPRT *);
78 static void svc_raw_ops(SVCXPRT *);
79 static bool_t svc_raw_control(SVCXPRT *, const u_int, void *);
80
81 char *__rpc_rawcombuf = NULL;
82
83 SVCXPRT *
svc_raw_create(void)84 svc_raw_create(void)
85 {
86 struct svc_raw_private *srp;
87 /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
88
89 mutex_lock(&svcraw_lock);
90 srp = svc_raw_private;
91 if (srp == NULL) {
92 srp = (struct svc_raw_private *)calloc(1, sizeof (*srp));
93 if (srp == NULL) {
94 mutex_unlock(&svcraw_lock);
95 return (NULL);
96 }
97 if (__rpc_rawcombuf == NULL) {
98 __rpc_rawcombuf = calloc(UDPMSGSIZE, sizeof (char));
99 if (__rpc_rawcombuf == NULL) {
100 free(srp);
101 mutex_unlock(&svcraw_lock);
102 return (NULL);
103 }
104 }
105 srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */
106 srp->server = svc_xprt_alloc();
107 if (srp->server == NULL) {
108 free(__rpc_rawcombuf);
109 free(srp);
110 mutex_unlock(&svcraw_lock);
111 return (NULL);
112 }
113 svc_raw_private = srp;
114 }
115 srp->server->xp_fd = FD_SETSIZE;
116 srp->server->xp_port = 0;
117 svc_raw_ops(srp->server);
118 srp->server->xp_verf.oa_base = srp->verf_body;
119 xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
120 xprt_register(srp->server);
121 mutex_unlock(&svcraw_lock);
122 return (srp->server);
123 }
124
125 /*ARGSUSED*/
126 static enum xprt_stat
svc_raw_stat(SVCXPRT * xprt)127 svc_raw_stat(SVCXPRT *xprt)
128 {
129 return (XPRT_IDLE);
130 }
131
132 /*ARGSUSED*/
133 static bool_t
svc_raw_recv(SVCXPRT * xprt,struct rpc_msg * msg)134 svc_raw_recv(SVCXPRT *xprt, struct rpc_msg *msg)
135 {
136 struct svc_raw_private *srp;
137 XDR *xdrs;
138
139 mutex_lock(&svcraw_lock);
140 srp = svc_raw_private;
141 if (srp == NULL) {
142 mutex_unlock(&svcraw_lock);
143 return (FALSE);
144 }
145 mutex_unlock(&svcraw_lock);
146
147 xdrs = &srp->xdr_stream;
148 xdrs->x_op = XDR_DECODE;
149 (void) XDR_SETPOS(xdrs, 0);
150 if (! xdr_callmsg(xdrs, msg)) {
151 return (FALSE);
152 }
153 return (TRUE);
154 }
155
156 /*ARGSUSED*/
157 static bool_t
svc_raw_reply(SVCXPRT * xprt,struct rpc_msg * msg)158 svc_raw_reply(SVCXPRT *xprt, struct rpc_msg *msg)
159 {
160 struct svc_raw_private *srp;
161 XDR *xdrs;
162 bool_t stat;
163 xdrproc_t xdr_proc;
164 caddr_t xdr_where;
165
166 mutex_lock(&svcraw_lock);
167 srp = svc_raw_private;
168 if (srp == NULL) {
169 mutex_unlock(&svcraw_lock);
170 return (FALSE);
171 }
172 mutex_unlock(&svcraw_lock);
173
174 xdrs = &srp->xdr_stream;
175 xdrs->x_op = XDR_ENCODE;
176 (void) XDR_SETPOS(xdrs, 0);
177 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
178 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
179 xdr_proc = msg->acpted_rply.ar_results.proc;
180 xdr_where = msg->acpted_rply.ar_results.where;
181 msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
182 msg->acpted_rply.ar_results.where = NULL;
183
184 stat = xdr_replymsg(xdrs, msg) &&
185 SVCAUTH_WRAP(&SVC_AUTH(xprt), xdrs, xdr_proc, xdr_where);
186 } else {
187 stat = xdr_replymsg(xdrs, msg);
188 }
189 if (!stat) {
190 return (FALSE);
191 }
192 (void) XDR_GETPOS(xdrs); /* called just for overhead */
193 return (TRUE);
194 }
195
196 /*ARGSUSED*/
197 static bool_t
svc_raw_getargs(SVCXPRT * xprt,xdrproc_t xdr_args,void * args_ptr)198 svc_raw_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
199 {
200 struct svc_raw_private *srp;
201
202 mutex_lock(&svcraw_lock);
203 srp = svc_raw_private;
204 if (srp == NULL) {
205 mutex_unlock(&svcraw_lock);
206 return (FALSE);
207 }
208 mutex_unlock(&svcraw_lock);
209
210 return (SVCAUTH_UNWRAP(&SVC_AUTH(xprt), &srp->xdr_stream,
211 xdr_args, args_ptr));
212 }
213
214 /*ARGSUSED*/
215 static bool_t
svc_raw_freeargs(SVCXPRT * xprt,xdrproc_t xdr_args,void * args_ptr)216 svc_raw_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
217 {
218 struct svc_raw_private *srp;
219 XDR *xdrs;
220
221 mutex_lock(&svcraw_lock);
222 srp = svc_raw_private;
223 if (srp == NULL) {
224 mutex_unlock(&svcraw_lock);
225 return (FALSE);
226 }
227 mutex_unlock(&svcraw_lock);
228
229 xdrs = &srp->xdr_stream;
230 xdrs->x_op = XDR_FREE;
231 return (*xdr_args)(xdrs, args_ptr);
232 }
233
234 /*ARGSUSED*/
235 static void
svc_raw_destroy(SVCXPRT * xprt)236 svc_raw_destroy(SVCXPRT *xprt)
237 {
238 }
239
240 /*ARGSUSED*/
241 static bool_t
svc_raw_control(SVCXPRT * xprt,const u_int rq,void * in)242 svc_raw_control(SVCXPRT *xprt, const u_int rq, void *in)
243 {
244 return (FALSE);
245 }
246
247 static void
svc_raw_ops(SVCXPRT * xprt)248 svc_raw_ops(SVCXPRT *xprt)
249 {
250 static struct xp_ops ops;
251 static struct xp_ops2 ops2;
252
253 /* VARIABLES PROTECTED BY ops_lock: ops */
254
255 mutex_lock(&ops_lock);
256 if (ops.xp_recv == NULL) {
257 ops.xp_recv = svc_raw_recv;
258 ops.xp_stat = svc_raw_stat;
259 ops.xp_getargs = svc_raw_getargs;
260 ops.xp_reply = svc_raw_reply;
261 ops.xp_freeargs = svc_raw_freeargs;
262 ops.xp_destroy = svc_raw_destroy;
263 ops2.xp_control = svc_raw_control;
264 }
265 xprt->xp_ops = &ops;
266 xprt->xp_ops2 = &ops2;
267 mutex_unlock(&ops_lock);
268 }
269