Imported Upstream version 0.3.4
[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 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 "syncServer.h"
34 #include "resolver.h"
35 #include "log.h"
36 #include "anytunError.h"
37
38 //using asio::ip::tcp;
39
40 SyncServer::SyncServer(std::string localaddr, std::string port, ConnectCallback onConnect)
41   : onConnect_(onConnect)
42 {
43   gResolver.resolveTcp(localaddr, port, boost::bind(&SyncServer::onResolve, this, _1), boost::bind(&SyncServer::onResolvError, this, _1));
44 }
45
46 SyncServer::~SyncServer()
47 {
48   std::list<AcceptorsElement>::iterator it = acceptors_.begin();
49   for(; it != acceptors_.end(); ++it) {
50     /// this might be a needed by a running thread, TODO cleanup
51     //    delete(it->acceptor_);
52   }
53 }
54
55 void SyncServer::onResolve(SyncTcpConnection::proto::resolver::iterator& it)
56 {
57   while(it != SyncTcpConnection::proto::resolver::iterator()) {
58     SyncTcpConnection::proto::endpoint e = *it;
59
60     AcceptorsElement acceptor;
61     acceptor.acceptor_ = new SyncTcpConnection::proto::acceptor(io_service_);
62     if(!acceptor.acceptor_) {
63       AnytunError::throwErr() << "memory error";
64     }
65
66     acceptor.acceptor_->open(e.protocol());
67 #ifndef _MSC_VER
68     if(e.protocol() == boost::asio::ip::tcp::v6()) {
69       acceptor.acceptor_->set_option(boost::asio::ip::v6_only(true));
70     }
71 #endif
72     acceptor.acceptor_->set_option(boost::asio::socket_base::reuse_address(true));
73     acceptor.acceptor_->bind(e);
74     acceptor.acceptor_->listen();
75     acceptor.started_ = false;
76
77     acceptors_.push_back(acceptor);
78
79     cLog.msg(Log::PRIO_NOTICE) << "sync server listening on " << e;
80
81     it++;
82   }
83
84   start_accept();
85   ready_sem_.up();
86 }
87
88 void SyncServer::onResolvError(const std::runtime_error& e)
89 {
90   cLog.msg(Log::PRIO_ERROR) << "sync server bind/listen failed: " << e.what();
91   // TODO: stop daemon??
92 }
93
94 void SyncServer::run()
95 {
96   ready_sem_.down();
97   io_service_.run();
98 }
99
100 void SyncServer::send(std::string message)
101 {
102   Lock lock(mutex_);
103   for(std::list<SyncTcpConnection::pointer>::iterator it = conns_.begin() ; it != conns_.end(); ++it) {
104     (*it)->Send(message);
105   }
106 }
107
108 void SyncServer::start_accept()
109 {
110   Lock lock(mutex_);
111
112   std::list<AcceptorsElement>::iterator it = acceptors_.begin();
113   for(; it != acceptors_.end(); ++it) {
114     if(!it->started_) {
115       SyncTcpConnection::pointer new_connection = SyncTcpConnection::create(it->acceptor_->get_io_service());
116       conns_.push_back(new_connection);
117       it->acceptor_->async_accept(new_connection->socket(),
118                                   boost::bind(&SyncServer::handle_accept, this, new_connection, boost::asio::placeholders::error, it));
119       it->started_ = true;
120     }
121   }
122 }
123
124 void SyncServer::handle_accept(SyncTcpConnection::pointer new_connection, const boost::system::error_code& error, std::list<AcceptorsElement>::iterator it)
125 {
126   if(!error) {
127     cLog.msg(Log::PRIO_INFO) << "new sync client connected from " << new_connection->socket().remote_endpoint();
128
129     new_connection->onConnect = onConnect_;
130     new_connection->start();
131     it->started_ = false;
132     start_accept();
133   }
134 }