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.
49 #include "connectionList.h"
50 #include "syncCommand.h"
52 #include <boost/archive/text_oarchive.hpp>
53 #include <boost/archive/text_iarchive.hpp>
57 #include "syncClient.h"
58 #include "syncTcpConnection.h"
60 #include <boost/array.hpp>
63 SyncClient::SyncClient(std::string hostname,std::string port)
64 :hostname_(hostname),port_(port)
68 void SyncClient::run()
70 bool connected(false);
73 boost::asio::io_service io_service;
74 SyncTcpConnection::proto::resolver resolver(io_service);
75 SyncTcpConnection::proto::resolver::query query(hostname_, port_);
76 SyncTcpConnection::proto::resolver::iterator endpoint_iterator = resolver.resolve(query);
77 SyncTcpConnection::proto::resolver::iterator end;
79 SyncTcpConnection::proto::socket socket(io_service);
80 boost::system::error_code error = boost::asio::error::host_not_found;
81 while(error && endpoint_iterator != end) {
83 socket.connect(*endpoint_iterator++, error);
86 throw boost::system::system_error(error);
89 cLog.msg(Log::PRIO_NOTICE) << "sync: connected to " << hostname_ <<":"<< port_;
92 readAndProcess(socket); //endless loop
93 } catch(std::exception& e) {
95 cLog.msg(Log::PRIO_NOTICE) << "sync: connection to " << hostname_ <<":"<< port_<< " lost ("<< e.what() << ") retrying every 10sec";
98 boost::this_thread::sleep(boost::posix_time::milliseconds(10000));
103 void SyncClient::readAndProcess(SyncTcpConnection::proto::socket& socket)
105 ConnectionList& cl_(gConnectionList);
106 size_t message_lenght ;
108 std::stringstream message_lenght_stream;
109 readExactly(socket,5,message_lenght_stream);
110 message_lenght_stream >> message_lenght;
111 std::stringstream void_stream;
112 readExactly(socket,1,void_stream); //skip space
113 std::stringstream sync_command_stream;
114 readExactly(socket,message_lenght, sync_command_stream);
115 //cLog.msg(Log::PRIO_NOTICE) << "recieved sync inforamtaion "<<tmp.str()<< std::endl;
116 boost::archive::text_iarchive ia(sync_command_stream);
117 SyncCommand scom(cl_);
122 void SyncClient::readExactly(SyncTcpConnection::proto::socket& socket,size_t toread, std::iostream& result)
125 while(toread > hasread) {
126 //TODO read bigger buffers
127 boost::array<char, 1> buf;
128 boost::system::error_code error;
129 size_t len = socket.read_some(boost::asio::buffer(buf), error);
130 if(error == boost::asio::error::eof) {
131 break; // Connection closed cleanly by peer.
133 throw boost::system::system_error(error); // Some other error.
135 //for (size_t pos=0; pos<len; pos++)