svc_tcp.c 11 KB

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