Skip to content

Commit

Permalink
net: avoid "extending" enum Network
Browse files Browse the repository at this point in the history
Check for TEREDO networks by `if (IsRFC4380())` and avoid "extending"
`enum Network`. As a result `GetExtNetwork()` and `NET_TEREDO` are now
unused and thus removed.
  • Loading branch information
vasild committed Jul 20, 2020
1 parent e4dab55 commit b4ecd8c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
51 changes: 24 additions & 27 deletions src/netaddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,16 +554,6 @@ uint64_t CNetAddr::GetHash() const
return nRet;
}

// private extensions to enum Network, only returned by GetExtNetwork,
// and only used in GetReachabilityFrom
static const int NET_TEREDO = NET_MAX + 1;
int static GetExtNetwork(const CNetAddr& addr)
{
if (addr.IsRFC4380())
return NET_TEREDO;
return addr.GetNetwork();
}

/** Calculates a metric for how reachable (*this) is from a given partner */
int CNetAddr::GetReachabilityFrom(const CNetAddr& paddrPartner) const
{
Expand All @@ -580,46 +570,53 @@ int CNetAddr::GetReachabilityFrom(const CNetAddr& paddrPartner) const
if (!IsRoutable() || IsInternal())
return REACH_UNREACHABLE;

int ourNet = GetExtNetwork(*this);
int theirNet = GetExtNetwork(paddrPartner);
if (IsRFC4380() /* TEREDO */ &&
(paddrPartner.IsIPv6() || !paddrPartner.IsRoutable() ||
paddrPartner.IsInternal())) {
return REACH_TEREDO;
}

if (paddrPartner.IsRFC4380() /* TEREDO */) {
switch (m_net) {
case NET_IPV4: return REACH_IPV4;
case NET_IPV6: return REACH_IPV6_WEAK;
default: return REACH_DEFAULT;
}
}

bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();

switch(theirNet) {
switch (paddrPartner.m_net) {
case NET_IPV4:
switch(ourNet) {
switch (m_net) {
default: return REACH_DEFAULT;
case NET_IPV4: return REACH_IPV4;
}
case NET_IPV6:
switch(ourNet) {
switch (m_net) {
default: return REACH_DEFAULT;
case NET_TEREDO: return REACH_TEREDO;
case NET_IPV4: return REACH_IPV4;
case NET_IPV6: return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
}
case NET_ONION:
switch(ourNet) {
switch (m_net) {
default: return REACH_DEFAULT;
case NET_IPV4: return REACH_IPV4; // Tor users can connect to IPv4 as well
case NET_ONION: return REACH_PRIVATE;
}
case NET_TEREDO:
switch(ourNet) {
default: return REACH_DEFAULT;
case NET_TEREDO: return REACH_TEREDO;
case NET_IPV6: return REACH_IPV6_WEAK;
case NET_IPV4: return REACH_IPV4;
}
case NET_UNROUTABLE:
default:
switch(ourNet) {
case NET_INTERNAL:
case NET_MAX:
switch (m_net) {
default: return REACH_DEFAULT;
case NET_TEREDO: return REACH_TEREDO;
case NET_IPV6: return REACH_IPV6_WEAK;
case NET_IPV4: return REACH_IPV4;
case NET_ONION: return REACH_PRIVATE; // either from Tor, or don't care about our address
}
}

/* not reachable */
return REACH_DEFAULT;
}

CService::CService() : port(0)
Expand Down
3 changes: 1 addition & 2 deletions src/netaddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
* belongs to both `NET_UNROUTABLE` and `NET_IPV4`.
* Keep these sequential starting from 0 and `NET_MAX` as the last entry.
* We have loops like `for (int i = 0; i < NET_MAX; i++)` that expect to iterate
* over all enum values and also `GetExtNetwork()` "extends" this enum by
* introducing standalone constants starting from `NET_MAX`.
* over all enum values.
*/
enum Network
{
Expand Down

0 comments on commit b4ecd8c

Please sign in to comment.