Imported Upstream version 0.3.3
[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     acceptor.acceptor_->open(e.protocol());
66 #ifndef _MSC_VER
67     if(e.protocol() == boost::asio::ip::tcp::v6())
68       acceptor.acceptor_->set_option(boost::asio::ip::v6_only(true));
69 #endif
70     acceptor.acceptor_->set_option(boost::asio::socket_base::reuse_address(true));
71     acceptor.acceptor_->bind(e);
72     acceptor.acceptor_->listen();
73     acceptor.started_ = false;
74
75     acceptors_.push_back(acceptor);
76
77     cLog.msg(Log::PRIO_NOTICE) << "sync server listening on " << e;
78
79     it++;
80   }
81
82   start_accept();
83   ready_sem_.up();
84 }
85
86 void SyncServer::onResolvError(const std::runtime_error& e)
87 {
88   cLog.msg(Log::PRIO_ERROR) << "sync server bind/listen failed: " << e.what();
89       // TODO: stop daemon??
90 }
91
92 void SyncServer::run()
93 {
94   ready_sem_.down();
95   io_service_.run();
96 }
97
98 void SyncServer::send(std::string message)
99 {
100   Lock lock(mutex_);
101   for(std::list<SyncTcpConnection::pointer>::iterator it = conns_.begin() ;it != conns_.end(); ++it)
102     (*it)->Send(message);
103 }
104
105 void SyncServer::start_accept()
106 {
107   Lock lock(mutex_);
108
109   std::list<AcceptorsElement>::iterator it = acceptors_.begin();
110   for(;it != acceptors_.end(); ++it) {
111     if(!it->started_) {
112       SyncTcpConnection::pointer new_connection = SyncTcpConnection::create(it->acceptor_->io_service());
113       conns_.push_back(new_connection);
114       it->acceptor_->async_accept(new_connection->socket(),
115                              boost::bind(&SyncServer::handle_accept, this, new_connection, boost::asio::placeholders::error, it));
116       it->started_ = true;
117     }
118   }
119 }
120
121 void SyncServer::handle_accept(SyncTcpConnection::pointer new_connection, const boost::system::error_code& error, std::list<AcceptorsElement>::iterator it)
122 {
123   if (!error) {
124     cLog.msg(Log::PRIO_INFO) << "new sync client connected from " << new_connection->socket().remote_endpoint();
125
126     new_connection->onConnect = onConnect_;
127     new_connection->start();
128     it->started_ = false;
129     start_accept();
130   }
131 }