p2p, refactor: tidy up LookupSubNet() #23219

pull jonatack wants to merge 2 commits into bitcoin:master from jonatack:LookupSubNet-todo-fix changing 3 files +32 −35
  1. jonatack commented at 1:11 PM on October 7, 2021: member

    This pull originally resolved a code TO-DO, as well as fixing different param names between the function declaration and definition, updating the function to current style standards, clearer variable naming, and improving the Doxygen documentation.

    Following the merge of #17160, it now does the non-TODO changes and also now drops an unused param to simplify the function.

  2. jonatack commented at 1:12 PM on October 7, 2021: member

    The resolved TODO is from c7f6ce74d3a5cf2a0 by @dongcarl.

        // TODO: Use LookupHost(const char *, CNetAddr&, bool) instead to just get
        //       one CNetAddr.
    
  3. in src/netbase.cpp:695 in 7f146abdef outdated
     712 | -                // Never allow lookup for netmask
     713 | -                if (LookupHost(strNetmask, vIP, 1, false, dns_lookup_function)) {
     714 | -                    ret = CSubNet(network, vIP[0]);
     715 | +            } else {
     716 | +                // Invalid number; try full netmask syntax. Never allow lookup for netmask.
     717 | +                CNetAddr full_netmask{addr};
    


    vasild commented at 1:28 PM on October 7, 2021:

    This variable is used as an output parameter. Need not be set to addr.

                    CNetAddr full_netmask;
    

    jonatack commented at 2:09 PM on October 7, 2021:

    indeed! done

  4. in src/netbase.cpp:681 in 7f146abdef outdated
     692 | -            std::string strNetmask = strSubnet.substr(slash + 1);
     693 | -            uint8_t n;
     694 | -            if (ParseUInt8(strNetmask, &n)) {
     695 | -                // If valid number, assume CIDR variable-length subnet masking
     696 | -                ret = CSubNet(network, n);
     697 | +    const size_t slash{strSubnet.find_last_of('/')};
    


    vasild commented at 1:31 PM on October 7, 2021:

    Maybe also rename the parameter strSubnet?


    jonatack commented at 2:08 PM on October 7, 2021:

    done, plus same param naming between function declaration and definition (subnet vs ret -> just subnet), and updated the doxygen

  5. in src/netbase.cpp:696 in 7f146abdef outdated
     713 | -                if (LookupHost(strNetmask, vIP, 1, false, dns_lookup_function)) {
     714 | -                    ret = CSubNet(network, vIP[0]);
     715 | +            } else {
     716 | +                // Invalid number; try full netmask syntax. Never allow lookup for netmask.
     717 | +                CNetAddr full_netmask{addr};
     718 | +                if (LookupHost(/*name=*/str_netmask, /*addr=*/full_netmask, /*fAllowLookup=*/false, dns_lookup_function)) {
    


    vasild commented at 1:41 PM on October 7, 2021:

    IMO /*name=*/ and /*addr=*/ is excessive here. I can understand using these comments in places where constants are passed, e.g.

    func(5, true);
    

    may be better as

    func(/*timeout=*/5, /*allow_lookup=*/true);
    

    But when using variables that have their own names, /*foo=*/ should not be necessary:

    int timeout = ...
    bool allow_lookup = ...
    // good enough
    func(timout, allow_lookup);
    

    jonatack commented at 2:07 PM on October 7, 2021:

    done

  6. shaavan commented at 1:58 PM on October 7, 2021: contributor

    Concept ACK

  7. jonatack force-pushed on Oct 7, 2021
  8. jonatack commented at 2:21 PM on October 7, 2021: member

    Thanks @vasild, updated with your feedback per git diff 7f146ab aa71b3c

  9. jonatack renamed this:
    p2p: remove unneeded CNetAddr vector in LookupSubNet() and update/tidy up
    p2p: rm CNetAddr vector and copy in LookupSubNet() and update/tidy up
    on Oct 7, 2021
  10. jonatack force-pushed on Oct 7, 2021
  11. vasild approved
  12. vasild commented at 2:38 PM on October 7, 2021: member

    ACK aa71b3c88ffadf7250a5bacd20d65bf9cc2f1267

  13. DrahtBot commented at 5:18 PM on October 7, 2021: member

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    No conflicts as of last run.

  14. DrahtBot added the label P2P on Oct 7, 2021
  15. stratospher commented at 5:53 PM on October 8, 2021: contributor

    tested ACK aa71b3c

    Agree that the CNetAddr vector is unnecessary in src/netbase.cpp. The comments and variable name updates look great too! Confirmed that the changes don’t affect the unit tests which include the netbase.h header file. The tests run successfully:

    • addrman_tests.cpp
    • net_tests.cpp
    • netbase_tests.cpp
  16. fanquake commented at 1:21 AM on October 11, 2021: member

    The same TODO is being solved in #17160.

  17. laanwj commented at 7:01 PM on December 6, 2021: member

    Needs rebase after #17160

  18. DrahtBot added the label Needs rebase on Dec 6, 2021
  19. jonatack force-pushed on Dec 7, 2021
  20. jonatack renamed this:
    p2p: rm CNetAddr vector and copy in LookupSubNet() and update/tidy up
    p2p: tidy up LookupSubNet()
    on Dec 7, 2021
  21. jonatack commented at 11:01 AM on December 7, 2021: member

    Thanks, rebased and updated the title and description.

  22. in src/netbase.h:179 in 49183e4651 outdated
     180 | + * @param[out] subnet_out  Internal subnet representation, if parsable/resolvable
     181 | + *                         from `subnet_str`.
     182 | + * @returns whether the operation succeeded or not.
     183 |   */
     184 | -bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet, DNSLookupFn dns_lookup_function = g_dns_lookup);
     185 | +bool LookupSubNet(const std::string& subnet_str, CSubNet& subnet_out, DNSLookupFn dns_lookup_function = g_dns_lookup);
    


    vasild commented at 11:21 AM on December 7, 2021:

    The third argument looks unnecessary: DNSLookupFn dns_lookup_function = g_dns_lookup. All the callers use just 2 arguments but more importantly, this function calls LookupHost(..., /*fAllowLookup=*/false, dns_lookup_function) so it does not allow lookups. I think it might as well call just LookupHost(..., /*fAllowLookup=*/false); and ditch its 3rd argument.


    jonatack commented at 11:58 AM on December 7, 2021:

    Good point. Done.

  23. vasild approved
  24. vasild commented at 11:22 AM on December 7, 2021: member

    ACK 49183e465100715d44f91ca865432ddf71de14fe

  25. jonatack renamed this:
    p2p: tidy up LookupSubNet()
    p2p, refactor: tidy up LookupSubNet()
    on Dec 7, 2021
  26. jonatack commented at 12:12 PM on December 7, 2021: member

    Rebased before the merge causing OOM in the CI and dropped an uneeded param per #23219 (review).

  27. jonatack force-pushed on Dec 7, 2021
  28. p2p, refactor: tidy up LookupSubNet()
    - consistent param naming between function declaration and definition
    - brackets, param naming and localvar naming per current standards
      in doc/developer-notes.md
    - update/improve doxygen documentation in the declaration
    - improve comments and other localvar names
    - constness
    - named args
    f0c9e68080
  29. p2p, refactor: drop unused DNSLookupFn param in LookupSubnet() c44c20108f
  30. jonatack force-pushed on Dec 7, 2021
  31. DrahtBot removed the label Needs rebase on Dec 7, 2021
  32. vasild approved
  33. vasild commented at 1:29 PM on December 7, 2021: member

    ACK c44c20108f7b7314f59f034110789385a24db280

  34. in src/test/fuzz/netbase_dns_lookup.cpp:67 in c44c20108f
      63 | @@ -64,7 +64,7 @@ FUZZ_TARGET(netbase_dns_lookup)
      64 |      }
      65 |      {
      66 |          CSubNet resolved_subnet;
      67 | -        if (LookupSubNet(name, resolved_subnet, fuzzed_dns_lookup_function)) {
      68 | +        if (LookupSubNet(name, resolved_subnet)) {
    


    laanwj commented at 12:55 PM on December 8, 2021:

    What is the reasoning behind this parameter no longer being needed?


    vasild commented at 1:40 PM on December 8, 2021:

    It is here: #23219 (review), LookupSubNet() never does lookups.

  35. in src/netbase.cpp:679 in c44c20108f
     675 | @@ -676,40 +676,36 @@ bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, uin
     676 |      return true;
     677 |  }
     678 |  
     679 | -bool LookupSubNet(const std::string& strSubnet, CSubNet& ret, DNSLookupFn dns_lookup_function)
     680 | +bool LookupSubNet(const std::string& subnet_str, CSubNet& subnet_out)
    


    laanwj commented at 12:56 PM on December 8, 2021:

    Thinking of it, a better API would be to return option<CSubNet> instead of a bool and output parameter. But not necessarily in this PR. It's more consistent with the other functions in netbase.h as it is now.


    jonatack commented at 4:05 PM on December 8, 2021:

    Good idea. I have a branch of netbase follow-ups, could do it there.

  36. dunxen commented at 6:43 AM on December 16, 2021: contributor

    ACK c44c201

  37. shaavan approved
  38. shaavan commented at 7:14 AM on December 16, 2021: contributor

    crACK c44c20108f7b7314f59f034110789385a24db280

  39. laanwj merged this on Dec 18, 2021
  40. laanwj closed this on Dec 18, 2021

  41. jonatack deleted the branch on Dec 18, 2021
  42. sidhujag referenced this in commit 08c502ff3c on Dec 18, 2021
  43. DrahtBot locked this on Dec 18, 2022

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: 2026-04-14 21:13 UTC

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