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