netbase: extend CreateSock() to support creating arbitrary sockets #30202

pull vasild wants to merge 1 commits into bitcoin:master from vasild:extend_CreateSock changing 6 files +33 −33
  1. vasild commented at 1:40 pm on May 30, 2024: contributor

    Allow the callers of CreateSock() to pass all 3 arguments to the socket(2) syscall. This makes it possible to create sockets of any domain/type/protocol. In addition to extending arguments, some extra safety checks were put in place.

    The need for this came up during the discussion in #30043 (review)

  2. DrahtBot commented at 1:40 pm on May 30, 2024: contributor

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

    Code Coverage

    For detailed information about the code coverage, see the test coverage report.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK tdb3, theStack, achow101
    Stale ACK laanwj

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

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #29876 (build: add -Wundef by fanquake)
    • #29641 (scripted-diff: Use LogInfo/LogDebug over LogPrintf/LogPrint by maflcko)
    • #29625 (Several randomness improvements by sipa)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  3. laanwj commented at 1:51 pm on May 30, 2024: member
    Concept and code review (but untested, will use this to make a testing harness for #30043) ACK 77e34ded543e19ba27cab8d0cfc464af27f04bbf
  4. in src/net.cpp:3032 in 77e34ded54 outdated
    3028@@ -3029,7 +3029,7 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
    3029         return false;
    3030     }
    3031 
    3032-    std::unique_ptr<Sock> sock = CreateSock(addrBind.GetSAFamily());
    3033+    std::unique_ptr<Sock> sock = CreateSock(addrBind.GetSAFamily(), SOCK_STREAM, IPPROTO_TCP);
    


    tdb3 commented at 10:49 pm on June 2, 2024:

    I’m assuming changing GetSAFamily()’s return type was avoided to keep the PR scoped (and since the change doesn’t seem necessary).

    Since GetSAFamily() returns sa_family_t, looks like this is a simple promotion from an unsigned short (sa_family_t) to int. This seems fine (it aligns with the typical way socket() args are typically used).


    laanwj commented at 11:58 am on June 3, 2024:
    Returning sa_family_t is slightly more self-documenting, imo, would prefer to keep it as-is. But yes it’s simply a type-def’d integer type not a real enum, i suppose because it’s “open”, as in, new OS headers could potentially define their own address family without having to update a central list.
  5. in src/netbase.cpp:497 in 77e34ded54 outdated
    491     // Not IPv4, IPv6 or UNIX
    492-    if (address_family == AF_UNSPEC) return nullptr;
    493-
    494-    int protocol{IPPROTO_TCP};
    495-#if HAVE_SOCKADDR_UN
    496-    if (address_family == AF_UNIX) protocol = 0;
    


    tdb3 commented at 11:02 pm on June 2, 2024:
    Seems ok to remove, since protocol is now an argument.
  6. in src/netbase.cpp:503 in 77e34ded54 outdated
    504         return nullptr;
    505     }
    506 
    507     auto sock = std::make_unique<Sock>(hSocket);
    508 
    509+    if (domain != AF_INET && domain != AF_INET6 && domain != AF_UNIX) {
    


    tdb3 commented at 11:06 pm on June 2, 2024:

    Seems good to add this sanity check to prevent setting non-pertinent sockopts.

    nit: Might be worthwhile to mention in the PR description that in addition to extending arguments, some extra safety checks were put in place.


    vasild commented at 6:46 am on June 12, 2024:
    Done
  7. in src/netbase.cpp:545 in 77e34ded54 outdated
    544+
    545     return sock;
    546 }
    547 
    548-std::function<std::unique_ptr<Sock>(const sa_family_t&)> CreateSock = CreateSockOS;
    549+std::function<std::unique_ptr<Sock>(int, int, int)> CreateSock = CreateSockOS;
    


    tdb3 commented at 11:10 pm on June 2, 2024:
    Since these are simple ints passed by value, seems ok to change from the previous const reference approach.
  8. tdb3 approved
  9. tdb3 commented at 11:22 pm on June 2, 2024: contributor

    ACK for 77e34ded543e19ba27cab8d0cfc464af27f04bbf

    Thanks. Seems like a reasonable change.

    Sanity checks performed:

    • Synced the last 1.5 day’s worth of blocks on signet. Binding and sync seemed to work fine.
    • Ran all unit and functional tests (passed).

    Left some minor notes (just thinking out loud, so no need to answer/comment unless you feel it’s necessary).

  10. in src/netbase.h:268 in 77e34ded54 outdated
    261@@ -262,16 +262,18 @@ CService LookupNumeric(const std::string& name, uint16_t portDefault = 0, DNSLoo
    262 CSubNet LookupSubNet(const std::string& subnet_str);
    263 
    264 /**
    265- * Create a TCP or UNIX socket in the given address family.
    266- * @param[in] address_family to use for the socket.
    267+ * Create a real socket from the operating system.
    268+ * @param[in] domain Communications domain, first argument to the socket(2) syscall.
    269+ * @param[in] type Type of the socket, second argument to the socket(2) syscall.
    270+ * @param[in] protocol The particular protocol to be used with the socke, third argument to the socket(2) syscall.
    


    theStack commented at 10:20 am on June 11, 2024:

    typo nit:

    0 * [@param](/bitcoin-bitcoin/contributor/param/)[in] protocol The particular protocol to be used with the socket, third argument to the socket(2) syscall.
    

    vasild commented at 6:51 am on June 12, 2024:

    Oh, it is usually either s, sock or socket. socke is something new, that is extra genuine ;-)

    Fixed, invalidating 3 ACKs but should be trivial to re-review.

    Thanks!

  11. theStack approved
  12. theStack commented at 10:20 am on June 11, 2024: contributor
    Concept and code-review ACK 77e34ded543e19ba27cab8d0cfc464af27f04bbf
  13. vasild force-pushed on Jun 12, 2024
  14. vasild commented at 6:49 am on June 12, 2024: contributor
    77e34ded54...5f549c35d9: fix typo in the comment: s/socke/socket/
  15. theStack approved
  16. theStack commented at 7:09 am on June 12, 2024: contributor
    re-ACK 5f549c35d9a75c7019fe8a96963b988df5498eed 🧦
  17. DrahtBot requested review from laanwj on Jun 12, 2024
  18. DrahtBot requested review from tdb3 on Jun 12, 2024
  19. tdb3 approved
  20. tdb3 commented at 1:02 am on June 13, 2024: contributor
    re-ACK 5f549c35d9a75c7019fe8a96963b988df5498eed
  21. achow101 commented at 4:18 pm on June 13, 2024: member
    ACK 5f549c35d9a75c7019fe8a96963b988df5498eed
  22. achow101 commented at 4:23 pm on June 13, 2024: member

    Silent merge conflict

     0../../../src/test/fuzz/i2p.cpp:30:16: error: no viable overloaded '='
     1   30 |     CreateSock = [&fuzzed_data_provider](const sa_family_t&) {
     2      |     ~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     3   31 |         return std::make_unique<FuzzedSock>(fuzzed_data_provider);
     4      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     5   32 |     };
     6      |     ~
     7/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.1.1/../../../../include/c++/14.1.1/bits/std_function.h:469:7: note: candidate function not viable: no known conversion from '(lambda at ../../../src/test/fuzz/i2p.cpp:30:18)' to 'const function<unique_ptr<Sock> (int, int, int)>' for 1st argument
     8  469 |       operator=(const function& __x)
     9      |       ^         ~~~~~~~~~~~~~~~~~~~
    10/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.1.1/../../../../include/c++/14.1.1/bits/std_function.h:487:7: note: candidate function not viable: no known conversion from '(lambda at ../../../src/test/fuzz/i2p.cpp:30:18)' to 'function<unique_ptr<Sock> (int, int, int)>' for 1st argument
    11  487 |       operator=(function&& __x) noexcept
    12      |       ^         ~~~~~~~~~~~~~~
    13/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.1.1/../../../../include/c++/14.1.1/bits/std_function.h:501:7: note: candidate function not viable: no known conversion from '(lambda at ../../../src/test/fuzz/i2p.cpp:30:18)' to 'nullptr_t' (aka 'std::nullptr_t') for 1st argument
    14  501 |       operator=(nullptr_t) noexcept
    15      |       ^         ~~~~~~~~~
    16/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.1.1/../../../../include/c++/14.1.1/bits/std_function.h:531:2: note: candidate template ignored: requirement '_Callable<(lambda at ../../../src/test/fuzz/i2p.cpp:30:18), (lambda at ../../../src/test/fuzz/i2p.cpp:30:18), std::__invoke_result<(lambda at ../../../src/test/fuzz/i2p.cpp:30:18) &, int, int, int>>::value' was not satisfied [with _Functor = (lambda at ../../../src/test/fuzz/i2p.cpp:30:18)]
    17  531 |         operator=(_Functor&& __f)
    18      |         ^
    19/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.1.1/../../../../include/c++/14.1.1/bits/std_function.h:541:2: note: candidate template ignored: could not match 'reference_wrapper<_Functor>' against '(lambda at ../../../src/test/fuzz/i2p.cpp:30:18)'
    20  541 |         operator=(reference_wrapper<_Functor> __f) noexcept
    21      |         ^
    221 error generated.
    23make[2]: *** [Makefile:17184: test/fuzz/fuzz-i2p.o] Error 1
    
  23. DrahtBot commented at 7:32 am on June 14, 2024: contributor

    🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the documentation.

    Possibly this is 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.

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

    Debug: https://github.com/bitcoin/bitcoin/runs/26113679661

  24. DrahtBot added the label CI failed on Jun 14, 2024
  25. netbase: extend CreateSock() to support creating arbitrary sockets
    Allow the callers of `CreateSock()` to pass all 3 arguments to the
    `socket(2)` syscall. This makes it possible to create sockets of
    any domain/type/protocol.
    1245d1388b
  26. vasild force-pushed on Jun 14, 2024
  27. vasild commented at 12:24 pm on June 14, 2024: contributor
    5f549c35d9...1245d1388b: rebase due to (silent) merge conflict
  28. DrahtBot requested review from achow101 on Jun 14, 2024
  29. DrahtBot requested review from theStack on Jun 14, 2024
  30. tdb3 approved
  31. tdb3 commented at 2:04 pm on June 14, 2024: contributor
    re ACK 1245d1388b003c46092937def7041917aecec8de
  32. DrahtBot removed the label CI failed on Jun 14, 2024
  33. theStack approved
  34. theStack commented at 10:18 am on June 18, 2024: contributor
    re-ACK 1245d1388b003c46092937def7041917aecec8de
  35. achow101 commented at 5:24 pm on June 20, 2024: member
    ACK 1245d1388b003c46092937def7041917aecec8de
  36. achow101 merged this on Jun 20, 2024
  37. achow101 closed this on Jun 20, 2024

  38. vasild deleted the branch on Jun 21, 2024

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: 2024-09-28 22:12 UTC

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