1 /* $NetBSD: clnt_raw.c,v 1.20 2000/12/10 04:12:03 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 #if defined(LIBC_SCCS) && !defined(lint) 33 static char *sccsid = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro"; 34 static char *sccsid = "@(#)clnt_raw.c 2.2 88/08/01 4.0 RPCSRC"; 35 #endif 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 /* 40 * clnt_raw.c 41 * 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 61 extern 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 */ 68 static 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 79 static enum clnt_stat clnt_raw_call(CLIENT *, rpcproc_t, xdrproc_t, void *, 80 xdrproc_t, void *, struct timeval); 81 static void clnt_raw_geterr(CLIENT *, struct rpc_err *); 82 static bool_t clnt_raw_freeres(CLIENT *, xdrproc_t, void *); 83 static void clnt_raw_abort(CLIENT *); 84 static bool_t clnt_raw_control(CLIENT *, u_int, void *); 85 static void clnt_raw_destroy(CLIENT *); 86 static struct clnt_ops *clnt_raw_ops(void); 87 88 /* 89 * Create a client handle for memory based rpc. 90 */ 91 CLIENT * 92 clnt_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 */ 143 static enum clnt_stat 144 clnt_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 168 call_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*/ 241 static void 242 clnt_raw_geterr(cl, err) 243 CLIENT *cl; 244 struct rpc_err *err; 245 { 246 } 247 248 249 /* ARGSUSED */ 250 static bool_t 251 clnt_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*/ 272 static void 273 clnt_raw_abort(cl) 274 CLIENT *cl; 275 { 276 } 277 278 /*ARGSUSED*/ 279 static bool_t 280 clnt_raw_control(cl, ui, str) 281 CLIENT *cl; 282 u_int ui; 283 void *str; 284 { 285 return (FALSE); 286 } 287 288 /*ARGSUSED*/ 289 static void 290 clnt_raw_destroy(cl) 291 CLIENT *cl; 292 { 293 } 294 295 static struct clnt_ops * 296 clnt_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