Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
Core: rewrite RI address add'er
Browse files Browse the repository at this point in the history
Note: template function was not needed afterall.
  • Loading branch information
anonimal committed Nov 18, 2017
1 parent 15a3b85 commit 98a3582
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 60 deletions.
26 changes: 14 additions & 12 deletions src/core/router/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <boost/program_options.hpp>

#include <fstream>
#include <tuple>

#include "core/router/i2np.h"
#include "core/router/info.h"
Expand Down Expand Up @@ -125,13 +126,14 @@ void RouterContext::Initialize(const boost::program_options::variables_map& map)

// NTCP
if (has_ntcp && !router.GetNTCPAddress())
router.AddNTCPAddress(host, port);
router.AddAddress(std::make_tuple(Transport::NTCP, host, port));
if (!has_ntcp)
RemoveTransport(core::RouterInfo::Transport::NTCP);

// SSU
if (has_ssu && !router.GetSSUAddress())
router.AddSSUAddress(host, port, router.GetIdentHash());
router.AddAddress(
std::make_tuple(Transport::SSU, host, port), router.GetIdentHash());
if (!has_ssu)
{
RemoveTransport(core::RouterInfo::Transport::SSU);
Expand Down Expand Up @@ -276,9 +278,11 @@ void RouterContext::SetReachable() {
for (std::size_t i = 0; i < addresses.size(); i++) {
if (addresses[i].transport == core::RouterInfo::Transport::SSU) {
// insert NTCP address with host/port form SSU
m_RouterInfo.AddNTCPAddress( // TODO(anonimal): but if NTCP is disabled?...
addresses[i].host.to_string().c_str(),
addresses[i].port);
m_RouterInfo.AddAddress( // TODO(anonimal): but if NTCP is disabled?...
std::make_tuple(
Transport::NTCP,
addresses[i].host.to_string(),
addresses[i].port));
break;
}
}
Expand Down Expand Up @@ -311,14 +315,12 @@ void RouterContext::UpdateNTCPV6Address(
}
if (!found) {
// create new address
m_RouterInfo.AddNTCPAddress(
host.to_string().c_str(),
port);
m_RouterInfo.AddSSUAddress(
host.to_string().c_str(),
port,
m_RouterInfo.AddAddress(
std::make_tuple(Transport::NTCP, host.to_string(), port));
m_RouterInfo.AddAddress(
std::make_tuple(Transport::SSU, host.to_string(), port),
GetIdentHash(),
kovri::core::GetMTU(host));
core::GetMTU(host));
updated = true;
}
if (updated)
Expand Down
78 changes: 47 additions & 31 deletions src/core/router/info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include <cstring>
#include <fstream>
#include <tuple>

#include "core/router/context.h"

Expand Down Expand Up @@ -76,11 +77,12 @@ RouterInfo::RouterInfo(

// Set default transports
if (has_transport.first)
AddNTCPAddress(point.first, point.second);
AddAddress(std::make_tuple(Transport::NTCP, point.first, point.second));

if (has_transport.second)
{
AddSSUAddress(point.first, point.second, hash);
AddAddress(
std::make_tuple(Transport::SSU, point.first, point.second), hash);
SetCaps(
m_Caps | core::RouterInfo::Cap::SSUTesting
| core::RouterInfo::Cap::SSUIntroducer);
Expand Down Expand Up @@ -524,39 +526,53 @@ void RouterInfo::SetRouterIdentity(const IdentityEx& identity)
m_Timestamp = kovri::core::GetMillisecondsSinceEpoch();
}

void RouterInfo::AddNTCPAddress(const std::string& host, std::uint16_t port)
{
Address addr;
addr.host = boost::asio::ip::address::from_string(host);
addr.port = port;
addr.transport = Transport::NTCP;
addr.cost = Size::NTCPCost;
addr.date = 0;
addr.mtu = 0;
m_Addresses.push_back(addr);
m_SupportedTransports |= addr.host.is_v6() ? SupportedTransport::NTCPv6
: SupportedTransport::NTCPv4;
}

void RouterInfo::AddSSUAddress(
const std::string& host,
std::uint16_t port,
void RouterInfo::AddAddress(
const std::tuple<Transport, std::string, std::uint16_t>& point,
const std::uint8_t* key,
std::uint16_t mtu)
const std::uint16_t mtu)
{
Address addr;
addr.host = boost::asio::ip::address::from_string(host);
addr.port = port;
addr.transport = Transport::SSU;
addr.cost = Size::SSUCost;
addr.date = 0;
addr.mtu = mtu;
std::memcpy(addr.key, key, 32);
addr.transport = std::get<0>(point);
addr.host = boost::asio::ip::address::from_string(std::get<1>(point));
addr.port = std::get<2>(point);
addr.date = 0; // TODO(anonimal): ?...

// Set transport-specific
switch (addr.transport)
{
case Transport::NTCP:
{
addr.cost = Size::NTCPCost;
addr.mtu = 0; // TODO(anonimal): ?...
m_SupportedTransports |= addr.host.is_v6()
? SupportedTransport::NTCPv6
: SupportedTransport::NTCPv4;
}
break;

case Transport::SSU:
{
addr.cost = Size::SSUCost;
addr.mtu = mtu;
if (!key)
throw std::runtime_error("RouterInfo: null SSU intro key");
addr.key = key;
m_SupportedTransports |= addr.host.is_v6()
? SupportedTransport::SSUv6
: SupportedTransport::SSUv4;
// Set our caps
m_Caps |= Cap::SSUTesting | Cap::SSUIntroducer;
}
break;

default:
throw std::runtime_error(
"RouterInfo: " + std::string(__func__) + ": unsupported transport");
break;
}

// Save address
m_Addresses.push_back(addr);
m_SupportedTransports |=
addr.host.is_v6() ? SupportedTransport::SSUv6 : SupportedTransport::SSUv4;
m_Caps |= Cap::SSUTesting;
m_Caps |= Cap::SSUIntroducer;
}

bool RouterInfo::AddIntroducer(const Address* address, std::uint32_t tag)
Expand Down
23 changes: 9 additions & 14 deletions src/core/router/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <map>
#include <memory>
#include <string>
#include <tuple>
#include <vector>

#include "core/crypto/signature.h"
Expand Down Expand Up @@ -487,20 +488,14 @@ class RouterInfo : public RouterInfoTraits, public RoutingDestination
/// @brief Set RI identity and current timestamp
void SetRouterIdentity(const IdentityEx& identity);


// TODO(anonimal): template address adder

/// @brief Adds SSU address to RI
/// @details Sets RI members appropriately, saves address object
void AddNTCPAddress(const std::string& host, std::uint16_t port);

/// @brief Adds SSU address to RI
/// @details Sets RI members appropriately, saves address object
void AddSSUAddress(
const std::string& host,
std::uint16_t port,
const std::uint8_t* key,
std::uint16_t mtu = 0);
/// @brief Adds/saves address + sets appropriate RI members
/// @param point Supported transport / Host string / Port integral
/// @param key Our intoducer key
/// @param mtu Address MTU
void AddAddress(
const std::tuple<Transport, std::string, std::uint16_t>& point,
const std::uint8_t* key = nullptr,
const std::uint16_t mtu = 0);

/// @brief Adds introducer to RI using SSU capable address object
/// @param address SSU capable address
Expand Down
7 changes: 5 additions & 2 deletions src/util/routerinfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "util/routerinfo.h"
#include <assert.h>
#include <memory>
#include <tuple>

#include "core/crypto/rand.h"
#include "core/router/info.h"
Expand Down Expand Up @@ -124,8 +125,10 @@ bool RouterInfoCommand::Impl(
// Set router info attributes
core::RouterInfo routerInfo;
routerInfo.SetRouterIdentity(keys.GetPublic());
routerInfo.AddSSUAddress(host, port, routerInfo.GetIdentHash());
routerInfo.AddNTCPAddress(host, port);
routerInfo.AddAddress(std::make_tuple(Transport::NTCP, host, port));
routerInfo.AddAddress(
std::make_tuple(Transport::SSU, host, port),
routerInfo.GetIdentHash());
// Set capabilities
routerInfo.SetCaps(core::RouterInfo::Cap::Reachable);
if (vm["ssuintroducer"].as<bool>())
Expand Down
4 changes: 3 additions & 1 deletion src/util/routerinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
#include <vector>
#include "util/command.h"

#include "core/router/info.h"

/**
* @class RouterInfoCommand
* @brief base class for routerinfo
*/

class RouterInfoCommand : public Command
class RouterInfoCommand : public Command, public kovri::core::RouterInfoTraits
{
public:
RouterInfoCommand();
Expand Down

0 comments on commit 98a3582

Please sign in to comment.