From 224b42352283f03ac3c8e4ffc27d1cd6f17d129c Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 21 Jan 2014 15:10:49 -0500 Subject: [PATCH 1/4] fixed incorrect memory access --- TunnelConfig.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TunnelConfig.h b/TunnelConfig.h index 29fb8575..b6eccda4 100644 --- a/TunnelConfig.h +++ b/TunnelConfig.h @@ -115,8 +115,9 @@ namespace tunnel while (hop) { - delete hop; + auto tmp = hop; hop = hop->next; + delete tmp; } } From 2d428fd090fd581601e8bb7fdd774bb29d079532 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 21 Jan 2014 16:07:16 -0500 Subject: [PATCH 2/4] use boost::asio::address for address --- NTCPSession.cpp | 4 ++-- NTCPSession.h | 2 +- RouterContext.cpp | 4 ++-- RouterInfo.cpp | 6 +++--- RouterInfo.h | 2 +- Transports.cpp | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/NTCPSession.cpp b/NTCPSession.cpp index 61e7944f..061dbbac 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -517,10 +517,10 @@ namespace ntcp } - NTCPClient::NTCPClient (boost::asio::io_service& service, const char * address, + NTCPClient::NTCPClient (boost::asio::io_service& service, boost::asio::ip::address& address, int port, i2p::data::RouterInfo& in_RouterInfo): NTCPSession (service, in_RouterInfo), - m_Endpoint (boost::asio::ip::address::from_string (address), port) + m_Endpoint (address, port) { Connect (); } diff --git a/NTCPSession.h b/NTCPSession.h index 5d33ef10..512d65ec 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -144,7 +144,7 @@ namespace ntcp { public: - NTCPClient (boost::asio::io_service& service, const char * address, int port, i2p::data::RouterInfo& in_RouterInfo); + NTCPClient (boost::asio::io_service& service, boost::asio::ip::address& address, int port, i2p::data::RouterInfo& in_RouterInfo); private: diff --git a/RouterContext.cpp b/RouterContext.cpp index 718a9f36..3e19c810 100644 --- a/RouterContext.cpp +++ b/RouterContext.cpp @@ -45,7 +45,7 @@ namespace i2p auto address = m_RouterInfo.GetNTCPAddress (); if (address) { - address->host = host; + address->host = boost::asio::ip::address::from_string (host); address->port = port; } @@ -80,4 +80,4 @@ namespace i2p std::ofstream fi (ROUTER_INFO); fi.write ((char *)m_RouterInfo.GetBuffer (), m_RouterInfo.GetBufferLen ()); } -} \ No newline at end of file +} diff --git a/RouterInfo.cpp b/RouterInfo.cpp index d804ecad..5125bfa9 100644 --- a/RouterInfo.cpp +++ b/RouterInfo.cpp @@ -101,7 +101,7 @@ namespace data r += ReadString (value, s); s.seekg (1, std::ios_base::cur); r++; // ; if (!strcmp (key, "host")) - address.host = value; + address.host = boost::asio::ip::address::from_string (value); else if (!strcmp (key, "port")) address.port = boost::lexical_cast(value); } @@ -166,7 +166,7 @@ namespace data std::stringstream properties; WriteString ("host", properties); properties << '='; - WriteString (address.host, properties); + WriteString (address.host.to_string (), properties); properties << ';'; WriteString ("port", properties); properties << '='; @@ -227,7 +227,7 @@ namespace data void RouterInfo::AddNTCPAddress (const char * host, int port) { Address addr; - addr.host = host; + addr.host = boost::asio::ip::address::from_string (host); addr.port = port; addr.transportStyle = eTransportNTCP; addr.cost = 2; diff --git a/RouterInfo.h b/RouterInfo.h index a8d59adf..09440946 100644 --- a/RouterInfo.h +++ b/RouterInfo.h @@ -27,7 +27,7 @@ namespace data struct Address { TransportStyle transportStyle; - std::string host; + boost::asio::ip::address host; int port; uint64_t date; uint8_t cost; diff --git a/Transports.cpp b/Transports.cpp index 371ea2e6..31d0325f 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -139,7 +139,7 @@ namespace i2p auto address = r->GetNTCPAddress (); if (address) { - session = new i2p::ntcp::NTCPClient (m_Service, address->host.c_str (), address->port, *r); + session = new i2p::ntcp::NTCPClient (m_Service, address->host, address->port, *r); AddNTCPSession (session); } else From ddb9a6b4772c8f7c039791e2f46697fdddff5b83 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 21 Jan 2014 18:01:11 -0500 Subject: [PATCH 3/4] check if IP address is valid --- NTCPSession.cpp | 2 +- NTCPSession.h | 2 +- RouterInfo.cpp | 11 ++++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/NTCPSession.cpp b/NTCPSession.cpp index 061dbbac..3e3d551d 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -517,7 +517,7 @@ namespace ntcp } - NTCPClient::NTCPClient (boost::asio::io_service& service, boost::asio::ip::address& address, + NTCPClient::NTCPClient (boost::asio::io_service& service, const boost::asio::ip::address& address, int port, i2p::data::RouterInfo& in_RouterInfo): NTCPSession (service, in_RouterInfo), m_Endpoint (address, port) diff --git a/NTCPSession.h b/NTCPSession.h index 512d65ec..71cf5af9 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -144,7 +144,7 @@ namespace ntcp { public: - NTCPClient (boost::asio::io_service& service, boost::asio::ip::address& address, int port, i2p::data::RouterInfo& in_RouterInfo); + NTCPClient (boost::asio::io_service& service, const boost::asio::ip::address& address, int port, i2p::data::RouterInfo& in_RouterInfo); private: diff --git a/RouterInfo.cpp b/RouterInfo.cpp index 5125bfa9..c630fac5 100644 --- a/RouterInfo.cpp +++ b/RouterInfo.cpp @@ -101,7 +101,16 @@ namespace data r += ReadString (value, s); s.seekg (1, std::ios_base::cur); r++; // ; if (!strcmp (key, "host")) - address.host = boost::asio::ip::address::from_string (value); + { + boost::system::error_code ecode; + address.host = boost::asio::ip::address::from_string (value, ecode); + if (ecode) + { + // TODO: we should try to resolve address here + LogPrint ("Unexpected address ", value); + SetUnreachable (true); + } + } else if (!strcmp (key, "port")) address.port = boost::lexical_cast(value); } From fcad81907471bc83b12e7da30644379e0fb210ab Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 21 Jan 2014 19:14:30 -0500 Subject: [PATCH 4/4] ipv4 only --- RouterInfo.cpp | 14 ++++++++++---- RouterInfo.h | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/RouterInfo.cpp b/RouterInfo.cpp index c630fac5..5c2eecef 100644 --- a/RouterInfo.cpp +++ b/RouterInfo.cpp @@ -265,22 +265,28 @@ namespace data return false; } - bool RouterInfo::IsNTCP () const + bool RouterInfo::IsNTCP (bool v4only) const { for (auto& address : m_Addresses) { if (address.transportStyle == eTransportNTCP) - return true; + { + if (!v4only || address.host.is_v4 ()) + return true; + } } return false; } - RouterInfo::Address * RouterInfo::GetNTCPAddress () + RouterInfo::Address * RouterInfo::GetNTCPAddress (bool v4only) { for (auto& address : m_Addresses) { if (address.transportStyle == eTransportNTCP) - return &address; + { + if (!v4only || address.host.is_v4 ()) + return &address; + } } return nullptr; } diff --git a/RouterInfo.h b/RouterInfo.h index 09440946..44dae3f1 100644 --- a/RouterInfo.h +++ b/RouterInfo.h @@ -45,14 +45,14 @@ namespace data const char * GetIdentHashAbbreviation () const { return m_IdentHashAbbreviation; }; uint64_t GetTimestamp () const { return m_Timestamp; }; const std::vector
& GetAddresses () const { return m_Addresses; }; - Address * GetNTCPAddress (); + Address * GetNTCPAddress (bool v4only = true); const RoutingKey& GetRoutingKey () const { return m_RoutingKey; }; void AddNTCPAddress (const char * host, int port); void SetProperty (const char * key, const char * value); const char * GetProperty (const char * key) const; bool IsFloodfill () const; - bool IsNTCP () const; + bool IsNTCP (bool v4only = true) const; void SetUnreachable (bool unreachable) { m_IsUnreachable = unreachable; }; bool IsUnreachable () const { return m_IsUnreachable; };