[RI] validate addresses when parsing RI

This commit is contained in:
R4SAS 2019-05-16 06:36:08 +03:00
parent 5da9abe26e
commit 67fd77987e
2 changed files with 19 additions and 17 deletions

View file

@ -15,6 +15,7 @@
#include "NetDb.hpp"
#include "RouterContext.h"
#include "RouterInfo.h"
#include "util.h"
namespace i2p
{
@ -207,21 +208,22 @@ namespace data
if (!strcmp (key, "host"))
{
boost::system::error_code ecode;
address->host = boost::asio::ip::address::from_string (value, ecode);
auto hostaddr = boost::asio::ip::address::from_string (value, ecode); // store in temporary variable
if (!ecode)
{
#if BOOST_VERSION >= 104900
if (!address->host.is_unspecified ()) // check if address is valid
if (!hostaddr.is_unspecified () && !i2p::util::net::IsInReservedRange(hostaddr)) // check if address is valid
#else
address->host.to_string (ecode);
if (!ecode)
hostaddr.to_string (ecode);
if (!ecode && !i2p::util::net::IsInReservedRange(hostaddr))
#endif
{
address->host = hostaddr;
// add supported protocol
if (address->host.is_v4 ())
supportedTransports |= (address->transportStyle == eTransportNTCP) ? eNTCPV4 : eSSUV4;
supportedTransports |= (address->transportStyle == eTransportNTCP) ? (isNTCP2Only ? eNTCP2V4 : eNTCPV4) : eSSUV4;
else
supportedTransports |= (address->transportStyle == eTransportNTCP) ? eNTCPV6 : eSSUV6;
supportedTransports |= (address->transportStyle == eTransportNTCP) ? (isNTCP2Only ? eNTCP2V6 : eNTCPV6) : eSSUV6;
}
}
}

View file

@ -53,6 +53,9 @@ int inet_pton_xp(int af, const char *src, void *dst)
#include <ifaddrs.h>
#endif
#define address_pair_v4(a,b) { boost::asio::ip::address_v4::from_string (a).to_ulong (), boost::asio::ip::address_v4::from_string (b).to_ulong () }
#define address_pair_v6(a,b) { boost::asio::ip::address_v6::from_string (a).to_bytes (), boost::asio::ip::address_v6::from_string (b).to_bytes () }
namespace i2p
{
namespace util
@ -340,9 +343,6 @@ namespace net
#endif
}
#define address_pair_v4(a,b) { boost::asio::ip::address_v4::from_string (a).to_ulong (), boost::asio::ip::address_v4::from_string (b).to_ulong () }
#define address_pair_v6(a,b) { boost::asio::ip::address_v6::from_string (a).to_bytes (), boost::asio::ip::address_v6::from_string (b).to_bytes () }
bool IsInReservedRange(const boost::asio::ip::address& host) {
// https://en.wikipedia.org/wiki/Reserved_IP_addresses
if(host.is_v4())