Skip to content

Commit

Permalink
[cli] Update command "br scan" to allow network interface selection for
Browse files Browse the repository at this point in the history
mDNS binding.

Within the OTBR practice application, the BR host will have several
interfaces. When users want to discover the border router using mDNS,
they will need to choose the specific interface.

This change introduces a new option for the CLI command `br scan`. User
can now specify a network interface using the syntax `br scan --netif
<network interface>. The chosen interface will be used for mDNS binding
through socket options.
  • Loading branch information
ZhangLe2016 committed Apr 12, 2024
1 parent 6269cc5 commit 688867a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
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,

/**
* The error occurs when attempting to bind a network interface to an mDNS socket."
*/
kSocketBindError = 20,
};

/**
Expand Down
20 changes: 11 additions & 9 deletions src/app/cli/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1433,28 +1433,30 @@ Interpreter::Value Interpreter::ProcessBr(const Expression &aExpr)
nlohmann::json baJson;
char mdnsSendBuffer[kMdnsBufferSize];

if (mContext.mCommandKeys.size() == 2 && mContext.mCommandKeys[0] == "--timeout")
{
auto it = std::find(mContext.mCommandKeys.begin(), mContext.mCommandKeys.end(), "--timeout");

if (it != mContext.mCommandKeys.end()) {
try
{
scanTimeout = stol(mContext.mCommandKeys[1]);
scanTimeout = stol(mContext.mCommandKeys[std::distance(mContext.mCommandKeys.begin(), it) + 1]);
} catch (...)
{
ExitNow(value = ERROR_INVALID_ARGS("Imparsable timeout value '{}'", aExpr[3]));
ExitNow(value = ERROR_INVALID_ARGS("Imparsable timeout value '{}'", mContext.mCommandKeys[std::distance(mContext.mCommandKeys.begin(), it)]));
}
}

if (mContext.mCommandKeys.size() == 2 && mContext.mCommandKeys[0] == "--netif")
{
netIf = mContext.mCommandKeys[1];
it = std::find(mContext.mCommandKeys.begin(), mContext.mCommandKeys.end(), "--netif");

if (it != mContext.mCommandKeys.end()) {
netIf = mContext.mCommandKeys[std::distance(mContext.mCommandKeys.begin(), it) + 1];
}

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

if (netIf != "" && setsockopt(mdnsSocket, SOL_SOCKET, SO_BINDTODEVICE, &netIf[0], sizeof(netIf)) < 0) {
ExitNow(value = ERROR_IO_ERROR("failed to bind network interface: {}", netIf));
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));
}

fdgMdnsSocket.mFD = mdnsSocket;
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_

0 comments on commit 688867a

Please sign in to comment.