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.
14 * Copyright (C) 2007-2014 Markus Grüneis, Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
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
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.
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/>.
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
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.
46 #include "syncServer.h"
49 #include "anytunError.h"
51 //using asio::ip::tcp;
53 SyncServer::SyncServer(std::string localaddr, std::string port, ConnectCallback onConnect)
54 : onConnect_(onConnect)
56 gResolver.resolveTcp(localaddr, port, boost::bind(&SyncServer::onResolve, this, _1), boost::bind(&SyncServer::onResolvError, this, _1));
59 SyncServer::~SyncServer()
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_);
68 void SyncServer::onResolve(SyncTcpConnection::proto::resolver::iterator it)
70 while(it != SyncTcpConnection::proto::resolver::iterator()) {
71 SyncTcpConnection::proto::endpoint e = *it;
73 AcceptorsElement acceptor;
74 acceptor.acceptor_ = new SyncTcpConnection::proto::acceptor(io_service_);
75 if(!acceptor.acceptor_) {
76 AnytunError::throwErr() << "memory error";
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));
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;
90 acceptors_.push_back(acceptor);
92 cLog.msg(Log::PRIO_NOTICE) << "sync server listening on " << e;
101 void SyncServer::onResolvError(const std::runtime_error& e)
103 cLog.msg(Log::PRIO_ERROR) << "sync server bind/listen failed: " << e.what();
104 // TODO: stop daemon??
107 void SyncServer::run()
113 void SyncServer::send(std::string message)
116 for(std::list<SyncTcpConnection::pointer>::iterator it = conns_.begin() ; it != conns_.end(); ++it) {
117 (*it)->Send(message);
121 void SyncServer::start_accept()
125 std::list<AcceptorsElement>::iterator it = acceptors_.begin();
126 for(; it != acceptors_.end(); ++it) {
128 #if BOOST_VERSION >= 107000
129 SyncTcpConnection::pointer new_connection = SyncTcpConnection::create(it->acceptor_->get_executor());
131 SyncTcpConnection::pointer new_connection = SyncTcpConnection::create(it->acceptor_->get_io_service());
133 conns_.push_back(new_connection);
134 it->acceptor_->async_accept(new_connection->socket(),
135 boost::bind(&SyncServer::handle_accept, this, new_connection, boost::asio::placeholders::error, it));
141 void SyncServer::handle_accept(SyncTcpConnection::pointer new_connection, const boost::system::error_code& error, std::list<AcceptorsElement>::iterator it)
144 cLog.msg(Log::PRIO_INFO) << "new sync client connected from " << new_connection->socket().remote_endpoint();
146 new_connection->onConnect = onConnect_;
147 new_connection->start();
148 it->started_ = false;