xref: /netbsd-src/lib/libc/rpc/svc_raw.c (revision 5aefcfdc06931dd97e76246d2fe0302f7b3fe094)
1 /*	$NetBSD: svc_raw.c,v 1.14 2000/07/06 03:10:35 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 #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 (NULL);
104 		}
105 		if (__rpc_rawcombuf == NULL)
106 			__rpc_rawcombuf = calloc(UDPMSGSIZE, sizeof (char));
107 		srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */
108 		svc_raw_private = srp;
109 	}
110 	srp->server.xp_fd = FD_SETSIZE;
111 	srp->server.xp_port = 0;
112 	srp->server.xp_p3 = NULL;
113 	svc_raw_ops(&srp->server);
114 	srp->server.xp_verf.oa_base = srp->verf_body;
115 	xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
116 	xprt_register(&srp->server);
117 	mutex_unlock(&svcraw_lock);
118 	return (&srp->server);
119 }
120 
121 /*ARGSUSED*/
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 /*ARGSUSED*/
130 static bool_t
131 svc_raw_recv(xprt, msg)
132 	SVCXPRT *xprt;
133 	struct rpc_msg *msg;
134 {
135 	struct svc_raw_private *srp;
136 	XDR *xdrs;
137 
138 	mutex_lock(&svcraw_lock);
139 	srp = svc_raw_private;
140 	if (srp == NULL) {
141 		mutex_unlock(&svcraw_lock);
142 		return (FALSE);
143 	}
144 	mutex_unlock(&svcraw_lock);
145 
146 	xdrs = &srp->xdr_stream;
147 	xdrs->x_op = XDR_DECODE;
148 	(void) XDR_SETPOS(xdrs, 0);
149 	if (! xdr_callmsg(xdrs, msg)) {
150 		return (FALSE);
151 	}
152 	return (TRUE);
153 }
154 
155 /*ARGSUSED*/
156 static bool_t
157 svc_raw_reply(xprt, msg)
158 	SVCXPRT *xprt;
159 	struct rpc_msg *msg;
160 {
161 	struct svc_raw_private *srp;
162 	XDR *xdrs;
163 
164 	mutex_lock(&svcraw_lock);
165 	srp = svc_raw_private;
166 	if (srp == NULL) {
167 		mutex_unlock(&svcraw_lock);
168 		return (FALSE);
169 	}
170 	mutex_unlock(&svcraw_lock);
171 
172 	xdrs = &srp->xdr_stream;
173 	xdrs->x_op = XDR_ENCODE;
174 	(void) XDR_SETPOS(xdrs, 0);
175 	if (! xdr_replymsg(xdrs, msg)) {
176 		return (FALSE);
177 	}
178 	(void) XDR_GETPOS(xdrs);  /* called just for overhead */
179 	return (TRUE);
180 }
181 
182 /*ARGSUSED*/
183 static bool_t
184 svc_raw_getargs(xprt, xdr_args, args_ptr)
185 	SVCXPRT *xprt;
186 	xdrproc_t xdr_args;
187 	caddr_t args_ptr;
188 {
189 	struct svc_raw_private *srp;
190 
191 	mutex_lock(&svcraw_lock);
192 	srp = svc_raw_private;
193 	if (srp == NULL) {
194 		mutex_unlock(&svcraw_lock);
195 		return (FALSE);
196 	}
197 	mutex_unlock(&svcraw_lock);
198 	return (*xdr_args)(&srp->xdr_stream, args_ptr);
199 }
200 
201 /*ARGSUSED*/
202 static bool_t
203 svc_raw_freeargs(xprt, xdr_args, args_ptr)
204 	SVCXPRT *xprt;
205 	xdrproc_t xdr_args;
206 	caddr_t args_ptr;
207 {
208 	struct svc_raw_private *srp;
209 	XDR *xdrs;
210 
211 	mutex_lock(&svcraw_lock);
212 	srp = svc_raw_private;
213 	if (srp == NULL) {
214 		mutex_unlock(&svcraw_lock);
215 		return (FALSE);
216 	}
217 	mutex_unlock(&svcraw_lock);
218 
219 	xdrs = &srp->xdr_stream;
220 	xdrs->x_op = XDR_FREE;
221 	return (*xdr_args)(xdrs, args_ptr);
222 }
223 
224 /*ARGSUSED*/
225 static void
226 svc_raw_destroy(xprt)
227 SVCXPRT *xprt;
228 {
229 }
230 
231 /*ARGSUSED*/
232 static bool_t
233 svc_raw_control(xprt, rq, in)
234 	SVCXPRT *xprt;
235 	const u_int	rq;
236 	void		*in;
237 {
238 	return (FALSE);
239 }
240 
241 static void
242 svc_raw_ops(xprt)
243 	SVCXPRT *xprt;
244 {
245 	static struct xp_ops ops;
246 	static struct xp_ops2 ops2;
247 #ifdef __REENT
248 	extern mutex_t ops_lock;
249 #endif
250 
251 /* VARIABLES PROTECTED BY ops_lock: ops */
252 
253 	mutex_lock(&ops_lock);
254 	if (ops.xp_recv == NULL) {
255 		ops.xp_recv = svc_raw_recv;
256 		ops.xp_stat = svc_raw_stat;
257 		ops.xp_getargs = svc_raw_getargs;
258 		ops.xp_reply = svc_raw_reply;
259 		ops.xp_freeargs = svc_raw_freeargs;
260 		ops.xp_destroy = svc_raw_destroy;
261 		ops2.xp_control = svc_raw_control;
262 	}
263 	xprt->xp_ops = &ops;
264 	xprt->xp_ops2 = &ops2;
265 	mutex_unlock(&ops_lock);
266 }
267