Skip to content

Commit

Permalink
Merge branch 'main' into remove-panid-struct
Browse files Browse the repository at this point in the history
  • Loading branch information
wgtdkp authored May 7, 2024
2 parents dbfa960 + 8390f0c commit e840da8
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 159 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ Makefile.in
/build
cmake-build-*

# Test
/tmp

#tools
.vscode
.idea
Expand Down
4 changes: 2 additions & 2 deletions src/app/border_agent_functions_mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ void ClearBorderAgentFunctionsMock()
gBorderAgentFunctionsMock = nullptr;
}

Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout)
Error DiscoverBorderAgent(BorderAgentHandler aBorderAgentHandler, size_t aTimeout, const std::string &aNetIf)
{
return gBorderAgentFunctionsMock->DiscoverBorderAgent(aBorderAgentHandler, aTimeout);
return gBorderAgentFunctionsMock->DiscoverBorderAgent(aBorderAgentHandler, aTimeout, aNetIf);
}

} // namespace commissioner
Expand Down
2 changes: 1 addition & 1 deletion src/app/border_agent_functions_mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class BorderAgentFunctionsMock
public:
virtual ~BorderAgentFunctionsMock() = default;

MOCK_METHOD(Error, DiscoverBorderAgent, (BorderAgentHandler, size_t));
MOCK_METHOD(Error, DiscoverBorderAgent, (BorderAgentHandler, size_t, const std::string &));
};

void SetBorderAgentFunctionsMock(ot::commissioner::BorderAgentFunctionsMock *ptr);
Expand Down
19 changes: 18 additions & 1 deletion src/app/br_discover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
#include "br_discover.hpp"

#include <chrono>
#ifdef __linux__
#include <sys/socket.h>
#else // __NetBSD__ || __FreeBSD__ || __APPLE__
#include <netinet/in.h>
#endif
#include <thread>

#include "common/error_macros.hpp"
Expand All @@ -38,20 +43,32 @@ 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;
static const char *kServiceName = "_meshcop._udp.local";

Error error;
uint8_t buf[kDefaultBufferSize];
int rval = 0;

auto begin = std::chrono::system_clock::now();

int socket = mdns_socket_open_ipv4();
VerifyOrExit(socket >= 0, error = ERROR_IO_ERROR("failed to open mDNS IPv4 socket"));

if (!aNetIf.empty())
{
#ifdef __linux__
rval = setsockopt(socket, SOL_SOCKET, SO_BINDTODEVICE, aNetIf.c_str(), aNetIf.size());
#else // __NetBSD__ || __FreeBSD__ || __APPLE__
rval = setsockopt(socket, IPPROTO_IPV6, IP_BOUND_IF, aNetIf.c_str(), aNetIf.size());
#endif // __linux__
VerifyOrExit(rval == 0,
error = ERROR_INVALID_ARGS("failed to bind network interface {}: {}", aNetIf, strerror(errno)));
}

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. Can be an empty string
*
*
*/
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
27 changes: 21 additions & 6 deletions src/app/cli/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@
#include <vector>

#include <fcntl.h>
#ifdef __linux__
#include <sys/socket.h>
#else // __NetBSD__ || __FreeBSD__ || __APPLE__
#include <netinet/in.h>
#endif
#include <sys/types.h>
#include <unistd.h>

Expand Down Expand Up @@ -112,8 +116,6 @@
#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 @@ -1420,6 +1422,7 @@ Interpreter::Value Interpreter::ProcessBr(const Expression &aExpr)
std::vector<BorderAgentOrErrorMsg> borderAgents;
nlohmann::json baJson;
char mdnsSendBuffer[kMdnsBufferSize];
int rval = 0;

auto it = std::find(mContext.mCommandKeys.begin(), mContext.mCommandKeys.end(), "--timeout");
if (it != mContext.mCommandKeys.end())
Expand Down Expand Up @@ -1451,9 +1454,15 @@ Interpreter::Value Interpreter::ProcessBr(const Expression &aExpr)
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)
if (!netIf.empty())
{
ExitNow(value = ERROR_INVALID_ARGS("failed to bind network interface {}: {}", netIf, strerror(errno)));
#ifdef __linux__
rval = setsockopt(mdnsSocket, SOL_SOCKET, SO_BINDTODEVICE, netIf.c_str(), netIf.size());
#else // __NetBSD__ || __FreeBSD__ || __APPLE__
rval = setsockopt(mdnsSocket, IPPROTO_IPV6, IP_BOUND_IF, netIf.c_str(), netIf.size());
#endif // __linux__
VerifyOrExit(rval == 0,
value = ERROR_INVALID_ARGS("failed to bind network interface {}: {}", netIf, strerror(errno)));
}

fdgMdnsSocket.mFD = mdnsSocket;
Expand Down Expand Up @@ -1796,14 +1805,20 @@ Interpreter::Value Interpreter::ProcessBorderAgent(const Expression &aExpr)

if (CaseInsensitiveEqual(aExpr[1], "discover"))
{
uint64_t timeout = 4000;
uint64_t timeout = 4000;
std::string netIf = "";

if (aExpr.size() >= 3)
{
SuccessOrExit(value = ParseInteger(timeout, aExpr[2]));
}

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
Loading

0 comments on commit e840da8

Please sign in to comment.