Torcontrol opt check #25136

pull amadeuszpawlik wants to merge 2 commits into bitcoin:master from amadeuszpawlik:torcontrol_opt_check changing 6 files +93 −5
  1. amadeuszpawlik commented at 8:56 pm on May 14, 2022: contributor

    Based on SplitHostPort from #22087

    • Adds new utility for easy sanity check of network addresses
    • Checks -torcontrol for a valid host:port string
    • Adds a return param in StartTorControl so that we can stop init and present a message if torcontrol input is invalid

    Solves #23589

  2. DrahtBot added the label Refactoring on May 14, 2022
  3. DrahtBot commented at 5:37 am on May 15, 2022: contributor

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

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK vasild
    Concept ACK laanwj
    Approach ACK w0xlt

    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:

    • #26261 (p2p: cleanup LookupIntern, Lookup and LookupHost by brunoerg)

    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.

  4. in src/util/network.cpp:13 in 69d4d7679d outdated
     8+
     9+SocketAddrError hasValidHostPort(std::string_view socketAddr)
    10+{
    11+    uint16_t port{0};
    12+    std::string hostname;
    13+    bool validPort = SplitHostPort(socketAddr, port, hostname);
    


    jonatack commented at 6:12 pm on May 24, 2022:

    Build error here

    0util/network.cpp:13:10: error: cannot initialize a variable of type 'bool' with an rvalue of type 'void'
    1    bool validPort = SplitHostPort(socketAddr, port, hostname);
    2         ^           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    31 error generated.
    

    amadeuszpawlik commented at 8:54 pm on May 26, 2022:
    thanks @jonatack, forgot a commit
  5. in src/util/network.cpp:19 in 69d4d7679d outdated
    14+
    15+    SocketAddrError res = SocketAddrError::OK;
    16+    if (!validPort || port == 0)
    17+        res = SocketAddrError::NO_PORT;
    18+    if (hostname.empty() || hostname.find(' ') != std::string::npos)
    19+        res = (res == SocketAddrError::NO_PORT) ? SocketAddrError::NO_HOSTPORT 
    


    jonatack commented at 6:13 pm on May 24, 2022:
    0        res = (res == SocketAddrError::NO_PORT) ? SocketAddrError::NO_HOSTPORT
    

    to appease the whitespace linter

    0+        res = (res == SocketAddrError::NO_PORT) ? SocketAddrError::NO_HOSTPORT 
    1^---- failure generated from lint-whitespace.py
    
  6. amadeuszpawlik force-pushed on May 26, 2022
  7. amadeuszpawlik marked this as ready for review on May 27, 2022
  8. laanwj commented at 1:59 pm on June 7, 2022: member
    Concept ACK, thanks for working on this!
  9. w0xlt commented at 2:25 pm on June 7, 2022: contributor
    Approach ACK
  10. fanquake commented at 1:01 am on October 12, 2022: member
    @amadeuszpawlik want to follow up here now that #22087 has been merged?
  11. DrahtBot added the label Needs rebase on Oct 12, 2022
  12. achow101 commented at 7:21 pm on October 12, 2022: member
  13. in src/util/strencodings.cpp:116 in f48c207c99 outdated
    116+              valid = (portOut != 0);
    117+            }
    118         }
    119+        in = in.substr(0, colon);
    120+    } else {
    121+      valid = true;
    


    vasild commented at 10:32 am on October 13, 2022:

    This contains in.substr(colon + 1) two times. Maybe put it in a variable (port_str?) to make the code a bit easier to read.

    There are a bunch of style issues. I would suggest to run clang-format or contrib/devtools/clang-format-diff.py on this.


    amadeuszpawlik commented at 7:36 pm on October 13, 2022:
    This is already fixed in #22087 (f8387c42343867779170a0f96ef64e6acff5c481) Rebasing this away
  14. in src/util/strencodings.h:100 in f48c207c99 outdated
    92+ * @param[in] in        The socket address string to split.
    93+ * @param[out] portOut  Port-portion of the input, if found and parsable.
    94+ * @param[out] hostOut  Host-portion of the input, if found.
    95+ * @return              true if port-portion is absent or within its allowed range, otherwise false
    96+ */
    97+bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut);
    


    vasild commented at 10:36 am on October 13, 2022:
    The parameters should use snake case, as per doc/developer-notes.md.
  15. in src/util/network.h:17 in f48c207c99 outdated
    12+    NO_HOST,
    13+    NO_PORT,
    14+    NO_HOSTPORT,
    15+};
    16+
    17+SocketAddrError hasValidHostPort(std::string_view socketAddr);
    


    vasild commented at 10:58 am on October 13, 2022:

    In commit d8f230435bd828a80c56c66d4e0a7db11fd627ce util: Adds network, socket address validation this is added like:

    0SocketAddrError hasValidHostPort(std::string_view socketAddr) {
    1
    2#endif // BITCOIN_UTIL_NETWORK_H
    

    which I guess will not compile. All commits should compile on their own.


    vasild commented at 11:12 am on October 13, 2022:
    I think there is no need to add a new file for this function. It can be put in netbase.cpp, looks in a similar category as e.g. ParseNetwork().

    vasild commented at 11:12 am on October 13, 2022:
    The name of the function should begin with a capital letter.

    amadeuszpawlik commented at 7:59 pm on October 13, 2022:
    Agreed
  16. in src/util/network.h:10 in f48c207c99 outdated
     5+#ifndef BITCOIN_UTIL_NETWORK_H
     6+#define BITCOIN_UTIL_NETWORK_H
     7+
     8+#include <string_view>
     9+
    10+enum class SocketAddrError {
    


    vasild commented at 11:17 am on October 13, 2022:
    The rest of the codebase is using just “address” or “network address” (to distinguish from a bitcoin address), or host. Introducing a new name for the same thing could be confusing.

    amadeuszpawlik commented at 7:36 pm on October 13, 2022:
    thanks
  17. vasild commented at 11:19 am on October 13, 2022: contributor
    Concept ACK
  18. amadeuszpawlik force-pushed on Oct 13, 2022
  19. amadeuszpawlik force-pushed on Oct 13, 2022
  20. amadeuszpawlik force-pushed on Oct 13, 2022
  21. amadeuszpawlik commented at 8:00 pm on October 13, 2022: contributor
    Thanks a lot @vasild for your review; applied your proposed changes
  22. DrahtBot removed the label Needs rebase on Oct 13, 2022
  23. fanquake commented at 3:35 am on October 14, 2022: member
    @amadeuszpawlik can you rebase again, to fix the failing CI (see #26306).
  24. util: Adds `network`, network address validation
    This utility implements the function `hasValidHostPort`, which conducts
    a sanity check for a valid host:port combination in a string.
    This can be used for rejection of invalid option strings. Rules for
    a valid hostname are: not empty and no spaces, for port: not empty
    and within `uint16` bounds.
    62dbf38972
  25. Ensure valid network address in `-torcontrol`
    - Returns error at init if `-torcontrol` is not a valid host:port combo.
    - `StartTorControl` returns a `bool`, and has a return parameter `error`
    where a suitable message is set at failure.
    
    Fixes #23589
    5be4336fc5
  26. amadeuszpawlik force-pushed on Oct 16, 2022
  27. in src/test/netbase_tests.cpp:91 in 5be4336fc5
    83@@ -84,6 +84,43 @@ BOOST_AUTO_TEST_CASE(netbase_properties)
    84 
    85 }
    86 
    87+bool static TestHasHostPort(const std::string& test, bool hasHost, bool hasPort)
    88+{
    89+    NetworkAddrError ret = HasValidHostPort(test);
    90+    switch (ret) {
    91+        case NetworkAddrError::OK:
    


    vasild commented at 9:55 am on October 24, 2022:

    style: case should be below switch

    It is good to use clang-format or contrib/devtools/clang-format-diff.py to avoid such suggestions.

  28. in src/torcontrol.cpp:663 in 5be4336fc5
    657@@ -658,23 +658,47 @@ static void TorControlThread(CService onion_service_target)
    658     event_base_dispatch(gBase);
    659 }
    660 
    661-void StartTorControl(CService onion_service_target)
    662+bool TorControlOptCheck(bilingual_str& error)
    663+{
    664+    NetworkAddrError eOpt = HasValidHostPort(gArgs.GetArg("-torcontrol", DEFAULT_TOR_CONTROL));
    


    vasild commented at 9:56 am on October 24, 2022:
    style: as per doc/developer-notes.md variables should use snake_case.
  29. in src/torcontrol.cpp:675 in 5be4336fc5
    671+            return false;
    672+        case NetworkAddrError::NO_PORT:
    673+            error = Untranslated("No port specified in -torcontrol");
    674+            return false;
    675+        case NetworkAddrError::NO_HOSTPORT:
    676+            error = Untranslated("No hostname:port specified in torcontrol");
    


    vasild commented at 9:58 am on October 24, 2022:

    nit: for consistency with above

    0            error = Untranslated("No hostname:port specified in -torcontrol");
    
  30. vasild approved
  31. vasild commented at 10:14 am on October 24, 2022: contributor

    ACK 5be4336fc5b0ca17fbd27998d5aa17bb83ab4d40

    The code looks ok, but I think it can be better: what about doing the checks in SplitHostPort() and not adding a new function HasValidHostPort()? The former already does some validity checks and it makes sense to do all checks in one place.

    Then the code would look something like this:

     0/**
     1 * Splits a host:port string into host and port.
     2 * Validates the host and the port.
     3 * 
     4 * [@param](/bitcoin-bitcoin/contributor/param/)[in] in    The string to split.
     5 * [@param](/bitcoin-bitcoin/contributor/param/)[out] host Host-portion of the input, if found and valid.
     6 * [@param](/bitcoin-bitcoin/contributor/param/)[out] port Port-portion of the input, if found and valid.
     7 */
     8void SplitHostPort(std::string_view in,
     9                   std::optional<std::string>& host,
    10                   std::optional<uint16_t>& port);
    11
    12...
    13
    14for (const auto& [option, port_required] :
    15     std::vector<std::tuple<const char*, bool>>{{"-i2psam", true},
    16                                                {"-onion", true},
    17                                                {"-proxy", true},
    18                                                {"-rpcbind", false},
    19                                                {"-torcontrol", true},
    20                                                {"-whitebind", false},
    21                                                {"-zmqpubhashblock", true},
    22                                                {"-zmqpubhashtx", true},
    23                                                {"-zmqpubrawblock", true},
    24                                                {"-zmqpubrawtx", true},
    25                                                {"-zmqpubsequence", true}}) {
    26    for (const std::string& str : args.GetArgs(option)) {
    27        std::optional<std::string> host;
    28        std::optional<uint16_t> port;
    29        SplitHostPort(str, host, port);
    30        if (!host.has_value()) {
    31            return InitError(InvalidHostErrMsg(option, str));
    32        }
    33        if (port_required && !port.has_value()) {
    34            return InitError(InvalidPortErrMsg(option, str));
    35        }
    36    }
    37}
    

    That would benefit/check also the other options, not just -torcontrol and there would not be a need to modify StartTorControl().

  32. fanquake commented at 12:38 pm on February 16, 2023: member
    @amadeuszpawlik are you planning on addressing any of the feedback here?
  33. vstoyanov commented at 1:04 pm on March 26, 2023: none
    @amadeuszpawlik @fanquake May I take over and wrap this one up?
  34. Empact commented at 9:11 pm on March 26, 2023: member
    @vstoyanov No need to ask for permission to contribute. I’d say go for it.
  35. fanquake added the label Up for grabs on Mar 27, 2023
  36. fanquake closed this on Mar 27, 2023

  37. bitcoin locked this on Mar 26, 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-12-04 06:12 UTC

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