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