Imported Upstream version 0.3
[anytun.git] / src / signalController.h
1 /*
2  *  anytun
3  *
4  *  The secure anycast tunneling protocol (satp) defines a protocol used
5  *  for communication between any combination of unicast and anycast
6  *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel
7  *  mode and allows tunneling of every ETHER TYPE protocol (e.g.
8  *  ethernet, ip, arp ...). satp directly includes cryptography and
9  *  message authentication based on the methodes used by SRTP.  It is
10  *  intended to deliver a generic, scaleable and secure solution for
11  *  tunneling and relaying of packets of any protocol.
12  *
13  *
14  *  Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl, 
15  *                          Christian Pointner <satp@wirdorange.org>
16  *
17  *  This file is part of Anytun.
18  *
19  *  Anytun is free software: you can redistribute it and/or modify
20  *  it under the terms of the GNU General Public License version 3 as
21  *  published by the Free Software Foundation.
22  *
23  *  Anytun is distributed in the hope that it will be useful,
24  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *  GNU General Public License for more details.
27  *
28  *  You should have received a copy of the GNU General Public License
29  *  along with anytun.  If not, see <http://www.gnu.org/licenses/>.
30  */
31
32 #ifndef _SIGNAL_CONTROLLER_H_
33 #define _SIGNAL_CONTROLLER_H_
34
35 #include <map>
36 #include <queue>
37
38 #include "threadUtils.hpp"
39
40 #ifndef _MSC_VER
41 #include <csignal>
42 #endif
43
44 #define SIGERROR -1
45
46 class SignalHandler
47 {
48 public:
49   virtual ~SignalHandler() {}
50
51   virtual int handle() { return 0; }
52   virtual int handle(const std::string& msg) { return 0; }
53
54 protected:
55   SignalHandler(int s) : sigNum(s) {}
56
57 private:
58   int sigNum;
59   friend class SignalController;
60 };
61
62 class SigErrorHandler : public SignalHandler
63 {
64 public:
65   SigErrorHandler() : SignalHandler(SIGERROR) {}
66   int handle(const std::string& msg);
67 };
68
69 #ifndef _MSC_VER
70 class SigIntHandler : public SignalHandler
71 {
72 public:
73   SigIntHandler() : SignalHandler(SIGINT) {}
74   int handle();
75 };
76
77 class SigQuitHandler : public SignalHandler
78 {
79 public:
80   SigQuitHandler() : SignalHandler(SIGQUIT) {}
81   int handle();
82 };
83
84 class SigHupHandler : public SignalHandler
85 {
86 public:
87   SigHupHandler() : SignalHandler(SIGHUP) {}
88   int handle();
89 };
90
91 class SigUsr1Handler : public SignalHandler
92 {
93 public:
94   SigUsr1Handler() : SignalHandler(SIGUSR1) {}
95   int handle();
96 };
97
98 class SigUsr2Handler : public SignalHandler
99 {
100 public:
101   SigUsr2Handler() : SignalHandler(SIGUSR2) {}
102   int handle();
103 };
104
105 class SigTermHandler : public SignalHandler
106 {
107 public:
108   SigTermHandler() : SignalHandler(SIGTERM) {}
109   int handle();
110 };
111
112 #else
113
114 class CtrlCHandler : public SignalHandler
115 {
116 public:
117   CtrlCHandler() : SignalHandler(CTRL_C_EVENT) {}
118   int handle();
119 };
120
121 class CtrlBreakHandler : public SignalHandler
122 {
123 public:
124   CtrlBreakHandler() : SignalHandler(CTRL_BREAK_EVENT) {}
125   int handle();
126 };
127
128 class CtrlCloseHandler : public SignalHandler
129 {
130 public:
131   CtrlCloseHandler() : SignalHandler(CTRL_BREAK_EVENT) {}
132   int handle();
133 };
134
135 class CtrlLogoffHandler : public SignalHandler
136 {
137 public:
138   CtrlLogoffHandler() : SignalHandler(CTRL_BREAK_EVENT) {}
139   int handle();
140 };
141
142 class CtrlShutdownHandler : public SignalHandler
143 {
144 public:
145   CtrlShutdownHandler() : SignalHandler(CTRL_BREAK_EVENT) {}
146   int handle();
147 };
148 #endif
149
150 class SignalController
151 {
152 public:
153   static SignalController& instance();
154 #ifndef _MSC_VER
155   void handle();
156 #else
157   static bool handle(DWORD ctrlType);
158 #endif
159
160   void init();
161   int run();
162   void inject(int sig, const std::string& msg = "");
163
164 private:
165   typedef std::map<int, SignalHandler*> HandlerMap;
166
167 #ifndef _MSC_VER
168   SignalController() : thread(NULL) {};
169 #else
170   SignalController() {};
171 #endif
172   ~SignalController();
173   SignalController(const SignalController &s);
174   void operator=(const SignalController &s);
175
176   static SignalController* inst;
177   static Mutex instMutex;
178   class instanceCleaner {
179     public: ~instanceCleaner() {
180       if(SignalController::inst != NULL)
181         delete SignalController::inst;
182     }
183   };
184   friend class instanceCleaner;
185
186   typedef std::pair<int, std::string> SigPair;
187   std::queue<SigPair> sigQueue;
188   Mutex sigQueueMutex;
189   Semaphore sigQueueSem;
190
191 #ifndef _MSC_VER  
192   boost::thread* thread;
193 #endif
194   HandlerMap handler;
195 };
196
197 extern SignalController& gSignalController;
198
199 #endif