Imported Upstream version 0.3.2
[anytun.git] / src / anytun-controld.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 <iostream>
34 #include <fstream>
35 #include <poll.h>
36 #include <fcntl.h>
37 #include <pwd.h>
38 #include <grp.h>
39 #include <string>
40
41 #include "datatypes.h"
42
43 #include "log.h"
44 #include "signalController.h"
45 #include "options.h"
46 #include "resolver.h"
47
48 #include "syncServer.h"
49 #include "daemon.hpp"
50
51 void syncOnConnect(SyncTcpConnection * connptr)
52 {
53   std::ifstream file(gOpt.getFileName().c_str());
54   if(file.is_open()) {
55     std::string line;
56     while (!file.eof()) {
57       getline (file,line);
58       connptr->Send(line);
59     }
60     file.close();
61   }
62 }
63
64 void syncListener()
65 {
66  boost::asio::io_service io_service;
67   try
68   {
69     SyncServer server(gOpt.getBindToAddr(), gOpt.getBindToPort(), boost::bind(syncOnConnect, _1));
70     server.run();
71   }
72   catch(std::runtime_error& e) {
73     cLog.msg(Log::PRIO_ERROR) << "sync listener thread died due to an uncaught runtime_error: " << e.what();
74   }
75   catch(std::exception& e) {
76     cLog.msg(Log::PRIO_ERROR) << "sync listener thread died due to an uncaught exception: " << e.what();
77   }
78 }
79
80 int main(int argc, char* argv[])
81 {
82   bool daemonized=false;
83   try 
84   {
85     try 
86     {
87       bool result = gOpt.parse(argc, argv);
88       if(!result) {
89         gOpt.printUsage();
90         exit(0);
91       }
92       StringList targets = gOpt.getLogTargets();
93       if(targets.empty()) {
94         cLog.addTarget("syslog:3,anytun-controld,daemon");
95       }
96       else {
97         StringList::const_iterator it;
98         for(it = targets.begin();it != targets.end(); ++it)
99           cLog.addTarget(*it);
100       }
101     }
102     catch(syntax_error& e)
103     {
104       std::cerr << e << std::endl;
105       gOpt.printUsage();
106       exit(-1);
107     }
108        
109     cLog.msg(Log::PRIO_NOTICE) << "anytun-controld started..."; 
110     gOpt.parse_post(); // print warnings
111
112
113     std::ifstream file( gOpt.getFileName().c_str() );
114     if( file.is_open() )
115       file.close();
116     else {
117       std::cout << "ERROR: unable to open file!" << std::endl;
118       exit(-1);
119     }
120     
121     PrivInfo privs(gOpt.getUsername(), gOpt.getGroupname());
122     if(gOpt.getDaemonize()) {
123       daemonize();
124       daemonized = true;
125     }
126
127     gSignalController.init();
128     gResolver.init();
129     
130     if(gOpt.getChrootDir() != "")
131       do_chroot(gOpt.getChrootDir());
132     
133     privs.drop();
134
135     boost::thread * syncListenerThread;
136     syncListenerThread = new boost::thread(boost::bind(syncListener));
137     
138     int ret = gSignalController.run();
139     
140     return ret;
141   }
142   catch(std::runtime_error& e)
143   {
144     if(daemonized)
145       cLog.msg(Log::PRIO_ERROR) << "uncaught runtime error, exiting: " << e.what();
146     else
147       std::cout << "uncaught runtime error, exiting: " << e.what() << std::endl;
148   }
149   catch(std::exception& e)
150   {
151     if(daemonized)
152       cLog.msg(Log::PRIO_ERROR) << "uncaught exception, exiting: " << e.what();
153     else
154       std::cout << "uncaught exception, exiting: " << e.what() << std::endl;
155   }
156 }
157