1 /* $OpenBSD: clnt_raw.c,v 1.16 2009/06/04 03:51:29 schwarze Exp $ */ 2 /* 3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 4 * unrestricted use provided that this legend is included on all tape 5 * media and as a part of the software program in whole or part. Users 6 * may copy or modify Sun RPC without charge, but are not authorized 7 * to license or distribute it to anyone else except as part of a product or 8 * program developed by the user. 9 * 10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 13 * 14 * Sun RPC is provided with no support and without any obligation on the 15 * part of Sun Microsystems, Inc. to assist in its use, correction, 16 * modification or enhancement. 17 * 18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 20 * OR ANY PART THEREOF. 21 * 22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 23 * or profits or other special, indirect and consequential damages, even if 24 * Sun has been advised of the possibility of such damages. 25 * 26 * Sun Microsystems, Inc. 27 * 2550 Garcia Avenue 28 * Mountain View, California 94043 29 */ 30 31 /* 32 * clnt_raw.c 33 * 34 * Copyright (C) 1984, Sun Microsystems, Inc. 35 * 36 * Memory based rpc for simple testing and timing. 37 * Interface to create an rpc client and server in the same process. 38 * This lets us similate rpc and get round trip overhead, without 39 * any interference from the kernel. 40 */ 41 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <errno.h> 45 #include <rpc/rpc.h> 46 47 #define MCALL_MSG_SIZE 24 48 49 /* 50 * This is the "network" we will be moving stuff over. 51 */ 52 static struct clntraw_private { 53 CLIENT client_object; 54 XDR xdr_stream; 55 char _raw_buf[UDPMSGSIZE]; 56 char mashl_callmsg[MCALL_MSG_SIZE]; 57 u_int mcnt; 58 } *clntraw_private; 59 60 static enum clnt_stat clntraw_call(CLIENT *, u_long, xdrproc_t, caddr_t, 61 xdrproc_t, caddr_t, struct timeval); 62 static void clntraw_abort(CLIENT *); 63 static void clntraw_geterr(CLIENT *, struct rpc_err *); 64 static bool_t clntraw_freeres(CLIENT *, xdrproc_t, caddr_t); 65 static bool_t clntraw_control(CLIENT *, u_int, void *); 66 static void clntraw_destroy(CLIENT *); 67 68 static struct clnt_ops client_ops = { 69 clntraw_call, 70 clntraw_abort, 71 clntraw_geterr, 72 clntraw_freeres, 73 clntraw_destroy, 74 clntraw_control 75 }; 76 77 void svc_getreq(int rdfds); 78 79 /* 80 * Create a client handle for memory based rpc. 81 */ 82 CLIENT * 83 clntraw_create(u_long prog, u_long vers) 84 { 85 struct clntraw_private *clp = clntraw_private; 86 struct rpc_msg call_msg; 87 XDR *xdrs; 88 CLIENT *client; 89 90 if (clp == NULL) { 91 clp = (struct clntraw_private *)calloc(1, sizeof (*clp)); 92 if (clp == NULL) 93 goto fail; 94 clntraw_private = clp; 95 } 96 xdrs = &clp->xdr_stream; 97 client = &clp->client_object; 98 /* 99 * pre-serialize the static part of the call msg and stash it away 100 */ 101 call_msg.rm_direction = CALL; 102 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; 103 call_msg.rm_call.cb_prog = prog; 104 call_msg.rm_call.cb_vers = vers; 105 xdrmem_create(xdrs, clp->mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE); 106 if (!xdr_callhdr(xdrs, &call_msg)) 107 goto fail; 108 clp->mcnt = XDR_GETPOS(xdrs); 109 XDR_DESTROY(xdrs); 110 111 /* 112 * Set xdrmem for client/server shared buffer 113 */ 114 xdrmem_create(xdrs, clp->_raw_buf, UDPMSGSIZE, XDR_FREE); 115 116 /* 117 * create client handle 118 */ 119 client->cl_ops = &client_ops; 120 client->cl_auth = authnone_create(); 121 if (client->cl_auth == NULL) 122 goto fail; 123 return (client); 124 125 fail: 126 mem_free((caddr_t)clntraw_private, sizeof(clntraw_private)); 127 clntraw_private = NULL; 128 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 129 rpc_createerr.cf_error.re_errno = errno; 130 return (NULL); 131 } 132 133 /* ARGSUSED */ 134 static enum clnt_stat 135 clntraw_call(CLIENT *h, u_long proc, xdrproc_t xargs, caddr_t argsp, 136 xdrproc_t xresults, caddr_t resultsp, struct timeval timeout) 137 { 138 struct clntraw_private *clp = clntraw_private; 139 XDR *xdrs; 140 struct rpc_msg msg; 141 enum clnt_stat status; 142 struct rpc_err error; 143 144 if (clp == NULL) 145 return (RPC_FAILED); 146 xdrs = &clp->xdr_stream; 147 call_again: 148 /* 149 * send request 150 */ 151 xdrs->x_op = XDR_ENCODE; 152 XDR_SETPOS(xdrs, 0); 153 ((struct rpc_msg *)clp->mashl_callmsg)->rm_xid ++ ; 154 if ((! XDR_PUTBYTES(xdrs, clp->mashl_callmsg, clp->mcnt)) || 155 (! XDR_PUTLONG(xdrs, (long *)&proc)) || 156 (! AUTH_MARSHALL(h->cl_auth, xdrs)) || 157 (! (*xargs)(xdrs, argsp))) { 158 return (RPC_CANTENCODEARGS); 159 } 160 (void)XDR_GETPOS(xdrs); /* called just to cause overhead */ 161 162 /* 163 * We have to call server input routine here because this is 164 * all going on in one process. Yuk. 165 */ 166 svc_getreq(1); 167 168 /* 169 * get results 170 */ 171 xdrs->x_op = XDR_DECODE; 172 XDR_SETPOS(xdrs, 0); 173 msg.acpted_rply.ar_verf = _null_auth; 174 msg.acpted_rply.ar_results.where = resultsp; 175 msg.acpted_rply.ar_results.proc = xresults; 176 if (! xdr_replymsg(xdrs, &msg)) { 177 /* xdr_replymsg() may have left some things allocated */ 178 int op = xdrs->x_op; 179 xdrs->x_op = XDR_FREE; 180 xdr_replymsg(xdrs, &msg); 181 xdrs->x_op = op; 182 return (RPC_CANTDECODERES); 183 } 184 _seterr_reply(&msg, &error); 185 status = error.re_status; 186 187 if (status == RPC_SUCCESS) { 188 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { 189 status = RPC_AUTHERROR; 190 } 191 } /* end successful completion */ 192 else { 193 if (AUTH_REFRESH(h->cl_auth)) 194 goto call_again; 195 } /* end of unsuccessful completion */ 196 197 if (status == RPC_SUCCESS) { 198 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { 199 status = RPC_AUTHERROR; 200 } 201 if (msg.acpted_rply.ar_verf.oa_base != NULL) { 202 xdrs->x_op = XDR_FREE; 203 (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf)); 204 } 205 } 206 207 return (status); 208 } 209 210 /*ARGSUSED*/ 211 static void 212 clntraw_geterr(CLIENT *clnt, struct rpc_err *err) 213 { 214 } 215 216 /* ARGSUSED */ 217 static bool_t 218 clntraw_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr) 219 { 220 struct clntraw_private *clp = clntraw_private; 221 XDR *xdrs; 222 bool_t rval; 223 224 if (clp == NULL) { 225 rval = (bool_t) RPC_FAILED; 226 return (rval); 227 } 228 xdrs = &clp->xdr_stream; 229 xdrs->x_op = XDR_FREE; 230 return ((*xdr_res)(xdrs, res_ptr)); 231 } 232 233 /*ARGSUSED*/ 234 static void 235 clntraw_abort(CLIENT *clnt) 236 { 237 } 238 239 /*ARGSUSED*/ 240 static bool_t 241 clntraw_control(CLIENT *clnt, u_int i, void *v) 242 { 243 return (FALSE); 244 } 245 246 /*ARGSUSED*/ 247 static void 248 clntraw_destroy(CLIENT *clnt) 249 { 250 } 251