xref: /netbsd-src/lib/libc/rpc/svc_raw.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: svc_raw.c,v 1.13 2000/06/02 23:11:16 fvdl 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 <stdlib.h>
57 
58 #ifdef __weak_alias
59 __weak_alias(svc_raw_create,_svc_raw_create)
60 #endif
61 
62 #ifndef UDPMSGSIZE
63 #define	UDPMSGSIZE 8800
64 #endif
65 
66 /*
67  * This is the "network" that we will be moving data over
68  */
69 static struct svc_raw_private {
70 	char	*raw_buf;	/* should be shared with the cl handle */
71 	SVCXPRT	server;
72 	XDR	xdr_stream;
73 	char	verf_body[MAX_AUTH_BYTES];
74 } *svc_raw_private;
75 
76 #ifdef __REENT
77 extern mutex_t	svcraw_lock;
78 #endif
79 
80 static enum xprt_stat svc_raw_stat __P((SVCXPRT *));
81 static bool_t svc_raw_recv __P((SVCXPRT *, struct rpc_msg *));
82 static bool_t svc_raw_reply __P((SVCXPRT *, struct rpc_msg *));
83 static bool_t svc_raw_getargs __P((SVCXPRT *, xdrproc_t, caddr_t));
84 static bool_t svc_raw_freeargs __P((SVCXPRT *, xdrproc_t, caddr_t));
85 static void svc_raw_destroy __P((SVCXPRT *));
86 static void svc_raw_ops __P((SVCXPRT *));
87 static bool_t svc_raw_control __P((SVCXPRT *, const u_int, void *));
88 
89 char *__rpc_rawcombuf = NULL;
90 
91 SVCXPRT *
92 svc_raw_create()
93 {
94 	struct svc_raw_private *srp;
95 /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
96 
97 	mutex_lock(&svcraw_lock);
98 	srp = svc_raw_private;
99 	if (srp == NULL) {
100 		srp = (struct svc_raw_private *)calloc(1, sizeof (*srp));
101 		if (srp == NULL) {
102 			mutex_unlock(&svcraw_lock);
103 			return ((SVCXPRT *)NULL);
104 		}
105 		if (__rpc_rawcombuf == NULL)
106 			__rpc_rawcombuf =
107 			    (char *)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 static enum xprt_stat
123 svc_raw_stat(xprt)
124 SVCXPRT *xprt; /* args needed to satisfy ANSI-C typechecking */
125 {
126 	return (XPRT_IDLE);
127 }
128 
129 static bool_t
130 svc_raw_recv(xprt, msg)
131 	SVCXPRT *xprt;
132 	struct rpc_msg *msg;
133 {
134 	struct svc_raw_private *srp;
135 	XDR *xdrs;
136 
137 	mutex_lock(&svcraw_lock);
138 	srp = svc_raw_private;
139 	if (srp == NULL) {
140 		mutex_unlock(&svcraw_lock);
141 		return (FALSE);
142 	}
143 	mutex_unlock(&svcraw_lock);
144 
145 	xdrs = &srp->xdr_stream;
146 	xdrs->x_op = XDR_DECODE;
147 	(void) XDR_SETPOS(xdrs, 0);
148 	if (! xdr_callmsg(xdrs, msg)) {
149 		return (FALSE);
150 	}
151 	return (TRUE);
152 }
153 
154 static bool_t
155 svc_raw_reply(xprt, msg)
156 	SVCXPRT *xprt;
157 	struct rpc_msg *msg;
158 {
159 	struct svc_raw_private *srp;
160 	XDR *xdrs;
161 
162 	mutex_lock(&svcraw_lock);
163 	srp = svc_raw_private;
164 	if (srp == NULL) {
165 		mutex_unlock(&svcraw_lock);
166 		return (FALSE);
167 	}
168 	mutex_unlock(&svcraw_lock);
169 
170 	xdrs = &srp->xdr_stream;
171 	xdrs->x_op = XDR_ENCODE;
172 	(void) XDR_SETPOS(xdrs, 0);
173 	if (! xdr_replymsg(xdrs, msg)) {
174 		return (FALSE);
175 	}
176 	(void) XDR_GETPOS(xdrs);  /* called just for overhead */
177 	return (TRUE);
178 }
179 
180 static bool_t
181 svc_raw_getargs(xprt, xdr_args, args_ptr)
182 	SVCXPRT *xprt;
183 	xdrproc_t xdr_args;
184 	caddr_t args_ptr;
185 {
186 	register struct svc_raw_private *srp;
187 
188 	mutex_lock(&svcraw_lock);
189 	srp = svc_raw_private;
190 	if (srp == NULL) {
191 		mutex_unlock(&svcraw_lock);
192 		return (FALSE);
193 	}
194 	mutex_unlock(&svcraw_lock);
195 	return (*xdr_args)(&srp->xdr_stream, args_ptr);
196 }
197 
198 static bool_t
199 svc_raw_freeargs(xprt, xdr_args, args_ptr)
200 	SVCXPRT *xprt;
201 	xdrproc_t xdr_args;
202 	caddr_t args_ptr;
203 {
204 	struct svc_raw_private *srp;
205 	XDR *xdrs;
206 
207 	mutex_lock(&svcraw_lock);
208 	srp = svc_raw_private;
209 	if (srp == NULL) {
210 		mutex_unlock(&svcraw_lock);
211 		return (FALSE);
212 	}
213 	mutex_unlock(&svcraw_lock);
214 
215 	xdrs = &srp->xdr_stream;
216 	xdrs->x_op = XDR_FREE;
217 	return (*xdr_args)(xdrs, args_ptr);
218 }
219 
220 static void
221 svc_raw_destroy(xprt)
222 SVCXPRT *xprt;
223 {
224 }
225 
226 static bool_t
227 svc_raw_control(xprt, rq, in)
228 	SVCXPRT *xprt;
229 	const u_int	rq;
230 	void		*in;
231 {
232 	return (FALSE);
233 }
234 
235 static void
236 svc_raw_ops(xprt)
237 	SVCXPRT *xprt;
238 {
239 	static struct xp_ops ops;
240 	static struct xp_ops2 ops2;
241 #ifdef __REENT
242 	extern mutex_t ops_lock;
243 #endif
244 
245 /* VARIABLES PROTECTED BY ops_lock: ops */
246 
247 	mutex_lock(&ops_lock);
248 	if (ops.xp_recv == NULL) {
249 		ops.xp_recv = svc_raw_recv;
250 		ops.xp_stat = svc_raw_stat;
251 		ops.xp_getargs = svc_raw_getargs;
252 		ops.xp_reply = svc_raw_reply;
253 		ops.xp_freeargs = svc_raw_freeargs;
254 		ops.xp_destroy = svc_raw_destroy;
255 		ops2.xp_control = svc_raw_control;
256 	}
257 	xprt->xp_ops = &ops;
258 	xprt->xp_ops2 = &ops2;
259 	mutex_unlock(&ops_lock);
260 }
261