svc_tcp.c 10 KB

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