Skip to content

Commit

Permalink
[cli] Update command "borderagent discovery" to allow network interfa…
Browse files Browse the repository at this point in the history
…ce selection for mDNS binding.

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

This change introduces a new option for the CLI command `borderagent
dicover`. Users can now specify a network interface using the syntax
`borderagent discovery <timeout> <network interface>`. The chosen
interface will be used for mDNS binding through socket options.
  • Loading branch information
ZhangLe2016 committed Apr 12, 2024
1 parent 0d6cfcc commit ad55466
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/app/br_discover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include <chrono>
#include <thread>
#include <sys/socket.h>

#include "common/error_macros.hpp"
#include "common/utils.hpp"
Expand All @@ -38,7 +39,7 @@ namespace ot {

namespace commissioner {

Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout)
Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout, const std::string &aNetIf)
{
static constexpr size_t kDefaultBufferSize = 1024 * 16;
static constexpr mdns_record_type_t kMdnsQueryType = MDNS_RECORDTYPE_PTR;
Expand All @@ -52,6 +53,10 @@ Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeou
int socket = mdns_socket_open_ipv4();
VerifyOrExit(socket >= 0, error = ERROR_IO_ERROR("failed to open mDNS IPv4 socket"));

if (aNetIf != "" && setsockopt(socket, SOL_SOCKET, SO_BINDTODEVICE, &aNetIf[0], sizeof(aNetIf)) < 0) {
ExitNow(error = ERROR_IO_ERROR("failed to bind network interface: {}", aNetIf));
}

if (mdns_query_send(socket, kMdnsQueryType, kServiceName, strlen(kServiceName), buf, sizeof(buf)) != 0)
{
ExitNow(error = ERROR_IO_ERROR("failed to send mDNS query"));
Expand Down
4 changes: 3 additions & 1 deletion src/app/br_discover.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ using BorderAgentHandler = std::function<void(const BorderAgent *aBorderAgent, c
* @param[in] aBorderAgentHandler The handler of a single Border Agent response.
* @param[in] aTimeout The time to wait for mDNS responses. Any
* response not within the interval is ignored.
* @param[in] aNetIf The specified network interface for mDNS binding.
*
*
*/
Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout);
Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout, const std::string &aNetIf);

} // namespace commissioner

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 @@ -377,7 +377,7 @@ The command `borderagent` provides access to Border Agent information and scans
```shell
> help borderagent
usage:
borderagent discover [<timeout-in-milliseconds>]
borderagent discover [<timeout-in-milliseconds>] [<specified-network-interface>]
borderagent get locator
[done]
>
Expand Down
8 changes: 7 additions & 1 deletion src/app/cli/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1793,8 +1793,14 @@ Interpreter::Value Interpreter::ProcessBorderAgent(const Expression &aExpr)
{
SuccessOrExit(value = ParseInteger(timeout, aExpr[2]));
}
std::string netIf = "";

SuccessOrExit(value = DiscoverBorderAgent(BorderAgentHandler, static_cast<size_t>(timeout)));
if (aExpr.size() == 4)
{
netIf = aExpr[3];
}

SuccessOrExit(value = DiscoverBorderAgent(BorderAgentHandler, static_cast<size_t>(timeout), netIf));
}
else if (CaseInsensitiveEqual(aExpr[1], "get"))
{
Expand Down

0 comments on commit ad55466

Please sign in to comment.