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