]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.sbin/faithd/tcp.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.sbin / faithd / tcp.c
1 /*      $KAME: tcp.c,v 1.13 2003/09/02 22:49:21 itojun Exp $    */
2
3 /*
4  * Copyright (C) 1997 and 1998 WIDE Project.
5  * All rights reserved.
6  *
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.
18  *
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.
30  *
31  * $FreeBSD$
32  */
33
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
38 #include <sys/time.h>
39 #include <sys/wait.h>
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <syslog.h>
45 #include <unistd.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <signal.h>
49
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 #include <netdb.h>
53
54 #include "faithd.h"
55
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;
65
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));
72
73 /*
74  * Inactivity timer:
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.
78  */
79 static void
80 sig_ctimeout(int sig)
81 {
82         /* parent side: record notification from the child */
83         if (dflag)
84                 syslog(LOG_DEBUG, "activity timer from child");
85         child_lastactive = time(NULL);
86 }
87
88 /* parent will terminate if child dies. */
89 static void
90 sig_child(int sig)
91 {
92         int status;
93         pid_t pid;
94
95         pid = wait3(&status, WNOHANG, (struct rusage *)0);
96         if (pid > 0 && WEXITSTATUS(status))
97                 syslog(LOG_WARNING, "child %ld exit status 0x%x",
98                     (long)pid, status);
99         exit_success("terminate connection due to child termination");
100 }
101
102 static void
103 notify_inactive()
104 {
105         time_t t;
106
107         /* only on parent side... */
108         if (ppid)
109                 return;
110
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" : "");
117         }
118
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 */
127         }
128 }
129
130 static void
131 notify_active()
132 {
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 */
141                         }
142                         child_lastactive = t;
143                 }
144         } else {
145                 /* parent side */
146                 parent_lastactive = time(NULL);
147         }
148 }
149
150 static void
151 send_data(int s_rcv, int s_snd, const char *service, int direction)
152 {
153         int cc;
154
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                 if (s_rcv >= FD_SETSIZE)
161                         exit_failure("descriptor too big");
162                 FD_SET(s_rcv, &exceptfds);
163         }
164
165         for (; tboff < tblen; tboff += cc) {
166                 cc = write(s_snd, tcpbuf + tboff, tblen - tboff);
167                 if (cc < 0)
168                         goto retry_or_err;
169         }
170 #ifdef DEBUG
171         if (tblen) {
172                 if (tblen >= sizeof(tcpbuf))
173                         tblen = sizeof(tcpbuf) - 1;
174                 tcpbuf[tblen] = '\0';
175                 syslog(LOG_DEBUG, "from %s (%dbytes): %s",
176                        direction == 1 ? "client" : "server", tblen, tcpbuf);
177         }
178 #endif /* DEBUG */
179         tblen = 0; tboff = 0;
180         if (s_snd >= FD_SETSIZE)
181                 exit_failure("descriptor too big");
182         FD_CLR(s_snd, &writefds);
183         if (s_rcv >= FD_SETSIZE)
184                 exit_failure("descriptor too big");
185         FD_SET(s_rcv, &readfds);
186         return;
187     retry_or_err:
188         if (errno != EAGAIN)
189                 exit_failure("writing relay data failed: %s", strerror(errno));
190         if (s_snd >= FD_SETSIZE)
191                 exit_failure("descriptor too big");
192         FD_SET(s_snd, &writefds);
193 }
194
195 static void
196 relay(int s_rcv, int s_snd, const char *service, int direction)
197 {
198         int atmark, error, maxfd;
199         struct timeval tv;
200         fd_set oreadfds, owritefds, oexceptfds;
201
202         FD_ZERO(&readfds);
203         FD_ZERO(&writefds);
204         FD_ZERO(&exceptfds);
205         fcntl(s_snd, F_SETFD, O_NONBLOCK);
206         oreadfds = readfds; owritefds = writefds; oexceptfds = exceptfds;
207         if (s_rcv >= FD_SETSIZE)
208                 exit_failure("descriptor too big");
209         FD_SET(s_rcv, &readfds);
210         FD_SET(s_rcv, &exceptfds);
211         oob_exists = 0;
212         maxfd = (s_rcv > s_snd) ? s_rcv : s_snd;
213
214         for (;;) {
215                 tv.tv_sec = FAITH_TIMEOUT / 4;
216                 tv.tv_usec = 0;
217                 oreadfds = readfds;
218                 owritefds = writefds;
219                 oexceptfds = exceptfds;
220                 error = select(maxfd + 1, &readfds, &writefds, &exceptfds, &tv);
221                 if (error == -1) {
222                         if (errno == EINTR)
223                                 continue;
224                         exit_failure("select: %s", strerror(errno));
225                 } else if (error == 0) {
226                         readfds = oreadfds;
227                         writefds = owritefds;
228                         exceptfds = oexceptfds;
229                         notify_inactive();
230                         continue;
231                 }
232
233                 /* activity notification */
234                 notify_active();
235
236                 if (FD_ISSET(s_rcv, &exceptfds)) {
237                         error = ioctl(s_rcv, SIOCATMARK, &atmark);
238                         if (error != -1 && atmark == 1) {
239                                 int cc;
240                             oob_read_retry:
241                                 cc = read(s_rcv, atmark_buf, 1);
242                                 if (cc == 1) {
243                                         if (s_rcv >= FD_SETSIZE)
244                                                 exit_failure("descriptor too big");
245                                         FD_CLR(s_rcv, &exceptfds);
246                                         if (s_snd >= FD_SETSIZE)
247                                                 exit_failure("descriptor too big");
248                                         FD_SET(s_snd, &writefds);
249                                         oob_exists = 1;
250                                 } else if (cc == -1) {
251                                         if (errno == EINTR)
252                                                 goto oob_read_retry;
253                                         exit_failure("reading oob data failed"
254                                                      ": %s",
255                                                      strerror(errno));
256                                 }
257                         }
258                 }
259                 if (FD_ISSET(s_rcv, &readfds)) {
260                     relaydata_read_retry:
261                         tblen = read(s_rcv, tcpbuf, sizeof(tcpbuf));
262                         tboff = 0;
263
264                         switch (tblen) {
265                         case -1:
266                                 if (errno == EINTR)
267                                         goto relaydata_read_retry;
268                                 exit_failure("reading relay data failed: %s",
269                                              strerror(errno));
270                                 /* NOTREACHED */
271                         case 0:
272                                 /* to close opposite-direction relay process */
273                                 shutdown(s_snd, 0);
274
275                                 close(s_rcv);
276                                 close(s_snd);
277                                 exit_success("terminating %s relay", service);
278                                 /* NOTREACHED */
279                         default:
280                                 if (s_rcv >= FD_SETSIZE)
281                                         exit_failure("descriptor too big");
282                                 FD_CLR(s_rcv, &readfds);
283                                 if (s_snd >= FD_SETSIZE)
284                                         exit_failure("descriptor too big");
285                                 FD_SET(s_snd, &writefds);
286                                 break;
287                         }
288                 }
289                 if (FD_ISSET(s_snd, &writefds))
290                         send_data(s_rcv, s_snd, service, direction);
291         }
292 }
293
294 void
295 tcp_relay(int s_src, int s_dst, const char *service)
296 {
297         syslog(LOG_INFO, "starting %s relay", service);
298
299         child_lastactive = parent_lastactive = time(NULL);
300
301         cpid = fork();
302         switch (cpid) {
303         case -1:
304                 exit_failure("tcp_relay: can't fork grand child: %s",
305                     strerror(errno));
306                 /* NOTREACHED */
307         case 0:
308                 /* child process: relay going traffic */
309                 ppid = getppid();
310                 /* this is child so reopen log */
311                 closelog();
312                 openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
313                 relay(s_src, s_dst, service, 1);
314                 /* NOTREACHED */
315         default:
316                 /* parent process: relay coming traffic */
317                 ppid = (pid_t)0;
318                 signal(SIGUSR1, sig_ctimeout);
319                 signal(SIGCHLD, sig_child);
320                 relay(s_dst, s_src, service, 0);
321                 /* NOTREACHED */
322         }
323 }