Skip to content
Open
8 changes: 3 additions & 5 deletions plugin/NetworkManagerImplementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,17 +654,13 @@ namespace WPEFramework
{
if(interface == "eth0")
{
m_ethIPv4Address = {};
m_ethIPv6Address = {};
m_ethConnected.store(false);
setDefaultInterface("wlan0"); // If WiFi is connected, make it the default interface
// As default interface is changed to wlan0, switch connectivity monitor to initial check
connectivityMonitor.switchToInitialCheck();
}
else if(interface == "wlan0")
{
m_wlanIPv4Address = {};
m_wlanIPv6Address = {};
m_wlanConnected.store(false);
bool triggerConnectivityCheck;
if(m_ethConnected.load())
Expand Down Expand Up @@ -775,7 +771,9 @@ namespace WPEFramework
}

_notificationLock.Lock();
NMLOG_INFO("Posting onIPAddressChange %s - %s", ipaddress.c_str(), interface.c_str());
NMLOG_INFO("Posting onIPAddressChange %s: %s %s %s",
(Exchange::INetworkManager::IP_ACQUIRED == status) ? "IP acquired" : "IP lost",
interface.c_str(), ipversion.c_str(), ipaddress.c_str());
for (const auto callback : _notificationCallbacks) {
callback->onIPAddressChange(interface, ipversion, ipaddress, status);
}
Expand Down
49 changes: 45 additions & 4 deletions plugin/NetworkManagerImplementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <arpa/inet.h>
#include <linux/rtnetlink.h>
#include <atomic>
#include <map>
#include <mutex>

using namespace std;
Expand Down Expand Up @@ -62,6 +63,45 @@ namespace WPEFramework
{
namespace Plugin
{
/* Returns true if the given string is an IPv6 link-local address (fe80::/10):
first byte 0xfe, second byte with top two bits == 10 (0x80..0xbf). */
inline bool isIPv6LinkLocal(const std::string& addr)
{
struct in6_addr sa6{};
return inet_pton(AF_INET6, addr.c_str(), &sa6) == 1 &&
sa6.s6_addr[0] == 0xfe && (sa6.s6_addr[1] & 0xc0) == 0x80;
}

/* Per-interface, per-address-family cache populated by libnm events. */
struct IpFamilyCache {
bool valid = false;
std::map<std::string, uint32_t> globalAddresses; // key=address, value=prefix length
std::string linkLocalAddress; // fe80:: for IPv6 (ula field), or 169.254.x.x for IPv4
std::string gateway;
std::string primarydns;
std::string secondarydns;
std::string dhcpserver;
bool autoconfig = false;

Exchange::INetworkManager::IPAddress toIPAddress(bool isIPv6) const {
Exchange::INetworkManager::IPAddress addr{};
addr.ipversion = isIPv6 ? "IPv6" : "IPv4";
addr.autoconfig = autoconfig;
addr.dhcpserver = dhcpserver;
addr.ula = linkLocalAddress;
addr.gateway = gateway;
addr.primarydns = primarydns;
addr.secondarydns = secondarydns;
if (!globalAddresses.empty()) {
const auto& first = *globalAddresses.begin();
addr.ipaddress = first.first;
addr.prefix = first.second;
}
return addr;
}
void clear() { *this = IpFamilyCache{}; }
};

class NetworkManagerImplementation : public Exchange::INetworkManager
{
enum NetworkEvents
Expand Down Expand Up @@ -315,10 +355,11 @@ namespace WPEFramework
std::mutex m_condVariableMutex;
std::condition_variable m_condVariable;
public:
IPAddress m_ethIPv4Address;
IPAddress m_wlanIPv4Address;
IPAddress m_ethIPv6Address;
IPAddress m_wlanIPv6Address;
IpFamilyCache m_ethIPv4Cache;
IpFamilyCache m_wlanIPv4Cache;
IpFamilyCache m_ethIPv6Cache;
IpFamilyCache m_wlanIPv6Cache;
mutable std::mutex m_ipCacheMutex;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use map, some thing like :
map<std::string, IpFamilyCache> ipAddrCache
This can reduce the code that we are adding to access the correct cache.
All the if loops in the implementations can be optimized.

std::atomic<bool> m_ethConnected;
std::atomic<bool> m_wlanConnected;
std::atomic<bool> m_ethEnabled;
Expand Down
Loading
Loading