fixed windows endian include
[anytun.git] / src / syncClient.cpp
blob2cc1f91283156ba0f88d834aed4dc47a64a14bc9
1 /*
2 * anytun
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-2008 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 version 3 as
21 * published by the Free Software Foundation.
23 * Anytun is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
31 #include <sstream>
32 #include <iostream>
33 #include <string>
35 #include <boost/archive/text_oarchive.hpp>
36 #include <boost/archive/text_iarchive.hpp>
39 #include "log.h"
40 //#include "connectionParam.h"
41 #include "syncClient.h"
42 #include "syncTcpConnection.h"
43 #include "buffer.h"
44 #include <boost/array.hpp>
47 SyncClient::SyncClient(std::string hostname,std::string port)
48 :hostname_( hostname),port_(port)
52 void SyncClient::run()
54 bool connected(false);
55 for(;;)
57 try
59 boost::asio::io_service io_service;
60 SyncTcpConnection::proto::resolver resolver(io_service);
61 SyncTcpConnection::proto::resolver::query query( hostname_, port_);
62 SyncTcpConnection::proto::resolver::iterator endpoint_iterator = resolver.resolve(query);
63 SyncTcpConnection::proto::resolver::iterator end;
65 SyncTcpConnection::proto::socket socket(io_service);
66 boost::system::error_code error = boost::asio::error::host_not_found;
67 while (error && endpoint_iterator != end)
69 socket.close();
70 socket.connect(*endpoint_iterator++, error);
72 if (error)
73 throw boost::system::system_error(error);
74 if (!connected)
75 cLog.msg(Log::PRIO_NOTICE) << "sync: connected to " << hostname_ <<":"<< port_;
76 connected=true;
77 readAndProcess(socket); //endless loop
79 catch (std::exception& e)
81 if (connected)
82 cLog.msg(Log::PRIO_NOTICE) << "sync: connection to " << hostname_ <<":"<< port_<< " lost ("<< e.what() << ") retrying every 10sec";
83 connected=false;
84 boost::this_thread::sleep(boost::posix_time::milliseconds(10000));
89 void SyncClient::readAndProcess(SyncTcpConnection::proto::socket & socket)
91 ConnectionList & cl_ (gConnectionList);
92 size_t message_lenght ;
93 for (;;)
95 std::stringstream message_lenght_stream;
96 readExactly(socket,5,message_lenght_stream);
97 message_lenght_stream >> message_lenght;
98 std::stringstream void_stream;
99 readExactly(socket,1,void_stream); //skip space
100 std::stringstream sync_command_stream;
101 readExactly(socket,message_lenght, sync_command_stream);
102 //cLog.msg(Log::PRIO_NOTICE) << "recieved sync inforamtaion "<<tmp.str()<< std::endl;
103 boost::archive::text_iarchive ia(sync_command_stream);
104 SyncCommand scom(cl_);
105 ia >> scom;
109 void SyncClient::readExactly(SyncTcpConnection::proto::socket & socket,size_t toread, std::iostream & result)
111 size_t hasread = 0;
112 while (toread > hasread)
114 //TODO read bigger buffers
115 boost::array<char, 1> buf;
116 boost::system::error_code error;
117 size_t len = socket.read_some(boost::asio::buffer(buf), error);
118 if (error == boost::asio::error::eof)
119 break; // Connection closed cleanly by peer.
120 else if (error)
121 throw boost::system::system_error(error); // Some other error.
122 //for (size_t pos=0; pos<len; pos++)
123 result<<buf[0];
124 hasread+=len;