svc_tcp.c 11 KB

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