xref: /netbsd-src/lib/libc/rpc/svc_raw.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: svc_raw.c,v 1.15 2001/01/04 14:42:22 lukem 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 #if 0
38 #if !defined(lint) && defined(SCCSIDS)
39 static char sccsid[] = "@(#)svc_raw.c 1.25 89/01/31 Copyr 1984 Sun Micro";
40 #endif
41 #endif
42 
43 /*
44  * svc_raw.c,   This a toy for simple testing and timing.
45  * Interface to create an rpc client and server in the same UNIX process.
46  * This lets us similate rpc and get rpc (round trip) overhead, without
47  * any interference from the kernal.
48  *
49  */
50 
51 #include "namespace.h"
52 #include "reentrant.h"
53 #include <rpc/rpc.h>
54 #include <sys/types.h>
55 #include <rpc/raw.h>
56 #include <assert.h>
57 #include <stdlib.h>
58 
59 #ifdef __weak_alias
60 __weak_alias(svc_raw_create,_svc_raw_create)
61 #endif
62 
63 #ifndef UDPMSGSIZE
64 #define	UDPMSGSIZE 8800
65 #endif
66 
67 /*
68  * This is the "network" that we will be moving data over
69  */
70 static struct svc_raw_private {
71 	char	*raw_buf;	/* should be shared with the cl handle */
72 	SVCXPRT	server;
73 	XDR	xdr_stream;
74 	char	verf_body[MAX_AUTH_BYTES];
75 } *svc_raw_private;
76 
77 #ifdef __REENT
78 extern mutex_t	svcraw_lock;
79 #endif
80 
81 static enum xprt_stat svc_raw_stat __P((SVCXPRT *));
82 static bool_t svc_raw_recv __P((SVCXPRT *, struct rpc_msg *));
83 static bool_t svc_raw_reply __P((SVCXPRT *, struct rpc_msg *));
84 static bool_t svc_raw_getargs __P((SVCXPRT *, xdrproc_t, caddr_t));
85 static bool_t svc_raw_freeargs __P((SVCXPRT *, xdrproc_t, caddr_t));
86 static void svc_raw_destroy __P((SVCXPRT *));
87 static void svc_raw_ops __P((SVCXPRT *));
88 static bool_t svc_raw_control __P((SVCXPRT *, const u_int, void *));
89 
90 char *__rpc_rawcombuf = NULL;
91 
92 SVCXPRT *
93 svc_raw_create()
94 {
95 	struct svc_raw_private *srp;
96 /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
97 
98 	mutex_lock(&svcraw_lock);
99 	srp = svc_raw_private;
100 	if (srp == NULL) {
101 		srp = (struct svc_raw_private *)calloc(1, sizeof (*srp));
102 		if (srp == NULL) {
103 			mutex_unlock(&svcraw_lock);
104 			return (NULL);
105 		}
106 		if (__rpc_rawcombuf == NULL)
107 			__rpc_rawcombuf = calloc(UDPMSGSIZE, sizeof (char));
108 		srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */
109 		svc_raw_private = srp;
110 	}
111 	srp->server.xp_fd = FD_SETSIZE;
112 	srp->server.xp_port = 0;
113 	srp->server.xp_p3 = NULL;
114 	svc_raw_ops(&srp->server);
115 	srp->server.xp_verf.oa_base = srp->verf_body;
116 	xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
117 	xprt_register(&srp->server);
118 	mutex_unlock(&svcraw_lock);
119 	return (&srp->server);
120 }
121 
122 /*ARGSUSED*/
123 static enum xprt_stat
124 svc_raw_stat(xprt)
125 SVCXPRT *xprt; /* args needed to satisfy ANSI-C typechecking */
126 {
127 	return (XPRT_IDLE);
128 }
129 
130 /*ARGSUSED*/
131 static bool_t
132 svc_raw_recv(xprt, msg)
133 	SVCXPRT *xprt;
134 	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
158 svc_raw_reply(xprt, msg)
159 	SVCXPRT *xprt;
160 	struct rpc_msg *msg;
161 {
162 	struct svc_raw_private *srp;
163 	XDR *xdrs;
164 
165 	mutex_lock(&svcraw_lock);
166 	srp = svc_raw_private;
167 	if (srp == NULL) {
168 		mutex_unlock(&svcraw_lock);
169 		return (FALSE);
170 	}
171 	mutex_unlock(&svcraw_lock);
172 
173 	xdrs = &srp->xdr_stream;
174 	xdrs->x_op = XDR_ENCODE;
175 	(void) XDR_SETPOS(xdrs, 0);
176 	if (! xdr_replymsg(xdrs, msg)) {
177 		return (FALSE);
178 	}
179 	(void) XDR_GETPOS(xdrs);  /* called just for overhead */
180 	return (TRUE);
181 }
182 
183 /*ARGSUSED*/
184 static bool_t
185 svc_raw_getargs(xprt, xdr_args, args_ptr)
186 	SVCXPRT *xprt;
187 	xdrproc_t xdr_args;
188 	caddr_t args_ptr;
189 {
190 	struct svc_raw_private *srp;
191 
192 	mutex_lock(&svcraw_lock);
193 	srp = svc_raw_private;
194 	if (srp == NULL) {
195 		mutex_unlock(&svcraw_lock);
196 		return (FALSE);
197 	}
198 	mutex_unlock(&svcraw_lock);
199 	return (*xdr_args)(&srp->xdr_stream, args_ptr);
200 }
201 
202 /*ARGSUSED*/
203 static bool_t
204 svc_raw_freeargs(xprt, xdr_args, args_ptr)
205 	SVCXPRT *xprt;
206 	xdrproc_t xdr_args;
207 	caddr_t args_ptr;
208 {
209 	struct svc_raw_private *srp;
210 	XDR *xdrs;
211 
212 	mutex_lock(&svcraw_lock);
213 	srp = svc_raw_private;
214 	if (srp == NULL) {
215 		mutex_unlock(&svcraw_lock);
216 		return (FALSE);
217 	}
218 	mutex_unlock(&svcraw_lock);
219 
220 	xdrs = &srp->xdr_stream;
221 	xdrs->x_op = XDR_FREE;
222 	return (*xdr_args)(xdrs, args_ptr);
223 }
224 
225 /*ARGSUSED*/
226 static void
227 svc_raw_destroy(xprt)
228 SVCXPRT *xprt;
229 {
230 }
231 
232 /*ARGSUSED*/
233 static bool_t
234 svc_raw_control(xprt, rq, in)
235 	SVCXPRT *xprt;
236 	const u_int	rq;
237 	void		*in;
238 {
239 	return (FALSE);
240 }
241 
242 static void
243 svc_raw_ops(xprt)
244 	SVCXPRT *xprt;
245 {
246 	static struct xp_ops ops;
247 	static struct xp_ops2 ops2;
248 #ifdef __REENT
249 	extern mutex_t ops_lock;
250 #endif
251 
252 	_DIAGASSERT(xprt != NULL);
253 
254 /* VARIABLES PROTECTED BY ops_lock: ops */
255 
256 	mutex_lock(&ops_lock);
257 	if (ops.xp_recv == NULL) {
258 		ops.xp_recv = svc_raw_recv;
259 		ops.xp_stat = svc_raw_stat;
260 		ops.xp_getargs = svc_raw_getargs;
261 		ops.xp_reply = svc_raw_reply;
262 		ops.xp_freeargs = svc_raw_freeargs;
263 		ops.xp_destroy = svc_raw_destroy;
264 		ops2.xp_control = svc_raw_control;
265 	}
266 	xprt->xp_ops = &ops;
267 	xprt->xp_ops2 = &ops2;
268 	mutex_unlock(&ops_lock);
269 }
270