1 /* $NetBSD: clnt_raw.c,v 1.18 2000/07/06 03:10:34 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 #include <sys/cdefs.h> 33 #if defined(LIBC_SCCS) && !defined(lint) 34 #if 0 35 static char *sccsid = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro"; 36 static char *sccsid = "@(#)clnt_raw.c 2.2 88/08/01 4.0 RPCSRC"; 37 #else 38 __RCSID("$NetBSD: clnt_raw.c,v 1.18 2000/07/06 03:10:34 christos Exp $"); 39 #endif 40 #endif 41 42 /* 43 * clnt_raw.c 44 * 45 * Copyright (C) 1984, Sun Microsystems, Inc. 46 * 47 * Memory based rpc for simple testing and timing. 48 * Interface to create an rpc client and server in the same process. 49 * This lets us similate rpc and get round trip overhead, without 50 * any interference from the kernal. 51 */ 52 53 #include "namespace.h" 54 #include "reentrant.h" 55 #include <assert.h> 56 #include <err.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 60 #include <rpc/rpc.h> 61 #include <rpc/raw.h> 62 63 #ifdef __weak_alias 64 __weak_alias(clntraw_create,_clntraw_create) 65 __weak_alias(clnt_raw_create,_clnt_raw_create) 66 #endif 67 68 #ifdef __REENT 69 extern mutex_t clntaw_lock; 70 #endif 71 72 #define MCALL_MSG_SIZE 24 73 74 /* 75 * This is the "network" we will be moving stuff over. 76 */ 77 static struct clntraw_private { 78 CLIENT client_object; 79 XDR xdr_stream; 80 char *_raw_buf; 81 union { 82 struct rpc_msg mashl_rpcmsg; 83 char mashl_callmsg[MCALL_MSG_SIZE]; 84 } u; 85 u_int mcnt; 86 } *clntraw_private; 87 88 extern void svc_getreq_common __P((int)); 89 90 91 static enum clnt_stat clnt_raw_call __P((CLIENT *, rpcproc_t, xdrproc_t, 92 caddr_t, xdrproc_t, caddr_t, struct timeval)); 93 static void clnt_raw_geterr __P((CLIENT *, struct rpc_err *)); 94 static bool_t clnt_raw_freeres __P((CLIENT *, xdrproc_t, caddr_t)); 95 static void clnt_raw_abort __P((CLIENT *)); 96 static bool_t clnt_raw_control __P((CLIENT *, u_int, char *)); 97 static void clnt_raw_destroy __P((CLIENT *)); 98 static struct clnt_ops *clnt_raw_ops __P((void)); 99 100 /* 101 * Create a client handle for memory based rpc. 102 */ 103 CLIENT * 104 clnt_raw_create(prog, vers) 105 rpcprog_t prog; 106 rpcvers_t vers; 107 { 108 struct clntraw_private *clp = clntraw_private; 109 struct rpc_msg call_msg; 110 XDR *xdrs = &clp->xdr_stream; 111 CLIENT *client = &clp->client_object; 112 113 mutex_lock(&clntraw_lock); 114 if (clp == NULL) { 115 clp = (struct clntraw_private *)calloc(1, sizeof (*clp)); 116 if (clp == NULL) { 117 mutex_unlock(&clntraw_lock); 118 return NULL; 119 } 120 if (__rpc_rawcombuf == NULL) 121 __rpc_rawcombuf = 122 (char *)calloc(UDPMSGSIZE, sizeof (char)); 123 clp->_raw_buf = __rpc_rawcombuf; 124 clntraw_private = clp; 125 } 126 /* 127 * pre-serialize the static part of the call msg and stash it away 128 */ 129 call_msg.rm_direction = CALL; 130 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; 131 /* XXX: prog and vers have been long historically :-( */ 132 call_msg.rm_call.cb_prog = (u_int32_t)prog; 133 call_msg.rm_call.cb_vers = (u_int32_t)vers; 134 xdrmem_create(xdrs, clp->u.mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE); 135 if (! xdr_callhdr(xdrs, &call_msg)) 136 warnx("clntraw_create - Fatal header serialization error."); 137 clp->mcnt = XDR_GETPOS(xdrs); 138 XDR_DESTROY(xdrs); 139 140 /* 141 * Set xdrmem for client/server shared buffer 142 */ 143 xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE); 144 145 /* 146 * create client handle 147 */ 148 client->cl_ops = clnt_raw_ops(); 149 client->cl_auth = authnone_create(); 150 mutex_unlock(&clntraw_lock); 151 return (client); 152 } 153 154 /* ARGSUSED */ 155 static enum clnt_stat 156 clnt_raw_call(h, proc, xargs, argsp, xresults, resultsp, timeout) 157 CLIENT *h; 158 rpcproc_t proc; 159 xdrproc_t xargs; 160 caddr_t argsp; 161 xdrproc_t xresults; 162 caddr_t resultsp; 163 struct timeval timeout; 164 { 165 struct clntraw_private *clp = clntraw_private; 166 XDR *xdrs = &clp->xdr_stream; 167 struct rpc_msg msg; 168 enum clnt_stat status; 169 struct rpc_err error; 170 171 _DIAGASSERT(h != NULL); 172 173 mutex_lock(&clntraw_lock); 174 if (clp == NULL) { 175 mutex_unlock(&clntraw_lock); 176 return (RPC_FAILED); 177 } 178 mutex_unlock(&clntraw_lock); 179 180 call_again: 181 /* 182 * send request 183 */ 184 xdrs->x_op = XDR_ENCODE; 185 XDR_SETPOS(xdrs, 0); 186 clp->u.mashl_rpcmsg.rm_xid ++ ; 187 if ((! XDR_PUTBYTES(xdrs, clp->u.mashl_callmsg, clp->mcnt)) || 188 (! XDR_PUTLONG(xdrs, (long *)(void *)&proc)) || 189 (! AUTH_MARSHALL(h->cl_auth, xdrs)) || 190 (! (*xargs)(xdrs, argsp))) { 191 return (RPC_CANTENCODEARGS); 192 } 193 (void)XDR_GETPOS(xdrs); /* called just to cause overhead */ 194 195 /* 196 * We have to call server input routine here because this is 197 * all going on in one process. Yuk. 198 */ 199 svc_getreq_common(FD_SETSIZE); 200 201 /* 202 * get results 203 */ 204 xdrs->x_op = XDR_DECODE; 205 XDR_SETPOS(xdrs, 0); 206 msg.acpted_rply.ar_verf = _null_auth; 207 msg.acpted_rply.ar_results.where = resultsp; 208 msg.acpted_rply.ar_results.proc = xresults; 209 if (! xdr_replymsg(xdrs, &msg)) { 210 /* 211 * It's possible for xdr_replymsg() to fail partway 212 * through its attempt to decode the result from the 213 * server. If this happens, it will leave the reply 214 * structure partially populated with dynamically 215 * allocated memory. (This can happen if someone uses 216 * clntudp_bufcreate() to create a CLIENT handle and 217 * specifies a receive buffer size that is too small.) 218 * This memory must be free()ed to avoid a leak. 219 */ 220 int op = xdrs->x_op; 221 xdrs->x_op = XDR_FREE; 222 xdr_replymsg(xdrs, &msg); 223 xdrs->x_op = op; 224 return (RPC_CANTDECODERES); 225 } 226 _seterr_reply(&msg, &error); 227 status = error.re_status; 228 229 if (status == RPC_SUCCESS) { 230 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { 231 status = RPC_AUTHERROR; 232 } 233 } /* end successful completion */ 234 else { 235 if (AUTH_REFRESH(h->cl_auth)) 236 goto call_again; 237 } /* end of unsuccessful completion */ 238 239 if (status == RPC_SUCCESS) { 240 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { 241 status = RPC_AUTHERROR; 242 } 243 if (msg.acpted_rply.ar_verf.oa_base != NULL) { 244 xdrs->x_op = XDR_FREE; 245 (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf)); 246 } 247 } 248 249 return (status); 250 } 251 252 /*ARGSUSED*/ 253 static void 254 clnt_raw_geterr(cl, err) 255 CLIENT *cl; 256 struct rpc_err *err; 257 { 258 } 259 260 261 /* ARGSUSED */ 262 static bool_t 263 clnt_raw_freeres(cl, xdr_res, res_ptr) 264 CLIENT *cl; 265 xdrproc_t xdr_res; 266 caddr_t res_ptr; 267 { 268 struct clntraw_private *clp = clntraw_private; 269 XDR *xdrs = &clp->xdr_stream; 270 bool_t rval; 271 272 mutex_lock(&clntraw_lock); 273 if (clp == NULL) { 274 rval = (bool_t) RPC_FAILED; 275 mutex_unlock(&clntraw_lock); 276 return (rval); 277 } 278 mutex_unlock(&clntraw_lock); 279 xdrs->x_op = XDR_FREE; 280 return ((*xdr_res)(xdrs, res_ptr)); 281 } 282 283 /*ARGSUSED*/ 284 static void 285 clnt_raw_abort(cl) 286 CLIENT *cl; 287 { 288 } 289 290 /*ARGSUSED*/ 291 static bool_t 292 clnt_raw_control(cl, ui, str) 293 CLIENT *cl; 294 u_int ui; 295 char *str; 296 { 297 return (FALSE); 298 } 299 300 /*ARGSUSED*/ 301 static void 302 clnt_raw_destroy(cl) 303 CLIENT *cl; 304 { 305 } 306 307 static struct clnt_ops * 308 clnt_raw_ops() 309 { 310 static struct clnt_ops ops; 311 #ifdef __REENT 312 extern mutex_t ops_lock; 313 #endif 314 315 /* VARIABLES PROTECTED BY ops_lock: ops */ 316 317 mutex_lock(&ops_lock); 318 if (ops.cl_call == NULL) { 319 ops.cl_call = clnt_raw_call; 320 ops.cl_abort = clnt_raw_abort; 321 ops.cl_geterr = clnt_raw_geterr; 322 ops.cl_freeres = clnt_raw_freeres; 323 ops.cl_destroy = clnt_raw_destroy; 324 ops.cl_control = clnt_raw_control; 325 } 326 mutex_unlock(&ops_lock); 327 return (&ops); 328 } 329