Imported Upstream version 0.3.2
[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-2009 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 as published by
21  *  the Free Software Foundation, either version 3 of the License, or
22  *  any later version.
23  *
24  *  Anytun is distributed in the hope that it will be useful,
25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *  GNU General Public License for more details.
28  *
29  *  You should have received a copy of the GNU General Public License
30  *  along with anytun.  If not, see <http://www.gnu.org/licenses/>.
31  */
32
33 #ifndef ANYTUN_signalController_h_INCLUDED
34 #define ANYTUN_signalController_h_INCLUDED
35
36 #include <map>
37 #include <queue>
38
39 #include "threadUtils.hpp"
40
41 #ifndef _MSC_VER
42 #include <csignal>
43 #endif
44
45 #define SIGERROR -1
46
47 class SignalHandler
48 {
49 public:
50   virtual ~SignalHandler() {}
51
52   virtual int handle() { return 0; }
53   virtual int handle(const std::string& msg) { return 0; }
54
55 protected:
56   SignalHandler(int s) : sigNum(s) {}
57
58 private:
59   int sigNum;
60   friend class SignalController;
61 };
62
63 class SigErrorHandler : public SignalHandler
64 {
65 public:
66   SigErrorHandler() : SignalHandler(SIGERROR) {}
67   int handle(const std::string& msg);
68 };
69
70 #ifndef _MSC_VER
71 class SigIntHandler : public SignalHandler
72 {
73 public:
74   SigIntHandler() : SignalHandler(SIGINT) {}
75   int handle();
76 };
77
78 class SigQuitHandler : public SignalHandler
79 {
80 public:
81   SigQuitHandler() : SignalHandler(SIGQUIT) {}
82   int handle();
83 };
84
85 class SigHupHandler : public SignalHandler
86 {
87 public:
88   SigHupHandler() : SignalHandler(SIGHUP) {}
89   int handle();
90 };
91
92 class SigUsr1Handler : public SignalHandler
93 {
94 public:
95   SigUsr1Handler() : SignalHandler(SIGUSR1) {}
96   int handle();
97 };
98
99 class SigUsr2Handler : public SignalHandler
100 {
101 public:
102   SigUsr2Handler() : SignalHandler(SIGUSR2) {}
103   int handle();
104 };
105
106 class SigTermHandler : public SignalHandler
107 {
108 public:
109   SigTermHandler() : SignalHandler(SIGTERM) {}
110   int handle();
111 };
112
113 #else
114
115 class CtrlCHandler : public SignalHandler
116 {
117 public:
118   CtrlCHandler() : SignalHandler(CTRL_C_EVENT) {}
119   int handle();
120 };
121
122 class CtrlBreakHandler : public SignalHandler
123 {
124 public:
125   CtrlBreakHandler() : SignalHandler(CTRL_BREAK_EVENT) {}
126   int handle();
127 };
128
129 class CtrlCloseHandler : public SignalHandler
130 {
131 public:
132   CtrlCloseHandler() : SignalHandler(CTRL_BREAK_EVENT) {}
133   int handle();
134 };
135
136 class CtrlLogoffHandler : public SignalHandler
137 {
138 public:
139   CtrlLogoffHandler() : SignalHandler(CTRL_BREAK_EVENT) {}
140   int handle();
141 };
142
143 class CtrlShutdownHandler : public SignalHandler
144 {
145 public:
146   CtrlShutdownHandler() : SignalHandler(CTRL_BREAK_EVENT) {}
147   int handle();
148 };
149 #endif
150
151 class SignalController
152 {
153 public:
154   static SignalController& instance();
155 #ifndef _MSC_VER
156   void handle();
157 #else
158   static bool handle(DWORD ctrlType);
159 #endif
160
161   void init();
162   int run();
163   void inject(int sig, const std::string& msg = "");
164
165 private:
166   typedef std::map<int, SignalHandler*> HandlerMap;
167
168   SignalController() {};
169   ~SignalController();
170   SignalController(const SignalController &s);
171   void operator=(const SignalController &s);
172
173   static SignalController* inst;
174   static Mutex instMutex;
175   class instanceCleaner {
176     public: ~instanceCleaner() {
177       if(SignalController::inst != NULL)
178         delete SignalController::inst;
179     }
180   };
181   friend class instanceCleaner;
182
183   typedef std::pair<int, std::string> SigPair;
184   std::queue<SigPair> sigQueue;
185   Mutex sigQueueMutex;
186   Semaphore sigQueueSem;
187
188   HandlerMap handler;
189 };
190
191 extern SignalController& gSignalController;
192
193 #endif