svc_tcp.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. #include <unistd.h>
  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. #warning Expect 3 warnings for initialization from incompatible pointer type
  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 *svctcp_create(sock, sendsize, recvsize)
  111. register int sock;
  112. u_int sendsize;
  113. u_int recvsize;
  114. {
  115. bool_t madesock = FALSE;
  116. register SVCXPRT *xprt;
  117. register struct tcp_rendezvous *r;
  118. struct sockaddr_in addr;
  119. int len = sizeof(struct sockaddr_in);
  120. if (sock == RPC_ANYSOCK) {
  121. if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
  122. perror("svc_tcp.c - tcp socket creation problem");
  123. return ((SVCXPRT *) NULL);
  124. }
  125. madesock = TRUE;
  126. }
  127. bzero((char *) &addr, sizeof(addr));
  128. addr.sin_family = AF_INET;
  129. if (bindresvport(sock, &addr)) {
  130. addr.sin_port = 0;
  131. (void) bind(sock, (struct sockaddr *) &addr, len);
  132. }
  133. if ((getsockname(sock, (struct sockaddr *) &addr, &len) != 0) ||
  134. (listen(sock, 2) != 0)) {
  135. perror("svctcp_.c - cannot getsockname or listen");
  136. if (madesock)
  137. (void) close(sock);
  138. return ((SVCXPRT *) NULL);
  139. }
  140. r = (struct tcp_rendezvous *) mem_alloc(sizeof(*r));
  141. if (r == NULL) {
  142. (void) fprintf(stderr, "svctcp_create: out of memory\n");
  143. return (NULL);
  144. }
  145. r->sendsize = sendsize;
  146. r->recvsize = recvsize;
  147. xprt = (SVCXPRT *) mem_alloc(sizeof(SVCXPRT));
  148. if (xprt == NULL) {
  149. (void) fprintf(stderr, "svctcp_create: out of memory\n");
  150. return (NULL);
  151. }
  152. xprt->xp_p2 = NULL;
  153. xprt->xp_p1 = (caddr_t) r;
  154. xprt->xp_verf = _null_auth;
  155. xprt->xp_ops = &svctcp_rendezvous_op;
  156. xprt->xp_port = ntohs(addr.sin_port);
  157. xprt->xp_sock = sock;
  158. xprt_register(xprt);
  159. return (xprt);
  160. }
  161. /*
  162. * Like svtcp_create(), except the routine takes any *open* UNIX file
  163. * descriptor as its first input.
  164. */
  165. SVCXPRT *svcfd_create(fd, sendsize, recvsize)
  166. int fd;
  167. u_int sendsize;
  168. u_int recvsize;
  169. {
  170. return (makefd_xprt(fd, sendsize, recvsize));
  171. }
  172. static SVCXPRT *makefd_xprt(fd, sendsize, recvsize)
  173. int fd;
  174. u_int sendsize;
  175. u_int recvsize;
  176. {
  177. register SVCXPRT *xprt;
  178. register struct tcp_conn *cd;
  179. xprt = (SVCXPRT *) mem_alloc(sizeof(SVCXPRT));
  180. if (xprt == (SVCXPRT *) NULL) {
  181. (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
  182. goto done;
  183. }
  184. cd = (struct tcp_conn *) mem_alloc(sizeof(struct tcp_conn));
  185. if (cd == (struct tcp_conn *) NULL) {
  186. (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
  187. mem_free((char *) xprt, sizeof(SVCXPRT));
  188. xprt = (SVCXPRT *) NULL;
  189. goto done;
  190. }
  191. cd->strm_stat = XPRT_IDLE;
  192. xdrrec_create(&(cd->xdrs), sendsize, recvsize,
  193. (caddr_t) xprt, readtcp, writetcp);
  194. xprt->xp_p2 = NULL;
  195. xprt->xp_p1 = (caddr_t) cd;
  196. xprt->xp_verf.oa_base = cd->verf_body;
  197. xprt->xp_addrlen = 0;
  198. xprt->xp_ops = &svctcp_op; /* truely deals with calls */
  199. xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
  200. xprt->xp_sock = fd;
  201. xprt_register(xprt);
  202. done:
  203. return (xprt);
  204. }
  205. static bool_t rendezvous_request(xprt)
  206. register SVCXPRT *xprt;
  207. {
  208. int sock;
  209. struct tcp_rendezvous *r;
  210. struct sockaddr_in addr;
  211. int len;
  212. r = (struct tcp_rendezvous *) xprt->xp_p1;
  213. again:
  214. len = sizeof(struct sockaddr_in);
  215. if ((sock = accept(xprt->xp_sock, (struct sockaddr *) &addr,
  216. &len)) < 0) {
  217. if (errno == EINTR)
  218. goto again;
  219. return (FALSE);
  220. }
  221. /*
  222. * make a new transporter (re-uses xprt)
  223. */
  224. xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
  225. xprt->xp_raddr = addr;
  226. xprt->xp_addrlen = len;
  227. return (FALSE); /* there is never an rpc msg to be processed */
  228. }
  229. static enum xprt_stat rendezvous_stat()
  230. {
  231. return (XPRT_IDLE);
  232. }
  233. static void svctcp_destroy(xprt)
  234. register SVCXPRT *xprt;
  235. {
  236. register struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
  237. xprt_unregister(xprt);
  238. (void) close(xprt->xp_sock);
  239. if (xprt->xp_port != 0) {
  240. /* a rendezvouser socket */
  241. xprt->xp_port = 0;
  242. } else {
  243. /* an actual connection socket */
  244. XDR_DESTROY(&(cd->xdrs));
  245. }
  246. mem_free((caddr_t) cd, sizeof(struct tcp_conn));
  247. mem_free((caddr_t) xprt, sizeof(SVCXPRT));
  248. }
  249. /*
  250. * All read operations timeout after 35 seconds.
  251. * A timeout is fatal for the connection.
  252. */
  253. static struct timeval wait_per_try = { 35, 0 };
  254. /*
  255. * reads data from the tcp conection.
  256. * any error is fatal and the connection is closed.
  257. * (And a read of zero bytes is a half closed stream => error.)
  258. */
  259. static int readtcp(xprt, buf, len)
  260. register SVCXPRT *xprt;
  261. caddr_t buf;
  262. register int len;
  263. {
  264. register int sock = xprt->xp_sock;
  265. #ifdef FD_SETSIZE
  266. fd_set mask;
  267. fd_set readfds;
  268. FD_ZERO(&mask);
  269. FD_SET(sock, &mask);
  270. #else
  271. register int mask = 1 << sock;
  272. int readfds;
  273. #endif /* def FD_SETSIZE */
  274. do {
  275. readfds = mask;
  276. if (select(_rpc_dtablesize(), &readfds, NULL, NULL,
  277. &wait_per_try) <= 0) {
  278. if (errno == EINTR) {
  279. continue;
  280. }
  281. goto fatal_err;
  282. }
  283. #ifdef FD_SETSIZE
  284. } while (!FD_ISSET(sock, &readfds));
  285. #else
  286. } while (readfds != mask);
  287. #endif /* def FD_SETSIZE */
  288. if ((len = read(sock, buf, len)) > 0) {
  289. return (len);
  290. }
  291. fatal_err:
  292. ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
  293. return (-1);
  294. }
  295. /*
  296. * writes data to the tcp connection.
  297. * Any error is fatal and the connection is closed.
  298. */
  299. static int writetcp(xprt, buf, len)
  300. register SVCXPRT *xprt;
  301. caddr_t buf;
  302. int len;
  303. {
  304. register int i, cnt;
  305. for (cnt = len; cnt > 0; cnt -= i, buf += i) {
  306. if ((i = write(xprt->xp_sock, buf, cnt)) < 0) {
  307. ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
  308. return (-1);
  309. }
  310. }
  311. return (len);
  312. }
  313. static enum xprt_stat svctcp_stat(xprt)
  314. SVCXPRT *xprt;
  315. {
  316. register struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
  317. if (cd->strm_stat == XPRT_DIED)
  318. return (XPRT_DIED);
  319. if (!xdrrec_eof(&(cd->xdrs)))
  320. return (XPRT_MOREREQS);
  321. return (XPRT_IDLE);
  322. }
  323. static bool_t svctcp_recv(xprt, msg)
  324. SVCXPRT *xprt;
  325. register struct rpc_msg *msg;
  326. {
  327. register struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
  328. register XDR *xdrs = &(cd->xdrs);
  329. xdrs->x_op = XDR_DECODE;
  330. (void) xdrrec_skiprecord(xdrs);
  331. if (xdr_callmsg(xdrs, msg)) {
  332. cd->x_id = msg->rm_xid;
  333. return (TRUE);
  334. }
  335. return (FALSE);
  336. }
  337. static bool_t svctcp_getargs(xprt, xdr_args, args_ptr)
  338. SVCXPRT *xprt;
  339. xdrproc_t xdr_args;
  340. caddr_t args_ptr;
  341. {
  342. return ((*xdr_args)
  343. (&(((struct tcp_conn *) (xprt->xp_p1))->xdrs), args_ptr));
  344. }
  345. static bool_t svctcp_freeargs(xprt, xdr_args, args_ptr)
  346. SVCXPRT *xprt;
  347. xdrproc_t xdr_args;
  348. caddr_t args_ptr;
  349. {
  350. register XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
  351. xdrs->x_op = XDR_FREE;
  352. return ((*xdr_args) (xdrs, args_ptr));
  353. }
  354. static bool_t svctcp_reply(xprt, msg)
  355. SVCXPRT *xprt;
  356. register struct rpc_msg *msg;
  357. {
  358. register struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
  359. register XDR *xdrs = &(cd->xdrs);
  360. register bool_t stat;
  361. xdrs->x_op = XDR_ENCODE;
  362. msg->rm_xid = cd->x_id;
  363. stat = xdr_replymsg(xdrs, msg);
  364. (void) xdrrec_endofrecord(xdrs, TRUE);
  365. return (stat);
  366. }