source: rtems-libbsd/freebsd/lib/libc/rpc/svc_raw.c

6-freebsd-12
Last change on this file was bb80d9d, checked in by Sebastian Huber <sebastian.huber@…>, on 08/09/18 at 12:02:09

Update to FreeBSD head 2017-12-01

Git mirror commit e724f51f811a4b2bd29447f8b85ab5c2f9b88266.

Update #3472.

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