4 * uAnytun is a tiny implementation of SATP. Unlike Anytun which is a full
5 * featured implementation uAnytun has no support for multiple connections
6 * or synchronisation. It is a small single threaded implementation intended
7 * to act as a client on small platforms.
8 * The secure anycast tunneling protocol (satp) defines a protocol used
9 * for communication between any combination of unicast and anycast
10 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
11 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
12 * ethernet, ip, arp ...). satp directly includes cryptography and
13 * message authentication based on the methods used by SRTP. It is
14 * intended to deliver a generic, scaleable and secure solution for
15 * tunneling and relaying of packets of any protocol.
18 * Copyright (C) 2007-2014 Christian Pointner <equinox@anytun.org>
20 * This file is part of uAnytun.
22 * uAnytun is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
27 * uAnytun is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with uAnytun. If not, see <http://www.gnu.org/licenses/>.
35 * In addition, as a special exception, the copyright holders give
36 * permission to link the code of portions of this program with the
37 * OpenSSL library under certain conditions as described in each
38 * individual source file, and distribute linked combinations
40 * You must obey the GNU General Public License in all respects
41 * for all of the code used other than OpenSSL. If you modify
42 * file(s) with this exception, you may extend this exception to your
43 * version of the file(s), but you are not obligated to do so. If you
44 * do not wish to do so, delete this exception statement from your
45 * version. If you delete this exception statement from all source
46 * files in the program, then also delete it here.
49 #include "datatypes.h"
52 #include "sig_handler.h"
60 static int sig_pipe_fds[2];
63 static void sig_handler(int sig)
66 int ret = read(sig_pipe_fds[0], &set, sizeof(sigset_t));
67 if(ret != sizeof(sigset_t))
71 ret = write(sig_pipe_fds[1], &set, sizeof(sigset_t));
77 if(pipe(sig_pipe_fds)) {
78 log_printf(ERROR, "signal handling init failed (pipe error: %s)", strerror(errno));
84 int fd_flags = fcntl(sig_pipe_fds[i], F_GETFL);
86 log_printf(ERROR, "signal handling init failed (pipe fd[%d] read flags error: %s)", i, strerror(errno));
89 if(fcntl(sig_pipe_fds[i], F_SETFL, fd_flags | O_NONBLOCK) == -1){
90 log_printf(ERROR, "signal handling init failed (pipe fd[%d] write flags error: %s)", i, strerror(errno));
95 struct sigaction act, ign;
96 act.sa_handler = sig_handler;
97 sigfillset(&act.sa_mask);
99 ign.sa_handler = SIG_IGN;
100 sigfillset(&ign.sa_mask);
103 if((sigaction(SIGINT, &act, NULL) < 0) ||
104 (sigaction(SIGQUIT, &act, NULL) < 0) ||
105 (sigaction(SIGTERM, &act, NULL) < 0) ||
106 (sigaction(SIGHUP, &act, NULL) < 0) ||
107 (sigaction(SIGUSR1, &act, NULL) < 0) ||
108 (sigaction(SIGUSR2, &act, NULL) < 0) ||
109 (sigaction(SIGCHLD, &ign, NULL) < 0) ||
110 (sigaction(SIGPIPE, &ign, NULL) < 0)) {
112 log_printf(ERROR, "signal handling init failed (sigaction error: %s)", strerror(errno));
113 close(sig_pipe_fds[0]);
114 close(sig_pipe_fds[1]);
117 return sig_pipe_fds[0];
122 sigset_t set, oldset, tmpset;
124 sigemptyset(&tmpset);
125 sigaddset(&tmpset, SIGINT);
126 sigaddset(&tmpset, SIGQUIT);
127 sigaddset(&tmpset, SIGTERM);
128 sigaddset(&tmpset, SIGHUP);
129 sigaddset(&tmpset, SIGUSR1);
130 sigaddset(&tmpset, SIGUSR2);
131 sigprocmask(SIG_BLOCK, &tmpset, &oldset);
133 int ret = read(sig_pipe_fds[0], &set, sizeof(sigset_t));
134 if(ret != sizeof(sigset_t))
137 int return_value = 0;
139 for(sig=1; sig < NSIG; ++sig) {
140 if(sigismember(&set, sig)) {
142 case SIGINT: log_printf(NOTICE, "SIG-Int caught, exitting"); return_value = SIGINT; break;
143 case SIGQUIT: log_printf(NOTICE, "SIG-Quit caught, exitting"); return_value = SIGQUIT; break;
144 case SIGTERM: log_printf(NOTICE, "SIG-Term caught, exitting"); return_value = SIGTERM; break;
145 case SIGHUP: log_printf(NOTICE, "SIG-Hup caught"); return_value = SIGHUP; break;
146 case SIGUSR1: log_printf(NOTICE, "SIG-Usr1 caught"); break;
147 case SIGUSR2: log_printf(NOTICE, "SIG-Usr2 caught"); break;
148 default: log_printf(WARNING, "unknown signal %d caught, ignoring", sig); break;
150 sigdelset(&set, sig);
154 sigprocmask(SIG_SETMASK, &oldset, NULL);
160 struct sigaction act;
161 act.sa_handler = SIG_DFL;
162 sigemptyset(&act.sa_mask);
165 sigaction(SIGINT, &act, NULL);
166 sigaction(SIGQUIT, &act, NULL);
167 sigaction(SIGTERM, &act, NULL);
168 sigaction(SIGHUP, &act, NULL);
169 sigaction(SIGUSR1, &act, NULL);
170 sigaction(SIGUSR2, &act, NULL);
171 sigaction(SIGPIPE, &act, NULL);
172 sigaction(SIGCHLD, &act, NULL);
174 close(sig_pipe_fds[0]);
175 close(sig_pipe_fds[1]);