Skip to content

Commit

Permalink
Merge pull request #103 from microsoft/nlstate
Browse files Browse the repository at this point in the history
Replace spot-use of runtime nl80211 protocol data with Nl80211ProtocolState helper
  • Loading branch information
abeltrano authored Jan 11, 2024
2 parents 3bd0c94 + be3c4c0 commit 1195467
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
#include <string_view>

#include <linux/if.h>
#include <linux/if_link.h>
#include <linux/rtnetlink.h>
#include <magic_enum.hpp>
#include <microsoft/net/netlink/nl80211/Netlink80211.hxx>
#include <microsoft/net/wifi/AccessPoint.hxx>
#include <microsoft/net/wifi/AccessPointDiscoveryAgentOperationsNetlink.hxx>
#include <microsoft/net/wifi/IAccessPoint.hxx>
#include <netlink/genl/ctrl.h>
#include <netlink/genl/genl.h>
#include <netlink/handlers.h>
#include <netlink/msg.h>
#include <netlink/netlink.h>
#include <netlink/socket.h>
#include <notstd/Scope.hxx>
#include <plog/Log.h>
#include <sys/epoll.h>
Expand All @@ -28,9 +27,11 @@ using namespace Microsoft::Net::Netlink::Nl80211;

using Microsoft::Net::Netlink::NetlinkMessage;
using Microsoft::Net::Netlink::NetlinkSocket;
using Microsoft::Net::Netlink::Nl80211::Nl80211ProtocolState;

AccessPointDiscoveryAgentOperationsNetlink::AccessPointDiscoveryAgentOperationsNetlink() :
m_cookie(CookieValid)
m_cookie(CookieValid),
m_netlink80211ProtocolState(Nl80211ProtocolState::Instance())
{}

AccessPointDiscoveryAgentOperationsNetlink::~AccessPointDiscoveryAgentOperationsNetlink()
Expand Down Expand Up @@ -93,29 +94,8 @@ AccessPointDiscoveryAgentOperationsNetlink::Start(AccessPointPresenceEventCallba
return;
}

// Lookup the nl80211 netlink id if not already done.
int nl80211NetlinkId = m_nl80211NetlinkId;
if (nl80211NetlinkId == -1) {
nl80211NetlinkId = genl_ctrl_resolve(netlinkSocket, NL80211_GENL_NAME);
if (nl80211NetlinkId < 0) {
LOG_ERROR << std::format("Failed to resolve nl80211 netlink id with error {} ({})", nl80211NetlinkId, nl_geterror(nl80211NetlinkId));
return;
}
m_nl80211NetlinkId = nl80211NetlinkId;
}

// Lookup the membership id for the "config" multicast group if not already done.
int nl80211MulticastGroupIdConfig = m_nl80211MulticastGroupIdConfig;
if (nl80211MulticastGroupIdConfig == -1) {
nl80211MulticastGroupIdConfig = genl_ctrl_resolve_grp(netlinkSocket, NL80211_GENL_NAME, NL80211_MULTICAST_GROUP_CONFIG);
if (nl80211MulticastGroupIdConfig < 0) {
LOG_ERROR << std::format("Failed to resolve nl80211 multicast group id for config with error {} ({})", nl80211MulticastGroupIdConfig, nl_geterror(nl80211MulticastGroupIdConfig));
return;
}
m_nl80211MulticastGroupIdConfig = nl80211MulticastGroupIdConfig;
}

// Subscribe to configuration messages.
int nl80211MulticastGroupIdConfig = m_netlink80211ProtocolState.MulticastGroupId[Nl80211MulticastGroup::Configuration];
ret = nl_socket_add_membership(netlinkSocket, nl80211MulticastGroupIdConfig);
if (ret < 0) {
const auto err = errno;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <linux/nl80211.h>
#include <microsoft/net/netlink/NetlinkMessage.hxx>
#include <microsoft/net/netlink/NetlinkSocket.hxx>
#include <microsoft/net/netlink/nl80211/Netlink80211ProtocolState.hxx>
#include <microsoft/net/wifi/IAccessPointDiscoveryAgentOperations.hxx>
#include <netlink/netlink.h>

Expand Down Expand Up @@ -89,9 +90,6 @@ private:
// Cookie used to invalidate the callback context.
static constexpr uint32_t CookieInvalid{ 0xDEADBEEFu };

int m_nl80211NetlinkId{ -1 };
int m_nl80211MulticastGroupIdConfig{ -1 };

uint32_t m_cookie{ CookieInvalid };
AccessPointPresenceEventCallback m_accessPointPresenceCallback{ nullptr };

Expand All @@ -105,6 +103,7 @@ private:
};

std::unordered_map<int, WifiInterfaceInfo> m_interfaceInfo;
Microsoft::Net::Netlink::Nl80211::Nl80211ProtocolState &m_netlink80211ProtocolState;
};
} // namespace Microsoft::Net::Wifi

Expand Down

0 comments on commit 1195467

Please sign in to comment.