1 /* $NetBSD: clnt_raw.c,v 1.5 1997/07/21 14:08:23 jtc 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 #include <sys/cdefs.h> 33 #if defined(LIBC_SCCS) && !defined(lint) 34 #if 0 35 static char *sccsid = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro"; 36 static char *sccsid = "@(#)clnt_raw.c 2.2 88/08/01 4.0 RPCSRC"; 37 #else 38 __RCSID("$NetBSD: clnt_raw.c,v 1.5 1997/07/21 14:08:23 jtc Exp $"); 39 #endif 40 #endif 41 42 /* 43 * clnt_raw.c 44 * 45 * Copyright (C) 1984, Sun Microsystems, Inc. 46 * 47 * Memory based rpc for simple testing and timing. 48 * Interface to create an rpc client and server in the same process. 49 * This lets us similate rpc and get round trip overhead, without 50 * any interference from the kernal. 51 */ 52 53 #include "namespace.h" 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <rpc/rpc.h> 57 58 #ifdef __weak_alias 59 __weak_alias(clntraw_create,_clntraw_create); 60 #endif 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[UDPMSGSIZE]; 71 char mashl_callmsg[MCALL_MSG_SIZE]; 72 u_int mcnt; 73 } *clntraw_private; 74 75 76 77 static enum clnt_stat clntraw_call __P((CLIENT *, u_long, xdrproc_t, 78 caddr_t, xdrproc_t, caddr_t, struct timeval)); 79 static void clntraw_geterr __P((CLIENT *, struct rpc_err *)); 80 static bool_t clntraw_freeres __P((CLIENT *, xdrproc_t, caddr_t)); 81 static void clntraw_abort __P((CLIENT *)); 82 static bool_t clntraw_control __P((CLIENT *, u_int, char *)); 83 static void clntraw_destroy __P((CLIENT *)); 84 85 static struct clnt_ops client_ops = { 86 clntraw_call, 87 clntraw_abort, 88 clntraw_geterr, 89 clntraw_freeres, 90 clntraw_destroy, 91 clntraw_control 92 }; 93 94 /* 95 * Create a client handle for memory based rpc. 96 */ 97 CLIENT * 98 clntraw_create(prog, vers) 99 u_long prog; 100 u_long vers; 101 { 102 register struct clntraw_private *clp = clntraw_private; 103 struct rpc_msg call_msg; 104 XDR *xdrs = &clp->xdr_stream; 105 CLIENT *client = &clp->client_object; 106 107 if (clp == 0) { 108 clp = (struct clntraw_private *)calloc(1, sizeof (*clp)); 109 if (clp == 0) 110 return (0); 111 clntraw_private = clp; 112 } 113 /* 114 * pre-serialize the staic 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 call_msg.rm_call.cb_prog = prog; 119 call_msg.rm_call.cb_vers = vers; 120 xdrmem_create(xdrs, clp->mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE); 121 if (! xdr_callhdr(xdrs, &call_msg)) { 122 perror("clnt_raw.c - Fatal header serialization error."); 123 } 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 = &client_ops; 136 client->cl_auth = authnone_create(); 137 return (client); 138 } 139 140 static enum clnt_stat 141 clntraw_call(h, proc, xargs, argsp, xresults, resultsp, timeout) 142 CLIENT *h; 143 u_long proc; 144 xdrproc_t xargs; 145 caddr_t argsp; 146 xdrproc_t xresults; 147 caddr_t resultsp; 148 struct timeval timeout; 149 { 150 register struct clntraw_private *clp = clntraw_private; 151 register XDR *xdrs = &clp->xdr_stream; 152 struct rpc_msg msg; 153 enum clnt_stat status; 154 struct rpc_err error; 155 156 if (clp == 0) 157 return (RPC_FAILED); 158 call_again: 159 /* 160 * send request 161 */ 162 xdrs->x_op = XDR_ENCODE; 163 XDR_SETPOS(xdrs, 0); 164 ((struct rpc_msg *)clp->mashl_callmsg)->rm_xid ++ ; 165 if ((! XDR_PUTBYTES(xdrs, clp->mashl_callmsg, clp->mcnt)) || 166 (! XDR_PUTLONG(xdrs, &proc)) || 167 (! AUTH_MARSHALL(h->cl_auth, xdrs)) || 168 (! (*xargs)(xdrs, argsp))) { 169 return (RPC_CANTENCODEARGS); 170 } 171 (void)XDR_GETPOS(xdrs); /* called just to cause overhead */ 172 173 /* 174 * We have to call server input routine here because this is 175 * all going on in one process. Yuk. 176 */ 177 svc_getreq(1); 178 179 /* 180 * get results 181 */ 182 xdrs->x_op = XDR_DECODE; 183 XDR_SETPOS(xdrs, 0); 184 msg.acpted_rply.ar_verf = _null_auth; 185 msg.acpted_rply.ar_results.where = resultsp; 186 msg.acpted_rply.ar_results.proc = xresults; 187 if (! xdr_replymsg(xdrs, &msg)) 188 return (RPC_CANTDECODERES); 189 _seterr_reply(&msg, &error); 190 status = error.re_status; 191 192 if (status == RPC_SUCCESS) { 193 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { 194 status = RPC_AUTHERROR; 195 } 196 } /* end successful completion */ 197 else { 198 if (AUTH_REFRESH(h->cl_auth)) 199 goto call_again; 200 } /* end of unsuccessful completion */ 201 202 if (status == RPC_SUCCESS) { 203 if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { 204 status = RPC_AUTHERROR; 205 } 206 if (msg.acpted_rply.ar_verf.oa_base != NULL) { 207 xdrs->x_op = XDR_FREE; 208 (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf)); 209 } 210 } 211 212 return (status); 213 } 214 215 /*ARGSUSED*/ 216 static void 217 clntraw_geterr(cl, err) 218 CLIENT *cl; 219 struct rpc_err *err; 220 { 221 } 222 223 224 static bool_t 225 clntraw_freeres(cl, xdr_res, res_ptr) 226 CLIENT *cl; 227 xdrproc_t xdr_res; 228 caddr_t res_ptr; 229 { 230 register struct clntraw_private *clp = clntraw_private; 231 register XDR *xdrs = &clp->xdr_stream; 232 bool_t rval; 233 234 if (clp == 0) 235 { 236 rval = (bool_t) RPC_FAILED; 237 return (rval); 238 } 239 xdrs->x_op = XDR_FREE; 240 return ((*xdr_res)(xdrs, res_ptr)); 241 } 242 243 /*ARGSUSED*/ 244 static void 245 clntraw_abort(cl) 246 CLIENT *cl; 247 { 248 } 249 250 /*ARGSUSED*/ 251 static bool_t 252 clntraw_control(cl, ui, str) 253 CLIENT *cl; 254 u_int ui; 255 char *str; 256 { 257 return (FALSE); 258 } 259 260 /*ARGSUSED*/ 261 static void 262 clntraw_destroy(cl) 263 CLIENT *cl; 264 { 265 } 266