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