-
Notifications
You must be signed in to change notification settings - Fork 36.2k
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
Torcontrol opt check #25136
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. ConflictsReviewers, this pull request conflicts with the following ones:
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. |
69d4d76
to
f48c207
Compare
Concept ACK, thanks for working on this! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approach ACK
@amadeuszpawlik want to follow up here now that #22087 has been merged? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK
e0a81da
to
7ac9da5
Compare
Thanks a lot @vasild for your review; applied your proposed changes |
@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
7ac9da5
to
5be4336
Compare
There was a problem hiding this 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()
.
switch (ret) { | ||
case NetworkAddrError::OK: |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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
error = Untranslated("No hostname:port specified in torcontrol"); | |
error = Untranslated("No hostname:port specified in -torcontrol"); |
@amadeuszpawlik are you planning on addressing any of the feedback here? |
@amadeuszpawlik @fanquake May I take over and wrap this one up? |
@vstoyanov No need to ask for permission to contribute. I'd say go for it. |
Based on
SplitHostPort
from #22087-torcontrol
for a valid host:port stringStartTorControl
so that we can stop init and present a message iftorcontrol
input is invalidSolves #23589