xref: /dragonfly/lib/libc/rpc/svc_raw.c (revision 99dd49c5)
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
29  * @(#)svc_raw.c 1.25 89/01/31 Copyr 1984 Sun Micro
30  * @(#)svc_raw.c	1.16	94/04/24 SMI
31  * $NetBSD: svc_raw.c,v 1.14 2000/07/06 03:10:35 christos Exp $
32  * $FreeBSD: src/lib/libc/rpc/svc_raw.c,v 1.15 2006/02/27 22:10:59 deischen Exp $
33  * $DragonFly: src/lib/libc/rpc/svc_raw.c,v 1.4 2005/11/13 12:27:04 swildner Exp $
34  */
35 /*
36  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
37  */
38 
39 /*
40  * svc_raw.c,   This a toy for simple testing and timing.
41  * Interface to create an rpc client and server in the same UNIX process.
42  * This lets us similate rpc and get rpc (round trip) overhead, without
43  * any interference from the kernel.
44  *
45  */
46 
47 #include "namespace.h"
48 #include "reentrant.h"
49 #include <rpc/rpc.h>
50 #include <sys/types.h>
51 #include <rpc/raw.h>
52 #include <stdlib.h>
53 #include "un-namespace.h"
54 #include "mt_misc.h"
55 
56 #ifndef UDPMSGSIZE
57 #define	UDPMSGSIZE 8800
58 #endif
59 
60 /*
61  * This is the "network" that we will be moving data over
62  */
63 static struct svc_raw_private {
64 	char	*raw_buf;	/* should be shared with the cl handle */
65 	SVCXPRT	server;
66 	XDR	xdr_stream;
67 	char	verf_body[MAX_AUTH_BYTES];
68 } *svc_raw_private;
69 
70 static enum xprt_stat svc_raw_stat(SVCXPRT *);
71 static bool_t svc_raw_recv(SVCXPRT *, struct rpc_msg *);
72 static bool_t svc_raw_reply(SVCXPRT *, struct rpc_msg *);
73 static bool_t svc_raw_getargs(SVCXPRT *, xdrproc_t, void *);
74 static bool_t svc_raw_freeargs(SVCXPRT *, xdrproc_t, void *);
75 static void svc_raw_destroy(SVCXPRT *);
76 static void svc_raw_ops(SVCXPRT *);
77 static bool_t svc_raw_control(SVCXPRT *, const u_int, void *);
78 
79 char *__rpc_rawcombuf = NULL;
80 
81 SVCXPRT *
82 svc_raw_create(void)
83 {
84 	struct svc_raw_private *srp;
85 /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
86 
87 	mutex_lock(&svcraw_lock);
88 	srp = svc_raw_private;
89 	if (srp == NULL) {
90 		srp = (struct svc_raw_private *)calloc(1, sizeof (*srp));
91 		if (srp == NULL) {
92 			mutex_unlock(&svcraw_lock);
93 			return (NULL);
94 		}
95 		if (__rpc_rawcombuf == NULL)
96 			__rpc_rawcombuf = calloc(UDPMSGSIZE, sizeof (char));
97 		srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */
98 		svc_raw_private = srp;
99 	}
100 	srp->server.xp_fd = FD_SETSIZE;
101 	srp->server.xp_port = 0;
102 	srp->server.xp_p3 = NULL;
103 	svc_raw_ops(&srp->server);
104 	srp->server.xp_verf.oa_base = srp->verf_body;
105 	xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
106 	xprt_register(&srp->server);
107 	mutex_unlock(&svcraw_lock);
108 	return (&srp->server);
109 }
110 
111 /*ARGSUSED*/
112 static enum xprt_stat
113 svc_raw_stat(SVCXPRT *xprt)
114 {
115 	return (XPRT_IDLE);
116 }
117 
118 /*ARGSUSED*/
119 static bool_t
120 svc_raw_recv(SVCXPRT *xprt, struct rpc_msg *msg)
121 {
122 	struct svc_raw_private *srp;
123 	XDR *xdrs;
124 
125 	mutex_lock(&svcraw_lock);
126 	srp = svc_raw_private;
127 	if (srp == NULL) {
128 		mutex_unlock(&svcraw_lock);
129 		return (FALSE);
130 	}
131 	mutex_unlock(&svcraw_lock);
132 
133 	xdrs = &srp->xdr_stream;
134 	xdrs->x_op = XDR_DECODE;
135 	XDR_SETPOS(xdrs, 0);
136 	if (! xdr_callmsg(xdrs, msg)) {
137 		return (FALSE);
138 	}
139 	return (TRUE);
140 }
141 
142 /*ARGSUSED*/
143 static bool_t
144 svc_raw_reply(SVCXPRT *xprt, struct rpc_msg *msg)
145 {
146 	struct svc_raw_private *srp;
147 	XDR *xdrs;
148 
149 	mutex_lock(&svcraw_lock);
150 	srp = svc_raw_private;
151 	if (srp == NULL) {
152 		mutex_unlock(&svcraw_lock);
153 		return (FALSE);
154 	}
155 	mutex_unlock(&svcraw_lock);
156 
157 	xdrs = &srp->xdr_stream;
158 	xdrs->x_op = XDR_ENCODE;
159 	XDR_SETPOS(xdrs, 0);
160 	if (! xdr_replymsg(xdrs, msg)) {
161 		return (FALSE);
162 	}
163 	XDR_GETPOS(xdrs);  /* called just for overhead */
164 	return (TRUE);
165 }
166 
167 /*ARGSUSED*/
168 static bool_t
169 svc_raw_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
170 {
171 	struct svc_raw_private *srp;
172 
173 	mutex_lock(&svcraw_lock);
174 	srp = svc_raw_private;
175 	if (srp == NULL) {
176 		mutex_unlock(&svcraw_lock);
177 		return (FALSE);
178 	}
179 	mutex_unlock(&svcraw_lock);
180 	return (*xdr_args)(&srp->xdr_stream, args_ptr);
181 }
182 
183 /*ARGSUSED*/
184 static bool_t
185 svc_raw_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
186 {
187 	struct svc_raw_private *srp;
188 	XDR *xdrs;
189 
190 	mutex_lock(&svcraw_lock);
191 	srp = svc_raw_private;
192 	if (srp == NULL) {
193 		mutex_unlock(&svcraw_lock);
194 		return (FALSE);
195 	}
196 	mutex_unlock(&svcraw_lock);
197 
198 	xdrs = &srp->xdr_stream;
199 	xdrs->x_op = XDR_FREE;
200 	return (*xdr_args)(xdrs, args_ptr);
201 }
202 
203 /*ARGSUSED*/
204 static void
205 svc_raw_destroy(SVCXPRT *xprt)
206 {
207 }
208 
209 /*ARGSUSED*/
210 static bool_t
211 svc_raw_control(SVCXPRT *xprt, const u_int rq, void *in)
212 {
213 	return (FALSE);
214 }
215 
216 static void
217 svc_raw_ops(SVCXPRT *xprt)
218 {
219 	static struct xp_ops ops;
220 	static struct xp_ops2 ops2;
221 
222 /* VARIABLES PROTECTED BY ops_lock: ops */
223 
224 	mutex_lock(&ops_lock);
225 	if (ops.xp_recv == NULL) {
226 		ops.xp_recv = svc_raw_recv;
227 		ops.xp_stat = svc_raw_stat;
228 		ops.xp_getargs = svc_raw_getargs;
229 		ops.xp_reply = svc_raw_reply;
230 		ops.xp_freeargs = svc_raw_freeargs;
231 		ops.xp_destroy = svc_raw_destroy;
232 		ops2.xp_control = svc_raw_control;
233 	}
234 	xprt->xp_ops = &ops;
235 	xprt->xp_ops2 = &ops2;
236 	mutex_unlock(&ops_lock);
237 }
238