clnt_tcp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
  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. * TCP based RPC supports 'batched calls'.
  34. * A sequence of calls may be batched-up in a send buffer. The rpc call
  35. * return immediately to the client even though the call was not necessarily
  36. * sent. The batching occurs if the results' xdr routine is NULL (0) AND
  37. * the rpc timeout value is zero (see clnt.h, rpc).
  38. *
  39. * Clients should NOT casually batch calls that in fact return results; that is,
  40. * the server side should be aware that a call is batched and not produce any
  41. * return message. Batched calls that produce many result messages can
  42. * deadlock (netlock) the client and the server....
  43. *
  44. * Now go hang yourself.
  45. */
  46. #include <netdb.h>
  47. #include <errno.h>
  48. #include <stdio.h>
  49. #include <unistd.h>
  50. #include <libintl.h>
  51. #include <rpc/rpc.h>
  52. #include <sys/poll.h>
  53. #include <sys/socket.h>
  54. #include <rpc/pmap_clnt.h>
  55. #include <wchar.h>
  56. #include <shlib-compat.h>
  57. extern u_long _create_xid (void);
  58. #define MCALL_MSG_SIZE 24
  59. struct ct_data
  60. {
  61. int ct_sock;
  62. bool_t ct_closeit;
  63. struct timeval ct_wait;
  64. bool_t ct_waitset; /* wait set by clnt_control? */
  65. struct sockaddr_in ct_addr;
  66. struct rpc_err ct_error;
  67. char ct_mcall[MCALL_MSG_SIZE]; /* marshalled callmsg */
  68. u_int ct_mpos; /* pos after marshal */
  69. XDR ct_xdrs;
  70. };
  71. static int readtcp (char *, char *, int);
  72. static int writetcp (char *, char *, int);
  73. static enum clnt_stat clnttcp_call (CLIENT *, u_long, xdrproc_t, caddr_t,
  74. xdrproc_t, caddr_t, struct timeval);
  75. static void clnttcp_abort (void);
  76. static void clnttcp_geterr (CLIENT *, struct rpc_err *);
  77. static bool_t clnttcp_freeres (CLIENT *, xdrproc_t, caddr_t);
  78. static bool_t clnttcp_control (CLIENT *, int, char *);
  79. static void clnttcp_destroy (CLIENT *);
  80. static const struct clnt_ops tcp_ops =
  81. {
  82. clnttcp_call,
  83. clnttcp_abort,
  84. clnttcp_geterr,
  85. clnttcp_freeres,
  86. clnttcp_destroy,
  87. clnttcp_control
  88. };
  89. /*
  90. * Create a client handle for a tcp/ip connection.
  91. * If *sockp<0, *sockp is set to a newly created TCP socket and it is
  92. * connected to raddr. If *sockp non-negative then
  93. * raddr is ignored. The rpc/tcp package does buffering
  94. * similar to stdio, so the client must pick send and receive buffer sizes,];
  95. * 0 => use the default.
  96. * If raddr->sin_port is 0, then a binder on the remote machine is
  97. * consulted for the right port number.
  98. * NB: *sockp is copied into a private area.
  99. * NB: It is the clients responsibility to close *sockp.
  100. * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
  101. * something more useful.
  102. */
  103. CLIENT *
  104. clnttcp_create (struct sockaddr_in *raddr, u_long prog, u_long vers,
  105. int *sockp, u_int sendsz, u_int recvsz)
  106. {
  107. CLIENT *h;
  108. struct ct_data *ct;
  109. struct rpc_msg call_msg;
  110. h = (CLIENT *) mem_alloc (sizeof (*h));
  111. ct = (struct ct_data *) mem_alloc (sizeof (*ct));
  112. if (h == NULL || ct == NULL)
  113. {
  114. struct rpc_createerr *ce = &get_rpc_createerr ();
  115. (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
  116. ce->cf_stat = RPC_SYSTEMERROR;
  117. ce->cf_error.re_errno = ENOMEM;
  118. goto fooy;
  119. }
  120. /*
  121. * If no port number given ask the pmap for one
  122. */
  123. if (raddr->sin_port == 0)
  124. {
  125. u_short port;
  126. if ((port = pmap_getport (raddr, prog, vers, IPPROTO_TCP)) == 0)
  127. {
  128. mem_free ((caddr_t) ct, sizeof (struct ct_data));
  129. mem_free ((caddr_t) h, sizeof (CLIENT));
  130. return ((CLIENT *) NULL);
  131. }
  132. raddr->sin_port = htons (port);
  133. }
  134. /*
  135. * If no socket given, open one
  136. */
  137. if (*sockp < 0)
  138. {
  139. *sockp = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
  140. (void) bindresvport (*sockp, (struct sockaddr_in *) 0);
  141. if ((*sockp < 0)
  142. || (__connect (*sockp, (struct sockaddr *) raddr,
  143. sizeof (*raddr)) < 0))
  144. {
  145. struct rpc_createerr *ce = &get_rpc_createerr ();
  146. ce->cf_stat = RPC_SYSTEMERROR;
  147. ce->cf_error.re_errno = errno;
  148. if (*sockp >= 0)
  149. (void) __close (*sockp);
  150. goto fooy;
  151. }
  152. ct->ct_closeit = TRUE;
  153. }
  154. else
  155. {
  156. ct->ct_closeit = FALSE;
  157. }
  158. /*
  159. * Set up private data struct
  160. */
  161. ct->ct_sock = *sockp;
  162. ct->ct_wait.tv_usec = 0;
  163. ct->ct_waitset = FALSE;
  164. ct->ct_addr = *raddr;
  165. /*
  166. * Initialize call message
  167. */
  168. call_msg.rm_xid = _create_xid ();
  169. call_msg.rm_direction = CALL;
  170. call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  171. call_msg.rm_call.cb_prog = prog;
  172. call_msg.rm_call.cb_vers = vers;
  173. /*
  174. * pre-serialize the static part of the call msg and stash it away
  175. */
  176. xdrmem_create (&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE, XDR_ENCODE);
  177. if (!xdr_callhdr (&(ct->ct_xdrs), &call_msg))
  178. {
  179. if (ct->ct_closeit)
  180. {
  181. (void) __close (*sockp);
  182. }
  183. goto fooy;
  184. }
  185. ct->ct_mpos = XDR_GETPOS (&(ct->ct_xdrs));
  186. XDR_DESTROY (&(ct->ct_xdrs));
  187. /*
  188. * Create a client handle which uses xdrrec for serialization
  189. * and authnone for authentication.
  190. */
  191. xdrrec_create (&(ct->ct_xdrs), sendsz, recvsz,
  192. (caddr_t) ct, readtcp, writetcp);
  193. h->cl_ops = (struct clnt_ops *) &tcp_ops;
  194. h->cl_private = (caddr_t) ct;
  195. h->cl_auth = authnone_create ();
  196. return h;
  197. fooy:
  198. /*
  199. * Something goofed, free stuff and barf
  200. */
  201. mem_free ((caddr_t) ct, sizeof (struct ct_data));
  202. mem_free ((caddr_t) h, sizeof (CLIENT));
  203. return ((CLIENT *) NULL);
  204. }
  205. #ifdef EXPORT_RPC_SYMBOLS
  206. libc_hidden_def (clnttcp_create)
  207. #else
  208. libc_hidden_nolink_sunrpc (clnttcp_create, GLIBC_2_0)
  209. #endif
  210. static enum clnt_stat
  211. clnttcp_call (CLIENT *h, u_long proc, xdrproc_t xdr_args, caddr_t args_ptr,
  212. xdrproc_t xdr_results, caddr_t results_ptr,
  213. struct timeval timeout)
  214. {
  215. struct ct_data *ct = (struct ct_data *) h->cl_private;
  216. XDR *xdrs = &(ct->ct_xdrs);
  217. struct rpc_msg reply_msg;
  218. u_long x_id;
  219. uint32_t *msg_x_id = (uint32_t *) (ct->ct_mcall); /* yuk */
  220. bool_t shipnow;
  221. int refreshes = 2;
  222. if (!ct->ct_waitset)
  223. {
  224. ct->ct_wait = timeout;
  225. }
  226. shipnow =
  227. (xdr_results == (xdrproc_t) 0 && ct->ct_wait.tv_sec == 0
  228. && ct->ct_wait.tv_usec == 0) ? FALSE : TRUE;
  229. call_again:
  230. xdrs->x_op = XDR_ENCODE;
  231. ct->ct_error.re_status = RPC_SUCCESS;
  232. x_id = ntohl (--(*msg_x_id));
  233. if ((!XDR_PUTBYTES (xdrs, ct->ct_mcall, ct->ct_mpos)) ||
  234. (!XDR_PUTLONG (xdrs, (long *) &proc)) ||
  235. (!AUTH_MARSHALL (h->cl_auth, xdrs)) ||
  236. (!(*xdr_args) (xdrs, args_ptr)))
  237. {
  238. if (ct->ct_error.re_status == RPC_SUCCESS)
  239. ct->ct_error.re_status = RPC_CANTENCODEARGS;
  240. (void) xdrrec_endofrecord (xdrs, TRUE);
  241. return (ct->ct_error.re_status);
  242. }
  243. if (!xdrrec_endofrecord (xdrs, shipnow))
  244. return ct->ct_error.re_status = RPC_CANTSEND;
  245. if (!shipnow)
  246. return RPC_SUCCESS;
  247. /*
  248. * Hack to provide rpc-based message passing
  249. */
  250. if (ct->ct_wait.tv_sec == 0 && ct->ct_wait.tv_usec == 0)
  251. {
  252. return ct->ct_error.re_status = RPC_TIMEDOUT;
  253. }
  254. /*
  255. * Keep receiving until we get a valid transaction id
  256. */
  257. xdrs->x_op = XDR_DECODE;
  258. while (TRUE)
  259. {
  260. reply_msg.acpted_rply.ar_verf = _null_auth;
  261. reply_msg.acpted_rply.ar_results.where = NULL;
  262. reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
  263. if (!xdrrec_skiprecord (xdrs))
  264. return (ct->ct_error.re_status);
  265. /* now decode and validate the response header */
  266. if (!xdr_replymsg (xdrs, &reply_msg))
  267. {
  268. if (ct->ct_error.re_status == RPC_SUCCESS)
  269. continue;
  270. return ct->ct_error.re_status;
  271. }
  272. if ((uint32_t) reply_msg.rm_xid == (uint32_t) x_id)
  273. break;
  274. }
  275. /*
  276. * process header
  277. */
  278. _seterr_reply (&reply_msg, &(ct->ct_error));
  279. if (ct->ct_error.re_status == RPC_SUCCESS)
  280. {
  281. if (!AUTH_VALIDATE (h->cl_auth, &reply_msg.acpted_rply.ar_verf))
  282. {
  283. ct->ct_error.re_status = RPC_AUTHERROR;
  284. ct->ct_error.re_why = AUTH_INVALIDRESP;
  285. }
  286. else if (!(*xdr_results) (xdrs, results_ptr))
  287. {
  288. if (ct->ct_error.re_status == RPC_SUCCESS)
  289. ct->ct_error.re_status = RPC_CANTDECODERES;
  290. }
  291. /* free verifier ... */
  292. if (reply_msg.acpted_rply.ar_verf.oa_base != NULL)
  293. {
  294. xdrs->x_op = XDR_FREE;
  295. (void) xdr_opaque_auth (xdrs, &(reply_msg.acpted_rply.ar_verf));
  296. }
  297. } /* end successful completion */
  298. else
  299. {
  300. /* maybe our credentials need to be refreshed ... */
  301. if (refreshes-- && AUTH_REFRESH (h->cl_auth))
  302. goto call_again;
  303. } /* end of unsuccessful completion */
  304. return ct->ct_error.re_status;
  305. }
  306. static void
  307. clnttcp_geterr (CLIENT *h, struct rpc_err *errp)
  308. {
  309. struct ct_data *ct =
  310. (struct ct_data *) h->cl_private;
  311. *errp = ct->ct_error;
  312. }
  313. static bool_t
  314. clnttcp_freeres (CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
  315. {
  316. struct ct_data *ct = (struct ct_data *) cl->cl_private;
  317. XDR *xdrs = &(ct->ct_xdrs);
  318. xdrs->x_op = XDR_FREE;
  319. return (*xdr_res) (xdrs, res_ptr);
  320. }
  321. static void
  322. clnttcp_abort (void)
  323. {
  324. }
  325. static bool_t
  326. clnttcp_control (CLIENT *cl, int request, char *info)
  327. {
  328. struct ct_data *ct = (struct ct_data *) cl->cl_private;
  329. u_long ul;
  330. uint32_t ui32;
  331. switch (request)
  332. {
  333. case CLSET_FD_CLOSE:
  334. ct->ct_closeit = TRUE;
  335. break;
  336. case CLSET_FD_NCLOSE:
  337. ct->ct_closeit = FALSE;
  338. break;
  339. case CLSET_TIMEOUT:
  340. ct->ct_wait = *(struct timeval *) info;
  341. ct->ct_waitset = TRUE;
  342. break;
  343. case CLGET_TIMEOUT:
  344. *(struct timeval *) info = ct->ct_wait;
  345. break;
  346. case CLGET_SERVER_ADDR:
  347. *(struct sockaddr_in *) info = ct->ct_addr;
  348. break;
  349. case CLGET_FD:
  350. *(int *)info = ct->ct_sock;
  351. break;
  352. case CLGET_XID:
  353. /*
  354. * use the knowledge that xid is the
  355. * first element in the call structure *.
  356. * This will get the xid of the PREVIOUS call
  357. */
  358. memcpy (&ui32, ct->ct_mcall, sizeof (ui32));
  359. ul = ntohl (ui32);
  360. memcpy (info, &ul, sizeof (ul));
  361. break;
  362. case CLSET_XID:
  363. /* This will set the xid of the NEXT call */
  364. memcpy (&ul, info, sizeof (ul));
  365. ui32 = htonl (ul - 1);
  366. memcpy (ct->ct_mcall, &ui32, sizeof (ui32));
  367. /* decrement by 1 as clnttcp_call() increments once */
  368. break;
  369. case CLGET_VERS:
  370. /*
  371. * This RELIES on the information that, in the call body,
  372. * the version number field is the fifth field from the
  373. * beginning of the RPC header. MUST be changed if the
  374. * call_struct is changed
  375. */
  376. memcpy (&ui32, ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT, sizeof (ui32));
  377. ul = ntohl (ui32);
  378. memcpy (info, &ul, sizeof (ul));
  379. break;
  380. case CLSET_VERS:
  381. memcpy (&ul, info, sizeof (ul));
  382. ui32 = htonl (ul);
  383. memcpy (ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT, &ui32, sizeof (ui32));
  384. break;
  385. case CLGET_PROG:
  386. /*
  387. * This RELIES on the information that, in the call body,
  388. * the program number field is the field from the
  389. * beginning of the RPC header. MUST be changed if the
  390. * call_struct is changed
  391. */
  392. memcpy (&ui32, ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT, sizeof (ui32));
  393. ul = ntohl (ui32);
  394. memcpy (info, &ul, sizeof (ul));
  395. break;
  396. case CLSET_PROG:
  397. memcpy (&ul, info, sizeof (ul));
  398. ui32 = htonl (ul);
  399. memcpy (ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT, &ui32, sizeof (ui32));
  400. break;
  401. /* The following are only possible with TI-RPC */
  402. case CLGET_RETRY_TIMEOUT:
  403. case CLSET_RETRY_TIMEOUT:
  404. case CLGET_SVC_ADDR:
  405. case CLSET_SVC_ADDR:
  406. case CLSET_PUSH_TIMOD:
  407. case CLSET_POP_TIMOD:
  408. default:
  409. return FALSE;
  410. }
  411. return TRUE;
  412. }
  413. static void
  414. clnttcp_destroy (CLIENT *h)
  415. {
  416. struct ct_data *ct =
  417. (struct ct_data *) h->cl_private;
  418. if (ct->ct_closeit)
  419. {
  420. (void) __close (ct->ct_sock);
  421. }
  422. XDR_DESTROY (&(ct->ct_xdrs));
  423. mem_free ((caddr_t) ct, sizeof (struct ct_data));
  424. mem_free ((caddr_t) h, sizeof (CLIENT));
  425. }
  426. /*
  427. * Interface between xdr serializer and tcp connection.
  428. * Behaves like the system calls, read & write, but keeps some error state
  429. * around for the rpc level.
  430. */
  431. static int
  432. readtcp (char *ctptr, char *buf, int len)
  433. {
  434. struct ct_data *ct = (struct ct_data *)ctptr;
  435. struct pollfd fd;
  436. int milliseconds = (ct->ct_wait.tv_sec * 1000) +
  437. (ct->ct_wait.tv_usec / 1000);
  438. if (len == 0)
  439. return 0;
  440. fd.fd = ct->ct_sock;
  441. fd.events = POLLIN;
  442. while (TRUE)
  443. {
  444. switch (__poll(&fd, 1, milliseconds))
  445. {
  446. case 0:
  447. ct->ct_error.re_status = RPC_TIMEDOUT;
  448. return -1;
  449. case -1:
  450. if (errno == EINTR)
  451. continue;
  452. ct->ct_error.re_status = RPC_CANTRECV;
  453. ct->ct_error.re_errno = errno;
  454. return -1;
  455. }
  456. break;
  457. }
  458. switch (len = __read (ct->ct_sock, buf, len))
  459. {
  460. case 0:
  461. /* premature eof */
  462. ct->ct_error.re_errno = ECONNRESET;
  463. ct->ct_error.re_status = RPC_CANTRECV;
  464. len = -1; /* it's really an error */
  465. break;
  466. case -1:
  467. ct->ct_error.re_errno = errno;
  468. ct->ct_error.re_status = RPC_CANTRECV;
  469. break;
  470. }
  471. return len;
  472. }
  473. static int
  474. writetcp (char *ctptr, char *buf, int len)
  475. {
  476. int i, cnt;
  477. struct ct_data *ct = (struct ct_data*)ctptr;
  478. for (cnt = len; cnt > 0; cnt -= i, buf += i)
  479. {
  480. if ((i = __write (ct->ct_sock, buf, cnt)) == -1)
  481. {
  482. ct->ct_error.re_errno = errno;
  483. ct->ct_error.re_status = RPC_CANTSEND;
  484. return -1;
  485. }
  486. }
  487. return len;
  488. }