clnt_raw.c revision 99996
197403Sobrien/* $NetBSD: clnt_raw.c,v 1.20 2000/12/10 04:12:03 christos Exp $ */ 297403Sobrien 3169691Skan/* 497403Sobrien * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 5132720Skan * unrestricted use provided that this legend is included on all tape 697403Sobrien * media and as a part of the software program in whole or part. Users 7132720Skan * may copy or modify Sun RPC without charge, but are not authorized 897403Sobrien * to license or distribute it to anyone else except as part of a product or 997403Sobrien * program developed by the user. 1097403Sobrien * 1197403Sobrien * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 12132720Skan * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 1397403Sobrien * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 1497403Sobrien * 1597403Sobrien * Sun RPC is provided with no support and without any obligation on the 1697403Sobrien * part of Sun Microsystems, Inc. to assist in its use, correction, 1797403Sobrien * modification or enhancement. 18132720Skan * 19169691Skan * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 20169691Skan * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 2197403Sobrien * OR ANY PART THEREOF. 2297403Sobrien * 2397403Sobrien * In no event will Sun Microsystems, Inc. be liable for any lost revenue 2497403Sobrien * or profits or other special, indirect and consequential damages, even if 2597403Sobrien * Sun has been advised of the possibility of such damages. 2697403Sobrien * 2797403Sobrien * Sun Microsystems, Inc. 2897403Sobrien * 2550 Garcia Avenue 2997403Sobrien * Mountain View, California 94043 3097403Sobrien */ 31169691Skan 3297403Sobrien#if defined(LIBC_SCCS) && !defined(lint) 3397403Sobrienstatic char *sccsid = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro"; 3497403Sobrienstatic char *sccsid = "@(#)clnt_raw.c 2.2 88/08/01 4.0 RPCSRC"; 3597403Sobrien#endif 36169691Skan#include <sys/cdefs.h> 3797403Sobrien__FBSDID("$FreeBSD: head/lib/libc/rpc/clnt_raw.c 99996 2002-07-14 23:14:02Z alfred $"); 3897403Sobrien 3997403Sobrien/* 4097403Sobrien * clnt_raw.c 4197403Sobrien * 42 * Copyright (C) 1984, Sun Microsystems, Inc. 43 * 44 * Memory based rpc for simple testing and timing. 45 * Interface to create an rpc client and server in the same process. 46 * This lets us similate rpc and get round trip overhead, without 47 * any interference from the kernel. 48 */ 49 50#include "namespace.h" 51#include "reentrant.h" 52#include <assert.h> 53#include <err.h> 54#include <stdio.h> 55#include <stdlib.h> 56 57#include <rpc/rpc.h> 58#include <rpc/raw.h> 59#include "un-namespace.h" 60 61extern mutex_t clntraw_lock; 62 63#define MCALL_MSG_SIZE 24 64 65/* 66 * This is the "network" we will be moving stuff over. 67 */ 68static struct clntraw_private { 69 CLIENT client_object; 70 XDR xdr_stream; 71 char *_raw_buf; 72 union { 73 struct rpc_msg mashl_rpcmsg; 74 char mashl_callmsg[MCALL_MSG_SIZE]; 75 } u; 76 u_int mcnt; 77} *clntraw_private; 78 79static enum clnt_stat clnt_raw_call(CLIENT *, rpcproc_t, xdrproc_t, void *, 80 xdrproc_t, void *, struct timeval); 81static void clnt_raw_geterr(CLIENT *, struct rpc_err *); 82static bool_t clnt_raw_freeres(CLIENT *, xdrproc_t, void *); 83static void clnt_raw_abort(CLIENT *); 84static bool_t clnt_raw_control(CLIENT *, u_int, void *); 85static void clnt_raw_destroy(CLIENT *); 86static struct clnt_ops *clnt_raw_ops(void); 87 88/* 89 * Create a client handle for memory based rpc. 90 */ 91CLIENT * 92clnt_raw_create(prog, vers) 93 rpcprog_t prog; 94 rpcvers_t vers; 95{ 96 struct clntraw_private *clp = clntraw_private; 97 struct rpc_msg call_msg; 98 XDR *xdrs = &clp->xdr_stream; 99 CLIENT *client = &clp->client_object; 100 101 mutex_lock(&clntraw_lock); 102 if (clp == NULL) { 103 clp = (struct clntraw_private *)calloc(1, sizeof (*clp)); 104 if (clp == NULL) { 105 mutex_unlock(&clntraw_lock); 106 return NULL; 107 } 108 if (__rpc_rawcombuf == NULL) 109 __rpc_rawcombuf = 110 (char *)calloc(UDPMSGSIZE, sizeof (char)); 111 clp->_raw_buf = __rpc_rawcombuf; 112 clntraw_private = clp; 113 } 114 /* 115 * pre-serialize the static part of the call msg and stash it away 116 */ 117 call_msg.rm_direction = CALL; 118 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; 119 /* XXX: prog and vers have been long historically :-( */ 120 call_msg.rm_call.cb_prog = (u_int32_t)prog; 121 call_msg.rm_call.cb_vers = (u_int32_t)vers; 122 xdrmem_create(xdrs, clp->u.mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE); 123 if (! xdr_callhdr(xdrs, &call_msg)) 124 warnx("clntraw_create - Fatal header serialization error."); 125 clp->mcnt = XDR_GETPOS(xdrs); 126 XDR_DESTROY(xdrs); 127 128 /* 129 * Set xdrmem for client/server shared buffer 130 */ 131 xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE); 132 133 /* 134 * create client handle 135 */ 136 client->cl_ops = clnt_raw_ops(); 137 client->cl_auth = authnone_create(); 138 mutex_unlock(&clntraw_lock); 139 return (client); 140} 141 142/* ARGSUSED */ 143static enum clnt_stat 144clnt_raw_call(h, proc, xargs, argsp, xresults, resultsp, timeout) 145 CLIENT *h; 146 rpcproc_t proc; 147 xdrproc_t xargs; 148 void *argsp; 149 xdrproc_t xresults; 150 void *resultsp; 151 struct timeval timeout; 152{ 153 struct clntraw_private *clp = clntraw_private; 154 XDR *xdrs = &clp->xdr_stream; 155 struct rpc_msg msg; 156 enum clnt_stat status; 157 struct rpc_err error; 158 159 assert(h != NULL); 160 161 mutex_lock(&clntraw_lock); 162 if (clp == NULL) { 163 mutex_unlock(&clntraw_lock); 164 return (RPC_FAILED); 165 } 166 mutex_unlock(&clntraw_lock); 167 168call_again: 169 /* 170 * send request 171 */ 172 xdrs->x_op = XDR_ENCODE; 173 XDR_SETPOS(xdrs, 0); 174 clp->u.mashl_rpcmsg.rm_xid ++ ; 175 if ((! XDR_PUTBYTES(xdrs, clp->u.mashl_callmsg, clp->mcnt)) || 176 (! XDR_PUTINT32(xdrs, &proc)) || 177 (! AUTH_MARSHALL(h->cl_auth, xdrs)) || 178 (! (*xargs)(xdrs, argsp))) { 179 return (RPC_CANTENCODEARGS); 180 } 181 (void)XDR_GETPOS(xdrs); /* called just to cause overhead */ 182 183 /* 184 * We have to call server input routine here because this is 185 * all going on in one process. Yuk. 186 */ 187 svc_getreq_common(FD_SETSIZE); 188 189 /* 190 * get results 191 */ 192 xdrs->x_op = XDR_DECODE; 193 XDR_SETPOS(xdrs, 0); 194 msg.acpted_rply.ar_verf = _null_auth; 195 msg.acpted_rply.ar_results.where = resultsp; 196 msg.acpted_rply.ar_results.proc = xresults; 197 if (! xdr_replymsg(xdrs, &msg)) { 198 /* 199 * It's possible for xdr_replymsg() to fail partway 200 * through its attempt to decode the result from the 201 * server. If this happens, it will leave the reply 202 * structure partially populated with dynamically 203 * allocated memory. (This can happen if someone uses 204 * clntudp_bufcreate() to create a CLIENT handle and 205 * specifies a receive buffer size that is too small.) 206 * This memory must be free()ed to avoid a leak. 207 */ 208 int op = xdrs->x_op; 209 xdrs->x_op = XDR_FREE; 210 xdr_replymsg(xdrs, &msg); 211 xdrs->x_op = op; 212 return (RPC_CANTDECODERES); 213 } 214 _seterr_reply(&msg, &error); 215 status = error.re_status; 216 217 if (status == RPC_SUCCESS) { 218 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { 219 status = RPC_AUTHERROR; 220 } 221 } /* end successful completion */ 222 else { 223 if (AUTH_REFRESH(h->cl_auth, &msg)) 224 goto call_again; 225 } /* end of unsuccessful completion */ 226 227 if (status == RPC_SUCCESS) { 228 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { 229 status = RPC_AUTHERROR; 230 } 231 if (msg.acpted_rply.ar_verf.oa_base != NULL) { 232 xdrs->x_op = XDR_FREE; 233 (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf)); 234 } 235 } 236 237 return (status); 238} 239 240/*ARGSUSED*/ 241static void 242clnt_raw_geterr(cl, err) 243 CLIENT *cl; 244 struct rpc_err *err; 245{ 246} 247 248 249/* ARGSUSED */ 250static bool_t 251clnt_raw_freeres(cl, xdr_res, res_ptr) 252 CLIENT *cl; 253 xdrproc_t xdr_res; 254 void *res_ptr; 255{ 256 struct clntraw_private *clp = clntraw_private; 257 XDR *xdrs = &clp->xdr_stream; 258 bool_t rval; 259 260 mutex_lock(&clntraw_lock); 261 if (clp == NULL) { 262 rval = (bool_t) RPC_FAILED; 263 mutex_unlock(&clntraw_lock); 264 return (rval); 265 } 266 mutex_unlock(&clntraw_lock); 267 xdrs->x_op = XDR_FREE; 268 return ((*xdr_res)(xdrs, res_ptr)); 269} 270 271/*ARGSUSED*/ 272static void 273clnt_raw_abort(cl) 274 CLIENT *cl; 275{ 276} 277 278/*ARGSUSED*/ 279static bool_t 280clnt_raw_control(cl, ui, str) 281 CLIENT *cl; 282 u_int ui; 283 void *str; 284{ 285 return (FALSE); 286} 287 288/*ARGSUSED*/ 289static void 290clnt_raw_destroy(cl) 291 CLIENT *cl; 292{ 293} 294 295static struct clnt_ops * 296clnt_raw_ops() 297{ 298 static struct clnt_ops ops; 299 extern mutex_t ops_lock; 300 301 /* VARIABLES PROTECTED BY ops_lock: ops */ 302 303 mutex_lock(&ops_lock); 304 if (ops.cl_call == NULL) { 305 ops.cl_call = clnt_raw_call; 306 ops.cl_abort = clnt_raw_abort; 307 ops.cl_geterr = clnt_raw_geterr; 308 ops.cl_freeres = clnt_raw_freeres; 309 ops.cl_destroy = clnt_raw_destroy; 310 ops.cl_control = clnt_raw_control; 311 } 312 mutex_unlock(&ops_lock); 313 return (&ops); 314} 315