Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: save the network type explicitly in CNetAddr #19534

Merged
merged 2 commits into from
Jul 29, 2020

Conversation

vasild
Copy link
Contributor

@vasild vasild commented Jul 16, 2020

(chopped off from #19031 to ease review)

Before this change, we would analyze the contents of CNetAddr::ip[16]
in order to tell which type is an address. Change this by introducing a
new member CNetAddr::m_net that explicitly tells the type of the
address.

This is necessary because in BIP155 we will not be able to tell the
address type by just looking at its raw representation (e.g. both TORv3
and I2P are "seemingly random" 32 bytes).

As a side effect of this change we no longer need to store IPv4
addresses encoded as IPv6 addresses - we can store them in proper 4
bytes (will be done in a separate commit). Also the code gets
somewhat simplified - instead of
memcmp(ip, pchIPv4, sizeof(pchIPv4)) == 0 we can use
m_net == NET_IPV4.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jul 16, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

No conflicts as of last run.

@vasild vasild force-pushed the explicit_network_type_in_CNetAddr branch from 2a6a9ce to eb22eed Compare July 16, 2020 18:12
@vasild
Copy link
Contributor Author

vasild commented Jul 16, 2020

Rebased to wake up Travis

@troygiorshev
Copy link
Contributor

ACK eb22eed

If you make any changes, consider running clang-format. No worries if you don't, we can always do it later in the roadmap. Might be nice to clean up the formatting of some of the other methods here as well, now that we're touching so much of this.

@Saibato
Copy link
Contributor

Saibato commented Jul 17, 2020

So if i read this correct, if this is merged we narrow before even having benefit and v3 , and say an address is not routable if one flag in CNetAddr m_net say's something else than m_net == NET_IPV6 and skip that the Bitcoin imperative 'data decides'

So you want to change even the old ADDRv1 logic while BIP155 say's don't. hmm...
The way bitcoin is used in the wild is often strange and has hidden paths, i can only urge you to not touch in any way the old ADDR logic and the way that net works.

And btw if ip is filled with << instead of memcpy and data don't decide, how sophisticated is the checksum plan for bit or byte errors in the new ADDRv1/v2 stream?

btw Since the BIP155 ADDv2 is to send ip2 and v3 just as 32 byte without checksum, i would also recommend to concat least sig byte of hash(m net | 32 byte ) before send, but that;s prob a PR for BIP155.
edit@saibato: Not that to provide piggy pack support for tor onions in ipv6 packets when ipv6 in #1021 was merged is a holy move, but time passed by and the value of bitcoin is not only the timestamp consensus, but AFAICS also the network itself.

@michaelfolkson
Copy link
Contributor

Concept ACK, Approach ACK.

The BIP refers to I2P and CJDNS overlay networks and gives them their own network IDs. An I2P address is a Tor v3 address and a CJDNS address is a IPV6 address (or at least they are the same size) right? I'm just trying to understand why they warrant their own reserved network IDs in the BIP but don't seem to be implemented here?

Copy link
Contributor

@jonatack jonatack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Light ACK eb22eed though viewed in isolation this change seems to be making the code more complex, not simpler.

@@ -322,13 +330,7 @@ enum Network CNetAddr::GetNetwork() const
if (!IsRoutable())
return NET_UNROUTABLE;

if (IsIPv4())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this code still needed in GetNetwork()?

    if (IsInternal())
        return NET_INTERNAL;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question - no. But it was not necessary before this PR either. Some good simplification can be done here even on master. I will see whether it is better to add a new commit to this PR or file it as a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correction - it is needed, otherwise this test would fail:

https://github.com/bitcoin/bitcoin/blob/476436b/src/test/netbase_tests.cpp#L45

Anyway, I made some simplifications on top of this PR that remove the "extending" of enum Network with NET_UNKNOWN and NET_TEREDO and also removed GetExtNetwork(). To avoid distractions, I will not append those commits to this PR but would rather open a new PR against master, once this PR gets merged.

Copy link
Contributor

@jonatack jonatack Jul 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @vasild, looking forward to reviewing the simplifications.

src/netaddress.h Outdated
Comment on lines 41 to 46

/// A set of dummy addresses that map a name to an IPv6 address.
NET_INTERNAL,
Copy link
Contributor

@Saibato Saibato Jul 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change:

/// A set of special crafted FC00/7 IPv6 addresses we use to map a string or FQDN to an IPv6 address.
/// We uses these important fake addresses also in CAddrMan to keep track of which DNS seeds were used 
/// and how we manage buckets and peers.

With that in mind when dev we can take special care in transit to ADDRv2 to not mess with this address realm,
because we need them later to get our new long addresses in and out from peers,dat ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I extended the comment, thanks!

In #19031 we drop the 6-byte fd6b:88c0:8724 prefix from these addresses and store them in memory as m_net=NET_INTERNAL, m_addr=...10 bytes.... Serializing that to addrv1 remains unchanged (we add the fd6b:88c0:8724 prefix) and they are never serialized as addrv2.

Copy link
Contributor

@Saibato Saibato Jul 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that related here and just to your info, the follow up construction on this run in an assert failure when i made the v3 patch,

The structures need also some failure tolerance and flexibility.
Don;t mind if i am harsh and sometimes sarcastic, but you do here heart surgery and better not look good but beat the drum

Also to get in an out of peers and DNS seeds is somewhat unrelated to the data structure on the ADDR net.
We could easy use intern more data and flags on our address than we use on the wire.
Those two structures need not to be the same.
edit@saiboto what let me tend to ADDRv2 is 2038. tyi.

@vasild
Copy link
Contributor Author

vasild commented Jul 20, 2020

The BIP refers to I2P and CJDNS overlay networks and gives them their own network IDs. An I2P address is a Tor v3 address and a CJDNS address is a IPV6 address (or at least they are the same size) right? I'm just trying to understand why they warrant their own reserved network IDs in the BIP but don't seem to be implemented here?

@michaelfolkson, the sizes of I2P and Torv3 addresses are equal (32 bytes) and also IPv6 and CJDNS addresses are both 16 bytes, but their interpretation is different - ie given some chunk of 32 bytes we need to know whether that is an I2P address or Torv3 address. This is why they have a separate network ids. Also there could be an overlap - e.g. some 16 bytes could both be valid IPv6 address and a valid CJDNS address.

The code in this PR and its parent PR (#19031) aims to implement ADDRv2 address format and signaling with the networks that are currently supported. Once that is done, on top of it, we will implement new networks like Torv3, I2P and others.

@vasild vasild force-pushed the explicit_network_type_in_CNetAddr branch from eb22eed to 662bb25 Compare July 20, 2020 14:16
@vasild
Copy link
Contributor Author

vasild commented Jul 20, 2020

Extended the NET_INTERNAL comment as per @Saibato's suggestion.

Comment on lines -238 to 252
if (memcmp(ip, pchLocal, 16) == 0)
if (IsIPv6() && memcmp(ip, pchLocal, 16) == 0)
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general some of this might be optimized out by the compiler,
And i would prefer for the transition period something like this that we can refactor out when all works fine
to an compiler and bytecode friendly version.

I would prefer constructions for all those ipv6 overrides in the rest of the code to look like this
suggested change:

if  (memcmp(ip, pchLocal, 16) == 0) && IsIPv6();

And some logging here and in the other overrides i.e.

LogPrint(BCLog::ADDV2, "old isXXXX() says so, ADDRv2 m_net did override with  value = xxx .....   

@jonatack
Copy link
Contributor

re-ACK 662bb25 per git diff eb22eed 662bb25 only change is improved documentation.

@troygiorshev
Copy link
Contributor

reACK 662bb25

Only change is in a comment.

@Saibato
Copy link
Contributor

Saibato commented Jul 21, 2020

tyi, I will not ACK or NACK any of ADDRv2 roadmap, Since Tor v2 will be deprecated sooner or later. I will try to start a side project to morph some old legacy tunnel code code to a special general 'bitcoin-peer-address' tor or whatever proxy wrapper, for all old nodes to have full v3 and after 2038 support, without need to softfork This will from day one, mitigate any chainsplit or censorship.

@vasild
Copy link
Contributor Author

vasild commented Jul 21, 2020

Just to clarify - ADDRv2/BIP155 does not involve a softfork, chainsplit or censorship. Old nodes (e.g. 0.19.1) can't have full support for TORv3 because they don't contain the code for it.

@Saibato
Copy link
Contributor

Saibato commented Jul 21, 2020

I full support ADDRv2 and i will comment and provide review.
Also imo ADDRv2 its inevitable for master.

My approach aims to let old tor nodes not behind and be forced to softfork when tor v2 fades out.

can't have full support for TORv3 because they don't contain the code for it

I love this cant's in software dev, because i can,
There is a great misconception in how MITM or say jmp esp works when its clear that you don;t have the space,bytes or source code on the wire, that always astound me; why not use a bit hacking voodoo for some useful things,when we could tunnel because we have solid keys

That wrapper is on track and orthogonal to this here and when ADDRv2 is there, it will be even more easy to support this.

Maybe it will make its path to contrib to assist all old node software in support of protocols aside from internet protocol.

edit@saibato
Btw:It also astound me in many defi or blockchain software projects how they think the real internet or tor works, there are multiple ways to do the wrapper, and one is of such nature. that there is a minor chance that you could hear some jaw bones touch the floor.

tl;tr

The status quo, so far is that even now every tor v2 proxy capable node, can even now connect over tor v3 9050 socks5 proxy to any node that provides a v3 inbound hidden service over torrc. What missing is a bit virgin seeding that could be done by a signed script file and banman and conman bitcoin logic and then address gossip about that and to store this, for node reboot, if you don;t want to run every time a long bash cli addnode <v3.onion> add .script or a long cmdline. All can be solved without touching the old software. And since we have a great FC00/7 space for grabs in all old nodes, that could i.e. bind tun or taps to them to proxy out to even stranger protocols.
What is the misconception, is that we need to know the full 256 address or greater pubkey space of other protocols, when in fact to work for bitcoin we only will ever have a short directory or index list we need to gossip, that fits easy in the enormous 80bit FC00/7 index space, we have in every node even now.
edit@saibato;
When this and the followup PR's gain track and we will have ADDRv2 nodes in the wild, users can run anyway stripped down master versions, they connect to local or remote with there old node to advertise there v3 address, if they wish so, but honestly the nature of hidden net, is that its hidden, so to advertise inbound is anyway not so sexy. I guess, if we would have ̶a̶ ̶b̶y̶ ̶s̶a̶t̶o̶s̶h̶i̶ ̶k̶e̶y̶s̶ signed v3/ip2 whatever seeds list, that will be enough to bootstrap and there would be almost none old node, to not use such a list?

@vasild
Copy link
Contributor Author

vasild commented Jul 21, 2020

Coverage report of modified code: https://people.freebsd.org/~vd/pr19534_662bb25ef_coverage/
(all modified code is covered by tests)

@Saibato
Copy link
Contributor

Saibato commented Jul 22, 2020

Just to clarify - ADDRv2/BIP155 does not involve a softfork, chainsplit or censorship. Old nodes (e.g. 0.19.1) can't have full support for TORv3 because they don't contain the code for it.

wow, that was really funny took,me a while to figure out the zero. is a morse placeholder. This kind of humor makes really fun and that i like so much when programing senator brains. I almost tend to blind ACK

src/netaddress.h Outdated Show resolved Hide resolved
src/netaddress.cpp Outdated Show resolved Hide resolved
@vasild
Copy link
Contributor Author

vasild commented Jul 27, 2020

Made 2 changes as per review suggestions:

  • Initialize CNetAddr::m_net in its definition instead of in the constructor
  • Move the code that detects non-IPv6 addresses embedded in IPv6 to a separate method

@vasild vasild force-pushed the explicit_network_type_in_CNetAddr branch from 662bb25 to 64897c5 Compare July 27, 2020 12:42
Copy link
Contributor

@troygiorshev troygiorshev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reACK 64897c5 via git range-diff master 662bb25 64897c5

Just one comment

src/netaddress.h Outdated Show resolved Hide resolved
Before this change, we would analyze the contents of `CNetAddr::ip[16]`
in order to tell which type is an address. Change this by introducing a
new member `CNetAddr::m_net` that explicitly tells the type of the
address.

This is necessary because in BIP155 we will not be able to tell the
address type by just looking at its raw representation (e.g. both TORv3
and I2P are "seemingly random" 32 bytes).

As a side effect of this change we no longer need to store IPv4
addresses encoded as IPv6 addresses - we can store them in proper 4
bytes (will be done in a separate commit). Also the code gets
somewhat simplified - instead of
`memcmp(ip, pchIPv4, sizeof(pchIPv4)) == 0` we can use
`m_net == NET_IPV4`.

Co-authored-by: Carl Dong <contact@carldong.me>
@vasild vasild force-pushed the explicit_network_type_in_CNetAddr branch from 64897c5 to bcfebb6 Compare July 27, 2020 13:13
@troygiorshev
Copy link
Contributor

reACK bcfebb6 via git range-diff master 64897c5 bcfebb6

@vasild
Copy link
Contributor Author

vasild commented Jul 27, 2020

Updated a comment to reflect that we now call SetLegacyIPv6() instead of SetRaw(), as per suggestion.

@jonatack
Copy link
Contributor

re-ACK bcfebb6 per git diff 662bb25 bcfebb6, code review, debug build/tests clean, ran bitcoind.

@promag
Copy link
Member

promag commented Jul 28, 2020

Concept ACK.

@laanwj
Copy link
Member

laanwj commented Jul 29, 2020

Code review ACK bcfebb6

@laanwj laanwj merged commit a76ccb0 into bitcoin:master Jul 29, 2020
@vasild vasild deleted the explicit_network_type_in_CNetAddr branch July 29, 2020 11:55
memcpy(ip, ipIn.ip, sizeof(ip));
}

void CNetAddr::SetLegacyIPv6(const uint8_t ipv6[16])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to pass around fixed-length data with a raw data pointer (of arbitrary length) when type safe alternatives like span or std::array (probably not applicable in this case) are available?

Copy link
Member

@laanwj laanwj Jul 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input has to be 16 bytes, in this case. Is there a way to enforce this in the type, for spans?
(sure, the [16] doesn't do anything here either except as a signal to the programmer)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but I presumed spans could be of non-dynamic extent as well. Does this not work?

const std::span<uint8_t, 16>& ipv6

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in this case the [16] is just a "signal to the programmer". In SetRaw() we have a bare pointer const uint8_t* when calling this function.

I have removed SetRaw() in the subsequent commit and will revisit this code.

Some experiments:

int f1(const std::span<int, 3>& p)
{
    return p[5]; // no warning
}

int f2(const int (&p)[3])
{
    return p[5]; // warning: array index 5 is past the end of the array (which contains 3 elements) [-Warray-bounds]
}

int f3(const std::array<int, 3>& p)
{
    return p[5]; // no warning
}

int main(int, char**) {
    int a[3];
    int b[2];

    f1(a);
    f1(b); // error: no known conversion from 'int [2]' to 'const std::span<int, 3>'

    f2(a);
    f2(b); // error: no known conversion from 'int [2]' to 'const int [3]'

    f3(std::array<int, 3>{a[0], a[1], a[2]});

    return 0;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, in #19628 I changed the argument to const uint8_t (&ipv6)[ADDR_IPv6_SIZE] mostly to make the interface clean. From 3 callers 2 need an abusive typecasts because they don't have an array (one has bare pointer (the fuzz tests) and the other has struct in6_addr).

Feel free to comment there: https://github.com/bitcoin/bitcoin/pull/19628/files#diff-b64569708508232923e5fe3059396334R28

@vasild
Copy link
Contributor Author

vasild commented Jul 30, 2020

Thanks to everybody involved!

I opened the next PR at #19628. It is the biggest one from the ADDRv2 roadmap (#19031) and is mostly (but not entirely) mechanical.

After it follow more "interesting" commits :)

sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jul 31, 2020
bcfebb6 net: save the network type explicitly in CNetAddr (Vasil Dimov)
100c64a net: document `enum Network` (Vasil Dimov)

Pull request description:

  (chopped off from bitcoin#19031 to ease review)

  Before this change, we would analyze the contents of `CNetAddr::ip[16]`
  in order to tell which type is an address. Change this by introducing a
  new member `CNetAddr::m_net` that explicitly tells the type of the
  address.

  This is necessary because in BIP155 we will not be able to tell the
  address type by just looking at its raw representation (e.g. both TORv3
  and I2P are "seemingly random" 32 bytes).

  As a side effect of this change we no longer need to store IPv4
  addresses encoded as IPv6 addresses - we can store them in proper 4
  bytes (will be done in a separate commit). Also the code gets
  somewhat simplified - instead of
  `memcmp(ip, pchIPv4, sizeof(pchIPv4)) == 0` we can use
  `m_net == NET_IPV4`.

ACKs for top commit:
  troygiorshev:
    reACK bcfebb6 via `git range-diff master 64897c5 bcfebb6`
  jonatack:
    re-ACK bcfebb6 per `git diff 662bb25 bcfebb6`, code review, debug build/tests clean, ran bitcoind.
  laanwj:
    Code review ACK bcfebb6

Tree-SHA512: 9347e2a50feac617a994bfb46a8f77e31c236bde882e4fd4f03eea4766cd5110216f5f3d24dee91d25218bab7f8bb6e1d2d6212a44db9e34594299fd6ff7606b
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 16, 2021
bcfebb6 net: save the network type explicitly in CNetAddr (Vasil Dimov)
100c64a net: document `enum Network` (Vasil Dimov)

Pull request description:

  (chopped off from bitcoin#19031 to ease review)

  Before this change, we would analyze the contents of `CNetAddr::ip[16]`
  in order to tell which type is an address. Change this by introducing a
  new member `CNetAddr::m_net` that explicitly tells the type of the
  address.

  This is necessary because in BIP155 we will not be able to tell the
  address type by just looking at its raw representation (e.g. both TORv3
  and I2P are "seemingly random" 32 bytes).

  As a side effect of this change we no longer need to store IPv4
  addresses encoded as IPv6 addresses - we can store them in proper 4
  bytes (will be done in a separate commit). Also the code gets
  somewhat simplified - instead of
  `memcmp(ip, pchIPv4, sizeof(pchIPv4)) == 0` we can use
  `m_net == NET_IPV4`.

ACKs for top commit:
  troygiorshev:
    reACK bcfebb6 via `git range-diff master 64897c5 bcfebb6`
  jonatack:
    re-ACK bcfebb6 per `git diff 662bb25 bcfebb6`, code review, debug build/tests clean, ran bitcoind.
  laanwj:
    Code review ACK bcfebb6

Tree-SHA512: 9347e2a50feac617a994bfb46a8f77e31c236bde882e4fd4f03eea4766cd5110216f5f3d24dee91d25218bab7f8bb6e1d2d6212a44db9e34594299fd6ff7606b
Signed-off-by: pasta <pasta@dashboost.org>

# Conflicts:
#	src/netaddress.cpp
#	src/netaddress.h
UdjinM6 pushed a commit to UdjinM6/dash that referenced this pull request Jan 18, 2021
bcfebb6 net: save the network type explicitly in CNetAddr (Vasil Dimov)
100c64a net: document `enum Network` (Vasil Dimov)

Pull request description:

  (chopped off from bitcoin#19031 to ease review)

  Before this change, we would analyze the contents of `CNetAddr::ip[16]`
  in order to tell which type is an address. Change this by introducing a
  new member `CNetAddr::m_net` that explicitly tells the type of the
  address.

  This is necessary because in BIP155 we will not be able to tell the
  address type by just looking at its raw representation (e.g. both TORv3
  and I2P are "seemingly random" 32 bytes).

  As a side effect of this change we no longer need to store IPv4
  addresses encoded as IPv6 addresses - we can store them in proper 4
  bytes (will be done in a separate commit). Also the code gets
  somewhat simplified - instead of
  `memcmp(ip, pchIPv4, sizeof(pchIPv4)) == 0` we can use
  `m_net == NET_IPV4`.

ACKs for top commit:
  troygiorshev:
    reACK bcfebb6 via `git range-diff master 64897c5 bcfebb6`
  jonatack:
    re-ACK bcfebb6 per `git diff 662bb25 bcfebb6`, code review, debug build/tests clean, ran bitcoind.
  laanwj:
    Code review ACK bcfebb6

Tree-SHA512: 9347e2a50feac617a994bfb46a8f77e31c236bde882e4fd4f03eea4766cd5110216f5f3d24dee91d25218bab7f8bb6e1d2d6212a44db9e34594299fd6ff7606b
Signed-off-by: pasta <pasta@dashboost.org>

# Conflicts:
#	src/netaddress.cpp
#	src/netaddress.h
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 18, 2021
bcfebb6 net: save the network type explicitly in CNetAddr (Vasil Dimov)
100c64a net: document `enum Network` (Vasil Dimov)

Pull request description:

  (chopped off from bitcoin#19031 to ease review)

  Before this change, we would analyze the contents of `CNetAddr::ip[16]`
  in order to tell which type is an address. Change this by introducing a
  new member `CNetAddr::m_net` that explicitly tells the type of the
  address.

  This is necessary because in BIP155 we will not be able to tell the
  address type by just looking at its raw representation (e.g. both TORv3
  and I2P are "seemingly random" 32 bytes).

  As a side effect of this change we no longer need to store IPv4
  addresses encoded as IPv6 addresses - we can store them in proper 4
  bytes (will be done in a separate commit). Also the code gets
  somewhat simplified - instead of
  `memcmp(ip, pchIPv4, sizeof(pchIPv4)) == 0` we can use
  `m_net == NET_IPV4`.

ACKs for top commit:
  troygiorshev:
    reACK bcfebb6 via `git range-diff master 64897c5 bcfebb6`
  jonatack:
    re-ACK bcfebb6 per `git diff 662bb25 bcfebb6`, code review, debug build/tests clean, ran bitcoind.
  laanwj:
    Code review ACK bcfebb6

Tree-SHA512: 9347e2a50feac617a994bfb46a8f77e31c236bde882e4fd4f03eea4766cd5110216f5f3d24dee91d25218bab7f8bb6e1d2d6212a44db9e34594299fd6ff7606b
Signed-off-by: pasta <pasta@dashboost.org>

# Conflicts:
#	src/netaddress.cpp
#	src/netaddress.h
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Feb 5, 2021
Summary:
```
Before this change, we would analyze the contents of CNetAddr::ip[16]
in order to tell which type is an address. Change this by introducing a
new member CNetAddr::m_net that explicitly tells the type of the
address.

This is necessary because in BIP155 we will not be able to tell the
address type by just looking at its raw representation (e.g. both TORv3
and I2P are "seemingly random" 32 bytes).

As a side effect of this change we no longer need to store IPv4
addresses encoded as IPv6 addresses - we can store them in proper 4
bytes (will be done in a separate commit). Also the code gets
somewhat simplified - instead of
memcmp(ip, pchIPv4, sizeof(pchIPv4)) == 0 we can use
m_net == NET_IPV4.
```

Backport of core [[bitcoin/bitcoin#19534 | PR19534]].

Depends on D9170 and D9171.

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D9172
random-zebra added a commit to PIVX-Project/PIVX that referenced this pull request Aug 11, 2021
ecde04a [Consensus] Bump Active Protocol version to 70923 for v5.3 (random-zebra)
b63e4f5 Consensus: Add v5.3 enforcement height for testnet. (furszy)
f44be94 Only relay IPv4, IPv6, Tor addresses (Pieter Wuille)
015298c fix: tor: Call event_base_loopbreak from the event's callback (furszy)
34ff7a8 Consensus: Add mnb ADDRv2 guard. (furszy)
b4515dc GUI: Present v3 onion addresses properly in MNs list. (furszy)
337d43d tests: don't export in6addr_loopback (Vasil Dimov)
2cde8e0 GUI: Do not show the tor v3 onion address in the topbar. (furszy)
0b5f406 Doc: update tor.md with latest upstream information. (furszy)
89df7f2 addrman: ensure old versions don't parse peers.dat (Vasil Dimov)
bb90c5c test: add getnetworkinfo network name regression tests (Jon Atack)
d8e01b5 rpc: update GetNetworksInfo() to not return unsupported networks (Jon Atack)
57fc7b0 net: update GetNetworkName() with all enum Network cases (Jon Atack)
647d60b tests: Modify rpc_bind to conform to bitcoin#14532 behaviour. (Carl Dong)
d4d6729 Allow running rpc_bind.py --nonloopback test without IPv6 (Kristaps Kaupe)
4a034d8 test: Add rpc_bind test to default-run tests (Wladimir J. van der Laan)
61a08af [tests] bind functional test nodes to 127.0.0.1  Prevents OSX firewall (Sjors Provoost)
6a4f1e0 test: Add basic addr relay test (furszy)
78aa61c net: Make addr relay mockable (furszy)
ba954ca Send and require SENDADDRV2 before VERACK (Pieter Wuille)
61c2ed4 Bump net protocol version + don't send 'sendaddrv2' to pre-70923 software (furszy)
ccd508a tor: make a TORv3 hidden service instead of TORv2 (Vasil Dimov)
6da9a14 net: advertise support for ADDRv2 via new message (furszy)
e58d5d0 Migrate to test_large_inv() to Misbehaving logging. (furszy)
d496b64 [QA] fix mininode CAddress ser/deser (Jonas Schnelli)
cec9567 net: CAddress & CAddrMan: (un)serialize as ADDRv2 Change the serialization of `CAddrMan` to serialize its addresses in ADDRv2/BIP155 format by default. Introduce a new `CAddrMan` format version (3). (furszy)
b8c1dda streams update: get rid of nType and nVersion. (furszy)
3eaa273 Support bypassing range check in ReadCompactSize (Pieter Wuille)
a237ba4 net: recognize TORv3/I2P/CJDNS networks (Vasil Dimov)
8e50853 util: make EncodeBase32 consume Spans (Sebastian Falbesoner)
1f67e30 net: CNetAddr: add support to (un)serialize as ADDRv2 (Vasil Dimov)
2455420 test: move HasReason so it can be reused (furszy)
d41adb4 util: move HasPrefix() so it can be reused (Vasil Dimov)
f6f86af Unroll Keccak-f implementation (Pieter Wuille)
45222e6 Implement keccak-f[1600] and SHA3-256 (Pieter Wuille)
08ad06d net: change CNetAddr::ip to have flexible size (furszy)
3337219 net: improve encapsulation of CNetAddr. (furszy)
910d5c4 test: Do not instantiate CAddrDB for static call (Hennadii Stepanov)
6b607ef Drop IsLimited in favor of IsReachable (Ben Woosley)
a40711b IsReachable is the inverse of IsLimited (DRY). Includes unit tests (marcaiaf)
8839828 net: don't accept non-left-contiguous netmasks (Vasil Dimov)
5d7f864 rpcbind: Warn about exposing RPC to untrusted networks (Luke Dashjr)
2a6abd8 CNetAddr: Add IsBindAny method to check for INADDR_ANY (Luke Dashjr)
4fdfa45 net: Always default rpcbind to localhost, never "all interfaces" (Luke Dashjr)
31064a8 net: Minor accumulated cleanups (furszy)
9f9c871 tests: Avoid using C-style NUL-terminated strings as arguments (practicalswift)
f6c52a3 tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters (practicalswift)
a751b9b net: Avoid using C-style NUL-terminated strings as arguments in the netbase interface (furszy)
f30869d test: add IsRFC2544 tests (Mark Tyneway)
ed5abe1 Net: Proper CService deserialization + GetIn6Addr return false if addr isn't an IPv6 addr (furszy)
86d73fb net: save the network type explicitly in CNetAddr (Vasil Dimov)
ad57dfc net: document `enum Network` (Vasil Dimov)
cb160de netaddress: Update CNetAddr for ORCHIDv2 (Carl Dong)
c3c04e4 net: Better misbehaving logging (furszy)
3660487 net: Use C++11 member initialization in protocol (Marco)
082baa3 refactor: Drop unused CBufferedFile::Seek() (Hennadii Stepanov)
e2d776a util: CBufferedFile fixes (Larry Ruane)
6921f42 streams: backport OverrideStream class (furszy)

Pull request description:

  Conjunction of a large number of back ports, updates and refactorings that made with the final goal of implementing v3 Onion addresses support (BIP155 https://github.com/bitcoin/bips/blob/master/bip-0155.mediawiki) before the tor v2 addresses EOL, scheduled, by the Tor project, for (1) July 15th: v2 addr support removal from the code base, and (2) October 15th: v2 addr network disable, where **every peer in our network running under Tor will loose the connection and drop the network**.

  As BIP155 describes, this is introducing a new P2P message to gossip longer node addresses over the P2P network. This is required to support new-generation Onion addresses, I2P, and potentially other networks that have longer endpoint addresses than fit in the 128 bits of the current addr message.

  In order to achieve the end goal, had to:
  1.  Create Span class and push it up to latest Bitcoin implementation.
  2.  Update the whole serialization framework and every object using it up to latest Bitcoin implementation (3-4 years ahead of what we currently are in master).
  3.  Update the address manager implementing ASN-based bucketing of the network nodes.
  4.  Update and refactor the netAddress and address manager tests to latest Bitcoin implementation (4 years ahead of what we currently are in master).
  5.  Several util string, vector, encodings, parsing, hashing backports and more..

  Important note:
  This PR it is not meant to be merged as a standalone PR, will decouple smaller ones moving on. Adding on each sub-PR its own description isolated from this big monster.

  Second note:
  This is still a **work-in-progress**, not ready for testing yet. I'm probably missing to mention few PRs that have already adapted to our sources. Just making it public so can decouple the changes, we can start merging them and i can continue working a bit more confortable (rebase a +170 commits separate branch is not fun..).

  ### List of back ported and adapted PRs:

  Span and Serialization:
  ----------------
  *  bitcoin#12886.
  *  bitcoin#12916.
  *  bitcoin#13558.
  *  bitcoin#13697. (Only Span's commit 29943a9)
  *  bitcoin#17850.
  *  bitcoin#17896.
  *  bitcoin#12752.
  *  bitcoin#16577.
  *  bitcoin#16670. (without faebf62)
  *  bitcoin#17957.
  *  bitcoin#18021.
  *  bitcoin#18087.
  *  bitcoin#18112 (only from 353f376 that we don't support).
  *  bitcoin#18167.
  *  bitcoin#18317.
  *  bitcoin#18591 (only Span's commit 0fbde48)
  *  bitcoin#18468.
  *  bitcoin#19020.
  *  bitcoin#19032.
  *  bitcoin#19367.
  *  bitcoin#19387.

  Net, NetAddress and AddrMan:
  ----------------

  *  bitcoin#7932.
  *  bitcoin#10756.
  *  bitcoin#10765.
  *  bitcoin#12218.
  *  bitcoin#12855.
  *  bitcoin#13532.
  *  bitcoin#13575.
  *  bitcoin#13815.
  *  bitcoin#14532.
  *  bitcoin#15051.
  *  bitcoin#15138.
  *  bitcoin#15689.
  *  bitcoin#16702.
  *  bitcoin#17243.
  *  bitcoin#17345.
  *  bitcoin#17754.
  *  bitcoin#17758.
  *  bitcoin#17812.
  *  bitcoin#18023.
  *  bitcoin#18454.
  *  bitcoin#18512.
  *  bitcoin#19314.
  *  bitcoin#19687

  Keys and Addresses encoding:
  ----------------
  * bitcoin#11372.
  * bitcoin#17511.
  * bitcoin#17721.

  Util:
  ----------------
  * bitcoin#9140.
  * bitcoin#16577.
  * bitcoin#16889.
  * bitcoin#19593.

  Bench:
  ----------------
  * bitcoin#16299.

  BIP155:
  ----------------
  *  bitcoin#19351.
  *  bitcoin#19360.
  *  bitcoin#19534.
  *  bitcoin#19628.
  *  bitcoin#19841.
  *  bitcoin#19845.
  *  bitcoin#19954.
  *  bitcoin#19991 (pending).
  *  bitcoin#19845.
  *  bitcoin#20000 (pending).
  *  bitcoin#20120.
  *  bitcoin#20284.
  *  bitcoin#20564.
  *  bitcoin#21157 (pending).
  *  bitcoin#21564 (pending).
  *  Fully removed v2 onion addr support.
  *  Add hardcoded seeds.
  *  Add release-notes, changes to files.md and every needed documentation.

  I'm currently working on the PRs marked as "pending", this isn't over, but I'm pretty pretty close :). What a long road..

ACKs for top commit:
  random-zebra:
    utACK ecde04a
  Fuzzbawls:
    ACK ecde04a

Tree-SHA512: 82c95fbda76fce63f96d8a9af7fa9a89cb1e1b302b7891e27118a6103af0be23606bf202c7332fa61908205e6b6351764e2ec23d753f1e2484028f57c2e8b51a
DeckerSU added a commit to DeckerSU/KomodoOcean that referenced this pull request Feb 8, 2022
backport of bitcoin/bitcoin#19534 to our
codebase.

CNetAddr, CService, CSubNet: declare operator!= via operator== .


TODO: solve addrman_serialization test (!), for now, after we introduce
m_net member in CNetAddr and have modified operator < - this test failed.
Return the operator < of CNetAddr to its previous state solve the issue.
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants