Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Torcontrol opt check #25136

Closed

Conversation

amadeuszpawlik
Copy link
Contributor

@amadeuszpawlik amadeuszpawlik commented May 14, 2022

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

@DrahtBot
Copy link
Contributor

DrahtBot commented May 15, 2022

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.

src/util/network.cpp Outdated Show resolved Hide resolved
src/util/network.cpp Outdated Show resolved Hide resolved
@amadeuszpawlik amadeuszpawlik marked this pull request as ready for review May 27, 2022 06:07
@laanwj
Copy link
Member

laanwj commented Jun 7, 2022

Concept ACK, thanks for working on this!

Copy link
Contributor

@w0xlt w0xlt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approach ACK

@fanquake
Copy link
Member

@amadeuszpawlik want to follow up here now that #22087 has been merged?

@achow101
Copy link
Member

cc @vasild @ryanofsky @mzumsande

Copy link
Contributor

@vasild vasild left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

src/util/strencodings.cpp Outdated Show resolved Hide resolved
src/util/strencodings.h Outdated Show resolved Hide resolved
src/util/network.h Outdated Show resolved Hide resolved
src/util/network.h Outdated Show resolved Hide resolved
src/util/network.h Outdated Show resolved Hide resolved
src/util/network.h Outdated Show resolved Hide resolved
@amadeuszpawlik amadeuszpawlik force-pushed the torcontrol_opt_check branch 3 times, most recently from e0a81da to 7ac9da5 Compare October 13, 2022 19:59
@amadeuszpawlik
Copy link
Contributor Author

Thanks a lot @vasild for your review; applied your proposed changes

@fanquake
Copy link
Member

@amadeuszpawlik can you rebase again, to fix the failing CI (see #26306).

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.
- 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 bitcoin#23589
Copy link
Contributor

@vasild vasild left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 5be4336

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:

/**
 * Splits a host:port string into host and port.
 * Validates the host and the port.
 * 
 * @param[in] in    The string to split.
 * @param[out] host Host-portion of the input, if found and valid.
 * @param[out] port Port-portion of the input, if found and valid.
 */
void SplitHostPort(std::string_view in,
                   std::optional<std::string>& host,
                   std::optional<uint16_t>& port);

...

for (const auto& [option, port_required] :
     std::vector<std::tuple<const char*, bool>>{{"-i2psam", true},
                                                {"-onion", true},
                                                {"-proxy", true},
                                                {"-rpcbind", false},
                                                {"-torcontrol", true},
                                                {"-whitebind", false},
                                                {"-zmqpubhashblock", true},
                                                {"-zmqpubhashtx", true},
                                                {"-zmqpubrawblock", true},
                                                {"-zmqpubrawtx", true},
                                                {"-zmqpubsequence", true}}) {
    for (const std::string& str : args.GetArgs(option)) {
        std::optional<std::string> host;
        std::optional<uint16_t> port;
        SplitHostPort(str, host, port);
        if (!host.has_value()) {
            return InitError(InvalidHostErrMsg(option, str));
        }
        if (port_required && !port.has_value()) {
            return InitError(InvalidPortErrMsg(option, str));
        }
    }
}

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

Comment on lines +90 to +91
switch (ret) {
case NetworkAddrError::OK:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: case should be below switch

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

void StartTorControl(CService onion_service_target)
bool TorControlOptCheck(bilingual_str& error)
{
NetworkAddrError eOpt = HasValidHostPort(gArgs.GetArg("-torcontrol", DEFAULT_TOR_CONTROL));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: as per doc/developer-notes.md variables should use snake_case.

error = Untranslated("No port specified in -torcontrol");
return false;
case NetworkAddrError::NO_HOSTPORT:
error = Untranslated("No hostname:port specified in torcontrol");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: for consistency with above

Suggested change
error = Untranslated("No hostname:port specified in torcontrol");
error = Untranslated("No hostname:port specified in -torcontrol");

@fanquake
Copy link
Member

@amadeuszpawlik are you planning on addressing any of the feedback here?

@vstoyanov
Copy link

@amadeuszpawlik @fanquake May I take over and wrap this one up?

@Empact
Copy link
Member

Empact commented Mar 26, 2023

@vstoyanov No need to ask for permission to contribute. I'd say go for it.

@fanquake fanquake closed this Mar 27, 2023
@bitcoin bitcoin locked and limited conversation to collaborators Mar 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants