Merge branch 'vendor/GCC50'
[dragonfly.git] / lib / libc / rpc / svc_raw.c
1 /*-
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without 
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice, 
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice, 
10  *   this list of conditions and the following disclaimer in the documentation 
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its 
13  *   contributors may be used to endorse or promote products derived 
14  *   from this software without specific prior written permission.
15  * 
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  * @(#)svc_raw.c 1.25 89/01/31 Copyr 1984 Sun Micro
29  * @(#)svc_raw.c        1.16    94/04/24 SMI
30  * $NetBSD: svc_raw.c,v 1.14 2000/07/06 03:10:35 christos Exp $
31  * $FreeBSD: src/lib/libc/rpc/svc_raw.c,v 1.15 2006/02/27 22:10:59 deischen Exp $
32  */
33 /*
34  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35  */
36
37 /*
38  * svc_raw.c,   This a toy for simple testing and timing.
39  * Interface to create an rpc client and server in the same UNIX process.
40  * This lets us similate rpc and get rpc (round trip) overhead, without
41  * any interference from the kernel.
42  *
43  */
44
45 #include "namespace.h"
46 #include "reentrant.h"
47 #include <rpc/rpc.h>
48 #include <sys/types.h>
49 #include <rpc/raw.h>
50 #include <stdlib.h>
51 #include "un-namespace.h"
52 #include "mt_misc.h"
53
54 #ifndef UDPMSGSIZE
55 #define UDPMSGSIZE 8800
56 #endif
57
58 /*
59  * This is the "network" that we will be moving data over
60  */
61 static struct svc_raw_private {
62         char    *raw_buf;       /* should be shared with the cl handle */
63         SVCXPRT server;
64         XDR     xdr_stream;
65         char    verf_body[MAX_AUTH_BYTES];
66 } *svc_raw_private;
67
68 static enum xprt_stat svc_raw_stat(SVCXPRT *);
69 static bool_t svc_raw_recv(SVCXPRT *, struct rpc_msg *);
70 static bool_t svc_raw_reply(SVCXPRT *, struct rpc_msg *);
71 static bool_t svc_raw_getargs(SVCXPRT *, xdrproc_t, void *);
72 static bool_t svc_raw_freeargs(SVCXPRT *, xdrproc_t, void *);
73 static void svc_raw_destroy(SVCXPRT *);
74 static void svc_raw_ops(SVCXPRT *);
75 static bool_t svc_raw_control(SVCXPRT *, const u_int, void *);
76
77 char *__rpc_rawcombuf = NULL;
78
79 SVCXPRT *
80 svc_raw_create(void)
81 {
82         struct svc_raw_private *srp;
83 /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */
84
85         mutex_lock(&svcraw_lock);
86         srp = svc_raw_private;
87         if (srp == NULL) {
88                 srp = (struct svc_raw_private *)calloc(1, sizeof (*srp));
89                 if (srp == NULL) {
90                         mutex_unlock(&svcraw_lock);
91                         return (NULL);
92                 }
93                 if (__rpc_rawcombuf == NULL)
94                         __rpc_rawcombuf = calloc(UDPMSGSIZE, sizeof (char));
95                 srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */
96                 svc_raw_private = srp;
97         }
98         srp->server.xp_fd = FD_SETSIZE;
99         srp->server.xp_port = 0;
100         srp->server.xp_p3 = NULL;
101         svc_raw_ops(&srp->server);
102         srp->server.xp_verf.oa_base = srp->verf_body;
103         xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE);
104         xprt_register(&srp->server);
105         mutex_unlock(&svcraw_lock);
106         return (&srp->server);
107 }
108
109 /*ARGSUSED*/
110 static enum xprt_stat
111 svc_raw_stat(SVCXPRT *xprt __unused)
112 {
113         return (XPRT_IDLE);
114 }
115
116 /*ARGSUSED*/
117 static bool_t
118 svc_raw_recv(SVCXPRT *xprt __unused, struct rpc_msg *msg)
119 {
120         struct svc_raw_private *srp;
121         XDR *xdrs;
122
123         mutex_lock(&svcraw_lock);
124         srp = svc_raw_private;
125         if (srp == NULL) {
126                 mutex_unlock(&svcraw_lock);
127                 return (FALSE);
128         }
129         mutex_unlock(&svcraw_lock);
130
131         xdrs = &srp->xdr_stream;
132         xdrs->x_op = XDR_DECODE;
133         XDR_SETPOS(xdrs, 0);
134         if (! xdr_callmsg(xdrs, msg)) {
135                 return (FALSE);
136         }
137         return (TRUE);
138 }
139
140 /*ARGSUSED*/
141 static bool_t
142 svc_raw_reply(SVCXPRT *xprt __unused, struct rpc_msg *msg)
143 {
144         struct svc_raw_private *srp;
145         XDR *xdrs;
146
147         mutex_lock(&svcraw_lock);
148         srp = svc_raw_private;
149         if (srp == NULL) {
150                 mutex_unlock(&svcraw_lock);
151                 return (FALSE);
152         }
153         mutex_unlock(&svcraw_lock);
154
155         xdrs = &srp->xdr_stream;
156         xdrs->x_op = XDR_ENCODE;
157         XDR_SETPOS(xdrs, 0);
158         if (! xdr_replymsg(xdrs, msg)) {
159                 return (FALSE);
160         }
161         XDR_GETPOS(xdrs);  /* called just for overhead */
162         return (TRUE);
163 }
164
165 /*ARGSUSED*/
166 static bool_t
167 svc_raw_getargs(SVCXPRT *xprt __unused, xdrproc_t xdr_args, void *args_ptr)
168 {
169         struct svc_raw_private *srp;
170
171         mutex_lock(&svcraw_lock);
172         srp = svc_raw_private;
173         if (srp == NULL) {
174                 mutex_unlock(&svcraw_lock);
175                 return (FALSE);
176         }
177         mutex_unlock(&svcraw_lock);
178         return (*xdr_args)(&srp->xdr_stream, args_ptr);
179 }
180
181 /*ARGSUSED*/
182 static bool_t
183 svc_raw_freeargs(SVCXPRT *xprt __unused, xdrproc_t xdr_args, void *args_ptr)
184 {
185         struct svc_raw_private *srp;
186         XDR *xdrs;
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
196         xdrs = &srp->xdr_stream;
197         xdrs->x_op = XDR_FREE;
198         return (*xdr_args)(xdrs, args_ptr);
199 }
200
201 /*ARGSUSED*/
202 static void
203 svc_raw_destroy(SVCXPRT *xprt __unused)
204 {
205 }
206
207 /*ARGSUSED*/
208 static bool_t
209 svc_raw_control(SVCXPRT *xprt __unused, const u_int rq __unused,
210     void *in __unused)
211 {
212         return (FALSE);
213 }
214
215 static void
216 svc_raw_ops(SVCXPRT *xprt)
217 {
218         static struct xp_ops ops;
219         static struct xp_ops2 ops2;
220
221 /* VARIABLES PROTECTED BY ops_lock: ops */
222
223         mutex_lock(&ops_lock);
224         if (ops.xp_recv == NULL) {
225                 ops.xp_recv = svc_raw_recv;
226                 ops.xp_stat = svc_raw_stat;
227                 ops.xp_getargs = svc_raw_getargs;
228                 ops.xp_reply = svc_raw_reply;
229                 ops.xp_freeargs = svc_raw_freeargs;
230                 ops.xp_destroy = svc_raw_destroy;
231                 ops2.xp_control = svc_raw_control;
232         }
233         xprt->xp_ops = &ops;
234         xprt->xp_ops2 = &ops2;
235         mutex_unlock(&ops_lock);
236 }