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

[cli] update br scan to allow network interface selection for mDNS binding #260

Merged
merged 16 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
6269cc5
[cli] Update command "br scan" to allow network interface selection for
ZhangLe2016 Apr 12, 2024
688867a
[cli] Update command "br scan" to allow network interface selection for
ZhangLe2016 Apr 12, 2024
6a068ab
[cli] Update command "br scan" to allow network interface selection for
ZhangLe2016 Apr 12, 2024
f364c98
[cli] Update command "br scan" to allow network interface selection for
ZhangLe2016 Apr 12, 2024
7960fc4
[cli] Update command "br scan" to allow network interface selection for
ZhangLe2016 Apr 12, 2024
14235a0
[cli] Update command "br scan" to allow network interface selection for
ZhangLe2016 Apr 12, 2024
80f2148
[cli] Update command "br scan" to allow network interface selection for
ZhangLe2016 Apr 12, 2024
73adfc2
[cli] Update command "br scan" to allow network interface selection for
ZhangLe2016 Apr 12, 2024
0ba74b9
[cli] Update command "br scan" to allow network interface selection for
ZhangLe2016 Apr 12, 2024
8e1b077
[cli] Update command "br scan" to allow network interface selection for
ZhangLe2016 Apr 12, 2024
bc053b4
Update src/app/cli/interpreter.cpp
ZhangLe2016 Apr 15, 2024
1931627
[cli] Update command "br scan" to allow network interface selection for
ZhangLe2016 Apr 12, 2024
b2f0328
[cli] Update command "br scan" to allow network interface selection for
ZhangLe2016 Apr 12, 2024
8f56b8f
[cli] Update command "br scan" to allow network interface selection for
ZhangLe2016 Apr 12, 2024
8299f80
[cli] Update command "br scan" to allow network interface selection for
ZhangLe2016 Apr 12, 2024
d70c564
Update include/commissioner/error.hpp
wgtdkp Apr 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/commissioner/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ enum class ErrorCode : int
* The error is out of the address space of OT Commissioner.
*/
kUnknown = 19,

wgtdkp marked this conversation as resolved.
Show resolved Hide resolved
wgtdkp marked this conversation as resolved.
Show resolved Hide resolved
/**
* The error occurs when attempting to bind a network interface to an mDNS socket."
*/
kSocketBindError = 20,
wgtdkp marked this conversation as resolved.
Show resolved Hide resolved
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/app/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ Each advanced command always sends associative `MGMT_*_(GET|SET).req` requests.
```shell
> help br
usage:
br scan [--nwk <network-alias-list> | --dom <domain-name>] [--export <json-file-path>] [--timeout <ms>]
br scan [--nwk <network-alias-list> | --dom <domain-name>] [--export <json-file-path>] [--timeout <ms>] [--netif <network-interface>]
br add <json-file-path>
br list [--nwk <network-alias-list> | --dom <domain-name>]
br delete (<br-record-id> | --nwk <network-alias-list> | --dom <domain-name>)
Expand Down
44 changes: 38 additions & 6 deletions src/app/cli/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <vector>

#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

Expand Down Expand Up @@ -111,6 +112,8 @@
#define WARN_NETWORK_SELECTION_DROPPED "Network selection was dropped by the command"
#define WARN_NETWORK_SELECTION_CHANGED "Network selection was changed by the command"

#define SO_BINDTODEVICE 25

#define COLOR_ALIAS_FAILED Console::Color::kYellow

namespace ot {
Expand Down Expand Up @@ -213,7 +216,8 @@ const std::map<std::string, std::string> &Interpreter::mUsageMap = *new std::map
{"br", "br list [--nwk <network-alias-list> | --dom <domain-name>]\n"
"br add <json-file-path>\n"
"br delete (<br-record-id> | --nwk <network-alias-list> | --dom <domain-name>)\n"
"br scan [--nwk <network-alias-list> | --dom <domain-name>] [--export <json-file-path>] [--timeout <ms>]\n"},
"br scan [--nwk <network-alias-list> | --dom <domain-name>] [--export <json-file-path>] [--timeout <ms>] "
"[--netif <network-interface>]\n"},
{"domain", "domain list [--dom <domain-name>]"},
{"network", "network save <network-data-file>\n"
"network sync\n"
Expand Down Expand Up @@ -1421,6 +1425,7 @@ Interpreter::Value Interpreter::ProcessBr(const Expression &aExpr)
const std::string kServiceName = "_meshcop._udp.local";

uint32_t scanTimeout = 10000;
std::string netIf = "";
int mdnsSocket = -1;
FDGuard fdgMdnsSocket;
std::thread selectThread;
Expand All @@ -1432,20 +1437,47 @@ Interpreter::Value Interpreter::ProcessBr(const Expression &aExpr)
nlohmann::json baJson;
char mdnsSendBuffer[kMdnsBufferSize];

if (mContext.mCommandKeys.size() == 2 && mContext.mCommandKeys[0] == "--timeout")
for (auto it = mContext.mCommandKeys.begin(); it != mContext.mCommandKeys.end(); ++it)
{
try
if (*it == "--timeout")
{
scanTimeout = stol(mContext.mCommandKeys[1]);
} catch (...)
if (++it != mContext.mCommandKeys.end())
{
try
{
scanTimeout = stol(*it);
} catch (...)
{
ExitNow(value = ERROR_INVALID_ARGS("Imparsable timeout value '{}'", *it));
}
wgtdkp marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
ExitNow(value = ERROR_INVALID_ARGS("Missing {} value", *--it));
}
}
else if (*it == "--netif")
{
ExitNow(value = ERROR_INVALID_ARGS("Imparsable timeout value '{}'", aExpr[3]));
if (++it != mContext.mCommandKeys.end())
{
netIf = *it;
}
else
{
ExitNow(value = ERROR_INVALID_ARGS("Missing {} value", *--it));
}
}
}
wgtdkp marked this conversation as resolved.
Show resolved Hide resolved

// Open IPv4 mDNS socket
mdnsSocket = mdns_socket_open_ipv4();
VerifyOrExit(mdnsSocket >= 0, value = ERROR_IO_ERROR("failed to open mDNS IPv4 socket"));

if (!netIf.empty() && setsockopt(mdnsSocket, SOL_SOCKET, SO_BINDTODEVICE, netIf.c_str(), netIf.size()) < 0)
{
ExitNow(value = ERROR_SOCKET_BIND_ERROR("failed to bind network interface {}: {}", netIf, strerror(errno)));
ZhangLe2016 marked this conversation as resolved.
Show resolved Hide resolved
}

fdgMdnsSocket.mFD = mdnsSocket;

// Initialize event library
Expand Down
2 changes: 2 additions & 0 deletions src/common/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ static std::string ErrorCodeToString(ErrorCode code)
return "REGISTRY_ERROR";
case ErrorCode::kUnknown:
return "UNKNOWN";
case ErrorCode::kSocketBindError:
return "SOCKET_BIND_ERROR";

default:
VerifyOrDie(false);
Expand Down
5 changes: 5 additions & 0 deletions src/common/error_macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,10 @@
{ \
ErrorCode::kUnknown, fmt::format(FMT_STRING((aFormat)), ##__VA_ARGS__) \
}
#define ERROR_SOCKET_BIND_ERROR(aFormat, ...) \
Error \
{ \
ErrorCode::kSocketBindError, fmt::format(FMT_STRING((aFormat)), ##__VA_ARGS__) \
}

#endif // ERROR_MACROS_HPP_
Loading