1 /* $KAME: tcp.c,v 1.8 2001/11/21 07:40:22 itojun Exp $ */
4 * Copyright (C) 1997 and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $FreeBSD: src/usr.sbin/faithd/tcp.c,v 1.1.2.3 2002/04/28 05:40:29 suz Exp $
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
56 static char tcpbuf[16*1024];
57 /* bigger than MSS and may be lesser than window size */
58 static int tblen, tboff, oob_exists;
59 static fd_set readfds, writefds, exceptfds;
60 static char atmark_buf[2];
61 static pid_t cpid = (pid_t)0;
62 static pid_t ppid = (pid_t)0;
63 volatile time_t child_lastactive = (time_t)0;
64 static time_t parent_lastactive = (time_t)0;
66 static void sig_ctimeout __P((int));
67 static void sig_child __P((int));
68 static void notify_inactive __P((void));
69 static void notify_active __P((void));
70 static void send_data __P((int, int, const char *, int));
71 static void relay __P((int, int, const char *, int));
75 * - child side (ppid != 0) will send SIGUSR1 to parent every (FAITH_TIMEOUT/4)
76 * second if traffic is active. if traffic is inactive, don't send SIGUSR1.
77 * - parent side (ppid == 0) will check the last SIGUSR1 it have seen.
82 /* parent side: record notification from the child */
84 syslog(LOG_DEBUG, "activity timer from child");
85 child_lastactive = time(NULL);
88 /* parent will terminate if child dies. */
95 pid = wait3(&status, WNOHANG, (struct rusage *)0);
96 if (pid && WEXITSTATUS(status))
97 syslog(LOG_WARNING, "child %d exit status 0x%x", pid, status);
98 exit_success("terminate connection due to child termination");
106 /* only on parent side... */
110 /* parent side should check for timeout. */
113 syslog(LOG_DEBUG, "parent side %sactive, child side %sactive",
114 (FAITH_TIMEOUT < t - parent_lastactive) ? "in" : "",
115 (FAITH_TIMEOUT < t - child_lastactive) ? "in" : "");
118 if (FAITH_TIMEOUT < t - child_lastactive
119 && FAITH_TIMEOUT < t - parent_lastactive) {
120 /* both side timeouted */
121 signal(SIGCHLD, SIG_DFL);
124 exit_failure("connection timeout");
133 /* child side: notify parent of active traffic */
136 if (FAITH_TIMEOUT / 4 < t - child_lastactive) {
137 if (kill(ppid, SIGUSR1) < 0) {
138 exit_failure("terminate connection due to parent termination");
141 child_lastactive = t;
145 parent_lastactive = time(NULL);
150 send_data(int s_rcv, int s_snd, const char *service, int direction)
155 cc = send(s_snd, atmark_buf, 1, MSG_OOB);
159 FD_SET(s_rcv, &exceptfds);
162 for (; tboff < tblen; tboff += cc) {
163 cc = write(s_snd, tcpbuf + tboff, tblen - tboff);
169 if (tblen >= sizeof(tcpbuf))
170 tblen = sizeof(tcpbuf) - 1;
171 tcpbuf[tblen] = '\0';
172 syslog(LOG_DEBUG, "from %s (%dbytes): %s",
173 direction == 1 ? "client" : "server", tblen, tcpbuf);
176 tblen = 0; tboff = 0;
177 FD_CLR(s_snd, &writefds);
178 FD_SET(s_rcv, &readfds);
182 exit_failure("writing relay data failed: %s", strerror(errno));
183 FD_SET(s_snd, &writefds);
187 relay(int s_rcv, int s_snd, const char *service, int direction)
189 int atmark, error, maxfd;
191 fd_set oreadfds, owritefds, oexceptfds;
196 fcntl(s_snd, F_SETFD, O_NONBLOCK);
197 oreadfds = readfds; owritefds = writefds; oexceptfds = exceptfds;
198 FD_SET(s_rcv, &readfds);
199 FD_SET(s_rcv, &exceptfds);
201 maxfd = (s_rcv > s_snd) ? s_rcv : s_snd;
204 tv.tv_sec = FAITH_TIMEOUT / 4;
207 owritefds = writefds;
208 oexceptfds = exceptfds;
209 error = select(maxfd + 1, &readfds, &writefds, &exceptfds, &tv);
213 exit_failure("select: %s", strerror(errno));
214 } else if (error == 0) {
216 writefds = owritefds;
217 exceptfds = oexceptfds;
222 /* activity notification */
225 if (FD_ISSET(s_rcv, &exceptfds)) {
226 error = ioctl(s_rcv, SIOCATMARK, &atmark);
227 if (error != -1 && atmark == 1) {
230 cc = read(s_rcv, atmark_buf, 1);
232 FD_CLR(s_rcv, &exceptfds);
233 FD_SET(s_snd, &writefds);
235 } else if (cc == -1) {
238 exit_failure("reading oob data failed"
244 if (FD_ISSET(s_rcv, &readfds)) {
245 relaydata_read_retry:
246 tblen = read(s_rcv, tcpbuf, sizeof(tcpbuf));
252 goto relaydata_read_retry;
253 exit_failure("reading relay data failed: %s",
257 /* to close opposite-direction relay process */
262 exit_success("terminating %s relay", service);
265 FD_CLR(s_rcv, &readfds);
266 FD_SET(s_snd, &writefds);
270 if (FD_ISSET(s_snd, &writefds))
271 send_data(s_rcv, s_snd, service, direction);
276 tcp_relay(int s_src, int s_dst, const char *service)
278 syslog(LOG_INFO, "starting %s relay", service);
280 child_lastactive = parent_lastactive = time(NULL);
285 exit_failure("tcp_relay: can't fork grand child: %s",
289 /* child process: relay going traffic */
291 /* this is child so reopen log */
293 openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
294 relay(s_src, s_dst, service, 1);
297 /* parent process: relay coming traffic */
299 signal(SIGUSR1, sig_ctimeout);
300 signal(SIGCHLD, sig_child);
301 relay(s_dst, s_src, service, 0);