1 /* $OpenBSD: clnt_raw.c,v 1.17 2010/09/01 14:43:34 millert 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 /* 35 * clnt_raw.c 36 * 37 * Memory based rpc for simple testing and timing. 38 * Interface to create an rpc client and server in the same process. 39 * This lets us similate rpc and get round trip overhead, without 40 * any interference from the kernel. 41 */ 42 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <errno.h> 46 #include <rpc/rpc.h> 47 48 #define MCALL_MSG_SIZE 24 49 50 /* 51 * This is the "network" we will be moving stuff over. 52 */ 53 static struct clntraw_private { 54 CLIENT client_object; 55 XDR xdr_stream; 56 char _raw_buf[UDPMSGSIZE]; 57 char mashl_callmsg[MCALL_MSG_SIZE]; 58 u_int mcnt; 59 } *clntraw_private; 60 61 static enum clnt_stat clntraw_call(CLIENT *, u_long, xdrproc_t, caddr_t, 62 xdrproc_t, caddr_t, struct timeval); 63 static void clntraw_abort(CLIENT *); 64 static void clntraw_geterr(CLIENT *, struct rpc_err *); 65 static bool_t clntraw_freeres(CLIENT *, xdrproc_t, caddr_t); 66 static bool_t clntraw_control(CLIENT *, u_int, void *); 67 static void clntraw_destroy(CLIENT *); 68 69 static struct clnt_ops client_ops = { 70 clntraw_call, 71 clntraw_abort, 72 clntraw_geterr, 73 clntraw_freeres, 74 clntraw_destroy, 75 clntraw_control 76 }; 77 78 void svc_getreq(int rdfds); 79 80 /* 81 * Create a client handle for memory based rpc. 82 */ 83 CLIENT * 84 clntraw_create(u_long prog, u_long vers) 85 { 86 struct clntraw_private *clp = clntraw_private; 87 struct rpc_msg call_msg; 88 XDR *xdrs; 89 CLIENT *client; 90 91 if (clp == NULL) { 92 clp = (struct clntraw_private *)calloc(1, sizeof (*clp)); 93 if (clp == NULL) 94 goto fail; 95 clntraw_private = clp; 96 } 97 xdrs = &clp->xdr_stream; 98 client = &clp->client_object; 99 /* 100 * pre-serialize the static part of the call msg and stash it away 101 */ 102 call_msg.rm_direction = CALL; 103 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; 104 call_msg.rm_call.cb_prog = prog; 105 call_msg.rm_call.cb_vers = vers; 106 xdrmem_create(xdrs, clp->mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE); 107 if (!xdr_callhdr(xdrs, &call_msg)) 108 goto fail; 109 clp->mcnt = XDR_GETPOS(xdrs); 110 XDR_DESTROY(xdrs); 111 112 /* 113 * Set xdrmem for client/server shared buffer 114 */ 115 xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE); 116 117 /* 118 * create client handle 119 */ 120 client->cl_ops = &client_ops; 121 client->cl_auth = authnone_create(); 122 if (client->cl_auth == NULL) 123 goto fail; 124 return (client); 125 126 fail: 127 mem_free((caddr_t)clntraw_private, sizeof(clntraw_private)); 128 clntraw_private = NULL; 129 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 130 rpc_createerr.cf_error.re_errno = errno; 131 return (NULL); 132 } 133 134 /* ARGSUSED */ 135 static enum clnt_stat 136 clntraw_call(CLIENT *h, u_long proc, xdrproc_t xargs, caddr_t argsp, 137 xdrproc_t xresults, caddr_t resultsp, struct timeval timeout) 138 { 139 struct clntraw_private *clp = clntraw_private; 140 XDR *xdrs; 141 struct rpc_msg msg; 142 enum clnt_stat status; 143 struct rpc_err error; 144 145 if (clp == NULL) 146 return (RPC_FAILED); 147 xdrs = &clp->xdr_stream; 148 call_again: 149 /* 150 * send request 151 */ 152 xdrs->x_op = XDR_ENCODE; 153 XDR_SETPOS(xdrs, 0); 154 ((struct rpc_msg *)clp->mashl_callmsg)->rm_xid ++ ; 155 if ((! XDR_PUTBYTES(xdrs, clp->mashl_callmsg, clp->mcnt)) || 156 (! XDR_PUTLONG(xdrs, (long *)&proc)) || 157 (! AUTH_MARSHALL(h->cl_auth, xdrs)) || 158 (! (*xargs)(xdrs, argsp))) { 159 return (RPC_CANTENCODEARGS); 160 } 161 (void)XDR_GETPOS(xdrs); /* called just to cause overhead */ 162 163 /* 164 * We have to call server input routine here because this is 165 * all going on in one process. Yuk. 166 */ 167 svc_getreq(1); 168 169 /* 170 * get results 171 */ 172 xdrs->x_op = XDR_DECODE; 173 XDR_SETPOS(xdrs, 0); 174 msg.acpted_rply.ar_verf = _null_auth; 175 msg.acpted_rply.ar_results.where = resultsp; 176 msg.acpted_rply.ar_results.proc = xresults; 177 if (! xdr_replymsg(xdrs, &msg)) { 178 /* xdr_replymsg() may have left some things allocated */ 179 int op = xdrs->x_op; 180 xdrs->x_op = XDR_FREE; 181 xdr_replymsg(xdrs, &msg); 182 xdrs->x_op = op; 183 return (RPC_CANTDECODERES); 184 } 185 _seterr_reply(&msg, &error); 186 status = error.re_status; 187 188 if (status == RPC_SUCCESS) { 189 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { 190 status = RPC_AUTHERROR; 191 } 192 } /* end successful completion */ 193 else { 194 if (AUTH_REFRESH(h->cl_auth)) 195 goto call_again; 196 } /* end of unsuccessful completion */ 197 198 if (status == RPC_SUCCESS) { 199 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { 200 status = RPC_AUTHERROR; 201 } 202 if (msg.acpted_rply.ar_verf.oa_base != NULL) { 203 xdrs->x_op = XDR_FREE; 204 (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf)); 205 } 206 } 207 208 return (status); 209 } 210 211 /*ARGSUSED*/ 212 static void 213 clntraw_geterr(CLIENT *clnt, struct rpc_err *err) 214 { 215 } 216 217 /* ARGSUSED */ 218 static bool_t 219 clntraw_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr) 220 { 221 struct clntraw_private *clp = clntraw_private; 222 XDR *xdrs; 223 bool_t rval; 224 225 if (clp == NULL) { 226 rval = (bool_t) RPC_FAILED; 227 return (rval); 228 } 229 xdrs = &clp->xdr_stream; 230 xdrs->x_op = XDR_FREE; 231 return ((*xdr_res)(xdrs, res_ptr)); 232 } 233 234 /*ARGSUSED*/ 235 static void 236 clntraw_abort(CLIENT *clnt) 237 { 238 } 239 240 /*ARGSUSED*/ 241 static bool_t 242 clntraw_control(CLIENT *clnt, u_int i, void *v) 243 { 244 return (FALSE); 245 } 246 247 /*ARGSUSED*/ 248 static void 249 clntraw_destroy(CLIENT *clnt) 250 { 251 } 252