svc_tcp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * svc_tcp.c, Server side for TCP/IP based RPC.
  3. *
  4. * Copyright (C) 2012-2019 Free Software Foundation, Inc.
  5. * This file is part of the GNU C Library.
  6. *
  7. * The GNU C Library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * The GNU C Library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with the GNU C Library; if not, see
  19. * <http://www.gnu.org/licenses/>.
  20. *
  21. * Copyright (c) 2010, Oracle America, Inc.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions are
  25. * met:
  26. *
  27. * * Redistributions of source code must retain the above copyright
  28. * notice, this list of conditions and the following disclaimer.
  29. * * Redistributions in binary form must reproduce the above
  30. * copyright notice, this list of conditions and the following
  31. * disclaimer in the documentation and/or other materials
  32. * provided with the distribution.
  33. * * Neither the name of the "Oracle America, Inc." nor the names of its
  34. * contributors may be used to endorse or promote products derived
  35. * from this software without specific prior written permission.
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  38. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  39. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  40. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  41. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  42. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  43. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  44. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  45. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  46. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  47. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  48. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. * Actually implements two flavors of transporter -
  51. * a tcp rendezvouser (a listener and connection establisher)
  52. * and a record/tcp stream.
  53. */
  54. #include <stdio.h>
  55. #include <unistd.h>
  56. #include <string.h>
  57. #include <libintl.h>
  58. #include <rpc/rpc.h>
  59. #include <sys/socket.h>
  60. #include <sys/poll.h>
  61. #include <errno.h>
  62. #include <stdlib.h>
  63. #include <wchar.h>
  64. #include <libio/iolibio.h>
  65. #include <shlib-compat.h>
  66. /*
  67. * Ops vector for TCP/IP based rpc service handle
  68. */
  69. static bool_t svctcp_recv (SVCXPRT *, struct rpc_msg *);
  70. static enum xprt_stat svctcp_stat (SVCXPRT *);
  71. static bool_t svctcp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
  72. static bool_t svctcp_reply (SVCXPRT *, struct rpc_msg *);
  73. static bool_t svctcp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
  74. static void svctcp_destroy (SVCXPRT *);
  75. static const struct xp_ops svctcp_op =
  76. {
  77. svctcp_recv,
  78. svctcp_stat,
  79. svctcp_getargs,
  80. svctcp_reply,
  81. svctcp_freeargs,
  82. svctcp_destroy
  83. };
  84. /*
  85. * Ops vector for TCP/IP rendezvous handler
  86. */
  87. static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
  88. static enum xprt_stat rendezvous_stat (SVCXPRT *);
  89. static void svctcp_rendezvous_abort (void) __attribute__ ((__noreturn__));
  90. /* This function makes sure abort() relocation goes through PLT
  91. and thus can be lazy bound. */
  92. static void
  93. svctcp_rendezvous_abort (void)
  94. {
  95. abort ();
  96. };
  97. static const struct xp_ops svctcp_rendezvous_op =
  98. {
  99. rendezvous_request,
  100. rendezvous_stat,
  101. (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
  102. (bool_t (*) (SVCXPRT *, struct rpc_msg *)) svctcp_rendezvous_abort,
  103. (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
  104. svctcp_destroy
  105. };
  106. static int readtcp (char*, char *, int);
  107. static int writetcp (char *, char *, int);
  108. static SVCXPRT *makefd_xprt (int, u_int, u_int);
  109. struct tcp_rendezvous
  110. { /* kept in xprt->xp_p1 */
  111. u_int sendsize;
  112. u_int recvsize;
  113. };
  114. struct tcp_conn
  115. { /* kept in xprt->xp_p1 */
  116. enum xprt_stat strm_stat;
  117. u_long x_id;
  118. XDR xdrs;
  119. char verf_body[MAX_AUTH_BYTES];
  120. };
  121. /*
  122. * Usage:
  123. * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
  124. *
  125. * Creates, registers, and returns a (rpc) tcp based transporter.
  126. * Once *xprt is initialized, it is registered as a transporter
  127. * see (svc.h, xprt_register). This routine returns
  128. * a NULL if a problem occurred.
  129. *
  130. * If sock<0 then a socket is created, else sock is used.
  131. * If the socket, sock is not bound to a port then svctcp_create
  132. * binds it to an arbitrary port. The routine then starts a tcp
  133. * listener on the socket's associated port. In any (successful) case,
  134. * xprt->xp_sock is the registered socket number and xprt->xp_port is the
  135. * associated port number.
  136. *
  137. * Since tcp streams do buffered io similar to stdio, the caller can specify
  138. * how big the send and receive buffers are via the second and third parms;
  139. * 0 => use the system default.
  140. */
  141. SVCXPRT *
  142. svctcp_create (int sock, u_int sendsize, u_int recvsize)
  143. {
  144. bool_t madesock = FALSE;
  145. SVCXPRT *xprt;
  146. struct tcp_rendezvous *r;
  147. struct sockaddr_in addr;
  148. socklen_t len = sizeof (struct sockaddr_in);
  149. if (sock == RPC_ANYSOCK)
  150. {
  151. if ((sock = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  152. {
  153. perror (_("svc_tcp.c - tcp socket creation problem"));
  154. return (SVCXPRT *) NULL;
  155. }
  156. madesock = TRUE;
  157. }
  158. memset ((char *) &addr, 0, sizeof (addr));
  159. addr.sin_family = AF_INET;
  160. if (bindresvport (sock, &addr))
  161. {
  162. addr.sin_port = 0;
  163. (void) __bind (sock, (struct sockaddr *) &addr, len);
  164. }
  165. if ((__getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
  166. (__listen (sock, SOMAXCONN) != 0))
  167. {
  168. perror (_("svc_tcp.c - cannot getsockname or listen"));
  169. if (madesock)
  170. (void) __close (sock);
  171. return (SVCXPRT *) NULL;
  172. }
  173. r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
  174. xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
  175. if (r == NULL || xprt == NULL)
  176. {
  177. (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
  178. mem_free (r, sizeof (*r));
  179. mem_free (xprt, sizeof (SVCXPRT));
  180. return NULL;
  181. }
  182. r->sendsize = sendsize;
  183. r->recvsize = recvsize;
  184. xprt->xp_p2 = NULL;
  185. xprt->xp_p1 = (caddr_t) r;
  186. xprt->xp_verf = _null_auth;
  187. xprt->xp_ops = &svctcp_rendezvous_op;
  188. xprt->xp_port = ntohs (addr.sin_port);
  189. xprt->xp_sock = sock;
  190. xprt_register (xprt);
  191. return xprt;
  192. }
  193. #ifdef EXPORT_RPC_SYMBOLS
  194. libc_hidden_def (svctcp_create)
  195. #else
  196. libc_hidden_nolink_sunrpc (svctcp_create, GLIBC_2_0)
  197. #endif
  198. /*
  199. * Like svtcp_create(), except the routine takes any *open* UNIX file
  200. * descriptor as its first input.
  201. */
  202. SVCXPRT *
  203. svcfd_create (int fd, u_int sendsize, u_int recvsize)
  204. {
  205. return makefd_xprt (fd, sendsize, recvsize);
  206. }
  207. libc_hidden_nolink_sunrpc (svcfd_create, GLIBC_2_0)
  208. static SVCXPRT *
  209. makefd_xprt (int fd, u_int sendsize, u_int recvsize)
  210. {
  211. SVCXPRT *xprt;
  212. struct tcp_conn *cd;
  213. xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
  214. cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
  215. if (xprt == (SVCXPRT *) NULL || cd == NULL)
  216. {
  217. (void) __fxprintf (NULL, "%s: %s", "svc_tcp: makefd_xprt",
  218. _("out of memory\n"));
  219. mem_free (xprt, sizeof (SVCXPRT));
  220. mem_free (cd, sizeof (struct tcp_conn));
  221. return NULL;
  222. }
  223. cd->strm_stat = XPRT_IDLE;
  224. xdrrec_create (&(cd->xdrs), sendsize, recvsize,
  225. (caddr_t) xprt, readtcp, writetcp);
  226. xprt->xp_p2 = NULL;
  227. xprt->xp_p1 = (caddr_t) cd;
  228. xprt->xp_verf.oa_base = cd->verf_body;
  229. xprt->xp_addrlen = 0;
  230. xprt->xp_ops = &svctcp_op; /* truly deals with calls */
  231. xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
  232. xprt->xp_sock = fd;
  233. xprt_register (xprt);
  234. return xprt;
  235. }
  236. static bool_t
  237. rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg)
  238. {
  239. int sock;
  240. struct tcp_rendezvous *r;
  241. struct sockaddr_in addr;
  242. socklen_t len;
  243. r = (struct tcp_rendezvous *) xprt->xp_p1;
  244. again:
  245. len = sizeof (struct sockaddr_in);
  246. if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0)
  247. {
  248. if (errno == EINTR)
  249. goto again;
  250. __svc_accept_failed ();
  251. return FALSE;
  252. }
  253. /*
  254. * make a new transporter (re-uses xprt)
  255. */
  256. xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
  257. memcpy (&xprt->xp_raddr, &addr, sizeof (addr));
  258. xprt->xp_addrlen = len;
  259. return FALSE; /* there is never an rpc msg to be processed */
  260. }
  261. static enum xprt_stat
  262. rendezvous_stat (SVCXPRT *xprt)
  263. {
  264. return XPRT_IDLE;
  265. }
  266. static void
  267. svctcp_destroy (SVCXPRT *xprt)
  268. {
  269. struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
  270. xprt_unregister (xprt);
  271. (void) __close (xprt->xp_sock);
  272. if (xprt->xp_port != 0)
  273. {
  274. /* a rendezvouser socket */
  275. xprt->xp_port = 0;
  276. }
  277. else
  278. {
  279. /* an actual connection socket */
  280. XDR_DESTROY (&(cd->xdrs));
  281. }
  282. mem_free ((caddr_t) cd, sizeof (struct tcp_conn));
  283. mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
  284. }
  285. /*
  286. * reads data from the tcp connection.
  287. * any error is fatal and the connection is closed.
  288. * (And a read of zero bytes is a half closed stream => error.)
  289. */
  290. static int
  291. readtcp (char *xprtptr, char *buf, int len)
  292. {
  293. SVCXPRT *xprt = (SVCXPRT *)xprtptr;
  294. int sock = xprt->xp_sock;
  295. int milliseconds = 35 * 1000;
  296. struct pollfd pollfd;
  297. do
  298. {
  299. pollfd.fd = sock;
  300. pollfd.events = POLLIN;
  301. switch (__poll (&pollfd, 1, milliseconds))
  302. {
  303. case -1:
  304. if (errno == EINTR)
  305. continue;
  306. /*FALLTHROUGH*/
  307. case 0:
  308. goto fatal_err;
  309. default:
  310. if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP)
  311. || (pollfd.revents & POLLNVAL))
  312. goto fatal_err;
  313. break;
  314. }
  315. }
  316. while ((pollfd.revents & POLLIN) == 0);
  317. if ((len = __read (sock, buf, len)) > 0)
  318. return len;
  319. fatal_err:
  320. ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
  321. return -1;
  322. }
  323. /*
  324. * writes data to the tcp connection.
  325. * Any error is fatal and the connection is closed.
  326. */
  327. static int
  328. writetcp (char *xprtptr, char * buf, int len)
  329. {
  330. SVCXPRT *xprt = (SVCXPRT *)xprtptr;
  331. int i, cnt;
  332. for (cnt = len; cnt > 0; cnt -= i, buf += i)
  333. {
  334. if ((i = __write (xprt->xp_sock, buf, cnt)) < 0)
  335. {
  336. ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
  337. return -1;
  338. }
  339. }
  340. return len;
  341. }
  342. static enum xprt_stat
  343. svctcp_stat (SVCXPRT *xprt)
  344. {
  345. struct tcp_conn *cd =
  346. (struct tcp_conn *) (xprt->xp_p1);
  347. if (cd->strm_stat == XPRT_DIED)
  348. return XPRT_DIED;
  349. if (!xdrrec_eof (&(cd->xdrs)))
  350. return XPRT_MOREREQS;
  351. return XPRT_IDLE;
  352. }
  353. static bool_t
  354. svctcp_recv (SVCXPRT *xprt, struct rpc_msg *msg)
  355. {
  356. struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
  357. XDR *xdrs = &(cd->xdrs);
  358. xdrs->x_op = XDR_DECODE;
  359. (void) xdrrec_skiprecord (xdrs);
  360. if (xdr_callmsg (xdrs, msg))
  361. {
  362. cd->x_id = msg->rm_xid;
  363. return TRUE;
  364. }
  365. cd->strm_stat = XPRT_DIED; /* XXXX */
  366. return FALSE;
  367. }
  368. static bool_t
  369. svctcp_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
  370. {
  371. return ((*xdr_args) (&(((struct tcp_conn *)
  372. (xprt->xp_p1))->xdrs), args_ptr));
  373. }
  374. static bool_t
  375. svctcp_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
  376. {
  377. XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
  378. xdrs->x_op = XDR_FREE;
  379. return ((*xdr_args) (xdrs, args_ptr));
  380. }
  381. static bool_t
  382. svctcp_reply (SVCXPRT *xprt, struct rpc_msg *msg)
  383. {
  384. struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
  385. XDR *xdrs = &(cd->xdrs);
  386. bool_t stat;
  387. xdrs->x_op = XDR_ENCODE;
  388. msg->rm_xid = cd->x_id;
  389. stat = xdr_replymsg (xdrs, msg);
  390. (void) xdrrec_endofrecord (xdrs, TRUE);
  391. return stat;
  392. }