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 */ 34 /* 35 * Copyright (c) 1986-1991 by Sun Microsystems Inc. 36 */ 37 38 /* 39 * svc_raw.c, This a toy for simple testing and timing. 40 * Interface to create an rpc client and server in the same UNIX process. 41 * This lets us similate rpc and get rpc (round trip) overhead, without 42 * any interference from the kernel. 43 * 44 */ 45 46 #include "namespace.h" 47 #include "reentrant.h" 48 #include <rpc/rpc.h> 49 #include <sys/types.h> 50 #include <rpc/raw.h> 51 #include <stdlib.h> 52 #include "un-namespace.h" 53 #include "mt_misc.h" 54 55 #ifndef UDPMSGSIZE 56 #define UDPMSGSIZE 8800 57 #endif 58 59 /* 60 * This is the "network" that we will be moving data over 61 */ 62 static struct svc_raw_private { 63 char *raw_buf; /* should be shared with the cl handle */ 64 SVCXPRT server; 65 XDR xdr_stream; 66 char verf_body[MAX_AUTH_BYTES]; 67 } *svc_raw_private; 68 69 static enum xprt_stat svc_raw_stat(SVCXPRT *); 70 static bool_t svc_raw_recv(SVCXPRT *, struct rpc_msg *); 71 static bool_t svc_raw_reply(SVCXPRT *, struct rpc_msg *); 72 static bool_t svc_raw_getargs(SVCXPRT *, xdrproc_t, void *); 73 static bool_t svc_raw_freeargs(SVCXPRT *, xdrproc_t, void *); 74 static void svc_raw_destroy(SVCXPRT *); 75 static void svc_raw_ops(SVCXPRT *); 76 static bool_t svc_raw_control(SVCXPRT *, const u_int, void *); 77 78 char *__rpc_rawcombuf = NULL; 79 80 SVCXPRT * 81 svc_raw_create(void) 82 { 83 struct svc_raw_private *srp; 84 /* VARIABLES PROTECTED BY svcraw_lock: svc_raw_private, srp */ 85 86 mutex_lock(&svcraw_lock); 87 srp = svc_raw_private; 88 if (srp == NULL) { 89 srp = (struct svc_raw_private *)calloc(1, sizeof (*srp)); 90 if (srp == NULL) { 91 mutex_unlock(&svcraw_lock); 92 return (NULL); 93 } 94 if (__rpc_rawcombuf == NULL) 95 __rpc_rawcombuf = calloc(UDPMSGSIZE, sizeof (char)); 96 srp->raw_buf = __rpc_rawcombuf; /* Share it with the client */ 97 svc_raw_private = srp; 98 } 99 srp->server.xp_fd = FD_SETSIZE; 100 srp->server.xp_port = 0; 101 srp->server.xp_p3 = NULL; 102 svc_raw_ops(&srp->server); 103 srp->server.xp_verf.oa_base = srp->verf_body; 104 xdrmem_create(&srp->xdr_stream, srp->raw_buf, UDPMSGSIZE, XDR_DECODE); 105 xprt_register(&srp->server); 106 mutex_unlock(&svcraw_lock); 107 return (&srp->server); 108 } 109 110 /*ARGSUSED*/ 111 static enum xprt_stat 112 svc_raw_stat(SVCXPRT *xprt __unused) 113 { 114 return (XPRT_IDLE); 115 } 116 117 /*ARGSUSED*/ 118 static bool_t 119 svc_raw_recv(SVCXPRT *xprt __unused, struct rpc_msg *msg) 120 { 121 struct svc_raw_private *srp; 122 XDR *xdrs; 123 124 mutex_lock(&svcraw_lock); 125 srp = svc_raw_private; 126 if (srp == NULL) { 127 mutex_unlock(&svcraw_lock); 128 return (FALSE); 129 } 130 mutex_unlock(&svcraw_lock); 131 132 xdrs = &srp->xdr_stream; 133 xdrs->x_op = XDR_DECODE; 134 XDR_SETPOS(xdrs, 0); 135 if (! xdr_callmsg(xdrs, msg)) { 136 return (FALSE); 137 } 138 return (TRUE); 139 } 140 141 /*ARGSUSED*/ 142 static bool_t 143 svc_raw_reply(SVCXPRT *xprt __unused, struct rpc_msg *msg) 144 { 145 struct svc_raw_private *srp; 146 XDR *xdrs; 147 148 mutex_lock(&svcraw_lock); 149 srp = svc_raw_private; 150 if (srp == NULL) { 151 mutex_unlock(&svcraw_lock); 152 return (FALSE); 153 } 154 mutex_unlock(&svcraw_lock); 155 156 xdrs = &srp->xdr_stream; 157 xdrs->x_op = XDR_ENCODE; 158 XDR_SETPOS(xdrs, 0); 159 if (! xdr_replymsg(xdrs, msg)) { 160 return (FALSE); 161 } 162 XDR_GETPOS(xdrs); /* called just for overhead */ 163 return (TRUE); 164 } 165 166 /*ARGSUSED*/ 167 static bool_t 168 svc_raw_getargs(SVCXPRT *xprt __unused, xdrproc_t xdr_args, void *args_ptr) 169 { 170 struct svc_raw_private *srp; 171 172 mutex_lock(&svcraw_lock); 173 srp = svc_raw_private; 174 if (srp == NULL) { 175 mutex_unlock(&svcraw_lock); 176 return (FALSE); 177 } 178 mutex_unlock(&svcraw_lock); 179 return (*xdr_args)(&srp->xdr_stream, args_ptr); 180 } 181 182 /*ARGSUSED*/ 183 static bool_t 184 svc_raw_freeargs(SVCXPRT *xprt __unused, xdrproc_t xdr_args, void *args_ptr) 185 { 186 struct svc_raw_private *srp; 187 XDR *xdrs; 188 189 mutex_lock(&svcraw_lock); 190 srp = svc_raw_private; 191 if (srp == NULL) { 192 mutex_unlock(&svcraw_lock); 193 return (FALSE); 194 } 195 mutex_unlock(&svcraw_lock); 196 197 xdrs = &srp->xdr_stream; 198 xdrs->x_op = XDR_FREE; 199 return (*xdr_args)(xdrs, args_ptr); 200 } 201 202 /*ARGSUSED*/ 203 static void 204 svc_raw_destroy(SVCXPRT *xprt __unused) 205 { 206 } 207 208 /*ARGSUSED*/ 209 static bool_t 210 svc_raw_control(SVCXPRT *xprt __unused, const u_int rq __unused, 211 void *in __unused) 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