svc_raw.c revision 92905
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 0 38#if defined(SCCSIDS) && !defined(lint) 39static char sccsid[] = "@(#)svc_raw.c 1.25 89/01/31 Copyr 1984 Sun Micro"; 40static char *rcsid = "$FreeBSD: head/lib/libc/rpc/svc_raw.c 92905 2002-03-21 22:49:10Z obrien $"; 41#endif 42#endif 43 44/* 45 * svc_raw.c, This a toy for simple testing and timing. 46 * Interface to create an rpc client and server in the same UNIX process. 47 * This lets us similate rpc and get rpc (round trip) overhead, without 48 * any interference from the kernel. 49 * 50 */ 51 52#include "namespace.h" 53#include "reentrant.h" 54#include <rpc/rpc.h> 55#include <sys/types.h> 56#include <rpc/raw.h> 57#include <stdlib.h> 58#include "un-namespace.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 */ 67static 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 74extern mutex_t svcraw_lock; 75 76static enum xprt_stat svc_raw_stat(SVCXPRT *); 77static bool_t svc_raw_recv(SVCXPRT *, struct rpc_msg *); 78static bool_t svc_raw_reply(SVCXPRT *, struct rpc_msg *); 79static bool_t svc_raw_getargs(SVCXPRT *, xdrproc_t, caddr_t); 80static bool_t svc_raw_freeargs(SVCXPRT *, xdrproc_t, caddr_t); 81static void svc_raw_destroy(SVCXPRT *); 82static void svc_raw_ops(SVCXPRT *); 83static bool_t svc_raw_control(SVCXPRT *, const u_int, void *); 84 85char *__rpc_rawcombuf = NULL; 86 87SVCXPRT * 88svc_raw_create() 89{ 90 struct svc_raw_private *srp; 91/* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */ 92 93 mutex_lock(&svcraw_lock); 94 srp = svc_raw_private; 95 if (srp == NULL) { 96 srp = (struct svc_raw_private *)calloc(1, sizeof (*srp)); 97 if (srp == NULL) { 98 mutex_unlock(&svcraw_lock); 99 return (NULL); 100 } 101 if (__rpc_rawcombuf == NULL) 102 __rpc_rawcombuf = calloc(UDPMSGSIZE, sizeof (char)); 103 srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */ 104 svc_raw_private = srp; 105 } 106 srp->server.xp_fd = FD_SETSIZE; 107 srp->server.xp_port = 0; 108 srp->server.xp_p3 = NULL; 109 svc_raw_ops(&srp->server); 110 srp->server.xp_verf.oa_base = srp->verf_body; 111 xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE); 112 xprt_register(&srp->server); 113 mutex_unlock(&svcraw_lock); 114 return (&srp->server); 115} 116 117/*ARGSUSED*/ 118static enum xprt_stat 119svc_raw_stat(xprt) 120SVCXPRT *xprt; /* args needed to satisfy ANSI-C typechecking */ 121{ 122 return (XPRT_IDLE); 123} 124 125/*ARGSUSED*/ 126static bool_t 127svc_raw_recv(xprt, msg) 128 SVCXPRT *xprt; 129 struct rpc_msg *msg; 130{ 131 struct svc_raw_private *srp; 132 XDR *xdrs; 133 134 mutex_lock(&svcraw_lock); 135 srp = svc_raw_private; 136 if (srp == NULL) { 137 mutex_unlock(&svcraw_lock); 138 return (FALSE); 139 } 140 mutex_unlock(&svcraw_lock); 141 142 xdrs = &srp->xdr_stream; 143 xdrs->x_op = XDR_DECODE; 144 (void) XDR_SETPOS(xdrs, 0); 145 if (! xdr_callmsg(xdrs, msg)) { 146 return (FALSE); 147 } 148 return (TRUE); 149} 150 151/*ARGSUSED*/ 152static bool_t 153svc_raw_reply(xprt, msg) 154 SVCXPRT *xprt; 155 struct rpc_msg *msg; 156{ 157 struct svc_raw_private *srp; 158 XDR *xdrs; 159 160 mutex_lock(&svcraw_lock); 161 srp = svc_raw_private; 162 if (srp == NULL) { 163 mutex_unlock(&svcraw_lock); 164 return (FALSE); 165 } 166 mutex_unlock(&svcraw_lock); 167 168 xdrs = &srp->xdr_stream; 169 xdrs->x_op = XDR_ENCODE; 170 (void) XDR_SETPOS(xdrs, 0); 171 if (! xdr_replymsg(xdrs, msg)) { 172 return (FALSE); 173 } 174 (void) XDR_GETPOS(xdrs); /* called just for overhead */ 175 return (TRUE); 176} 177 178/*ARGSUSED*/ 179static bool_t 180svc_raw_getargs(xprt, xdr_args, args_ptr) 181 SVCXPRT *xprt; 182 xdrproc_t xdr_args; 183 caddr_t args_ptr; 184{ 185 struct svc_raw_private *srp; 186 187 mutex_lock(&svcraw_lock); 188 srp = svc_raw_private; 189 if (srp == NULL) { 190 mutex_unlock(&svcraw_lock); 191 return (FALSE); 192 } 193 mutex_unlock(&svcraw_lock); 194 return (*xdr_args)(&srp->xdr_stream, args_ptr); 195} 196 197/*ARGSUSED*/ 198static bool_t 199svc_raw_freeargs(xprt, xdr_args, args_ptr) 200 SVCXPRT *xprt; 201 xdrproc_t xdr_args; 202 caddr_t args_ptr; 203{ 204 struct svc_raw_private *srp; 205 XDR *xdrs; 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 xdrs = &srp->xdr_stream; 216 xdrs->x_op = XDR_FREE; 217 return (*xdr_args)(xdrs, args_ptr); 218} 219 220/*ARGSUSED*/ 221static void 222svc_raw_destroy(xprt) 223SVCXPRT *xprt; 224{ 225} 226 227/*ARGSUSED*/ 228static bool_t 229svc_raw_control(xprt, rq, in) 230 SVCXPRT *xprt; 231 const u_int rq; 232 void *in; 233{ 234 return (FALSE); 235} 236 237static void 238svc_raw_ops(xprt) 239 SVCXPRT *xprt; 240{ 241 static struct xp_ops ops; 242 static struct xp_ops2 ops2; 243 extern mutex_t ops_lock; 244 245/* VARIABLES PROTECTED BY ops_lock: ops */ 246 247 mutex_lock(&ops_lock); 248 if (ops.xp_recv == NULL) { 249 ops.xp_recv = svc_raw_recv; 250 ops.xp_stat = svc_raw_stat; 251 ops.xp_getargs = svc_raw_getargs; 252 ops.xp_reply = svc_raw_reply; 253 ops.xp_freeargs = svc_raw_freeargs; 254 ops.xp_destroy = svc_raw_destroy; 255 ops2.xp_control = svc_raw_control; 256 } 257 xprt->xp_ops = &ops; 258 xprt->xp_ops2 = &ops2; 259 mutex_unlock(&ops_lock); 260} 261