xref: /netbsd-src/lib/libc/rpc/svc_raw.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: svc_raw.c,v 1.21 2008/05/24 15:59:59 christos Exp $	*/
2 
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 /*
32  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33  */
34 
35 /* #ident	"@(#)svc_raw.c	1.16	94/04/24 SMI" */
36 
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 #if 0
40 static char sccsid[] = "@(#)svc_raw.c 1.25 89/01/31 Copyr 1984 Sun Micro";
41 #else
42 __RCSID("$NetBSD: svc_raw.c,v 1.21 2008/05/24 15:59:59 christos Exp $");
43 #endif
44 #endif
45 
46 /*
47  * svc_raw.c,   This a toy for simple testing and timing.
48  * Interface to create an rpc client and server in the same UNIX process.
49  * This lets us similate rpc and get rpc (round trip) overhead, without
50  * any interference from the kernel.
51  *
52  */
53 
54 #include "namespace.h"
55 #include "reentrant.h"
56 #include <rpc/rpc.h>
57 #include <sys/types.h>
58 #include <rpc/raw.h>
59 #include <assert.h>
60 #include <stdlib.h>
61 
62 #ifdef __weak_alias
63 __weak_alias(svc_raw_create,_svc_raw_create)
64 #endif
65 
66 #ifndef UDPMSGSIZE
67 #define	UDPMSGSIZE 8800
68 #endif
69 
70 /*
71  * This is the "network" that we will be moving data over
72  */
73 static struct svc_raw_private {
74 	char	*raw_buf;	/* should be shared with the cl handle */
75 	SVCXPRT	server;
76 	XDR	xdr_stream;
77 	char	verf_body[MAX_AUTH_BYTES];
78 } *svc_raw_private;
79 
80 #ifdef _REENTRANT
81 extern mutex_t	svcraw_lock;
82 #endif
83 
84 static enum xprt_stat svc_raw_stat __P((SVCXPRT *));
85 static bool_t svc_raw_recv __P((SVCXPRT *, struct rpc_msg *));
86 static bool_t svc_raw_reply __P((SVCXPRT *, struct rpc_msg *));
87 static bool_t svc_raw_getargs __P((SVCXPRT *, xdrproc_t, caddr_t));
88 static bool_t svc_raw_freeargs __P((SVCXPRT *, xdrproc_t, caddr_t));
89 static void svc_raw_destroy __P((SVCXPRT *));
90 static void svc_raw_ops __P((SVCXPRT *));
91 static bool_t svc_raw_control __P((SVCXPRT *, const u_int, void *));
92 
93 char *__rpc_rawcombuf = NULL;
94 
95 SVCXPRT *
96 svc_raw_create()
97 {
98 	struct svc_raw_private *srp;
99 /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
100 
101 	mutex_lock(&svcraw_lock);
102 	srp = svc_raw_private;
103 	if (srp == NULL) {
104 		srp = calloc(1, sizeof(*srp));
105 		if (srp == NULL)
106 			goto out;
107 		if (__rpc_rawcombuf == NULL)
108 			__rpc_rawcombuf = malloc(UDPMSGSIZE);
109 		if (__rpc_rawcombuf == NULL)
110 			goto out;
111 		srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */
112 		svc_raw_private = srp;
113 	}
114 	srp->server.xp_fd = FD_SETSIZE;
115 	srp->server.xp_port = 0;
116 	srp->server.xp_p3 = NULL;
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 out:
124 	if (srp != NULL)
125 		free(srp);
126 	mutex_unlock(&svcraw_lock);
127 	return (NULL);
128 }
129 
130 /*ARGSUSED*/
131 static enum xprt_stat
132 svc_raw_stat(xprt)
133 SVCXPRT *xprt; /* args needed to satisfy ANSI-C typechecking */
134 {
135 	return (XPRT_IDLE);
136 }
137 
138 /*ARGSUSED*/
139 static bool_t
140 svc_raw_recv(xprt, msg)
141 	SVCXPRT *xprt;
142 	struct rpc_msg *msg;
143 {
144 	struct svc_raw_private *srp;
145 	XDR *xdrs;
146 
147 	mutex_lock(&svcraw_lock);
148 	srp = svc_raw_private;
149 	if (srp == NULL) {
150 		mutex_unlock(&svcraw_lock);
151 		return (FALSE);
152 	}
153 	mutex_unlock(&svcraw_lock);
154 
155 	xdrs = &srp->xdr_stream;
156 	xdrs->x_op = XDR_DECODE;
157 	(void) XDR_SETPOS(xdrs, 0);
158 	if (! xdr_callmsg(xdrs, msg)) {
159 		return (FALSE);
160 	}
161 	return (TRUE);
162 }
163 
164 /*ARGSUSED*/
165 static bool_t
166 svc_raw_reply(xprt, msg)
167 	SVCXPRT *xprt;
168 	struct rpc_msg *msg;
169 {
170 	struct svc_raw_private *srp;
171 	XDR *xdrs;
172 
173 	mutex_lock(&svcraw_lock);
174 	srp = svc_raw_private;
175 	if (srp == NULL) {
176 		mutex_unlock(&svcraw_lock);
177 		return (FALSE);
178 	}
179 	mutex_unlock(&svcraw_lock);
180 
181 	xdrs = &srp->xdr_stream;
182 	xdrs->x_op = XDR_ENCODE;
183 	(void) XDR_SETPOS(xdrs, 0);
184 	if (! xdr_replymsg(xdrs, msg)) {
185 		return (FALSE);
186 	}
187 	(void) XDR_GETPOS(xdrs);  /* called just for overhead */
188 	return (TRUE);
189 }
190 
191 /*ARGSUSED*/
192 static bool_t
193 svc_raw_getargs(xprt, xdr_args, args_ptr)
194 	SVCXPRT *xprt;
195 	xdrproc_t xdr_args;
196 	caddr_t args_ptr;
197 {
198 	struct svc_raw_private *srp;
199 
200 	mutex_lock(&svcraw_lock);
201 	srp = svc_raw_private;
202 	if (srp == NULL) {
203 		mutex_unlock(&svcraw_lock);
204 		return (FALSE);
205 	}
206 	mutex_unlock(&svcraw_lock);
207 	return (*xdr_args)(&srp->xdr_stream, args_ptr);
208 }
209 
210 /*ARGSUSED*/
211 static bool_t
212 svc_raw_freeargs(xprt, xdr_args, args_ptr)
213 	SVCXPRT *xprt;
214 	xdrproc_t xdr_args;
215 	caddr_t args_ptr;
216 {
217 	struct svc_raw_private *srp;
218 	XDR *xdrs;
219 
220 	mutex_lock(&svcraw_lock);
221 	srp = svc_raw_private;
222 	if (srp == NULL) {
223 		mutex_unlock(&svcraw_lock);
224 		return (FALSE);
225 	}
226 	mutex_unlock(&svcraw_lock);
227 
228 	xdrs = &srp->xdr_stream;
229 	xdrs->x_op = XDR_FREE;
230 	return (*xdr_args)(xdrs, args_ptr);
231 }
232 
233 /*ARGSUSED*/
234 static void
235 svc_raw_destroy(xprt)
236 SVCXPRT *xprt;
237 {
238 }
239 
240 /*ARGSUSED*/
241 static bool_t
242 svc_raw_control(xprt, rq, in)
243 	SVCXPRT *xprt;
244 	const u_int	rq;
245 	void		*in;
246 {
247 	return (FALSE);
248 }
249 
250 static void
251 svc_raw_ops(xprt)
252 	SVCXPRT *xprt;
253 {
254 	static struct xp_ops ops;
255 	static struct xp_ops2 ops2;
256 #ifdef _REENTRANT
257 	extern mutex_t ops_lock;
258 #endif
259 
260 	_DIAGASSERT(xprt != NULL);
261 
262 /* VARIABLES PROTECTED BY ops_lock: ops */
263 
264 	mutex_lock(&ops_lock);
265 	if (ops.xp_recv == NULL) {
266 		ops.xp_recv = svc_raw_recv;
267 		ops.xp_stat = svc_raw_stat;
268 		ops.xp_getargs = svc_raw_getargs;
269 		ops.xp_reply = svc_raw_reply;
270 		ops.xp_freeargs = svc_raw_freeargs;
271 		ops.xp_destroy = svc_raw_destroy;
272 		ops2.xp_control = svc_raw_control;
273 	}
274 	xprt->xp_ops = &ops;
275 	xprt->xp_ops2 = &ops2;
276 	mutex_unlock(&ops_lock);
277 }
278