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