New upstream version 0.3.6
[anytun.git] / src / syncServer.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 methods 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-2014 Markus Grüneis, 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  *  In addition, as a special exception, the copyright holders give
33  *  permission to link the code of portions of this program with the
34  *  OpenSSL library under certain conditions as described in each
35  *  individual source file, and distribute linked combinations
36  *  including the two.
37  *  You must obey the GNU General Public License in all respects
38  *  for all of the code used other than OpenSSL.  If you modify
39  *  file(s) with this exception, you may extend this exception to your
40  *  version of the file(s), but you are not obligated to do so.  If you
41  *  do not wish to do so, delete this exception statement from your
42  *  version.  If you delete this exception statement from all source
43  *  files in the program, then also delete it here.
44  */
45
46 #include "syncServer.h"
47 #include "resolver.h"
48 #include "log.h"
49 #include "anytunError.h"
50
51 //using asio::ip::tcp;
52
53 SyncServer::SyncServer(std::string localaddr, std::string port, ConnectCallback onConnect)
54   : onConnect_(onConnect)
55 {
56   gResolver.resolveTcp(localaddr, port, boost::bind(&SyncServer::onResolve, this, _1), boost::bind(&SyncServer::onResolvError, this, _1));
57 }
58
59 SyncServer::~SyncServer()
60 {
61   std::list<AcceptorsElement>::iterator it = acceptors_.begin();
62   for(; it != acceptors_.end(); ++it) {
63     /// this might be a needed by a running thread, TODO cleanup
64     //    delete(it->acceptor_);
65   }
66 }
67
68 void SyncServer::onResolve(SyncTcpConnection::proto::resolver::iterator it)
69 {
70   while(it != SyncTcpConnection::proto::resolver::iterator()) {
71     SyncTcpConnection::proto::endpoint e = *it;
72
73     AcceptorsElement acceptor;
74     acceptor.acceptor_ = new SyncTcpConnection::proto::acceptor(io_service_);
75     if(!acceptor.acceptor_) {
76       AnytunError::throwErr() << "memory error";
77     }
78
79     acceptor.acceptor_->open(e.protocol());
80 #if !defined(_MSC_VER) && !defined(MINGW)
81     if(e.protocol() == boost::asio::ip::tcp::v6()) {
82       acceptor.acceptor_->set_option(boost::asio::ip::v6_only(true));
83     }
84 #endif
85     acceptor.acceptor_->set_option(boost::asio::socket_base::reuse_address(true));
86     acceptor.acceptor_->bind(e);
87     acceptor.acceptor_->listen();
88     acceptor.started_ = false;
89
90     acceptors_.push_back(acceptor);
91
92     cLog.msg(Log::PRIO_NOTICE) << "sync server listening on " << e;
93
94     it++;
95   }
96
97   start_accept();
98   ready_sem_.up();
99 }
100
101 void SyncServer::onResolvError(const std::runtime_error& e)
102 {
103   cLog.msg(Log::PRIO_ERROR) << "sync server bind/listen failed: " << e.what();
104   // TODO: stop daemon??
105 }
106
107 void SyncServer::run()
108 {
109   ready_sem_.down();
110   io_service_.run();
111 }
112
113 void SyncServer::send(std::string message)
114 {
115   Lock lock(mutex_);
116   for(std::list<SyncTcpConnection::pointer>::iterator it = conns_.begin() ; it != conns_.end(); ++it) {
117     (*it)->Send(message);
118   }
119 }
120
121 void SyncServer::start_accept()
122 {
123   Lock lock(mutex_);
124
125   std::list<AcceptorsElement>::iterator it = acceptors_.begin();
126   for(; it != acceptors_.end(); ++it) {
127     if(!it->started_) {
128       SyncTcpConnection::pointer new_connection = SyncTcpConnection::create(it->acceptor_->get_io_service());
129       conns_.push_back(new_connection);
130       it->acceptor_->async_accept(new_connection->socket(),
131                                   boost::bind(&SyncServer::handle_accept, this, new_connection, boost::asio::placeholders::error, it));
132       it->started_ = true;
133     }
134   }
135 }
136
137 void SyncServer::handle_accept(SyncTcpConnection::pointer new_connection, const boost::system::error_code& error, std::list<AcceptorsElement>::iterator it)
138 {
139   if(!error) {
140     cLog.msg(Log::PRIO_INFO) << "new sync client connected from " << new_connection->socket().remote_endpoint();
141
142     new_connection->onConnect = onConnect_;
143     new_connection->start();
144     it->started_ = false;
145     start_accept();
146   }
147 }