From 42ed312384421b428ac136bf0f828a6db7fcb717 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 21 Nov 2018 11:23:48 -0500 Subject: [PATCH] handle NTCP2 RouterInfo flag --- libi2pd/NTCP2.cpp | 4 ++-- libi2pd/NTCP2.h | 2 ++ libi2pd/NetDb.cpp | 15 +++++++++++++-- libi2pd/NetDb.hpp | 3 ++- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/libi2pd/NTCP2.cpp b/libi2pd/NTCP2.cpp index 5940e49a..a88d2a3a 100644 --- a/libi2pd/NTCP2.cpp +++ b/libi2pd/NTCP2.cpp @@ -695,7 +695,7 @@ namespace transport SendTerminationAndTerminate (eNTCP2IncorrectSParameter); return; } - i2p::data::netdb.PostI2NPMsg (CreateI2NPMessage (eI2NPDummyMsg, buf.data () + 4, size - 1)); // TODO: should insert ri and not parse it twice + i2p::data::netdb.PostI2NPMsg (CreateI2NPMessage (eI2NPDummyMsg, buf.data () + 3, size)); // TODO: should insert ri and not parse it twice // TODO: process options // ready to communicate @@ -860,7 +860,7 @@ namespace transport case eNTCP2BlkRouterInfo: { LogPrint (eLogDebug, "NTCP2: RouterInfo flag=", (int)frame[offset]); - i2p::data::netdb.PostI2NPMsg (CreateI2NPMessage (eI2NPDummyMsg, frame + offset + 1, size - 1)); + i2p::data::netdb.PostI2NPMsg (CreateI2NPMessage (eI2NPDummyMsg, frame + offset, size)); break; } case eNTCP2BlkI2NPMessage: diff --git a/libi2pd/NTCP2.h b/libi2pd/NTCP2.h index ec8ae527..eb46b2f8 100644 --- a/libi2pd/NTCP2.h +++ b/libi2pd/NTCP2.h @@ -73,6 +73,8 @@ namespace transport eNTCP2Banned, // 17 }; + // RouterInfo flags + const uint8_t NTCP2_ROUTER_INFO_FLAG_REQUEST_FLOOD = 0x01; typedef std::array NTCP2FrameBuffer; struct NTCP2Establisher diff --git a/libi2pd/NetDb.cpp b/libi2pd/NetDb.cpp index cbe23d59..d1b3abc5 100644 --- a/libi2pd/NetDb.cpp +++ b/libi2pd/NetDb.cpp @@ -12,6 +12,7 @@ #include "I2NPProtocol.h" #include "Tunnel.h" #include "Transports.h" +#include "NTCP2.h" #include "RouterContext.h" #include "Garlic.h" #include "NetDb.hpp" @@ -99,8 +100,8 @@ namespace data HandleDatabaseLookupMsg (msg); break; case eI2NPDummyMsg: - // plain RouterInfo from NTCP2 for now - AddRouterInfo (msg->GetPayload (), msg->GetPayloadLength ()); + // plain RouterInfo from NTCP2 with flag for now + HandleNTCP2RouterInfoMsg (msg); break; default: // WTF? LogPrint (eLogError, "NetDb: unexpected message type ", (int) msg->GetTypeID ()); @@ -574,6 +575,16 @@ namespace data transports.SendMessage (from, dest->CreateRequestMessage (nullptr, nullptr)); } + void NetDb::HandleNTCP2RouterInfoMsg (std::shared_ptr m) + { + uint8_t flood = m->GetPayload ()[0] & NTCP2_ROUTER_INFO_FLAG_REQUEST_FLOOD; + bool updated = AddRouterInfo (m->GetPayload () + 1, m->GetPayloadLength () - 1); // without flag + if (flood && updated && context.IsFloodfill ()) + { + // TODO: flood + LogPrint (eLogInfo, "NetDb: NTCP RouterInfo flood is not implemented"); + } + } void NetDb::HandleDatabaseStoreMsg (std::shared_ptr m) { diff --git a/libi2pd/NetDb.hpp b/libi2pd/NetDb.hpp index 18377b4f..8d42d760 100644 --- a/libi2pd/NetDb.hpp +++ b/libi2pd/NetDb.hpp @@ -65,7 +65,8 @@ namespace data void HandleDatabaseStoreMsg (std::shared_ptr msg); void HandleDatabaseSearchReplyMsg (std::shared_ptr msg); void HandleDatabaseLookupMsg (std::shared_ptr msg); - + void HandleNTCP2RouterInfoMsg (std::shared_ptr m); + std::shared_ptr GetRandomRouter () const; std::shared_ptr GetRandomRouter (std::shared_ptr compatibleWith) const; std::shared_ptr GetHighBandwidthRandomRouter (std::shared_ptr compatibleWith) const;