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] remove definition of SO_BINDTODEVICE from interpreter module #263

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Changes from all commits
Commits
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
17 changes: 13 additions & 4 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 @@ -1436,6 +1438,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 @@ -1467,9 +1470,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
Loading