Imported Upstream version 0.3.3
[anytun.git] / src / signalController.cpp
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 #include <map>
34 #include <iostream>
35 #include <boost/bind.hpp>
36
37 #include "signalController.h"
38 #include "log.h"
39 #include "anytunError.h"
40 #include "threadUtils.hpp"
41
42 SignalController* SignalController::inst = NULL;
43 Mutex SignalController::instMutex;
44 SignalController& gSignalController = SignalController::instance();
45
46 SignalController& SignalController::instance()
47 {
48         Lock lock(instMutex);
49         static instanceCleaner c;
50         if(!inst)
51                 inst = new SignalController();
52
53         return *inst;
54 }
55
56 int SigErrorHandler(int /*sig*/, const std::string& msg)
57 {
58   AnytunError::throwErr() << msg;
59
60   return 0;
61 }
62
63 //use system specific signal handler
64 #ifndef _MSC_VER
65 #include "signalHandler.hpp"
66 #else
67  #ifdef WIN_SERVICE
68  #include "win32/signalServiceHandler.hpp"
69  #else
70  #include "win32/signalHandler.hpp"
71  #endif
72 #endif
73
74 void SignalController::init(DaemonService& service)
75 {
76   registerSignalHandler(*this, service);
77   handler[SIGERROR] = boost::bind(SigErrorHandler, _1, _2);
78 }
79
80 void SignalController::inject(int sig, const std::string& msg)
81 {
82   {
83     Lock lock(sigQueueMutex);
84     sigQueue.push(SigPair(sig, msg));
85   }
86   sigQueueSem.up();
87 }
88
89 int SignalController::run()
90 {
91   for(CallbackMap::iterator it = callbacks.begin(); it != callbacks.end(); ++it)
92     if(it->first == CALLB_RUNNING)
93       it->second();
94
95   int ret = 0;
96   while(1) {
97     sigQueueSem.down();
98     SigPair sig;
99     {
100       Lock lock(sigQueueMutex);
101       sig = sigQueue.front();
102       sigQueue.pop();
103     }
104     
105     HandlerMap::iterator it = handler.find(sig.first);
106     if(it != handler.end())
107     {
108       ret = it->second(sig.first, sig.second);
109
110       if(ret)
111         break;
112     }
113     else {
114       it = handler.find(SIGUNKNOWN);
115       if(it != handler.end())
116         it->second(sig.first, sig.second);
117       else
118         cLog.msg(Log::PRIO_NOTICE) << "SIG " << sig.first << " caught with message '" << sig.second << "' - ignoring";
119     }
120   }
121
122   for(CallbackMap::iterator it = callbacks.begin(); it != callbacks.end(); ++it)
123     if(it->first == CALLB_STOPPING)
124       it->second();
125
126   return ret;
127 }
128