net: Use GetAdaptersAddresses to get local addresses on Windows #31014

pull laanwj wants to merge 1 commits into bitcoin:master from laanwj:2024-10-windows-address-discovery changing 1 files +70 −21
  1. laanwj commented at 6:57 pm on October 1, 2024: member

    Instead of a gethostname hack, which is not guaranteed to return all addresses, use the official way of calling GetAdaptersAddresses to get local network addresses on Windows.

    Do the same checks as the UNIX path: interface is up, interface is not loopback.

    Suggested by Ava Chow.

    Addiional changes:

    • Cleanup: move out FromSockAddr in netif.cpp from MacOS and use it everywhere appropriate. This avoids code duplication.
  2. laanwj added the label Windows on Oct 1, 2024
  3. laanwj added the label P2P on Oct 1, 2024
  4. DrahtBot commented at 6:57 pm on October 1, 2024: contributor

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

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/31014.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK davidgumberg
    Concept ACK 1440000bytes, jonatack
    Stale ACK sipa

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

    Conflicts

    No conflicts as of last run.

  5. laanwj force-pushed on Oct 1, 2024
  6. laanwj force-pushed on Oct 1, 2024
  7. laanwj force-pushed on Oct 1, 2024
  8. laanwj force-pushed on Oct 1, 2024
  9. laanwj force-pushed on Oct 1, 2024
  10. laanwj force-pushed on Oct 2, 2024
  11. DrahtBot added the label Needs rebase on Oct 2, 2024
  12. laanwj force-pushed on Oct 3, 2024
  13. DrahtBot removed the label Needs rebase on Oct 3, 2024
  14. 1440000bytes commented at 8:29 pm on October 3, 2024: none

    Instead of a gethostname hack, use the official way of calling GetAdaptersAddresses to get local network addresses on Windows.

    Concept ACK

  15. laanwj force-pushed on Oct 25, 2024
  16. in src/common/netif.cpp:287 in 135e8b2971 outdated
    268@@ -276,9 +269,46 @@ std::vector<CNetAddr> GetLocalAddresses()
    269 {
    270     std::vector<CNetAddr> addresses;
    271 #ifdef WIN32
    272-    char pszHostName[256] = "";
    273-    if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR) {
    274-        addresses = LookupHost(pszHostName, 0, true);
    275+    DWORD status = 0;
    276+    constexpr size_t MAX_ADAPTER_ADDR_SIZE = 4 * 1000 * 1000; // Absolute maximum size of adapter addresses structure we're willing to handle, as a precaution.
    


    laanwj commented at 11:12 am on October 25, 2024:
    Added an upper limit here to avoid hanging forever or consuming absurd amounts of resources in case of a windows bug. i would imagine 4MB of adapter addresses ought to be enough for anyone.
  17. laanwj commented at 1:04 pm on January 13, 2025: member

    Can anyone review here please?

    Mind that the first commit “Add optional length checking to CService::SetSockAddr” is also part of #31022.

    Edit: needs rebasing for #31022 (review) assuming that goes in first Edit.2: done, took over the same commit, network address length determination logic was moved to FromSockAddr for the unknown-length case

  18. laanwj force-pushed on Jan 14, 2025
  19. jonatack commented at 9:48 pm on January 15, 2025: member
    Concept ACK
  20. in src/common/netif.cpp:298 in 219872fc75 outdated
    296+        if (status == ERROR_BUFFER_OVERFLOW && out_buf.size() < MAX_ADAPTER_ADDR_SIZE) {
    297+            // If status == ERROR_BUFFER_OVERFLOW, out_buf_len will contain the needed size.
    298+            // Unfortunately, this cannot be fully relied on, because another process may have added interfaces.
    299+            // So to avoid getting stuck due to a race condition, double the buffer size at least
    300+            // once before retrying (but only up to the maximum allowed size).
    301+            do {
    


    sipa commented at 5:58 pm on January 17, 2025:

    The loops seems unnecessary to me.

    0out_buf.resize(std::min(std::max(out_buf_len, out_buf.size() * 2), MAX_ADAPTER_ADDR_SIZE));
    

    laanwj commented at 1:58 pm on January 21, 2025:
    Agree, the nested loop here isn’t necessary. It’s already looping.
  21. sipa commented at 6:01 pm on January 17, 2025: member
    Concept and code review ACK 219872fc75e552c87c20b5c1ce7e15fa887eec20, but I am not familiar with the Windows network API, and have not tested it.
  22. DrahtBot requested review from jonatack on Jan 17, 2025
  23. laanwj commented at 2:00 pm on January 21, 2025: member

    and have not tested it.

    FWIW, i have tested this on a windows machine. Though a virtual one on amazon EC. Both IPv4 and IPv6 addrsses were picked up.

    Of course, more testing is very welcome.

  24. achow101 referenced this in commit c65233230f on Feb 11, 2025
  25. DrahtBot added the label Needs rebase on Feb 12, 2025
  26. laanwj force-pushed on Feb 12, 2025
  27. laanwj commented at 11:45 am on February 12, 2025: member
    Rebased for merge of #31022. This got rid of the first commit, which was shared with that PR.
  28. DrahtBot removed the label Needs rebase on Feb 12, 2025
  29. in src/common/netif.cpp:34 in 851085a7f2 outdated
    30@@ -31,6 +31,30 @@
    31 
    32 namespace {
    33 
    34+//! Return CNetAddr for the specified OS-level network address. If a length is provided, it is checked for validity.
    


    davidgumberg commented at 0:41 am on February 13, 2025:
    0If a length is provided, it is checked for validity.
    

    Maybe it would be good to mention that it is checked for validity inside of SetSockAddr(), or is this detail that the caller of FromSockAddr() does not need to know?


    laanwj commented at 9:23 am on February 13, 2025:

    Right, the logic here has been swapped since this comment was written 😄

    i think that sentence is redundant. That the length is checked is implied, why pass it otherwise? The line below already describes which length is used when it’s not passed.

  30. davidgumberg commented at 0:43 am on February 13, 2025: contributor

    Tested crACK https://github.com/bitcoin/bitcoin/pull/31014/commits/851085a7f2761584402080b1767328d86cde4d94

    Nice refactor in reusing FromSockAddr() and probably makes local address discovery on Windows less brittle.

    Ran this branch and master on a local Windows 10 22H2 machine with multiple network adapters, both Discover()‘ed the same IPv6 addresses from all adapters, which matched up with what ifconfig reported as the machine’s addresses. The device does not have any IPv4 addresses which pass CNetAddr::IsRoutable() so no Discover() messages were logged for IPv4 so I have not tested IPv4.

  31. DrahtBot requested review from sipa on Feb 13, 2025
  32. laanwj commented at 9:45 am on February 13, 2025: member

    Thanks for testing!

    DrahtBot shouldn’t have re-requested review, i first need to address sipa’s comment in the first place.

  33. maflcko commented at 9:56 am on February 13, 2025: member

    DrahtBot shouldn’t have re-requested review, i first need to address sipa’s comment in the first place.

    For outstanding review comment the bot waits for the next ack to happen, after which it assumes anything outstanding to be addressed. It is not perfect, but I can’t come up with something better, and it seems fine as long any other reviewer notices.

  34. laanwj commented at 10:04 am on February 13, 2025: member

    For outstanding review comment the bot waits for the next ack to happen, after which it assumes anything outstanding to be addressed. It is not perfect, but I can’t come up with something better, and it seems fine as long any other reviewer notices.

    Yes the heuristic’s usually correct, don’t know anything better. Was just noting it, as github has no way to un-request review.

  35. DrahtBot removed review request from sipa on Feb 13, 2025
  36. laanwj force-pushed on Feb 13, 2025
  37. laanwj commented at 12:49 pm on February 13, 2025: member
    • Removed the nested loop in GetAdaptersAddresses (@sipa)
    • Made doc comment for FromSockAddr more concise (@davidgumberg)
  38. DrahtBot added the label CI failed on Feb 13, 2025
  39. DrahtBot commented at 1:30 pm on February 13, 2025: contributor

    🚧 At least one of the CI tasks failed. Debug: https://github.com/bitcoin/bitcoin/runs/37163426494

    Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:

    • Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    • A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.

    • An intermittent issue.

    Leave a comment here, if you need help tracking down a confusing failure.

  40. laanwj force-pushed on Feb 13, 2025
  41. net: Use GetAdaptersAddresses to get local addresses on Windows
    Instead of a `gethostname` hack, use the official way of calling
    `GetAdaptersAddresses` to get local network addresses on Windows.
    
    As additional cleanup, move out `FromSockAddr` from MacOS and use it
    everywhere appropriate.
    
    Suggested by Ava Chow.
    b9d4d5f66a
  42. laanwj force-pushed on Feb 13, 2025
  43. DrahtBot removed the label CI failed on Feb 13, 2025
  44. davidgumberg commented at 5:11 am on February 14, 2025: contributor
  45. maflcko commented at 6:19 am on February 14, 2025: member
    @davidgumberg Looks like you reviewed a stale commit?
  46. laanwj commented at 7:05 am on February 14, 2025: member
  47. davidgumberg commented at 7:34 am on February 14, 2025: contributor

    @davidgumberg Looks like you reviewed a stale commit?

    Oops.

    utreACK https://github.com/bitcoin/bitcoin/commit/b9d4d5f66a5a35c47e7abc9ec6ef5ab242b3f1e1

    Recent rebase makes a comment change, and removes an unnecessary loop, addressing reviewer feedback.

  48. DrahtBot requested review from sipa on Feb 14, 2025

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-02-22 06:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me