clnt_raw.c 6.5 KB

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