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.
14 * Copyright (C) 2007-2009 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/>.
33 #include "syncServer.h"
36 #include "anytunError.h"
38 //using asio::ip::tcp;
40 SyncServer::SyncServer(std::string localaddr, std::string port, ConnectCallback onConnect)
41 : onConnect_(onConnect)
43 gResolver.resolveTcp(localaddr, port, boost::bind(&SyncServer::onResolve, this, _1), boost::bind(&SyncServer::onResolvError, this, _1));
46 SyncServer::~SyncServer()
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_);
55 void SyncServer::onResolve(SyncTcpConnection::proto::resolver::iterator& it)
57 while(it != SyncTcpConnection::proto::resolver::iterator()) {
58 SyncTcpConnection::proto::endpoint e = *it;
60 AcceptorsElement acceptor;
61 acceptor.acceptor_ = new SyncTcpConnection::proto::acceptor(io_service_);
62 if(!acceptor.acceptor_)
63 AnytunError::throwErr() << "memory error";
65 acceptor.acceptor_->open(e.protocol());
67 if(e.protocol() == boost::asio::ip::tcp::v6())
68 acceptor.acceptor_->set_option(boost::asio::ip::v6_only(true));
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;
75 acceptors_.push_back(acceptor);
77 cLog.msg(Log::PRIO_NOTICE) << "sync server listening on " << e;
86 void SyncServer::onResolvError(const std::runtime_error& e)
88 cLog.msg(Log::PRIO_ERROR) << "sync server bind/listen failed: " << e.what();
89 // TODO: stop daemon??
92 void SyncServer::run()
98 void SyncServer::send(std::string message)
101 for(std::list<SyncTcpConnection::pointer>::iterator it = conns_.begin() ;it != conns_.end(); ++it)
102 (*it)->Send(message);
105 void SyncServer::start_accept()
109 std::list<AcceptorsElement>::iterator it = acceptors_.begin();
110 for(;it != acceptors_.end(); ++it) {
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));
121 void SyncServer::handle_accept(SyncTcpConnection::pointer new_connection, const boost::system::error_code& error, std::list<AcceptorsElement>::iterator it)
124 cLog.msg(Log::PRIO_INFO) << "new sync client connected from " << new_connection->socket().remote_endpoint();
126 new_connection->onConnect = onConnect_;
127 new_connection->start();
128 it->started_ = false;