svc_tcp.c 10 KB

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