1 /* $NetBSD: clnt_raw.c,v 1.32 2013/03/11 20:19:29 tron Exp $ */ 2 3 /* 4 * Copyright (c) 2010, Oracle America, Inc. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above 13 * copyright notice, this list of conditions and the following 14 * disclaimer in the documentation and/or other materials 15 * provided with the distribution. 16 * * Neither the name of the "Oracle America, Inc." nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 #if defined(LIBC_SCCS) && !defined(lint) 36 #if 0 37 static char *sccsid = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro"; 38 static char *sccsid = "@(#)clnt_raw.c 2.2 88/08/01 4.0 RPCSRC"; 39 #else 40 __RCSID("$NetBSD: clnt_raw.c,v 1.32 2013/03/11 20:19:29 tron Exp $"); 41 #endif 42 #endif 43 44 /* 45 * clnt_raw.c 46 * 47 * Copyright (C) 1984, Sun Microsystems, Inc. 48 * 49 * Memory based rpc for simple testing and timing. 50 * Interface to create an rpc client and server in the same process. 51 * This lets us similate rpc and get round trip overhead, without 52 * any interference from the kernel. 53 */ 54 55 #include "namespace.h" 56 #include "reentrant.h" 57 #include <assert.h> 58 #include <err.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 62 #include <rpc/rpc.h> 63 #include <rpc/raw.h> 64 65 #ifdef __weak_alias 66 __weak_alias(clntraw_create,_clntraw_create) 67 __weak_alias(clnt_raw_create,_clnt_raw_create) 68 #endif 69 70 #ifdef _REENTRANT 71 extern mutex_t clntraw_lock; 72 #endif 73 74 #define MCALL_MSG_SIZE 24 75 76 /* 77 * This is the "network" we will be moving stuff over. 78 */ 79 static struct clntraw_private { 80 CLIENT client_object; 81 XDR xdr_stream; 82 char *_raw_buf; 83 union { 84 struct rpc_msg mashl_rpcmsg; 85 char mashl_callmsg[MCALL_MSG_SIZE]; 86 } u; 87 u_int mcnt; 88 } *clntraw_private; 89 90 static enum clnt_stat clnt_raw_call(CLIENT *, rpcproc_t, xdrproc_t, 91 const char *, xdrproc_t, caddr_t, struct timeval); 92 static void clnt_raw_geterr(CLIENT *, struct rpc_err *); 93 static bool_t clnt_raw_freeres(CLIENT *, xdrproc_t, caddr_t); 94 static void clnt_raw_abort(CLIENT *); 95 static bool_t clnt_raw_control(CLIENT *, u_int, char *); 96 static void clnt_raw_destroy(CLIENT *); 97 static struct clnt_ops *clnt_raw_ops(void); 98 99 /* 100 * Create a client handle for memory based rpc. 101 */ 102 CLIENT * 103 clnt_raw_create(rpcprog_t prog, rpcvers_t vers) 104 { 105 struct clntraw_private *clp = clntraw_private; 106 struct rpc_msg call_msg; 107 XDR *xdrs = &clp->xdr_stream; 108 CLIENT *client = &clp->client_object; 109 110 mutex_lock(&clntraw_lock); 111 if (clp == NULL) { 112 clp = calloc((size_t)1, sizeof (*clp)); 113 if (clp == NULL) 114 goto out; 115 if (__rpc_rawcombuf == NULL) 116 __rpc_rawcombuf = 117 malloc(UDPMSGSIZE); 118 if (__rpc_rawcombuf == NULL) 119 goto out; 120 clp->_raw_buf = __rpc_rawcombuf; 121 clntraw_private = clp; 122 } 123 /* 124 * pre-serialize the static part of the call msg and stash it away 125 */ 126 call_msg.rm_direction = CALL; 127 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; 128 /* XXX: prog and vers have been long historically :-( */ 129 call_msg.rm_call.cb_prog = (u_int32_t)prog; 130 call_msg.rm_call.cb_vers = (u_int32_t)vers; 131 xdrmem_create(xdrs, clp->u.mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE); 132 if (! xdr_callhdr(xdrs, &call_msg)) 133 warnx("%s: Fatal header serialization error", __func__); 134 clp->mcnt = XDR_GETPOS(xdrs); 135 XDR_DESTROY(xdrs); 136 137 /* 138 * Set xdrmem for client/server shared buffer 139 */ 140 xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE); 141 142 /* 143 * create client handle 144 */ 145 client->cl_ops = clnt_raw_ops(); 146 client->cl_auth = authnone_create(); 147 mutex_unlock(&clntraw_lock); 148 return (client); 149 out: 150 if (clp) 151 free(clp); 152 mutex_unlock(&clntraw_lock); 153 return NULL; 154 155 } 156 157 /* ARGSUSED */ 158 static enum clnt_stat 159 clnt_raw_call(CLIENT *h, rpcproc_t proc, xdrproc_t xargs, const char *argsp, 160 xdrproc_t xresults, caddr_t resultsp, struct timeval timeout) 161 { 162 struct clntraw_private *clp = clntraw_private; 163 XDR *xdrs = &clp->xdr_stream; 164 struct rpc_msg msg; 165 enum clnt_stat status; 166 struct rpc_err error; 167 168 _DIAGASSERT(h != NULL); 169 170 mutex_lock(&clntraw_lock); 171 if (clp == NULL) { 172 mutex_unlock(&clntraw_lock); 173 return (RPC_FAILED); 174 } 175 mutex_unlock(&clntraw_lock); 176 177 call_again: 178 /* 179 * send request 180 */ 181 xdrs->x_op = XDR_ENCODE; 182 XDR_SETPOS(xdrs, 0); 183 clp->u.mashl_rpcmsg.rm_xid ++ ; 184 if ((! XDR_PUTBYTES(xdrs, clp->u.mashl_callmsg, clp->mcnt)) || 185 (! XDR_PUTINT32(xdrs, (int32_t *)&proc)) || 186 (! AUTH_MARSHALL(h->cl_auth, xdrs)) || 187 (! (*xargs)(xdrs, __UNCONST(argsp)))) { 188 return (RPC_CANTENCODEARGS); 189 } 190 (void)XDR_GETPOS(xdrs); /* called just to cause overhead */ 191 192 /* 193 * We have to call server input routine here because this is 194 * all going on in one process. Yuk. 195 */ 196 svc_getreq_common(FD_SETSIZE); 197 198 /* 199 * get results 200 */ 201 xdrs->x_op = XDR_DECODE; 202 XDR_SETPOS(xdrs, 0); 203 msg.acpted_rply.ar_verf = _null_auth; 204 msg.acpted_rply.ar_results.where = resultsp; 205 msg.acpted_rply.ar_results.proc = xresults; 206 if (! xdr_replymsg(xdrs, &msg)) { 207 /* 208 * It's possible for xdr_replymsg() to fail partway 209 * through its attempt to decode the result from the 210 * server. If this happens, it will leave the reply 211 * structure partially populated with dynamically 212 * allocated memory. (This can happen if someone uses 213 * clntudp_bufcreate() to create a CLIENT handle and 214 * specifies a receive buffer size that is too small.) 215 * This memory must be free()ed to avoid a leak. 216 */ 217 int op = xdrs->x_op; 218 xdrs->x_op = XDR_FREE; 219 xdr_replymsg(xdrs, &msg); 220 xdrs->x_op = op; 221 return (RPC_CANTDECODERES); 222 } 223 _seterr_reply(&msg, &error); 224 status = error.re_status; 225 226 if (status == RPC_SUCCESS) { 227 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { 228 status = RPC_AUTHERROR; 229 } 230 } /* end successful completion */ 231 else { 232 if (AUTH_REFRESH(h->cl_auth)) 233 goto call_again; 234 } /* end of unsuccessful completion */ 235 236 if (status == RPC_SUCCESS) { 237 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { 238 status = RPC_AUTHERROR; 239 } 240 if (msg.acpted_rply.ar_verf.oa_base != NULL) { 241 xdrs->x_op = XDR_FREE; 242 (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf)); 243 } 244 } 245 246 return (status); 247 } 248 249 /*ARGSUSED*/ 250 static void 251 clnt_raw_geterr(CLIENT *cl, struct rpc_err *error) 252 { 253 } 254 255 256 /* ARGSUSED */ 257 static bool_t 258 clnt_raw_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr) 259 { 260 struct clntraw_private *clp = clntraw_private; 261 XDR *xdrs = &clp->xdr_stream; 262 bool_t rval; 263 264 mutex_lock(&clntraw_lock); 265 if (clp == NULL) { 266 rval = (bool_t) RPC_FAILED; 267 mutex_unlock(&clntraw_lock); 268 return (rval); 269 } 270 mutex_unlock(&clntraw_lock); 271 xdrs->x_op = XDR_FREE; 272 return ((*xdr_res)(xdrs, res_ptr)); 273 } 274 275 /*ARGSUSED*/ 276 static void 277 clnt_raw_abort(CLIENT *cl) 278 { 279 } 280 281 /*ARGSUSED*/ 282 static bool_t 283 clnt_raw_control(CLIENT *cl, u_int ui, char *str) 284 { 285 return (FALSE); 286 } 287 288 /*ARGSUSED*/ 289 static void 290 clnt_raw_destroy(CLIENT *cl) 291 { 292 } 293 294 static struct clnt_ops * 295 clnt_raw_ops(void) 296 { 297 static struct clnt_ops ops; 298 #ifdef _REENTRANT 299 extern mutex_t ops_lock; 300 #endif 301 302 /* VARIABLES PROTECTED BY ops_lock: ops */ 303 304 mutex_lock(&ops_lock); 305 if (ops.cl_call == NULL) { 306 ops.cl_call = clnt_raw_call; 307 ops.cl_abort = clnt_raw_abort; 308 ops.cl_geterr = clnt_raw_geterr; 309 ops.cl_freeres = clnt_raw_freeres; 310 ops.cl_destroy = clnt_raw_destroy; 311 ops.cl_control = clnt_raw_control; 312 } 313 mutex_unlock(&ops_lock); 314 return (&ops); 315 } 316