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";
66 acceptor.acceptor_->open(e.protocol());
68 if(e.protocol() == boost::asio::ip::tcp::v6()) {
69 acceptor.acceptor_->set_option(boost::asio::ip::v6_only(true));
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;
77 acceptors_.push_back(acceptor);
79 cLog.msg(Log::PRIO_NOTICE) << "sync server listening on " << e;
88 void SyncServer::onResolvError(const std::runtime_error& e)
90 cLog.msg(Log::PRIO_ERROR) << "sync server bind/listen failed: " << e.what();
91 // TODO: stop daemon??
94 void SyncServer::run()
100 void SyncServer::send(std::string message)
103 for(std::list<SyncTcpConnection::pointer>::iterator it = conns_.begin() ; it != conns_.end(); ++it) {
104 (*it)->Send(message);
108 void SyncServer::start_accept()
112 std::list<AcceptorsElement>::iterator it = acceptors_.begin();
113 for(; it != acceptors_.end(); ++it) {
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));
124 void SyncServer::handle_accept(SyncTcpConnection::pointer new_connection, const boost::system::error_code& error, std::list<AcceptorsElement>::iterator it)
127 cLog.msg(Log::PRIO_INFO) << "new sync client connected from " << new_connection->socket().remote_endpoint();
129 new_connection->onConnect = onConnect_;
130 new_connection->start();
131 it->started_ = false;