svc_tcp.c 9.9 KB

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