From 0f8ea92a532fad2d586999db499db2d681b8b39e Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 1 Mar 2015 21:08:34 -0500 Subject: [PATCH] handle local destination port --- Datagram.cpp | 6 +++--- Datagram.h | 4 ++-- Destination.cpp | 7 +++++-- Streaming.cpp | 2 +- Streaming.h | 5 ++++- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Datagram.cpp b/Datagram.cpp index 2378659a..9a44f2d3 100644 --- a/Datagram.cpp +++ b/Datagram.cpp @@ -67,7 +67,7 @@ namespace datagram } } - void DatagramDestination::HandleDatagram (const uint8_t * buf, size_t len) + void DatagramDestination::HandleDatagram (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len) { i2p::data::IdentityEx identity; size_t identityLen = identity.FromBuffer (buf, len); @@ -91,7 +91,7 @@ namespace datagram LogPrint (eLogWarning, "Datagram signature verification failed"); } - void DatagramDestination::HandleDataMessagePayload (const uint8_t * buf, size_t len) + void DatagramDestination::HandleDataMessagePayload (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len) { // unzip it CryptoPP::Gunzip decompressor; @@ -102,7 +102,7 @@ namespace datagram if (uncompressedLen <= MAX_DATAGRAM_SIZE) { decompressor.Get (uncompressed, uncompressedLen); - HandleDatagram (uncompressed, uncompressedLen); + HandleDatagram (fromPort, toPort, uncompressed, uncompressedLen); } else LogPrint ("Received datagram size ", uncompressedLen, " exceeds max size"); diff --git a/Datagram.h b/Datagram.h index 279f5d96..0d8aafba 100644 --- a/Datagram.h +++ b/Datagram.h @@ -27,7 +27,7 @@ namespace datagram ~DatagramDestination () {}; void SendDatagramTo (const uint8_t * payload, size_t len, std::shared_ptr remote); - void HandleDataMessagePayload (const uint8_t * buf, size_t len); + void HandleDataMessagePayload (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len); void SetReceiver (const Receiver& receiver) { m_Receiver = receiver; }; void ResetReceiver () { m_Receiver = nullptr; }; @@ -36,7 +36,7 @@ namespace datagram I2NPMessage * CreateDataMessage (const uint8_t * payload, size_t len); void SendMsg (I2NPMessage * msg, std::shared_ptr remote); - void HandleDatagram (const uint8_t * buf, size_t len); + void HandleDatagram (uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len); private: diff --git a/Destination.cpp b/Destination.cpp index db2628cb..0271282c 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -6,6 +6,7 @@ #include "ElGamal.h" #include "Timestamp.h" #include "NetDb.h" +#include "AddressBook.h" #include "Destination.h" namespace i2p @@ -46,7 +47,7 @@ namespace client } m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (this, inboundTunnelLen, outboundTunnelLen); if (m_IsPublic) - LogPrint (eLogInfo, "Local address ", GetIdentHash().ToBase32 (), ".b32.i2p created"); + LogPrint (eLogInfo, "Local address ", i2p::client::GetB32Address(GetIdentHash()), " created"); m_StreamingDestination = new i2p::stream::StreamingDestination (*this); // TODO: } @@ -367,6 +368,8 @@ namespace client uint32_t length = bufbe32toh (buf); buf += 4; // we assume I2CP payload + uint16_t fromPort = bufbe16toh (buf + 4), // source + toPort = bufbe16toh (buf + 6); // destination switch (buf[9]) { case PROTOCOL_TYPE_STREAMING: @@ -379,7 +382,7 @@ namespace client case PROTOCOL_TYPE_DATAGRAM: // datagram protocol if (m_DatagramDestination) - m_DatagramDestination->HandleDataMessagePayload (buf, length); + m_DatagramDestination->HandleDataMessagePayload (fromPort, toPort, buf, length); else LogPrint ("Missing streaming destination"); break; diff --git a/Streaming.cpp b/Streaming.cpp index d4fbc208..f5376f06 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -661,7 +661,7 @@ namespace stream htobe32buf (buf, size); // length buf += 4; compressor.Get (buf, size); - htobuf16(buf + 4, 0); // source port + htobe16buf (buf + 4, m_LocalDestination.GetLocalPort ()); // source port htobe16buf (buf + 6, m_Port); // destination port buf[9] = i2p::client::PROTOCOL_TYPE_STREAMING; // streaming protocol msg->len += size + 4; diff --git a/Streaming.h b/Streaming.h index e91b11c7..d48a9389 100644 --- a/Streaming.h +++ b/Streaming.h @@ -174,7 +174,8 @@ namespace stream typedef std::function)> Acceptor; - StreamingDestination (i2p::client::ClientDestination& owner): m_Owner (owner) {}; + StreamingDestination (i2p::client::ClientDestination& owner, uint16_t localPort = 0): + m_Owner (owner), m_LocalPort (localPort) {}; ~StreamingDestination () {}; void Start (); @@ -186,6 +187,7 @@ namespace stream void ResetAcceptor () { if (m_Acceptor) m_Acceptor (nullptr); m_Acceptor = nullptr; }; bool IsAcceptorSet () const { return m_Acceptor != nullptr; }; i2p::client::ClientDestination& GetOwner () { return m_Owner; }; + uint16_t GetLocalPort () const { return m_LocalPort; }; void HandleDataMessagePayload (const uint8_t * buf, size_t len); @@ -197,6 +199,7 @@ namespace stream private: i2p::client::ClientDestination& m_Owner; + uint16_t m_LocalPort; std::mutex m_StreamsMutex; std::map > m_Streams; Acceptor m_Acceptor;