From ad55466dc516b0c22e2670bfd5584bdde1e6baea Mon Sep 17 00:00:00 2001 From: lezhan Date: Thu, 11 Apr 2024 16:04:02 +0000 Subject: [PATCH] [cli] Update command "borderagent discovery" to allow network interface 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 `. The chosen interface will be used for mDNS binding through socket options. --- src/app/br_discover.cpp | 7 ++++++- src/app/br_discover.hpp | 4 +++- src/app/cli/README.md | 2 +- src/app/cli/interpreter.cpp | 8 +++++++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/app/br_discover.cpp b/src/app/br_discover.cpp index f0928756a..5edde0913 100644 --- a/src/app/br_discover.cpp +++ b/src/app/br_discover.cpp @@ -30,6 +30,7 @@ #include #include +#include #include "common/error_macros.hpp" #include "common/utils.hpp" @@ -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; @@ -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")); diff --git a/src/app/br_discover.hpp b/src/app/br_discover.hpp index 91273b95f..79e660572 100644 --- a/src/app/br_discover.hpp +++ b/src/app/br_discover.hpp @@ -56,9 +56,11 @@ using BorderAgentHandler = std::function help borderagent usage: -borderagent discover [] +borderagent discover [] [] borderagent get locator [done] > diff --git a/src/app/cli/interpreter.cpp b/src/app/cli/interpreter.cpp index 346cc2ea5..722a8c960 100644 --- a/src/app/cli/interpreter.cpp +++ b/src/app/cli/interpreter.cpp @@ -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(timeout))); + if (aExpr.size() == 4) + { + netIf = aExpr[3]; + } + + SuccessOrExit(value = DiscoverBorderAgent(BorderAgentHandler, static_cast(timeout), netIf)); } else if (CaseInsensitiveEqual(aExpr[1], "get")) {