Imported Upstream version 0.3.5
[anytun.git] / src / syncClient.cpp
1 /*
2  *  anytun
3  *
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.
12  *
13  *
14  *  Copyright (C) 2007-2014 Markus Grüneis, Othmar Gsenger, Erwin Nindl,
15  *                          Christian Pointner <satp@wirdorange.org>
16  *
17  *  This file is part of Anytun.
18  *
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
22  *  any later version.
23  *
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.
28  *
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/>.
31  *
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
36  *  including the two.
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.
44  */
45
46 #include <sstream>
47 #include <iostream>
48 #include <string>
49 #include "connectionList.h"
50 #include "syncCommand.h"
51
52 #include <boost/archive/text_oarchive.hpp>
53 #include <boost/archive/text_iarchive.hpp>
54
55
56 #include "log.h"
57 #include "syncClient.h"
58 #include "syncTcpConnection.h"
59 #include "buffer.h"
60 #include <boost/array.hpp>
61
62
63 SyncClient::SyncClient(std::string hostname,std::string port)
64   :hostname_(hostname),port_(port)
65 {
66 }
67
68 void SyncClient::run()
69 {
70   bool connected(false);
71   for(;;) {
72     try {
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;
78
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) {
82         socket.close();
83         socket.connect(*endpoint_iterator++, error);
84       }
85       if(error) {
86         throw boost::system::system_error(error);
87       }
88       if(!connected) {
89         cLog.msg(Log::PRIO_NOTICE) << "sync: connected to " << hostname_ <<":"<< port_;
90       }
91       connected=true;
92       readAndProcess(socket); //endless loop
93     } catch(std::exception& e) {
94       if(connected) {
95         cLog.msg(Log::PRIO_NOTICE) << "sync: connection to " << hostname_ <<":"<< port_<< " lost ("<< e.what() << ") retrying every 10sec";
96       }
97       connected=false;
98       boost::this_thread::sleep(boost::posix_time::milliseconds(10000));
99     }
100   }
101 }
102
103 void SyncClient::readAndProcess(SyncTcpConnection::proto::socket& socket)
104 {
105   ConnectionList& cl_(gConnectionList);
106   size_t message_lenght ;
107   for(;;) {
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_);
118     ia >> scom;
119   }
120 }
121
122 void SyncClient::readExactly(SyncTcpConnection::proto::socket& socket,size_t toread, std::iostream& result)
123 {
124   size_t hasread = 0;
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.
132     } else if(error) {
133       throw boost::system::system_error(error);  // Some other error.
134     }
135     //for (size_t pos=0; pos<len; pos++)
136     result<<buf[0];
137     hasread+=len;
138   }
139 }