Commit graph

107 commits

Author SHA1 Message Date
Vasil Dimov
64d6f77907
net: put CJDNS prefix byte in a constant 2023-10-05 15:10:32 +02:00
Andrew Chow
41cb17fdb6
Merge bitcoin/bitcoin#28078: net, refactor: remove unneeded exports, use helpers over low-level code, use optional
4ecfd3eaf4 Inline short, often-called, rarely-changed basic CNetAddr getters (Jon Atack)
5316ae5dd8 Convert GetLocal() to std::optional and remove out-param (Jon Atack)
f1304db136 Use higher-level CNetAddr and CNode helpers in net.cpp (Jon Atack)
07f5891588 Add CNetAddr::IsPrivacyNet() and CNode::IsConnectedThroughPrivacyNet() (Jon Atack)
df488563b2 GetLocal() type-safety, naming, const, and formatting cleanups (stickies-v)
fb4265747c Add and use CNetAddr::HasCJDNSPrefix() helper (Jon Atack)
5ba73cd0ee Move GetLocal() declaration from header to implementation (Jon Atack)
11426f6557 Move CaptureMessageToFile() declaration from header to implementation (Jon Atack)
deccf1c484 Move IsPeerAddrLocalGood() declaration from header to implementation (Jon Atack)

Pull request description:

  and other improvements noticed while reviewing #27411.

  Addresses https://github.com/bitcoin/bitcoin/pull/27411#discussion_r1263969104 and https://github.com/bitcoin/bitcoin/pull/27411#discussion_r1263967598.

  See commit messages for details.

ACKs for top commit:
  achow101:
    ACK 4ecfd3eaf4
  vasild:
    ACK 4ecfd3eaf4
  stickies-v:
    ACK 4ecfd3eaf4

Tree-SHA512: d792bb2cb24690aeae9bedf97e92b64fb6fd080c39385a4bdb8ed05f37f3134d85bda99da025490829c03017fd56382afe7951cdd039aede1c08ba98fb1aa7f9
2023-09-21 11:26:16 -04:00
Anthony Towns
5e5c8f86b6 serialize: add SER_PARAMS_OPFUNC 2023-09-14 10:25:26 +10:00
MarcoFalke
fac81affb5
Use serialization parameters for CAddress serialization
This also cleans up the addrman (de)serialization code paths to only
allow `Disk` serialization. Some unit tests previously forced a
`Network` serialization, which does not make sense, because Bitcoin Core
in production will always `Disk` serialize.
This cleanup idea was suggested by Pieter Wuille and implemented by Anthony
Towns.

Co-authored-by: Pieter Wuille <pieter@wuille.net>
Co-authored-by: Anthony Towns <aj@erisian.com.au>
2023-09-05 10:13:25 +02:00
MarcoFalke
aaaa3fa947
Replace READWRITEAS macro with AsBase wrapping function
Co-authored-by: Pieter Wuille <pieter@wuille.net>
2023-08-28 17:48:15 +02:00
Jon Atack
4ecfd3eaf4 Inline short, often-called, rarely-changed basic CNetAddr getters
and make them nodiscard.

Member functions containing a few lines of code are usually inlined, either
implicitly by defining them in the declaration as done here, or declared inline.

References
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-inline
https://google.github.io/styleguide/cppguide#Inline_Functions
https://www.ibm.com/docs/en/i/7.1?topic=only-inline-member-functions-c
2023-07-19 12:43:05 -06:00
Jon Atack
07f5891588 Add CNetAddr::IsPrivacyNet() and CNode::IsConnectedThroughPrivacyNet()
Co-authored-by: Vasil Dimov <vd@FreeBSD.org>
2023-07-19 12:40:45 -06:00
Jon Atack
fb4265747c Add and use CNetAddr::HasCJDNSPrefix() helper 2023-07-19 11:49:27 -06:00
Sebastian Falbesoner
7d92b1430a refactor: use Span for SipHash::Write
Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz>
2023-07-17 13:27:46 +02:00
Martin Zumsande
e4d541c7cf net, refactor: pass reference for peer address in GetReachabilityFrom
The address of the peer always exists (because addr is a member of
CNode), so it was not possible to pass a nullptr before.
Also remove NET_UNKNOWN, which is unused now.
2023-06-05 11:02:47 -04:00
Andrew Chow
35fbc97208
Merge bitcoin/bitcoin#25619: net: avoid overriding non-virtual ToString() in CService and use better naming
c9d548c91f net: remove CService::ToStringPort() (Vasil Dimov)
fd4f0f41e9 gui: simplify OptionsDialog::updateDefaultProxyNets() (Vasil Dimov)
96c791dd20 net: remove CService::ToString() use ToStringAddrPort() instead (Vasil Dimov)
944a9de08a net: remove CNetAddr::ToString() and use ToStringAddr() instead (Vasil Dimov)
043b9de59a scripted-diff: rename ToStringIP[Port]() to ToStringAddr[Port]() (Vasil Dimov)

Pull request description:

  Before this PR we had the somewhat confusing combination of methods:

  `CNetAddr::ToStringIP()`
  `CNetAddr::ToString()` (duplicate of the above)
  `CService::ToStringIPPort()`
  `CService::ToString()` (duplicate of the above, overrides a non-virtual method from `CNetAddr`)
  `CService::ToStringPort()`

  Avoid [overriding non-virtual methods](https://github.com/bitcoin/bitcoin/pull/25349/#issuecomment-1185226396).

  "IP" stands for "Internet Protocol" and while sometimes "IP addresses" are called just "IPs", it is incorrect to call Tor or I2P addresses "IPs". Thus use "Addr" instead of "IP".

  Change the above to:

  `CNetAddr::ToStringAddr()`
  `CService::ToStringAddrPort()`

  The changes touch a lot of files, but are mostly mechanical.

ACKs for top commit:
  sipa:
    utACK c9d548c91f
  achow101:
    ACK c9d548c91f
  jonatack:
    re-ACK c9d548c91f only change since my previous reviews is rebase, but as a sanity check rebased to current master and at each commit quickly re-reviewed and re-verified clean build and green unit tests
  LarryRuane:
    ACK c9d548c91f

Tree-SHA512: 633fb044bdecf9f551b5e3314c385bf10e2b78e8027dc51ec324b66b018da35e5b01f3fbe6295bbc455ea1bcd1a3629de1918d28de510693afaf6a52693f2157
2023-02-17 13:34:40 -05:00
Jon Atack
0e015146bd net: remove orphaned CSubNet::SanityCheck()
CSubNet::SanityCheck() was added in #20140, and not removed in #22570
when it became orphaned code.
2023-02-15 14:41:58 -08:00
Hennadii Stepanov
306ccd4927
scripted-diff: Bump copyright headers
-BEGIN VERIFY SCRIPT-
./contrib/devtools/copyright_header.py update ./
-END VERIFY SCRIPT-

Commits of previous years:
- 2021: f47dda2c58
- 2020: fa0074e2d8
- 2019: aaaaad6ac9
2022-12-24 23:49:50 +00:00
Vasil Dimov
c9d548c91f
net: remove CService::ToStringPort()
It is used only internally in `CService::ToStringAddrPort()`.
2022-12-12 11:54:23 +01:00
Vasil Dimov
96c791dd20
net: remove CService::ToString() use ToStringAddrPort() instead
Both methods do the same thing, so simplify to having just one.

`ToString()` is too generic in this case and it is unclear what it does,
given that there are similar methods:
`ToStringAddr()` (inherited from `CNetAddr`),
`ToStringPort()` and
`ToStringAddrPort()`.
2022-12-12 11:54:20 +01:00
Vasil Dimov
944a9de08a
net: remove CNetAddr::ToString() and use ToStringAddr() instead
Both methods do the same thing, so simplify to having just one.

Further, `CService` inherits `CNetAddr` and `CService::ToString()`
overrides `CNetAddr::ToString()` but the latter is not virtual which
may be confusing. Avoid such a confusion by not having non-virtual
methods with the same names in inheritance.
2022-12-12 11:48:31 +01:00
Vasil Dimov
043b9de59a
scripted-diff: rename ToStringIP[Port]() to ToStringAddr[Port]()
"IP" stands for "Internet Protocol".

"IP address" is sometimes shortened to just "IP" or "address".

However, Tor or I2P addresses are not "IP addresses", nor "IPs".

Thus, use "Addr" instead of "IP" for addresses that could be IP, Tor or
I2P addresses:

`CService::ToStringIPPort()` -> `CService::ToStringAddrPort()`
`CNetAddr::ToStringIP()` -> `CNetAddr::ToStringAddr()`

-BEGIN VERIFY SCRIPT-
sed -i 's/ToStringIPPort/ToStringAddrPort/g' -- $(git grep -l ToStringIPPort src)
sed -i 's/ToStringIP/ToStringAddr/g' -- $(git grep -l ToStringIP src)
-END VERIFY SCRIPT-
2022-12-12 11:48:30 +01:00
dergoegge
c9ba3f836e [netaddress] Make OnionToString public 2022-11-17 14:52:45 +00:00
fanquake
cc7b2fdd70
refactor: move compat.h into compat/ 2022-07-20 10:34:46 +01:00
Ben Woosley
71a8dbe5da
refactor: Remove defunct attributes.h includes
Since the removal of NODISCARD in 81d5af42f4,
the only attributes def is LIFETIMEBOUND, and it's included in many more
places that it is used.

This removes all includes which do not have an associated use of LIFETIMEBOUND,
and adds it to the following files, due to their use of the same:
* src/validationinterface.h
* src/script/standard.h
2022-05-21 13:54:33 -05:00
pasta
ab1ea29ba1 refactor: make GetRand a template, remove GetRandInt 2022-04-22 09:04:39 -05:00
John Newbery
1b978a7e8c [netgroupman] Move GetMappedAS() and GetGroup() logic to NetGroupManager
Reviewer hint: use:

`git diff --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space`
2022-04-20 14:35:52 +01:00
Vasil Dimov
36ee76d1af
net: remove unused CNetAddr::GetHash() 2022-02-11 15:21:52 +01:00
Vasil Dimov
2e38a0e686
net: add CServiceHash constructor so the caller can provide the salts
This new constructor will be useful if we just want to hash a `CService`
object without the two `GetRand()` calls (in `RelayAddress()` in a
subsequent commit).
2022-02-11 15:21:50 +01:00
Hennadii Stepanov
f47dda2c58
scripted-diff: Bump copyright headers
-BEGIN VERIFY SCRIPT-
./contrib/devtools/copyright_header.py update ./
-END VERIFY SCRIPT-

Commits of previous years:
* 2020: fa0074e2d8
* 2019: aaaaad6ac9
2021-12-30 19:36:57 +02:00
Pieter Wuille
568dd2f839 Replace MakeSpan helper with Span deduction guide 2021-11-29 17:58:53 -05:00
W. J. van der Laan
2ef186a140
Merge bitcoin/bitcoin#22881: doc: provide context for CNetAddr::UnserializeV1Array() and span.h with lifetimebound
33c6a208a9 span, doc: provide span.h context and explain lifetimebound definition (Jon Atack)
d14395bc5d net, doc: provide context for UnserializeV1Array() (Jon Atack)

Pull request description:

  Add contextual documentation for developers and future readers of the code regarding
  - CNetAddr::UnserializeV1Array (see #22140)
  - Span and why it defines Clang lifetimebound locally rather than using the one in attributes.h

ACKs for top commit:
  laanwj:
    Documentation review ACK 33c6a208a9

Tree-SHA512: cb8e6a6c23b36c9ef7499257e97c5378ec895bb9122b79b63b572d9721a1ae6ce6c0be7ad61bdf976c255527ae750fc9ff4b3e03c07c6c797d14dbc82ea9fb3a
2021-11-17 17:06:55 +01:00
Vasil Dimov
29ff79c0a2
net: relay CJDNS addresses even if we are not connected to CJDNS
This will help with propagation, so that multi-homed nodes can learn
CJDNS addresses outside of the CJDNS network.
2021-11-03 14:58:53 +01:00
Vasil Dimov
6387f397b3
net: recognize CJDNS addresses as such
In some cases addresses come from an external source as a string or as a
`struct sockaddr_in6`, without a tag to tell whether it is a private
IPv6 or a CJDNS address. In those cases interpret the address as a CJDNS
address instead of an IPv6 address if `-cjdnsreachable` is set and the
seemingly-IPv6-address belongs to `fc00::/8`. Those external sources are:

* `-externalip=`
* `-bind=`
* UPnP
* `getifaddrs(3)` (called through `-discover`)
* `addnode`
* `connect`
* incoming connections (returned by `accept(2)`)
2021-11-03 14:58:50 +01:00
Pieter Wuille
92617b7a75 Make AddrMan support multiple ports per IP 2021-10-22 12:06:36 -04:00
Amiti Uttarwar
dd8f7f2500 scripted-diff: Rename CAddrMan to AddrMan
-BEGIN VERIFY SCRIPT-
git grep -l CAddrMan src/ test/ | xargs sed -i 's/CAddrMan/AddrMan/g'
-END VERIFY SCRIPT-
2021-09-28 22:21:10 -04:00
Jon Atack
d14395bc5d
net, doc: provide context for UnserializeV1Array() 2021-09-23 17:32:28 +02:00
John Newbery
bfdf4ef334 [asmap] Remove SanityCheckASMap() from netaddress
SanityCheckASMap(asmap, bits) simply calls through to SanityCheckASMap(asmap)
in util/asmap. Update all callers to simply call that function.
2021-09-07 13:31:10 +01:00
MarcoFalke
fa1eddb1a3
Fix whitespace in touched files
Leaving the incorrect indentation would be frustrating because:
* Some editor may fix up the whitespace when editing a file, so before
  commiting the whitespace changes need to be undone.
* It makes it harder to use clang-format-diff on a change.

Can be trivially reviewed with --word-diff-regex=. --ignore-all-space
2021-07-30 11:24:59 +02:00
MarcoFalke
fa4e6afdae
Remove unused CSubNet serialize code 2021-07-30 11:24:41 +02:00
W. J. van der Laan
a88fa1a555
Merge bitcoin/bitcoin#22211: net: relay I2P addresses even if not reachable (by us)
7593b06bd1 test: ensure I2P addresses are relayed (Vasil Dimov)
e7468139a1 test: make CAddress in functional tests comparable (Vasil Dimov)
33e211d2a4 test: implement ser/unser of I2P addresses in functional tests (Vasil Dimov)
86742811ce test: use NODE_* constants instead of magic numbers (Vasil Dimov)
ba45f02708 net: relay I2P addresses even if not reachable (by us) (Vasil Dimov)

Pull request description:

  Nodes that can reach the I2P network (have set `-i2psam=`) will relay
  I2P addresses even without this patch. However, nodes that can't reach
  the I2P network will not. This was done as a precaution in
  https://github.com/bitcoin/bitcoin/pull/20119 before anybody could
  connect to I2P because then, for sure, it would have been useless.

  Now, however, we have I2P support and a bunch of I2P nodes, so get all
  nodes on the network to relay I2P addresses to help with propagation,
  similarly to what we do with Tor addresses.

ACKs for top commit:
  jonatack:
    ACK 7593b06bd1
  naumenkogs:
    ACK 7593b06bd1.
  laanwj:
    Code review ACK 7593b06bd1
  kristapsk:
    ACK 7593b06bd1. Code looks correct, tested that functional test suite passes and also that `test/functional/p2p_addrv2_replay.py` fails if I undo changes in `IsRelayable()`.

Tree-SHA512: c9feec4a9546cc06bc2fec6d74f999a3c0abd3d15b7c421c21fcf2d610eb94611489e33d61bdcd5a4f42041a6d84aa892f7ae293b0d4f755309a8560b113b735
2021-07-15 16:53:34 +02:00
Vasil Dimov
38f900290c
net: change assumed I2P port to 0
* When accepting an I2P connection, assume the peer has port 0 instead
  of the default 8333 (for mainnet). It is not being sent to us, so we
  must assume something.
* When deriving our own I2P listen CService use port 0 instead of the
  default 8333 (for mainnet). So that we later advertise it to peers
  with port 0.

In the I2P protocol SAM 3.1 and older (we use 3.1) ports are not used,
so they are irrelevant. However in SAM 3.2 and newer ports are used and
from the point of view of SAM 3.2, a peer using SAM 3.1 seems to have
specified port=0.
2021-07-09 11:19:35 +02:00
fanquake
1a369f006f
Merge bitcoin/bitcoin#18722: addrman: improve performance by using more suitable containers
a92485b2c2 addrman: use unordered_map instead of map (Vasil Dimov)

Pull request description:

  `CAddrMan` uses `std::map` internally even though it does not require
  that the map's elements are sorted. `std::map`'s access time is
  `O(log(map size))`. `std::unordered_map` is more suitable as it has a
  `O(1)` access time.

  This patch lowers the execution times of `CAddrMan`'s methods as follows
  (as per `src/bench/addrman.cpp`):

  ```
  AddrMan::Add(): -3.5%
  AddrMan::GetAddr(): -76%
  AddrMan::Good(): -0.38%
  AddrMan::Select(): -45%
  ```

ACKs for top commit:
  jonatack:
    ACK a92485b2c2
  achow101:
    ACK a92485b2c2
  hebasto:
    re-ACK a92485b2c2, only suggested changes and rebased since my [previous](https://github.com/bitcoin/bitcoin/pull/18722#pullrequestreview-666663681) review.

Tree-SHA512: d82959a00e6bd68a6c4c5a265dd08849e6602ac3231293b7a3a3b7bf82ab1d3ba77f8ca682919c15c5d601b13e468b8836fcf19595248116635f7a50d02ed603
2021-06-12 11:41:27 +08:00
Vasil Dimov
ba45f02708
net: relay I2P addresses even if not reachable (by us)
Nodes that can reach the I2P network (have set `-i2psam=`) will relay
I2P addresses even without this patch. However, nodes that can't reach
the I2P network will not. This was done as a precaution in
https://github.com/bitcoin/bitcoin/pull/20119 before anybody could
connect to I2P because then, for sure, it would have been useless.

Now, however, we have I2P support and a bunch of I2P nodes, so get all
nodes on the network to relay I2P addresses to help with propagation,
similarly to what we do with Tor addresses.
2021-06-10 14:16:41 +02:00
Jon Atack
3d39042144
p2p: remove torv2 in SetIP() and ADDR_TORV2_SIZE constant 2021-06-03 13:36:46 +02:00
Jon Atack
cff5ec477a
p2p: remove pre-addrv2 onions from SerializeV1Array() 2021-06-03 13:36:44 +02:00
Jon Atack
1d631e956f
p2p: remove BIP155Network::TORV2 from GetBIP155Network() 2021-06-03 13:36:40 +02:00
Vasil Dimov
a92485b2c2
addrman: use unordered_map instead of map
`CAddrMan` uses `std::map` internally even though it does not require
that the map's elements are sorted. `std::map`'s access time is
`O(log(map size))`. `std::unordered_map` is more suitable as it has a
`O(1)` access time.

This patch lowers the execution times of `CAddrMan`'s methods as follows
(as per `src/bench/addrman.cpp`):

```
AddrMan::Add(): -3.5%
AddrMan::GetAddr(): -76%
AddrMan::Good(): -0.38%
AddrMan::Select(): -45%
```
2021-05-28 16:40:15 +02:00
Jon Atack
f8e94002fc
p2p: remove torv2/ADDR_TORV2_SIZE from SetTor() 2021-05-27 22:54:39 +02:00
Vasil Dimov
cff65c4a27
net: extend CNetAddr::SetSpecial() to support I2P
Recognize also I2P addresses in the form `base32hashofpublickey.b32.i2p`
from `CNetAddr::SetSpecial()`.

This makes `Lookup()` support them, which in turn makes it possible to
manually connect to an I2P node by using
`-proxy=i2p_socks5_proxy:port -addnode=i2p_address.b32.i2p:port`

Co-authored-by: Lucas Ontivero <lucasontivero@gmail.com>
2021-03-01 13:22:11 +01:00
Jon Atack
1c3af37881
net: create GetNetworkNames() 2021-02-02 00:01:36 +01:00
Wladimir J. van der Laan
675af2a515
Merge #20852: net: allow CSubNet of non-IP networks
39b43298d9 test: add test for banning of non-IP addresses (Vasil Dimov)
94d335da7f net: allow CSubNet of non-IP networks (Vasil Dimov)

Pull request description:

  Allow creation of valid `CSubNet` objects of non-IP networks and only
  match the single address they were created from (like /32 for IPv4 or
  /128 for IPv6).

  This fixes a deficiency in `CConnman::DisconnectNode(const CNetAddr& addr)`
  and in `BanMan` which assume that creating a subnet from any address
  using the `CSubNet(CNetAddr)` constructor would later match that address
  only. Before this change a non-IP subnet would be invalid and would not
  match any address.

ACKs for top commit:
  jonatack:
    Code review re-ACK 39b43298d9 per `git diff 5e95ce6 39b4329`; only change since last review is improvements to the functional test; verified the test fails on master @ 616eace0 where expected (`assert(self.is_banned(node, tor_addr))` fails and unban unfails)
  laanwj:
    code review ACK 39b43298d9

Tree-SHA512: 3239b26d0f2fa2d1388b4fdbc1d05ce4ac1980be699c6ec46049409baefcb2006b1e72b889871e2210e897f6725c48e873f68457eea7e6e4958ab4f959d20297
2021-01-11 11:27:47 +01:00
Vasil Dimov
94d335da7f
net: allow CSubNet of non-IP networks
Allow creation of valid `CSubNet` objects of non-IP networks and only
match the single address they were created from (like /32 for IPv4 or
/128 for IPv6).

This fixes a deficiency in `CConnman::DisconnectNode(const CNetAddr& addr)`
and in `BanMan` which assume that creating a subnet from any address
using the `CSubNet(CNetAddr)` constructor would later match that address
only. Before this change a non-IP subnet would be invalid and would not
match any address.
2021-01-10 15:51:15 +01:00
Ikko Ashimine
1112035d32
doc: fix various typos
Co-authored-by: Peter Yordanov <ppyordanov@yahoo.com>
2021-01-04 12:31:31 +08:00
MarcoFalke
2fa085a5d7
Merge #20033: refactor: minor whitespace fixups, s/const/constexpr/ and remove template (followup to #19845)
89836a82ee style: minor improvements as a followup to #19845 (Vasil Dimov)

Pull request description:

  Address suggestions:
  https://github.com/bitcoin/bitcoin/pull/19845#discussion_r495486760
  https://github.com/bitcoin/bitcoin/pull/19845#discussion_r495488051
  https://github.com/bitcoin/bitcoin/pull/19845#discussion_r495730125

ACKs for top commit:
  jonatack:
    re-ACK 89836a8 change since previous review is replacing std::runtime_error with std::exception, built/ran unit tests with gcc debian 10.2.0-15, then broke a few v3 net_tests involving `BOOST_CHECK_EXCEPTION`, rebuilt, ran `src/test/test_bitcoin -t net_tests -l all` and checked the error reporting.
  hebasto:
    re-ACK 89836a82ee
  theStack:
    ACK 89836a82ee

Tree-SHA512: 36477fdccabe5a8ad91fbabb4655cc363a3a7ca237a98ae6dd4a9fae4a4113762040f864d4ca13a47d081f7d16e5bd487edbfb61ab50a37e4a0424e9bec30b24
2020-11-16 07:57:34 +01:00