2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro
30 * @(#)svc_tcp.c 2.2 88/08/01 4.0 RPCSRC
31 * $FreeBSD: src/lib/libc/rpc/svc_tcp.c,v 1.18.2.3 2001/09/05 22:29:23 dec Exp $
32 * $DragonFly: src/lib/libcr/rpc/Attic/svc_tcp.c,v 1.3 2004/10/25 19:38:25 drhodus Exp $
36 * svc_tcp.c, Server side for TCP/IP based RPC.
38 * Copyright (C) 1984, Sun Microsystems, Inc.
40 * Actually implements two flavors of transporter -
41 * a tcp rendezvouser (a listner and connection establisher)
42 * and a record/tcp stream.
50 #include <sys/socket.h>
51 #include <sys/ioctl.h>
55 * Ops vector for TCP/IP based rpc service handle
57 static bool_t svctcp_recv();
58 static enum xprt_stat svctcp_stat();
59 static bool_t svctcp_getargs();
60 static bool_t svctcp_reply();
61 static bool_t svctcp_freeargs();
62 static void svctcp_destroy();
64 static struct xp_ops svctcp_op = {
74 * Ops vector for TCP/IP rendezvous handler
76 static bool_t rendezvous_request();
77 static enum xprt_stat rendezvous_stat();
79 static struct xp_ops svctcp_rendezvous_op = {
88 static int readtcp(), writetcp();
89 static SVCXPRT *makefd_xprt();
91 struct tcp_rendezvous { /* kept in xprt->xp_p1 */
96 struct tcp_conn { /* kept in xprt->xp_p1 */
97 enum xprt_stat strm_stat;
100 char verf_body[MAX_AUTH_BYTES];
105 * xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
107 * Creates, registers, and returns a (rpc) tcp based transporter.
108 * Once *xprt is initialized, it is registered as a transporter
109 * see (svc.h, xprt_register). This routine returns
110 * a NULL if a problem occurred.
112 * If sock<0 then a socket is created, else sock is used.
113 * If the socket, sock is not bound to a port then svctcp_create
114 * binds it to an arbitrary port. The routine then starts a tcp
115 * listener on the socket's associated port. In any (successful) case,
116 * xprt->xp_sock is the registered socket number and xprt->xp_port is the
117 * associated port number.
119 * Since tcp streams do buffered io similar to stdio, the caller can specify
120 * how big the send and receive buffers are via the second and third parms;
121 * 0 => use the system default.
124 svctcp_create(sock, sendsize, recvsize)
129 bool_t madesock = FALSE;
131 struct tcp_rendezvous *r;
132 struct sockaddr_in addr;
133 int len = sizeof(struct sockaddr_in);
136 if (sock == RPC_ANYSOCK) {
137 if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
138 perror("svctcp_.c - tcp socket creation problem");
139 return ((SVCXPRT *)NULL);
144 if (ioctl(sock, FIONBIO, &on) < 0) {
145 perror("svc_tcp.c - cannot turn on non-blocking mode");
148 return ((SVCXPRT *)NULL);
150 memset(&addr, 0, sizeof (addr));
151 addr.sin_len = sizeof(struct sockaddr_in);
152 addr.sin_family = AF_INET;
153 if (bindresvport(sock, &addr)) {
155 (void)bind(sock, (struct sockaddr *)&addr, len);
157 if ((getsockname(sock, (struct sockaddr *)&addr, &len) != 0) ||
158 (listen(sock, 2) != 0)) {
159 perror("svctcp_.c - cannot getsockname or listen");
162 return ((SVCXPRT *)NULL);
164 r = (struct tcp_rendezvous *)mem_alloc(sizeof(*r));
166 (void) fprintf(stderr, "svctcp_create: out of memory\n");
169 r->sendsize = sendsize;
170 r->recvsize = recvsize;
171 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
173 (void) fprintf(stderr, "svctcp_create: out of memory\n");
177 xprt->xp_p1 = (caddr_t)r;
178 xprt->xp_verf = _null_auth;
179 xprt->xp_ops = &svctcp_rendezvous_op;
180 xprt->xp_port = ntohs(addr.sin_port);
181 xprt->xp_sock = sock;
187 * Like svtcp_create(), except the routine takes any *open* UNIX file
188 * descriptor as its first input.
191 svcfd_create(fd, sendsize, recvsize)
197 return (makefd_xprt(fd, sendsize, recvsize));
201 makefd_xprt(fd, sendsize, recvsize)
209 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
210 if (xprt == (SVCXPRT *)NULL) {
211 (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
214 cd = (struct tcp_conn *)mem_alloc(sizeof(struct tcp_conn));
215 if (cd == (struct tcp_conn *)NULL) {
216 (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
217 mem_free((char *) xprt, sizeof(SVCXPRT));
218 xprt = (SVCXPRT *)NULL;
221 cd->strm_stat = XPRT_IDLE;
222 xdrrec_create(&(cd->xdrs), sendsize, recvsize,
223 (caddr_t)xprt, readtcp, writetcp);
225 xprt->xp_p1 = (caddr_t)cd;
226 xprt->xp_verf.oa_base = cd->verf_body;
227 xprt->xp_addrlen = 0;
228 xprt->xp_ops = &svctcp_op; /* truely deals with calls */
229 xprt->xp_port = 0; /* this is a connection, not a rendezvouser */
237 rendezvous_request(xprt)
241 struct tcp_rendezvous *r;
242 struct sockaddr_in addr;
246 r = (struct tcp_rendezvous *)xprt->xp_p1;
248 len = sizeof(struct sockaddr_in);
249 if ((sock = accept(xprt->xp_sock, (struct sockaddr *)&addr,
256 * Guard against FTP bounce attacks.
258 if (addr.sin_port == htons(20)) {
263 * The listening socket is in FIONBIO mode and we inherit it.
266 if (ioctl(sock, FIONBIO, &off) < 0) {
271 * make a new transporter (re-uses xprt)
273 xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
274 xprt->xp_raddr = addr;
275 xprt->xp_addrlen = len;
276 return (FALSE); /* there is never an rpc msg to be processed */
279 static enum xprt_stat
290 struct tcp_conn *cd = (struct tcp_conn *)xprt->xp_p1;
292 xprt_unregister(xprt);
293 (void)_close(xprt->xp_sock);
294 if (xprt->xp_port != 0) {
295 /* a rendezvouser socket */
298 /* an actual connection socket */
299 XDR_DESTROY(&(cd->xdrs));
301 mem_free((caddr_t)cd, sizeof(struct tcp_conn));
302 mem_free((caddr_t)xprt, sizeof(SVCXPRT));
306 * All read operations timeout after 35 seconds.
307 * A timeout is fatal for the connection.
309 static struct timeval wait_per_try = { 35, 0 };
312 * reads data from the tcp conection.
313 * any error is fatal and the connection is closed.
314 * (And a read of zero bytes is a half closed stream => error.)
316 * Note: we have to be careful here not to allow ourselves to become
317 * blocked too long in this routine. While we're waiting for data from one
318 * client, another client may be trying to connect. To avoid this situation,
319 * some code from svc_run() is transplanted here: the select() loop checks
320 * all RPC descriptors including the one we want and calls svc_getreqset2()
321 * to handle new requests if any are detected.
324 readtcp(xprt, buf, len)
329 int sock = xprt->xp_sock;
330 struct timeval start, delta, tv;
331 struct timeval tmp1, tmp2;
333 extern fd_set *__svc_fdset;
334 extern int __svc_fdsetsize;
336 delta = wait_per_try;
338 gettimeofday(&start, NULL);
340 int bytes = howmany(__svc_fdsetsize, NFDBITS) *
344 fds = (fd_set *)malloc(bytes);
347 memcpy(fds, __svc_fdset, bytes);
349 /* XXX we know the other bits are still clear */
351 tv = delta; /* in case select() implements writeback */
352 switch (select(svc_maxfd + 1, fds, NULL, NULL, &tv)) {
354 memset(fds, 0, bytes);
357 gettimeofday(&tmp1, NULL);
358 timersub(&tmp1, &start, &tmp2);
359 timersub(&wait_per_try, &tmp2, &tmp1);
360 if (tmp1.tv_sec < 0 || !timerisset(&tmp1))
367 if (!FD_ISSET(sock, fds)) {
368 svc_getreqset2(fds, svc_maxfd + 1);
369 gettimeofday(&tmp1, NULL);
370 timersub(&tmp1, &start, &tmp2);
371 timersub(&wait_per_try, &tmp2, &tmp1);
372 if (tmp1.tv_sec < 0 || !timerisset(&tmp1))
378 } while (!FD_ISSET(sock, fds));
379 if ((len = _read(sock, buf, len)) > 0) {
385 ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
392 * writes data to the tcp connection.
393 * Any error is fatal and the connection is closed.
396 writetcp(xprt, buf, len)
403 for (cnt = len; cnt > 0; cnt -= i, buf += i) {
404 if ((i = _write(xprt->xp_sock, buf, cnt)) < 0) {
405 ((struct tcp_conn *)(xprt->xp_p1))->strm_stat =
413 static enum xprt_stat
417 struct tcp_conn *cd =
418 (struct tcp_conn *)(xprt->xp_p1);
420 if (cd->strm_stat == XPRT_DIED)
422 if (! xdrrec_eof(&(cd->xdrs)))
423 return (XPRT_MOREREQS);
428 svctcp_recv(xprt, msg)
432 struct tcp_conn *cd =
433 (struct tcp_conn *)(xprt->xp_p1);
434 XDR *xdrs = &(cd->xdrs);
436 xdrs->x_op = XDR_DECODE;
437 (void)xdrrec_skiprecord(xdrs);
438 if (xdr_callmsg(xdrs, msg)) {
439 cd->x_id = msg->rm_xid;
442 cd->strm_stat = XPRT_DIED; /* XXXX */
447 svctcp_getargs(xprt, xdr_args, args_ptr)
453 return ((*xdr_args)(&(((struct tcp_conn *)(xprt->xp_p1))->xdrs), args_ptr));
457 svctcp_freeargs(xprt, xdr_args, args_ptr)
463 &(((struct tcp_conn *)(xprt->xp_p1))->xdrs);
465 xdrs->x_op = XDR_FREE;
466 return ((*xdr_args)(xdrs, args_ptr));
470 svctcp_reply(xprt, msg)
474 struct tcp_conn *cd =
475 (struct tcp_conn *)(xprt->xp_p1);
476 XDR *xdrs = &(cd->xdrs);
479 xdrs->x_op = XDR_ENCODE;
480 msg->rm_xid = cd->x_id;
481 stat = xdr_replymsg(xdrs, msg);
482 (void)xdrrec_endofrecord(xdrs, TRUE);