Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / usr.sbin / faithd / tcp.c
blob309371229da16f2e0078e691bc351eb7c5822a3e
1 /* $KAME: tcp.c,v 1.8 2001/11/21 07:40:22 itojun Exp $ */
3 /*
4 * Copyright (C) 1997 and 1998 WIDE Project.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
29 * SUCH DAMAGE.
31 * $FreeBSD: src/usr.sbin/faithd/tcp.c,v 1.1.2.3 2002/04/28 05:40:29 suz Exp $
32 * $DragonFly: src/usr.sbin/faithd/tcp.c,v 1.5 2007/05/18 17:05:12 dillon Exp $
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/ioctl.h>
39 #include <sys/time.h>
40 #include <sys/wait.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <syslog.h>
46 #include <unistd.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <signal.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <netdb.h>
55 #include "faithd.h"
57 static char tcpbuf[16*1024];
58 /* bigger than MSS and may be lesser than window size */
59 static int tblen, tboff, oob_exists;
60 static fd_set readfds, writefds, exceptfds;
61 static char atmark_buf[2];
62 static pid_t cpid = (pid_t)0;
63 static pid_t ppid = (pid_t)0;
64 volatile time_t child_lastactive = (time_t)0;
65 static time_t parent_lastactive = (time_t)0;
67 static void sig_ctimeout(int);
68 static void sig_child(int);
69 static void notify_inactive(void);
70 static void notify_active(void);
71 static void send_data(int, int, const char *, int);
72 static void relay(int, int, const char *, int);
75 * Inactivity timer:
76 * - child side (ppid != 0) will send SIGUSR1 to parent every (FAITH_TIMEOUT/4)
77 * second if traffic is active. if traffic is inactive, don't send SIGUSR1.
78 * - parent side (ppid == 0) will check the last SIGUSR1 it have seen.
80 static void
81 sig_ctimeout(int sig)
83 /* parent side: record notification from the child */
84 if (dflag)
85 syslog(LOG_DEBUG, "activity timer from child");
86 child_lastactive = time(NULL);
89 /* parent will terminate if child dies. */
90 static void
91 sig_child(int sig)
93 int status;
94 pid_t pid;
96 pid = wait3(&status, WNOHANG, NULL);
97 if (pid && WEXITSTATUS(status))
98 syslog(LOG_WARNING, "child %d exit status 0x%x", pid, status);
99 exit_success("terminate connection due to child termination");
102 static void
103 notify_inactive(void)
105 time_t t;
107 /* only on parent side... */
108 if (ppid)
109 return;
111 /* parent side should check for timeout. */
112 t = time(NULL);
113 if (dflag) {
114 syslog(LOG_DEBUG, "parent side %sactive, child side %sactive",
115 (FAITH_TIMEOUT < t - parent_lastactive) ? "in" : "",
116 (FAITH_TIMEOUT < t - child_lastactive) ? "in" : "");
119 if (FAITH_TIMEOUT < t - child_lastactive
120 && FAITH_TIMEOUT < t - parent_lastactive) {
121 /* both side timeouted */
122 signal(SIGCHLD, SIG_DFL);
123 kill(cpid, SIGTERM);
124 wait(NULL);
125 exit_failure("connection timeout");
126 /* NOTREACHED */
130 static void
131 notify_active(void)
133 if (ppid) {
134 /* child side: notify parent of active traffic */
135 time_t t;
136 t = time(NULL);
137 if (FAITH_TIMEOUT / 4 < t - child_lastactive) {
138 if (kill(ppid, SIGUSR1) < 0) {
139 exit_failure("terminate connection due to parent termination");
140 /* NOTREACHED */
142 child_lastactive = t;
144 } else {
145 /* parent side */
146 parent_lastactive = time(NULL);
150 static void
151 send_data(int s_rcv, int s_snd, const char *service, int direction)
153 int cc;
155 if (oob_exists) {
156 cc = send(s_snd, atmark_buf, 1, MSG_OOB);
157 if (cc == -1)
158 goto retry_or_err;
159 oob_exists = 0;
160 FD_SET(s_rcv, &exceptfds);
163 for (; tboff < tblen; tboff += cc) {
164 cc = write(s_snd, tcpbuf + tboff, tblen - tboff);
165 if (cc < 0)
166 goto retry_or_err;
168 #ifdef DEBUG
169 if (tblen) {
170 if (tblen >= sizeof(tcpbuf))
171 tblen = sizeof(tcpbuf) - 1;
172 tcpbuf[tblen] = '\0';
173 syslog(LOG_DEBUG, "from %s (%dbytes): %s",
174 direction == 1 ? "client" : "server", tblen, tcpbuf);
176 #endif /* DEBUG */
177 tblen = 0; tboff = 0;
178 FD_CLR(s_snd, &writefds);
179 FD_SET(s_rcv, &readfds);
180 return;
181 retry_or_err:
182 if (errno != EAGAIN)
183 exit_failure("writing relay data failed: %s", strerror(errno));
184 FD_SET(s_snd, &writefds);
187 static void
188 relay(int s_rcv, int s_snd, const char *service, int direction)
190 int atmark, error, maxfd;
191 struct timeval tv;
192 fd_set oreadfds, owritefds, oexceptfds;
194 FD_ZERO(&readfds);
195 FD_ZERO(&writefds);
196 FD_ZERO(&exceptfds);
197 fcntl(s_snd, F_SETFD, O_NONBLOCK);
198 oreadfds = readfds; owritefds = writefds; oexceptfds = exceptfds;
199 FD_SET(s_rcv, &readfds);
200 FD_SET(s_rcv, &exceptfds);
201 oob_exists = 0;
202 maxfd = (s_rcv > s_snd) ? s_rcv : s_snd;
204 for (;;) {
205 tv.tv_sec = FAITH_TIMEOUT / 4;
206 tv.tv_usec = 0;
207 oreadfds = readfds;
208 owritefds = writefds;
209 oexceptfds = exceptfds;
210 error = select(maxfd + 1, &readfds, &writefds, &exceptfds, &tv);
211 if (error == -1) {
212 if (errno == EINTR)
213 continue;
214 exit_failure("select: %s", strerror(errno));
215 } else if (error == 0) {
216 readfds = oreadfds;
217 writefds = owritefds;
218 exceptfds = oexceptfds;
219 notify_inactive();
220 continue;
223 /* activity notification */
224 notify_active();
226 if (FD_ISSET(s_rcv, &exceptfds)) {
227 error = ioctl(s_rcv, SIOCATMARK, &atmark);
228 if (error != -1 && atmark == 1) {
229 int cc;
230 oob_read_retry:
231 cc = read(s_rcv, atmark_buf, 1);
232 if (cc == 1) {
233 FD_CLR(s_rcv, &exceptfds);
234 FD_SET(s_snd, &writefds);
235 oob_exists = 1;
236 } else if (cc == -1) {
237 if (errno == EINTR)
238 goto oob_read_retry;
239 exit_failure("reading oob data failed"
240 ": %s",
241 strerror(errno));
245 if (FD_ISSET(s_rcv, &readfds)) {
246 relaydata_read_retry:
247 tblen = read(s_rcv, tcpbuf, sizeof(tcpbuf));
248 tboff = 0;
250 switch (tblen) {
251 case -1:
252 if (errno == EINTR)
253 goto relaydata_read_retry;
254 exit_failure("reading relay data failed: %s",
255 strerror(errno));
256 /* NOTREACHED */
257 case 0:
258 /* to close opposite-direction relay process */
259 shutdown(s_snd, SHUT_RD);
261 close(s_rcv);
262 close(s_snd);
263 exit_success("terminating %s relay", service);
264 /* NOTREACHED */
265 default:
266 FD_CLR(s_rcv, &readfds);
267 FD_SET(s_snd, &writefds);
268 break;
271 if (FD_ISSET(s_snd, &writefds))
272 send_data(s_rcv, s_snd, service, direction);
276 void
277 tcp_relay(int s_src, int s_dst, const char *service)
279 syslog(LOG_INFO, "starting %s relay", service);
281 child_lastactive = parent_lastactive = time(NULL);
283 cpid = fork();
284 switch (cpid) {
285 case -1:
286 exit_failure("tcp_relay: can't fork grand child: %s",
287 strerror(errno));
288 /* NOTREACHED */
289 case 0:
290 /* child process: relay going traffic */
291 ppid = getppid();
292 /* this is child so reopen log */
293 closelog();
294 openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
295 relay(s_src, s_dst, service, 1);
296 /* NOTREACHED */
297 default:
298 /* parent process: relay coming traffic */
299 ppid = (pid_t)0;
300 signal(SIGUSR1, sig_ctimeout);
301 signal(SIGCHLD, sig_child);
302 relay(s_dst, s_src, service, 0);
303 /* NOTREACHED */