From 1a9422c3f9e57513c1aea05d557cefc79ff1a454 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 27 May 2016 16:22:42 -0400 Subject: [PATCH] send SetDateMessage --- I2CP.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- I2CP.h | 9 +++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/I2CP.cpp b/I2CP.cpp index 5c718a48..fbd3e51b 100644 --- a/I2CP.cpp +++ b/I2CP.cpp @@ -1,9 +1,9 @@ #include #include "I2PEndian.h" #include "Log.h" +#include "Timestamp.h" #include "I2CP.h" - namespace i2p { namespace client @@ -104,14 +104,67 @@ namespace client { } + void I2CPSession::SendI2CPMessage (uint8_t type, const uint8_t * payload, size_t len) + { + auto l = len + I2CP_HEADER_SIZE; + uint8_t * buf = new uint8_t[l]; + htobe32buf (buf + I2CP_HEADER_LENGTH_OFFSET, len); + buf[I2CP_HEADER_TYPE_OFFSET] = type; + memcpy (buf + I2CP_HEADER_SIZE, payload, len); + boost::asio::async_write (*m_Socket, boost::asio::buffer (buf, l), boost::asio::transfer_all (), + std::bind(&I2CPSession::HandleI2CPMessageSent, shared_from_this (), + std::placeholders::_1, std::placeholders::_2, buf)); + } + + void I2CPSession::HandleI2CPMessageSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, const uint8_t * buf) + { + delete[] buf; + if (ecode && ecode != boost::asio::error::operation_aborted) + Terminate (); + } + + std::string I2CPSession::ExtractString (const uint8_t * buf, size_t len) + { + uint8_t l = buf[0]; + if (l > len) l = len; + return std::string ((const char *)buf, l); + } + + size_t I2CPSession::PutString (uint8_t * buf, size_t len, const std::string& str) + { + auto l = str.length (); + if (l + 1 >= len) l = len - 1; + if (l > 255) l = 255; // 1 byte max + buf[0] = l; + memcpy (buf + 1, str.c_str (), l); + return l + 1; + } + void I2CPSession::GetDateMessageHandler (const uint8_t * buf, size_t len) { + // get version + auto version = ExtractString (buf, len); + auto l = version.length () + 1 + 8; + uint8_t * payload = new uint8_t[l]; + // set date + auto ts = i2p::util::GetMillisecondsSinceEpoch (); + htobe64buf (payload, ts); + // echo vesrion back + PutString (payload + 8, l - 8, version); + SendI2CPMessage (I2CP_SET_DATE_MESSAGE, payload, l); + } + + void I2CPSession::CreateSessionMessageHandler (const uint8_t * buf, size_t len) + { + // TODO + m_Destination = std::make_shared(*this, nullptr, false); } I2CPServer::I2CPServer (const std::string& interface, int port) { memset (m_MessagesHandlers, 0, sizeof (m_MessagesHandlers)); - m_MessagesHandlers[I2CP_GET_DATE_MESSAGE] = &I2CPSession::GetDateMessageHandler; + m_MessagesHandlers[I2CP_GET_DATE_MESSAGE] = &I2CPSession::GetDateMessageHandler; + m_MessagesHandlers[I2CP_CREATE_SESSION_MESSAGE ] = &I2CPSession::CreateSessionMessageHandler; } } } diff --git a/I2CP.h b/I2CP.h index 020fd22a..c495d34e 100644 --- a/I2CP.h +++ b/I2CP.h @@ -19,6 +19,8 @@ namespace client const size_t I2CP_HEADER_SIZE = I2CP_HEADER_TYPE_OFFSET + 1; const uint8_t I2CP_GET_DATE_MESSAGE = 32; + const uint8_t I2CP_SET_DATE_MESSAGE = 33; + const uint8_t I2CP_CREATE_SESSION_MESSAGE = 1; class I2CPSession; class I2CPDestination: public LeaseSetDestination @@ -52,6 +54,7 @@ namespace client // message handlers void GetDateMessageHandler (const uint8_t * buf, size_t len); + void CreateSessionMessageHandler (const uint8_t * buf, size_t len); private: @@ -61,6 +64,12 @@ namespace client void HandleNextMessage (const uint8_t * buf); void Terminate (); + void SendI2CPMessage (uint8_t type, const uint8_t * payload, size_t len); + void HandleI2CPMessageSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, const uint8_t * buf); + + std::string ExtractString (const uint8_t * buf, size_t len); + size_t PutString (uint8_t * buf, size_t len, const std::string& str); + private: I2CPServer& m_Owner;