svc_tcp.c 12 KB

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