svc_tcp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* @(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC */
  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. #if 0
  31. static char sccsid[] = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33. /*
  34. * svc_tcp.c, Server side for TCP/IP based RPC.
  35. *
  36. * Copyright (C) 1984, Sun Microsystems, Inc.
  37. *
  38. * Actually implements two flavors of transporter -
  39. * a tcp rendezvouser (a listener and connection establisher)
  40. * and a record/tcp stream.
  41. */
  42. #include <stdio.h>
  43. #include <unistd.h>
  44. #include <string.h>
  45. #include "rpc_private.h"
  46. #include <sys/socket.h>
  47. #include <sys/poll.h>
  48. #include <errno.h>
  49. #include <stdlib.h>
  50. #ifdef USE_IN_LIBIO
  51. # include <wchar.h>
  52. # include <libio/iolibio.h>
  53. # define fputs(s, f) _IO_fputs (s, f)
  54. #endif
  55. /*
  56. * Ops vector for TCP/IP based rpc service handle
  57. */
  58. static bool_t svctcp_recv (SVCXPRT *, struct rpc_msg *);
  59. static enum xprt_stat svctcp_stat (SVCXPRT *);
  60. static bool_t svctcp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
  61. static bool_t svctcp_reply (SVCXPRT *, struct rpc_msg *);
  62. static bool_t svctcp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
  63. static void svctcp_destroy (SVCXPRT *);
  64. static const struct xp_ops svctcp_op =
  65. {
  66. svctcp_recv,
  67. svctcp_stat,
  68. svctcp_getargs,
  69. svctcp_reply,
  70. svctcp_freeargs,
  71. svctcp_destroy
  72. };
  73. /*
  74. * Ops vector for TCP/IP rendezvous handler
  75. */
  76. static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
  77. static enum xprt_stat rendezvous_stat (SVCXPRT *);
  78. static void svctcp_rendezvous_abort (void) attribute_noreturn;
  79. /* This function makes sure abort() relocation goes through PLT
  80. and thus can be lazy bound. */
  81. static void
  82. svctcp_rendezvous_abort (void)
  83. {
  84. abort ();
  85. };
  86. static const struct xp_ops svctcp_rendezvous_op =
  87. {
  88. rendezvous_request,
  89. rendezvous_stat,
  90. (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
  91. (bool_t (*) (SVCXPRT *, struct rpc_msg *)) svctcp_rendezvous_abort,
  92. (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
  93. svctcp_destroy
  94. };
  95. static int readtcp (char*, char *, int);
  96. static int writetcp (char *, char *, int);
  97. static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function;
  98. struct tcp_rendezvous
  99. { /* kept in xprt->xp_p1 */
  100. u_int sendsize;
  101. u_int recvsize;
  102. };
  103. struct tcp_conn
  104. { /* kept in xprt->xp_p1 */
  105. enum xprt_stat strm_stat;
  106. u_long x_id;
  107. XDR xdrs;
  108. char verf_body[MAX_AUTH_BYTES];
  109. };
  110. /*
  111. * Usage:
  112. * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
  113. *
  114. * Creates, registers, and returns a (rpc) tcp based transporter.
  115. * Once *xprt is initialized, it is registered as a transporter
  116. * see (svc.h, xprt_register). This routine returns
  117. * a NULL if a problem occurred.
  118. *
  119. * If sock<0 then a socket is created, else sock is used.
  120. * If the socket, sock is not bound to a port then svctcp_create
  121. * binds it to an arbitrary port. The routine then starts a tcp
  122. * listener on the socket's associated port. In any (successful) case,
  123. * xprt->xp_sock is the registered socket number and xprt->xp_port is the
  124. * associated port number.
  125. *
  126. * Since tcp streams do buffered io similar to stdio, the caller can specify
  127. * how big the send and receive buffers are via the second and third parms;
  128. * 0 => use the system default.
  129. */
  130. SVCXPRT *
  131. svctcp_create (int sock, u_int sendsize, u_int recvsize)
  132. {
  133. bool_t madesock = FALSE;
  134. SVCXPRT *xprt;
  135. struct tcp_rendezvous *r;
  136. struct sockaddr_in addr;
  137. socklen_t len = sizeof (struct sockaddr_in);
  138. if (sock == RPC_ANYSOCK)
  139. {
  140. if ((sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  141. {
  142. perror (_("svc_tcp.c - tcp socket creation problem"));
  143. return (SVCXPRT *) NULL;
  144. }
  145. madesock = TRUE;
  146. }
  147. memset ((char *) &addr, 0, sizeof (addr));
  148. addr.sin_family = AF_INET;
  149. if (bindresvport (sock, &addr))
  150. {
  151. addr.sin_port = 0;
  152. (void) bind (sock, (struct sockaddr *) &addr, len);
  153. }
  154. if ((getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
  155. (listen (sock, 2) != 0))
  156. {
  157. perror (_("svc_tcp.c - cannot getsockname or listen"));
  158. if (madesock)
  159. (void) close (sock);
  160. return (SVCXPRT *) NULL;
  161. }
  162. r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
  163. xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
  164. if (r == NULL || xprt == NULL)
  165. {
  166. #ifdef USE_IN_LIBIO
  167. if (_IO_fwide (stderr, 0) > 0)
  168. (void) __fwprintf (stderr, L"%s", _("svctcp_create: out of memory\n"));
  169. else
  170. #endif
  171. (void) fputs (_("svctcp_create: out of memory\n"), stderr);
  172. mem_free (r, sizeof (*r));
  173. mem_free (xprt, sizeof (SVCXPRT));
  174. return NULL;
  175. }
  176. r->sendsize = sendsize;
  177. r->recvsize = recvsize;
  178. xprt->xp_p2 = NULL;
  179. xprt->xp_p1 = (caddr_t) r;
  180. xprt->xp_verf = _null_auth;
  181. xprt->xp_ops = &svctcp_rendezvous_op;
  182. xprt->xp_port = ntohs (addr.sin_port);
  183. xprt->xp_sock = sock;
  184. xprt_register (xprt);
  185. return xprt;
  186. }
  187. /*
  188. * Like svtcp_create(), except the routine takes any *open* UNIX file
  189. * descriptor as its first input.
  190. */
  191. SVCXPRT *
  192. svcfd_create (int fd, u_int sendsize, u_int recvsize);
  193. SVCXPRT *
  194. svcfd_create (int fd, u_int sendsize, u_int recvsize)
  195. {
  196. return makefd_xprt (fd, sendsize, recvsize);
  197. }
  198. static SVCXPRT *
  199. internal_function
  200. makefd_xprt (int fd, u_int sendsize, u_int recvsize)
  201. {
  202. SVCXPRT *xprt;
  203. struct tcp_conn *cd;
  204. xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
  205. cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
  206. if (xprt == (SVCXPRT *) NULL || cd == NULL)
  207. {
  208. #ifdef USE_IN_LIBIO
  209. if (_IO_fwide (stderr, 0) > 0)
  210. (void) __fwprintf (stderr, L"%s",
  211. _("svc_tcp: makefd_xprt: out of memory\n"));
  212. else
  213. #endif
  214. (void) fputs (_("svc_tcp: makefd_xprt: out of memory\n"), stderr);
  215. mem_free (xprt, sizeof (SVCXPRT));
  216. mem_free (cd, sizeof (struct tcp_conn));
  217. return NULL;
  218. }
  219. cd->strm_stat = XPRT_IDLE;
  220. xdrrec_create (&(cd->xdrs), sendsize, recvsize,
  221. (caddr_t) xprt, readtcp, writetcp);
  222. xprt->xp_p2 = NULL;
  223. xprt->xp_p1 = (caddr_t) cd;
  224. xprt->xp_verf.oa_base = cd->verf_body;
  225. xprt->xp_addrlen = 0;
  226. xprt->xp_ops = &svctcp_op; /* truly deals with calls */
  227. xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
  228. xprt->xp_sock = fd;
  229. xprt_register (xprt);
  230. return xprt;
  231. }
  232. static bool_t
  233. rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg attribute_unused)
  234. {
  235. int sock;
  236. struct tcp_rendezvous *r;
  237. struct sockaddr_in addr;
  238. socklen_t len;
  239. r = (struct tcp_rendezvous *) xprt->xp_p1;
  240. again:
  241. len = sizeof (struct sockaddr_in);
  242. if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0)
  243. {
  244. if (errno == EINTR)
  245. goto again;
  246. return FALSE;
  247. }
  248. /*
  249. * make a new transporter (re-uses xprt)
  250. */
  251. xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
  252. memcpy (&xprt->xp_raddr, &addr, sizeof (addr));
  253. xprt->xp_addrlen = len;
  254. return FALSE; /* there is never an rpc msg to be processed */
  255. }
  256. static enum xprt_stat
  257. rendezvous_stat (SVCXPRT *xprt attribute_unused)
  258. {
  259. return XPRT_IDLE;
  260. }
  261. static void
  262. svctcp_destroy (SVCXPRT *xprt)
  263. {
  264. struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
  265. xprt_unregister (xprt);
  266. (void) close (xprt->xp_sock);
  267. if (xprt->xp_port != 0)
  268. {
  269. /* a rendezvouser socket */
  270. xprt->xp_port = 0;
  271. }
  272. else
  273. {
  274. /* an actual connection socket */
  275. XDR_DESTROY (&(cd->xdrs));
  276. }
  277. mem_free ((caddr_t) cd, sizeof (struct tcp_conn));
  278. mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
  279. }
  280. /*
  281. * reads data from the tcp connection.
  282. * any error is fatal and the connection is closed.
  283. * (And a read of zero bytes is a half closed stream => error.)
  284. */
  285. static int
  286. readtcp (char *xprtptr, char *buf, int len)
  287. {
  288. SVCXPRT *xprt = (SVCXPRT *)xprtptr;
  289. int sock = xprt->xp_sock;
  290. int milliseconds = 35 * 1000;
  291. struct pollfd pollfd;
  292. do
  293. {
  294. pollfd.fd = sock;
  295. pollfd.events = POLLIN;
  296. switch (poll (&pollfd, 1, milliseconds))
  297. {
  298. case -1:
  299. if (errno == EINTR)
  300. continue;
  301. /*FALLTHROUGH*/
  302. case 0:
  303. goto fatal_err;
  304. default:
  305. if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP)
  306. || (pollfd.revents & POLLNVAL))
  307. goto fatal_err;
  308. break;
  309. }
  310. }
  311. while ((pollfd.revents & POLLIN) == 0);
  312. if ((len = read (sock, buf, len)) > 0)
  313. return len;
  314. fatal_err:
  315. ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
  316. return -1;
  317. }
  318. /*
  319. * writes data to the tcp connection.
  320. * Any error is fatal and the connection is closed.
  321. */
  322. static int
  323. writetcp (char *xprtptr, char * buf, int len)
  324. {
  325. SVCXPRT *xprt = (SVCXPRT *)xprtptr;
  326. int i, cnt;
  327. for (cnt = len; cnt > 0; cnt -= i, buf += i)
  328. {
  329. if ((i = write (xprt->xp_sock, buf, cnt)) < 0)
  330. {
  331. ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
  332. return -1;
  333. }
  334. }
  335. return len;
  336. }
  337. static enum xprt_stat
  338. svctcp_stat (SVCXPRT *xprt)
  339. {
  340. struct tcp_conn *cd =
  341. (struct tcp_conn *) (xprt->xp_p1);
  342. if (cd->strm_stat == XPRT_DIED)
  343. return XPRT_DIED;
  344. if (!xdrrec_eof (&(cd->xdrs)))
  345. return XPRT_MOREREQS;
  346. return XPRT_IDLE;
  347. }
  348. static bool_t
  349. svctcp_recv (SVCXPRT *xprt, struct rpc_msg *msg)
  350. {
  351. struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
  352. XDR *xdrs = &(cd->xdrs);
  353. xdrs->x_op = XDR_DECODE;
  354. (void) xdrrec_skiprecord (xdrs);
  355. if (xdr_callmsg (xdrs, msg))
  356. {
  357. cd->x_id = msg->rm_xid;
  358. return TRUE;
  359. }
  360. cd->strm_stat = XPRT_DIED; /* XXXX */
  361. return FALSE;
  362. }
  363. static bool_t
  364. svctcp_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
  365. {
  366. return ((*xdr_args) (&(((struct tcp_conn *)
  367. (xprt->xp_p1))->xdrs), args_ptr));
  368. }
  369. static bool_t
  370. svctcp_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
  371. {
  372. XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
  373. xdrs->x_op = XDR_FREE;
  374. return ((*xdr_args) (xdrs, args_ptr));
  375. }
  376. static bool_t
  377. svctcp_reply (SVCXPRT *xprt, struct rpc_msg *msg)
  378. {
  379. struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
  380. XDR *xdrs = &(cd->xdrs);
  381. bool_t stat;
  382. xdrs->x_op = XDR_ENCODE;
  383. msg->rm_xid = cd->x_id;
  384. stat = xdr_replymsg (xdrs, msg);
  385. (void) xdrrec_endofrecord (xdrs, TRUE);
  386. return stat;
  387. }