using boost sleep now
[anytun.git] / src / syncClient.cpp
blobc6be0d8729873a071d26329c70476c68511370c2
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),missing_chars(-1)
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 for (;;)
79 boost::array<char, 1> buf;
80 boost::system::error_code error;
82 size_t len = socket.read_some(boost::asio::buffer(buf), error);
84 if (error == boost::asio::error::eof)
85 break; // Connection closed cleanly by peer.
86 else if (error)
87 throw boost::system::system_error(error); // Some other error.
89 OnRawData(buf.data(), len);
92 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 connected=false;
97 boost::this_thread::sleep(boost::posix_time::milliseconds(10000));
102 void SyncClient::OnRawData(const char *buf,size_t len)
103 //void SyncClientSocket::OnLine(const std::string& line)
105 ConnectionList & cl_ (gConnectionList);
106 for(size_t index=0;index<len;index++)
108 // std::cout << buf[index];
109 iss_ << buf[index];
110 buffer_size_++;
112 while (1)
114 // cLog.msg(Log::PRIO_NOTICE) << "buffer size "<< buffer_size_ << " missing_chars " << missing_chars;
115 if(missing_chars==-1 && buffer_size_>5)
117 char * buffer = new char [6+1];
118 iss_.read(buffer,6);
119 std::stringstream tmp;
120 tmp.write(buffer,6);
121 tmp>>missing_chars;
122 // cLog.msg(Log::PRIO_NOTICE) << "recieved sync inforamtaion "<<tmp.str()<<"bytes of data"<< std::endl;
123 delete[] buffer;
124 buffer_size_-=6;
125 } else
126 if(missing_chars>0 && missing_chars<=buffer_size_)
128 char * buffer = new char [missing_chars+1];
129 iss_.read(buffer,missing_chars);
130 std::stringstream tmp;
131 tmp.write(buffer,missing_chars);
132 // cLog.msg(Log::PRIO_NOTICE) << "recieved sync inforamtaion from " << GetRemoteHostname() <<" \""<<tmp.str()<<'"'<< std::endl;
133 boost::archive::text_iarchive ia(tmp);
134 SyncCommand scom(cl_);
135 ia >> scom;
136 buffer_size_-=missing_chars;
137 missing_chars=-1;
138 delete[] buffer;
139 } else
140 break;
143 //u_int16_t mux = scom.getMux();
144 //const ConnectionParam & conn = cl_.getConnection(mux)->second;
145 //cLog.msg(Log::PRIO_NOTICE) << "sync connection #"<<mux<<" remote host " << conn.remote_host_ << ":" << conn.remote_port_ << std::endl;
148 //void StatusClientSocket::InitSSLServer()
150 // InitializeContext("server.pem", "keypwd", SSLv23_method());
154 //void StatusClientSocket::Init()
156 // EnableSSL();