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