diff --git a/CMakeLists.txt b/CMakeLists.txt index a04ad16b..8c9e1d74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,14 @@ option(USE_RDK_LOGGER "Enable RDK Logger for logging" OFF ) option(ENABLE_UNIT_TESTING "Enable unit tests" OFF) option(USE_TELEMETRY "Enable Telemetry T2 support" OFF) +# Backend identity macros are consumed by shared headers; define them globally +# so all targets (plugin, tests, tools) compile against the same API surface. +if(ENABLE_GNOME_NETWORKMANAGER AND ENABLE_GNOME_GDBUS) + add_compile_definitions(NM_BACKEND_GDBUS=1) +elseif(NOT ENABLE_GNOME_NETWORKMANAGER) + add_compile_definitions(NM_BACKEND_RDK=1) +endif() + if (USE_TELEMETRY) find_package(T2 REQUIRED) add_compile_definitions(USE_TELEMETRY=1) diff --git a/definition/NetworkManager.json b/definition/NetworkManager.json index 65fb6e6c..8aac650d 100644 --- a/definition/NetworkManager.json +++ b/definition/NetworkManager.json @@ -47,9 +47,9 @@ "example": 24 }, "ula": { - "summary": "The IPv6 Unified Local Address", + "summary": "The IPv6 Unique Local Address", "type": "string", - "example": "d00:410:2016::" + "example": "fd00:410:2016::" }, "gateway": { "summary": "The gateway address", diff --git a/docs/NetworkManagerPlugin.md b/docs/NetworkManagerPlugin.md index 2c31542f..dde1cae4 100644 --- a/docs/NetworkManagerPlugin.md +++ b/docs/NetworkManagerPlugin.md @@ -453,7 +453,7 @@ Gets the IP setting for the given interface. "ipaddress": "192.168.1.101", "prefix": 24, "gateway": "192.168.1.1", - "ula": "d00:410:2016::", + "ula": "", "primarydns": "192.168.1.1", "secondarydns": "192.168.1.2", "success": true diff --git a/plugin/NetworkManagerImplementation.cpp b/plugin/NetworkManagerImplementation.cpp index b4b5bca6..7ec1a3f9 100644 --- a/plugin/NetworkManagerImplementation.cpp +++ b/plugin/NetworkManagerImplementation.cpp @@ -19,6 +19,9 @@ #include #include +#include +#include +#include #include "NetworkManagerImplementation.h" #if USE_TELEMETRY @@ -654,8 +657,6 @@ 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 @@ -663,8 +664,6 @@ namespace WPEFramework } else if(interface == "wlan0") { - m_wlanIPv4Address = {}; - m_wlanIPv6Address = {}; m_wlanConnected.store(false); bool triggerConnectivityCheck; if(m_ethConnected.load()) @@ -775,7 +774,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); } @@ -1184,5 +1185,152 @@ namespace WPEFramework } #endif } + + bool isIPv4LinkLocal(const std::string& addr) + { + struct in_addr sa{}; + return inet_pton(AF_INET, addr.c_str(), &sa) == 1 && + (ntohl(sa.s_addr) & 0xffff0000u) == 0xa9fe0000u; + } + + 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; + } + + bool isIPv6ULA(const std::string& addr) + { + struct in6_addr sa6{}; + return inet_pton(AF_INET6, addr.c_str(), &sa6) == 1 && + (sa6.s6_addr[0] & 0xfe) == 0xfc; + } + + /* Parse a MAC string into 6 bytes. Accepts both "AA:BB:CC:DD:EE:FF" and "aabbccddeeff". */ + static bool parseMac(const std::string& mac, uint8_t out[6]) + { + unsigned int b[6]; + if (mac.size() >= 17 && + sscanf(mac.c_str(), "%02x:%02x:%02x:%02x:%02x:%02x", + &b[0], &b[1], &b[2], &b[3], &b[4], &b[5]) == 6) + { + for (int i = 0; i < 6; ++i) out[i] = static_cast(b[i]); + return true; + } + if (mac.size() >= 12 && + sscanf(mac.c_str(), "%02x%02x%02x%02x%02x%02x", + &b[0], &b[1], &b[2], &b[3], &b[4], &b[5]) == 6) + { + for (int i = 0; i < 6; ++i) out[i] = static_cast(b[i]); + return true; + } + return false; + } + + bool isIPv6MacBased(const std::string& ipv6Addr, const std::string& macAddr) + { + struct in6_addr sa6{}; + uint8_t mac[6]; + if (inet_pton(AF_INET6, ipv6Addr.c_str(), &sa6) != 1 || !parseMac(macAddr, mac)) + return false; + + /* Build 8-byte EUI-64 identifier from 6-byte MAC: + mac[0..2] | ff:fe | mac[3..5], then flip the U/L bit (bit 1) of byte 0. */ + uint8_t eui64[8]; + eui64[0] = mac[0] ^ 0x02; // flip Universal/Local bit + eui64[1] = mac[1]; + eui64[2] = mac[2]; + eui64[3] = 0xff; + eui64[4] = 0xfe; + eui64[5] = mac[3]; + eui64[6] = mac[4]; + eui64[7] = mac[5]; + + /* Compare against the interface-ID (last 8 bytes) of the IPv6 address. */ + if (memcmp(&sa6.s6_addr[8], eui64, 8) == 0) + { + NMLOG_DEBUG("MAC %s based global v6 address %s", macAddr.c_str(), ipv6Addr.c_str()); + return true; + } + return false; + } + + bool NetworkManagerImplementation::lookupIpCache( + const std::string& iface, const std::string& ipFamily, + Exchange::INetworkManager::IPAddress& out) const + { + std::lock_guard lock(m_ipCacheMutex); + auto it = m_ipCacheMap.find({iface, ipFamily}); + if (it != m_ipCacheMap.end() && it->second.valid) { + out = it->second.toIPAddress(); + return true; + } + return false; + } + + std::set NetworkManagerImplementation::swapIpCache( + const std::string& iface, const std::string& ipFamily, + IpFamilyCache newCache) + { + std::set oldKeys; + std::lock_guard lock(m_ipCacheMutex); + IpFamilyCache& cache = m_ipCacheMap[{iface, ipFamily}]; + for (const auto& kv : cache.globalAddresses) + oldKeys.insert(kv.first); + cache = std::move(newCache); + return oldKeys; + } + + Exchange::INetworkManager::IPAddress IpFamilyCache::toIPAddress() const + { + Exchange::INetworkManager::IPAddress addr{}; + /* Detect IP version from any available address. */ + bool isIPv6 = false; + { + const std::string* sample = nullptr; + if (!globalAddresses.empty()) + sample = &globalAddresses.begin()->first; + else if (!uniqueLocalAddresses.empty()) + sample = &(*uniqueLocalAddresses.begin()); + else if (!linkLocalAddresses.empty()) + sample = &(*linkLocalAddresses.begin()); + if (sample) { + struct in6_addr sa6{}; + isIPv6 = (inet_pton(AF_INET6, sample->c_str(), &sa6) == 1); + } + } + addr.ipversion = isIPv6 ? "IPv6" : "IPv4"; + addr.autoconfig = autoconfig; + addr.dhcpserver = dhcpserver; + addr.ula = uniqueLocalAddresses.empty() ? "" : *uniqueLocalAddresses.begin(); + addr.gateway = gateway; + addr.primarydns = primarydns; + addr.secondarydns = secondarydns; + /* Prefer non-MAC-based global; fall back to MAC-based if all are MAC-based. */ + const std::string* bestGlobal = nullptr; + uint32_t bestPrefix = 0; + const std::string* fallbackMac = nullptr; + uint32_t fallbackMacPrefix = 0; + for (const auto& kv : globalAddresses) { + if (kv.second.type == ADDR_GLOBAL) { + bestGlobal = &kv.first; + bestPrefix = kv.second.prefix; + break; + } + if (!fallbackMac) { + fallbackMac = &kv.first; + fallbackMacPrefix = kv.second.prefix; + } + } + if (bestGlobal) { + addr.ipaddress = *bestGlobal; + addr.prefix = bestPrefix; + } else if (fallbackMac) { + addr.ipaddress = *fallbackMac; + addr.prefix = fallbackMacPrefix; + } + return addr; + } } } diff --git a/plugin/NetworkManagerImplementation.h b/plugin/NetworkManagerImplementation.h index f5bd49b1..b55aeb2d 100644 --- a/plugin/NetworkManagerImplementation.h +++ b/plugin/NetworkManagerImplementation.h @@ -26,7 +26,9 @@ #include #include #include +#include #include +#include using namespace std; @@ -62,6 +64,47 @@ namespace WPEFramework { namespace Plugin { + /* Returns true if the given string is an IPv4 link-local address (169.254.0.0/16). */ + bool isIPv4LinkLocal(const std::string& addr); + + /* Returns true if the given string is an IPv6 link-local address (fe80::/10). */ + bool isIPv6LinkLocal(const std::string& addr); + + /* Returns true if the given string is an IPv6 Unique Local Address (ULA, fc00::/7). */ + bool isIPv6ULA(const std::string& addr); + + /* Returns true if the given global IPv6 address is derived (EUI-64) from the MAC. */ + bool isIPv6MacBased(const std::string& ipv6Addr, const std::string& macAddr); + + /* Sub-classification of global-scope addresses in the IP cache. */ + enum GlobalAddressType : uint8_t { + ADDR_GLOBAL, // non-MAC-based global (preferred by GetIPSettings) + ADDR_GLOBAL_MAC_BASED, // EUI-64 global derived from interface MAC (fallback) + }; + + struct GlobalAddressInfo { + uint32_t prefix; + GlobalAddressType type; + GlobalAddressInfo() : prefix(0), type(ADDR_GLOBAL) {} + GlobalAddressInfo(uint32_t p, GlobalAddressType t) : prefix(p), type(t) {} + }; + + /* Per-interface, per-address-family cache populated by libnm events. */ + struct IpFamilyCache { + bool valid = false; + std::map globalAddresses; // event-diffable global addresses + std::set linkLocalAddresses; // fe80::/10 or 169.254.x.x — not diffed for events + std::set uniqueLocalAddresses; // fc00::/7 (IPv6 only) — not diffed for events + std::string gateway; + std::string primarydns; + std::string secondarydns; + std::string dhcpserver; + bool autoconfig = false; + + Exchange::INetworkManager::IPAddress toIPAddress() const; + void clear() { *this = IpFamilyCache{}; } + }; + class NetworkManagerImplementation : public Exchange::INetworkManager { enum NetworkEvents @@ -315,10 +358,18 @@ namespace WPEFramework std::mutex m_condVariableMutex; std::condition_variable m_condVariable; public: +#if defined(NM_BACKEND_GDBUS) || defined(NM_BACKEND_RDK) IPAddress m_ethIPv4Address; IPAddress m_wlanIPv4Address; IPAddress m_ethIPv6Address; IPAddress m_wlanIPv6Address; +#endif + bool lookupIpCache(const std::string& iface, const std::string& ipFamily, + Exchange::INetworkManager::IPAddress& out) const; + std::set swapIpCache(const std::string& iface, + const std::string& ipFamily, + IpFamilyCache newCache); + std::atomic m_ethConnected; std::atomic m_wlanConnected; std::atomic m_ethEnabled; @@ -343,6 +394,8 @@ namespace WPEFramework private: string m_defaultInterface; mutable std::mutex m_defaultInterfaceMutex; + std::map, IpFamilyCache> m_ipCacheMap; + mutable std::mutex m_ipCacheMutex; }; } } diff --git a/plugin/gnome/NetworkManagerGnomeEvents.cpp b/plugin/gnome/NetworkManagerGnomeEvents.cpp index 761c39fb..7a87cc20 100644 --- a/plugin/gnome/NetworkManagerGnomeEvents.cpp +++ b/plugin/gnome/NetworkManagerGnomeEvents.cpp @@ -30,6 +30,8 @@ #include "NetworkManagerGnomeUtils.h" #include "NetworkManagerImplementation.h" #include "INetworkManager.h" +#include + #ifdef ENABLE_MIGRATION_MFRMGR_SUPPORT #include "NetworkManagerGnomeMfrMgr.h" #endif @@ -74,6 +76,198 @@ namespace WPEFramework } } + /* Refresh the per-interface/per-family IP cache from current libnm state and + emit acquired/lost events for address-set differences. + + Build a fresh IpFamilyCache from current libnm state for one device/family, + swap it into _instance under the cache mutex, then emit acquired/lost events + for any address-set differences outside the lock. */ + static void refreshIpFamilyCache(NMDevice* device, bool isIPv6) + { + if (!device || !NM_IS_DEVICE(device) || !_instance) + return; + + const char* iface = nm_device_get_iface(device); + if (!iface) return; + std::string ifname = iface; + + bool isEth = (ifname == nmUtils::ethIface()); + bool isWlan = (ifname == nmUtils::wlanIface()); + if (!isEth && !isWlan) return; + + /* Build the new snapshot locally (no locks held during NM calls). + * Skip the NM read when the device is in a disconnected/down state + * so that newCache stays empty and the diff emits IP_LOST for every + * address still in the cache. This also prevents spurious + * "IP acquired" events from intermediate NM signals (nameserver, + * gateway clearing) that fire after the cache has been emptied + * but before NM clears addresses on the config object. */ + NMDeviceState devState = nm_device_get_state(device); + bool skipRead = (devState <= NM_DEVICE_STATE_DISCONNECTED); + IpFamilyCache newCache; + NMActiveConnection* conn = skipRead ? nullptr : nm_device_get_active_connection(device); + if (conn) { + /* autoconfig: method "auto" or "dhcp" → true */ + NMConnection* nmConn = NM_CONNECTION(nm_active_connection_get_connection(conn)); + if (nmConn) { + NMSettingIPConfig* ipSetting = isIPv6 + ? NM_SETTING_IP_CONFIG(nm_connection_get_setting_ip6_config(nmConn)) + : NM_SETTING_IP_CONFIG(nm_connection_get_setting_ip4_config(nmConn)); + if (ipSetting) { + const char* method = nm_setting_ip_config_get_method(ipSetting); + newCache.autoconfig = method && + (g_strcmp0(method, "auto") == 0 || g_strcmp0(method, "dhcp") == 0); + } + } + } + + /* IP config read is device-level and does not require an active connection. */ + NMIPConfig* ipConfig = skipRead ? nullptr + : (isIPv6 ? nm_device_get_ip6_config(device) + : nm_device_get_ip4_config(device)); + + if (ipConfig) { + GPtrArray* ipAddresses = nm_ip_config_get_addresses(ipConfig); + std::string macAddr; + if (isIPv6) { + const char* hw = nm_device_get_hw_address(device); + if (hw) macAddr = hw; + } + if (ipAddresses) { + for (guint i = 0; i < ipAddresses->len; i++) { + NMIPAddress* addr = (NMIPAddress*)g_ptr_array_index(ipAddresses, i); + if (!addr) continue; + const char* addrStr = nm_ip_address_get_address(addr); + if (!addrStr) continue; + std::string addrString = addrStr; + uint32_t prefix = nm_ip_address_get_prefix(addr); + if (isIPv6) { + if (isIPv6LinkLocal(addrString)) { + newCache.linkLocalAddresses.insert(addrString); + } else if (isIPv6ULA(addrString)) { + newCache.uniqueLocalAddresses.insert(addrString); + } else { + GlobalAddressType type = (!macAddr.empty() && isIPv6MacBased(addrString, macAddr)) + ? ADDR_GLOBAL_MAC_BASED : ADDR_GLOBAL; + newCache.globalAddresses.emplace(addrString, GlobalAddressInfo{prefix, type}); + } + } else { + if (isIPv4LinkLocal(addrString)) { + newCache.linkLocalAddresses.insert(addrString); + } else { + newCache.globalAddresses.emplace(addrString, GlobalAddressInfo{prefix, ADDR_GLOBAL}); + } + } + } + } + + const char* gw = nm_ip_config_get_gateway(ipConfig); + if (gw) newCache.gateway = gw; + + const char* const* dnsArr = nm_ip_config_get_nameservers(ipConfig); + if (dnsArr && dnsArr[0]) { + newCache.primarydns = dnsArr[0]; + if (dnsArr[1]) newCache.secondarydns = dnsArr[1]; + } + + NMDhcpConfig* dhcpConfig = isIPv6 + ? nm_device_get_dhcp6_config(device) + : nm_device_get_dhcp4_config(device); + if (dhcpConfig) { + const char* server = nm_dhcp_config_get_one_option(dhcpConfig, "dhcp_server_identifier"); + if (server) newCache.dhcpserver = server; + } + + newCache.valid = true; + } + + /* Swap new snapshot into instance cache; collect old global address keys for diff. */ + std::set oldKeys = _instance->swapIpCache( + ifname, isIPv6 ? "IPv6" : "IPv4", newCache); + + /* Emit address acquired/lost events from global-address key diff (outside the lock). */ + std::string family = isIPv6 ? "IPv6" : "IPv4"; + for (const auto& kv : newCache.globalAddresses) { + if (oldKeys.find(kv.first) == oldKeys.end()) { + _instance->ReportIPAddressChange(ifname, family, kv.first, Exchange::INetworkManager::IP_ACQUIRED); + } + } + for (const auto& key : oldKeys) { + if (newCache.globalAddresses.find(key) == newCache.globalAddresses.end()) { + _instance->ReportIPAddressChange(ifname, family, key, Exchange::INetworkManager::IP_LOST); + } + } + } + + static void ip4ChangedCb(NMIPConfig *ipConfig, GParamSpec *pspec, gpointer userData) + { + NMDevice *device = (NMDevice*)userData; + if (!device || !NM_IS_DEVICE(device)) return; + refreshIpFamilyCache(device, false); + } + + static void ip6ChangedCb(NMIPConfig *ipConfig, GParamSpec *pspec, gpointer userData) + { + NMDevice *device = (NMDevice*)userData; + if (!device || !NM_IS_DEVICE(device)) return; + refreshIpFamilyCache(device, true); + } + + /* Called when DHCP options change mid-lease (e.g. renewed with different server/options). */ + static void dhcp4OptionsCb(NMDhcpConfig *dhcpConfig, GParamSpec *pspec, gpointer userData) + { + NMDevice *device = (NMDevice*)userData; + if (!device || !NM_IS_DEVICE(device)) return; + refreshIpFamilyCache(device, false); + } + + static void dhcp6OptionsCb(NMDhcpConfig *dhcpConfig, GParamSpec *pspec, gpointer userData) + { + NMDevice *device = (NMDevice*)userData; + if (!device || !NM_IS_DEVICE(device)) return; + refreshIpFamilyCache(device, true); + } + + /* Called when the ip4-config or ip6-config object on a device is replaced + (e.g. after reconnect). Re-attaches notify handlers to the new object. */ + static void ip4ConfigChangedCb(NMDevice *device, GParamSpec *pspec, gpointer userData) + { + if (!device || !NM_IS_DEVICE(device)) return; + NMIPConfig* ipv4Config = nm_device_get_ip4_config(device); + if (ipv4Config) { + g_signal_handlers_disconnect_by_func(ipv4Config, (gpointer)ip4ChangedCb, device); + g_signal_connect(ipv4Config, "notify::addresses", G_CALLBACK(ip4ChangedCb), device); + g_signal_connect(ipv4Config, "notify::gateway", G_CALLBACK(ip4ChangedCb), device); + g_signal_connect(ipv4Config, "notify::nameservers", G_CALLBACK(ip4ChangedCb), device); + } + /* Re-attach DHCP options handler to the (possibly new) DHCP config object. */ + NMDhcpConfig* dhcp4 = nm_device_get_dhcp4_config(device); + if (dhcp4) { + g_signal_handlers_disconnect_by_func(dhcp4, (gpointer)dhcp4OptionsCb, device); + g_signal_connect(dhcp4, "notify::options", G_CALLBACK(dhcp4OptionsCb), device); + } + refreshIpFamilyCache(device, false); + } + + static void ip6ConfigChangedCb(NMDevice *device, GParamSpec *pspec, gpointer userData) + { + if (!device || !NM_IS_DEVICE(device)) return; + NMIPConfig* ipv6Config = nm_device_get_ip6_config(device); + if (ipv6Config) { + g_signal_handlers_disconnect_by_func(ipv6Config, (gpointer)ip6ChangedCb, device); + g_signal_connect(ipv6Config, "notify::addresses", G_CALLBACK(ip6ChangedCb), device); + g_signal_connect(ipv6Config, "notify::gateway", G_CALLBACK(ip6ChangedCb), device); + g_signal_connect(ipv6Config, "notify::nameservers", G_CALLBACK(ip6ChangedCb), device); + } + /* Re-attach DHCP options handler to the (possibly new) DHCP config object. */ + NMDhcpConfig* dhcp6 = nm_device_get_dhcp6_config(device); + if (dhcp6) { + g_signal_handlers_disconnect_by_func(dhcp6, (gpointer)dhcp6OptionsCb, device); + g_signal_connect(dhcp6, "notify::options", G_CALLBACK(dhcp6OptionsCb), device); + } + refreshIpFamilyCache(device, true); + } + void GnomeNetworkManagerEvents::deviceStateChangeCb(NMDevice *device, GParamSpec *pspec, NMEvents *nmEvents) { static bool isEthDisabled = false; @@ -125,11 +319,15 @@ namespace WPEFramework case NM_DEVICE_STATE_UNKNOWN: wifiState = "WIFI_STATE_UNINSTALLED"; GnomeNetworkManagerEvents::onWIFIStateChanged(Exchange::INetworkManager::WIFI_STATE_UNINSTALLED); + refreshIpFamilyCache(device, false); + refreshIpFamilyCache(device, true); GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_REMOVED, nmUtils::wlanIface()); break; case NM_DEVICE_STATE_UNMANAGED: wifiState = "WIFI_STATE_DISABLED"; GnomeNetworkManagerEvents::onWIFIStateChanged(Exchange::INetworkManager::WIFI_STATE_DISABLED); + refreshIpFamilyCache(device, false); + refreshIpFamilyCache(device, true); GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_REMOVED, nmUtils::wlanIface()); isWlanDisabled = true; break; @@ -137,6 +335,8 @@ namespace WPEFramework case NM_DEVICE_STATE_DISCONNECTED: wifiState = "WIFI_STATE_DISCONNECTED"; GnomeNetworkManagerEvents::onWIFIStateChanged(Exchange::INetworkManager::WIFI_STATE_DISCONNECTED); + refreshIpFamilyCache(device, false); + refreshIpFamilyCache(device, true); GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_LINK_DOWN, nmUtils::wlanIface()); break; case NM_DEVICE_STATE_PREPARE: @@ -212,11 +412,15 @@ namespace WPEFramework { case NM_DEVICE_STATE_UNKNOWN: case NM_DEVICE_STATE_UNMANAGED: + refreshIpFamilyCache(device, false); + refreshIpFamilyCache(device, true); GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_REMOVED, nmUtils::ethIface()); isEthDisabled = true; break; case NM_DEVICE_STATE_UNAVAILABLE: case NM_DEVICE_STATE_DISCONNECTED: + refreshIpFamilyCache(device, false); + refreshIpFamilyCache(device, true); GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_LINK_DOWN, nmUtils::ethIface()); break; case NM_DEVICE_STATE_PREPARE: @@ -261,101 +465,6 @@ namespace WPEFramework } } - static void ip4ChangedCb(NMIPConfig *ipConfig, GParamSpec *pspec, gpointer userData) - { - if (!ipConfig) { - NMLOG_ERROR("IP config is null"); - return; - } - - NMDevice *device = (NMDevice*)userData; - if((device == NULL) || (!NM_IS_DEVICE(device))) - return; - - const char* iface = nm_device_get_iface(device); - if(iface == NULL) - return; - std::string ifname = iface; - - GPtrArray *addresses = nm_ip_config_get_addresses(ipConfig); - if (!addresses) { - NMLOG_ERROR("No addresses found"); - return; - } - else { - if(addresses->len == 0) { - GnomeNetworkManagerEvents::onAddressChangeCb(ifname, "", false, false); - return; - } - } - - for (guint i = 0; i < addresses->len; ++i) { - NMIPAddress *address = (NMIPAddress *)g_ptr_array_index(addresses, i); - if(address == NULL) - { - NMLOG_WARNING("IPv4 address is null"); - continue; - } - if (nm_ip_address_get_family(address) == AF_INET) { - const char *ipAddress = nm_ip_address_get_address(address); - if(ipAddress != NULL) { - GnomeNetworkManagerEvents::onAddressChangeCb(iface, ipAddress, true, false); - } - } - } - } - - static void ip6ChangedCb(NMIPConfig *ipConfig, GParamSpec *pspec, gpointer userData) - { - if (!ipConfig) { - NMLOG_ERROR("ip config is null"); - return; - } - - NMDevice *device = (NMDevice*)userData; - if( ((device != NULL) && NM_IS_DEVICE(device)) ) - { - const char* iface = nm_device_get_iface(device); - if(iface == NULL) - return; - std::string ifname = iface; - GPtrArray *addresses = nm_ip_config_get_addresses(ipConfig); - if (!addresses) { - NMLOG_ERROR("No addresses found"); - return; - } - else { - if(addresses->len == 0) { - GnomeNetworkManagerEvents::onAddressChangeCb(ifname, "", false, true); - return; - } - } - - for (guint i = 0; i < addresses->len; ++i) { - NMIPAddress *address = (NMIPAddress *)g_ptr_array_index(addresses, i); - if(address == NULL) - { - NMLOG_WARNING("IPv6 address is null"); - continue; - } - if (nm_ip_address_get_family(address) == AF_INET6) { - const char *ipaddr = nm_ip_address_get_address(address); - //int prefix = nm_ip_address_get_prefix(address); - if(ipaddr != NULL) { - std::string ipAddress = ipaddr; - if (ipAddress.compare(0, 5, "fe80:") == 0 || - ipAddress.compare(0, 6, "fe80::") == 0) { - NMLOG_DEBUG("%s It's link-local ip", ipAddress.c_str()); - continue; // It's link-local so skiping - } - GnomeNetworkManagerEvents::onAddressChangeCb(iface, ipAddress, true, true); - break; // SLAAC protocol may include multip ipv6 address posting only one Global address - } - } - } - } - } - static void deviceAddedCB(NMClient *client, NMDevice *device, NMEvents *nmEvents) { if( ((device != NULL) && NM_IS_DEVICE(device)) ) @@ -374,17 +483,35 @@ namespace WPEFramework if(ifname == nmUtils::ethIface() || ifname == nmUtils::wlanIface()) { g_signal_connect(device, "notify::" NM_DEVICE_STATE, G_CALLBACK(GnomeNetworkManagerEvents::deviceStateChangeCb), nmEvents); - // TODO call notify::" NM_DEVICE_ACTIVE_CONNECTION if needed + g_signal_connect(device, "notify::ip4-config", G_CALLBACK(ip4ConfigChangedCb), nmEvents); + g_signal_connect(device, "notify::ip6-config", G_CALLBACK(ip6ConfigChangedCb), nmEvents); NMIPConfig *ipv4Config = nm_device_get_ip4_config(device); NMIPConfig *ipv6Config = nm_device_get_ip6_config(device); if (ipv4Config) { - g_signal_connect(ipv4Config, "notify::addresses", G_CALLBACK(ip4ChangedCb), device); + g_signal_connect(ipv4Config, "notify::addresses", G_CALLBACK(ip4ChangedCb), device); + g_signal_connect(ipv4Config, "notify::gateway", G_CALLBACK(ip4ChangedCb), device); + g_signal_connect(ipv4Config, "notify::nameservers", G_CALLBACK(ip4ChangedCb), device); } if (ipv6Config) { - g_signal_connect(ipv6Config, "notify::addresses", G_CALLBACK(ip6ChangedCb), device); + g_signal_connect(ipv6Config, "notify::addresses", G_CALLBACK(ip6ChangedCb), device); + g_signal_connect(ipv6Config, "notify::gateway", G_CALLBACK(ip6ChangedCb), device); + g_signal_connect(ipv6Config, "notify::nameservers", G_CALLBACK(ip6ChangedCb), device); } + /* Subscribe to DHCP option changes so dhcpserver stays current mid-lease. */ + NMDhcpConfig* dhcp4Added = nm_device_get_dhcp4_config(device); + NMDhcpConfig* dhcp6Added = nm_device_get_dhcp6_config(device); + if (dhcp4Added) + g_signal_connect(dhcp4Added, "notify::options", G_CALLBACK(dhcp4OptionsCb), device); + if (dhcp6Added) + g_signal_connect(dhcp6Added, "notify::options", G_CALLBACK(dhcp6OptionsCb), device); + + /* Seed the IP cache so GetIPSettings works immediately if the + device already has an address (e.g. hotplug in activated state). */ + refreshIpFamilyCache(device, false); + refreshIpFamilyCache(device, true); + if (NM_IS_DEVICE_WIFI(device)) { // Register signal handler for WiFi scanning events to detect when scan operations complete @@ -405,14 +532,45 @@ namespace WPEFramework std::string ifname = nm_device_get_iface(device); if(ifname == nmUtils::wlanIface()) { GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_REMOVED, nmUtils::wlanIface()); - g_signal_handlers_disconnect_by_func(device, (gpointer)GnomeNetworkManagerEvents::deviceStateChangeCb, nmEvents); NMLOG_INFO("WIFI device removed: %s", ifname.c_str()); } else if(ifname == nmUtils::ethIface()) { GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_REMOVED, nmUtils::ethIface()); - g_signal_handlers_disconnect_by_func(device, (gpointer)GnomeNetworkManagerEvents::deviceStateChangeCb, nmEvents); NMLOG_INFO("ETHERNET device removed: %s", ifname.c_str()); } + else { + return; // not a tracked interface + } + + /* Disconnect all device-level signals (state, ip4/ip6-config changes). */ + g_signal_handlers_disconnect_by_data(device, nmEvents); + + /* Disconnect IP config property signals (addresses, gateway, nameservers). */ + NMIPConfig *ipv4Config = nm_device_get_ip4_config(device); + NMIPConfig *ipv6Config = nm_device_get_ip6_config(device); + if (ipv4Config) + g_signal_handlers_disconnect_by_func(ipv4Config, (gpointer)ip4ChangedCb, device); + if (ipv6Config) + g_signal_handlers_disconnect_by_func(ipv6Config, (gpointer)ip6ChangedCb, device); + + /* Disconnect DHCP option signals. */ + NMDhcpConfig* dhcp4 = nm_device_get_dhcp4_config(device); + NMDhcpConfig* dhcp6 = nm_device_get_dhcp6_config(device); + if (dhcp4) + g_signal_handlers_disconnect_by_func(dhcp4, (gpointer)dhcp4OptionsCb, device); + if (dhcp6) + g_signal_handlers_disconnect_by_func(dhcp6, (gpointer)dhcp6OptionsCb, device); + + /* Clear IP cache for the removed device (emits IP_LOST for any cached addresses). */ + if (_instance) { + for (const char* family : {"IPv4", "IPv6"}) { + IpFamilyCache empty; + std::set oldKeys = _instance->swapIpCache(ifname, family, empty); + for (const auto& key : oldKeys) { + _instance->ReportIPAddressChange(ifname, family, key, Exchange::INetworkManager::IP_LOST); + } + } + } } // guint disconnected_count = g_signal_handlers_disconnect_matched( _nmEventInstance->activeConn, @@ -492,6 +650,8 @@ namespace WPEFramework /* Register device state change event */ g_signal_connect(device, "notify::" NM_DEVICE_STATE, G_CALLBACK(GnomeNetworkManagerEvents::deviceStateChangeCb), nmEvents); + g_signal_connect(device, "notify::ip4-config", G_CALLBACK(ip4ConfigChangedCb), nmEvents); + g_signal_connect(device, "notify::ip6-config", G_CALLBACK(ip6ConfigChangedCb), nmEvents); if(NM_IS_DEVICE_WIFI(device)) { nmEvents->wifiDevice = NM_DEVICE_WIFI(device); g_signal_connect(nmEvents->wifiDevice, "notify::" NM_DEVICE_WIFI_LAST_SCAN, G_CALLBACK(GnomeNetworkManagerEvents::onAvailableSSIDsCb), nmEvents); @@ -500,18 +660,32 @@ namespace WPEFramework NMIPConfig *ipv4Config = nm_device_get_ip4_config(device); NMIPConfig *ipv6Config = nm_device_get_ip6_config(device); if (ipv4Config) { - ip4ChangedCb(ipv4Config, NULL, device); // posting event if interface already connected - g_signal_connect(ipv4Config, "notify::addresses", G_CALLBACK(ip4ChangedCb), device); + g_signal_connect(ipv4Config, "notify::addresses", G_CALLBACK(ip4ChangedCb), device); + g_signal_connect(ipv4Config, "notify::gateway", G_CALLBACK(ip4ChangedCb), device); + g_signal_connect(ipv4Config, "notify::nameservers", G_CALLBACK(ip4ChangedCb), device); } else NMLOG_WARNING("IPv4 config is null for device: %s, No IPv4 monitor", ifname.c_str()); if (ipv6Config) { - ip6ChangedCb(ipv6Config, NULL, device); - g_signal_connect(ipv6Config, "notify::addresses", G_CALLBACK(ip6ChangedCb), device); + g_signal_connect(ipv6Config, "notify::addresses", G_CALLBACK(ip6ChangedCb), device); + g_signal_connect(ipv6Config, "notify::gateway", G_CALLBACK(ip6ChangedCb), device); + g_signal_connect(ipv6Config, "notify::nameservers", G_CALLBACK(ip6ChangedCb), device); } else NMLOG_WARNING("IPv6 config is null for device: %s, No IPv6 monitor", ifname.c_str()); + + /* Subscribe to DHCP option changes so dhcpserver stays current mid-lease. */ + NMDhcpConfig* dhcp4Init = nm_device_get_dhcp4_config(device); + NMDhcpConfig* dhcp6Init = nm_device_get_dhcp6_config(device); + if (dhcp4Init) + g_signal_connect(dhcp4Init, "notify::options", G_CALLBACK(dhcp4OptionsCb), device); + if (dhcp6Init) + g_signal_connect(dhcp6Init, "notify::options", G_CALLBACK(dhcp6OptionsCb), device); + + /* Seed the IP cache from current state for already-connected devices. */ + refreshIpFamilyCache(device, false); + refreshIpFamilyCache(device, true); } else NMLOG_DEBUG("device type not eth/wifi %s", ifname.c_str()); @@ -593,6 +767,14 @@ namespace WPEFramework if (ipv6Config) { g_signal_handlers_disconnect_by_func(ipv6Config, (gpointer)ip6ChangedCb, device); } + + // Clean up DHCP option signals + NMDhcpConfig* dhcp4 = nm_device_get_dhcp4_config(device); + NMDhcpConfig* dhcp6 = nm_device_get_dhcp6_config(device); + if (dhcp4) + g_signal_handlers_disconnect_by_func(dhcp4, (gpointer)dhcp4OptionsCb, device); + if (dhcp6) + g_signal_handlers_disconnect_by_func(dhcp6, (gpointer)dhcp6OptionsCb, device); } } } @@ -714,53 +896,6 @@ namespace WPEFramework } } - void GnomeNetworkManagerEvents::onAddressChangeCb(std::string iface, std::string ipAddress, bool acquired, bool isIPv6) - { - /* - * notify::addresses g signal only send ipaddress when accuired time only. - * we need to post ip address when ipaddress lost case also so we caching the ip address per interface - */ - static std::map ipv6Map; - static std::map ipv4Map; - - if(acquired) - { - if (isIPv6) - { - if (ipv6Map[iface].find(ipAddress) == std::string::npos) { // same ip comes multiple time so avoding that - ipv6Map[iface] = ipAddress; - } - else // same ip not posting - return; - } - else - { - ipv4Map[iface] = ipAddress; - } - } - else - { - if (isIPv6) - { - ipAddress = ipv6Map[iface]; - ipv6Map[iface].clear(); - } - else - { - ipAddress = ipv4Map[iface]; - ipv4Map[iface].clear(); - } - if(ipAddress.empty()) - return; // empty ip address not posting event - } - Exchange::INetworkManager::IPStatus ipStatus{}; - if (acquired) - ipStatus = Exchange::INetworkManager::IP_ACQUIRED; - if(_instance != nullptr) - _instance->ReportIPAddressChange(iface, isIPv6?"IPv6":"IPv4", ipAddress, ipStatus); - NMLOG_INFO("iface:%s - ipaddress:%s - %s - %s", iface.c_str(), ipAddress.c_str(), acquired?"acquired":"lost", isIPv6?"isIPv6":"isIPv4"); - } - bool GnomeNetworkManagerEvents::apToJsonObject(NMAccessPoint *ap, JsonObject& ssidObj) { GBytes *ssid = NULL; diff --git a/plugin/gnome/NetworkManagerGnomeEvents.h b/plugin/gnome/NetworkManagerGnomeEvents.h index ee29ce6d..bf282de7 100644 --- a/plugin/gnome/NetworkManagerGnomeEvents.h +++ b/plugin/gnome/NetworkManagerGnomeEvents.h @@ -44,7 +44,6 @@ namespace WPEFramework public: static void onInterfaceStateChangeCb(uint8_t newState, std::string iface); // ReportInterfaceStateChange - static void onAddressChangeCb(std::string iface, std::string ipAddress, bool acqired, bool isIPv6); // ReportIPAddressChange static void onActiveInterfaceChangeCb(std::string newInterface); // ReportActiveInterfaceChange static void onAvailableSSIDsCb(NMDeviceWifi *wifiDevice, GParamSpec *pspec, gpointer userData); // ReportAvailableSSIDs static void onWIFIStateChanged(uint8_t state); // ReportWiFiStateChange diff --git a/plugin/gnome/NetworkManagerGnomeProxy.cpp b/plugin/gnome/NetworkManagerGnomeProxy.cpp index b661a8de..d124461d 100644 --- a/plugin/gnome/NetworkManagerGnomeProxy.cpp +++ b/plugin/gnome/NetworkManagerGnomeProxy.cpp @@ -22,9 +22,6 @@ #include "NetworkManagerGnomeUtils.h" #include #include -#include - -#define IN_IS_ADDR_LINKLOCAL(a) ((((uint32_t)ntohl(a)) & 0xffff0000U) == 0xa9fe0000U) using namespace WPEFramework; using namespace WPEFramework::Plugin; using namespace std; @@ -649,40 +646,9 @@ namespace WPEFramework return Core::ERROR_GENERAL; } - bool static isAutoConnectEnabled(NMActiveConnection* activeConn) - { - NMConnection *connection = NM_CONNECTION(nm_active_connection_get_connection(activeConn)); - if(connection == NULL) - return false; - - NMSettingIPConfig *ipConfig = nm_connection_get_setting_ip4_config(connection); - if(ipConfig) - { - const char* ipConfMethod = nm_setting_ip_config_get_method (ipConfig); - if(ipConfMethod != NULL && g_strcmp0(ipConfMethod, "auto") == 0) - return true; - else - NMLOG_WARNING("ip configuration: %s", ipConfMethod != NULL? ipConfMethod: "null"); - } - - return false; - } - /* @brief Get IP Address Of the Interface */ uint32_t NetworkManagerImplementation::GetIPSettings(string& interface /* @inout */, const string &ipversion /* @in */, IPAddress& result /* @out */) { - NMActiveConnection *conn = NULL; - NMIPConfig *ip4_config = NULL; - NMIPConfig *ip6_config = NULL; - const gchar *gateway = NULL; - char **dnsArr = NULL; - NMDhcpConfig *dhcp4_config = NULL; - NMDhcpConfig *dhcp6_config = NULL; - const char* dhcpserver; - NMSettingConnection *settings = NULL; - NMDevice *device = NULL; - string ipversionStr; - std::string wifiname = nmUtils::wlanIface(), ethname = nmUtils::ethIface(); if(interface.empty()) @@ -705,271 +671,23 @@ namespace WPEFramework return Core::ERROR_GENERAL; } - if(ipversion.empty()) - { - ipversionStr = "IPV4"; - } - else - { - ipversionStr = ipversion; - } - - // Add caching optimization similar to RDK proxy - if (wifiname == interface) - { - if(nmUtils::caseInsensitiveCompare(ipversionStr, "IPV4") && !m_wlanIPv4Address.ipaddress.empty()) - { - NMLOG_DEBUG("%s IPv4 address from cache", wifiname.c_str()); - result = m_wlanIPv4Address; - return Core::ERROR_NONE; - } - else if(nmUtils::caseInsensitiveCompare(ipversion, "IPV6") && !m_wlanIPv6Address.ipaddress.empty()) - { - NMLOG_DEBUG("%s IPv6 address from cache", wifiname.c_str()); - result = m_wlanIPv6Address; - return Core::ERROR_NONE; - } - } - else if (ethname == interface) - { - if(nmUtils::caseInsensitiveCompare(ipversionStr, "IPV4") && !m_ethIPv4Address.ipaddress.empty()) - { - NMLOG_DEBUG("%s IPv4 address from cache", ethname.c_str()); - result = m_ethIPv4Address; - return Core::ERROR_NONE; - } - else if(nmUtils::caseInsensitiveCompare(ipversion, "IPV6") && !m_ethIPv6Address.ipaddress.empty()) - { - NMLOG_DEBUG("%s IPv6 address from cache", ethname.c_str()); - result = m_ethIPv6Address; - return Core::ERROR_NONE; - } - } - - if(m_nmClient == nullptr) - { - NMLOG_WARNING("NMClient is null"); - return Core::ERROR_RPC_CALL_FAILED; - } - - /* Drain any pending D-Bus property-change events queued on m_nmContext - * before reading libnm GObject state. Because m_nmContext is isolated - * from the event thread, nobody else can run it — so this loop is - * single-threaded and safe. It ensures m_nmClient reflects the latest state - * from NetworkManager before we start iterating connections/addresses. */ - if (m_nmContext) { - for (int i = 0; i < 100 && g_main_context_iteration(m_nmContext, FALSE); ++i){ - // Intentional empty body: just flushing the event queue - } - } - - device = nm_client_get_device_by_iface(m_nmClient, interface.c_str()); - if (device == NULL) - { - NMLOG_FATAL("libnm doesn't have device corresponding to %s", interface.c_str()); - return Core::ERROR_GENERAL; - } - - NMDeviceState deviceState = NM_DEVICE_STATE_UNKNOWN; - deviceState = nm_device_get_state(device); - if(deviceState < NM_DEVICE_STATE_DISCONNECTED) - { - NMLOG_WARNING("%s state is not a valid state: (%d)", interface.c_str(), deviceState); - return Core::ERROR_GENERAL; - } + string ipversionStr = ipversion.empty() ? "IPv4" : ipversion; + std::string family = nmUtils::caseInsensitiveCompare(ipversionStr, "IPv6") ? "IPv6" : "IPv4"; - // if(ipversion.empty()) - // NMLOG_DEBUG("ipversion is empty default value IPv4"); + result = IPAddress{}; + result.ipversion = family; - const GPtrArray *connections = nm_client_get_active_connections(m_nmClient); - if(connections == NULL) + // Serve from event-driven cache + if (lookupIpCache(interface, family, result)) { - NMLOG_WARNING("no active connection; ip is not assigned to interface"); - return Core::ERROR_GENERAL; + NMLOG_DEBUG("%s %s address from cache", interface.c_str(), family.c_str()); + result.ipversion = family; } - - for (guint i = 0; i < connections->len; i++) - { - if(connections->pdata[i] == NULL) - continue; - - NMActiveConnection *connection = NM_ACTIVE_CONNECTION(connections->pdata[i]); - if (connection == nullptr) - continue; - - NMRemoteConnection* retConn = nm_active_connection_get_connection(connection); - if(retConn == NULL) - { - NMLOG_INFO("remote connection is null"); - continue; - } - - settings = nm_connection_get_setting_connection(NM_CONNECTION(retConn)); - if(settings == NULL) - continue; - if (g_strcmp0(nm_setting_connection_get_interface_name(settings), interface.c_str()) == 0) - { - conn = connection; - break; - } - } - - if (conn == NULL) + else { - NMLOG_WARNING("no active connection on %s interface", interface.c_str()); - return Core::ERROR_GENERAL; + NMLOG_DEBUG("no %s address on %s", family.c_str(), interface.c_str()); } - result.autoconfig = isAutoConnectEnabled(conn); - - if(ipversion.empty() || nmUtils::caseInsensitiveCompare(ipversion, "IPV4")) - { - const GPtrArray *ipByte = nullptr; - result.ipversion = "IPv4"; - ip4_config = nm_active_connection_get_ip4_config(conn); - NMIPAddress *ipAddr = NULL; - std::string ipStr; - struct sockaddr_in sa; - if (ip4_config) - ipByte = nm_ip_config_get_addresses(ip4_config); - else - NMLOG_WARNING("no IPv4 configurtion on %s", interface.c_str()); - if(ipByte) - { - for (guint i = 0; i < ipByte->len; i++) - { - ipStr.clear(); - ipAddr = static_cast(ipByte->pdata[i]); - if(ipAddr) - { - const char* addr = nm_ip_address_get_address(ipAddr); - if(addr) - ipStr = addr; - } - if(!ipStr.empty()) - { - // Skip link-local IPv4 addresses (169.254.x.x) - inet_pton(AF_INET, ipStr.c_str(), &(sa.sin_addr)); - if(IN_IS_ADDR_LINKLOCAL(sa.sin_addr.s_addr)) - { - NMLOG_DEBUG("Skipping link-local IPv4 address: %s", ipStr.c_str()); - continue; - } - result.ipaddress = ipStr; - result.prefix = nm_ip_address_get_prefix(ipAddr); - NMLOG_DEBUG("IPv4 addr: %s/%d", result.ipaddress.c_str(), result.prefix); - } - } - gateway = nm_ip_config_get_gateway(ip4_config); - if(gateway) - result.gateway = gateway; - dnsArr = (char **)nm_ip_config_get_nameservers(ip4_config); - if(dnsArr) - { - if(dnsArr[0]) - result.primarydns = std::string(dnsArr[0]); - if(dnsArr[1]) - result.secondarydns = std::string(dnsArr[1]); - } - dhcp4_config = nm_active_connection_get_dhcp4_config(conn); - if(dhcp4_config) - { - dhcpserver = nm_dhcp_config_get_one_option (dhcp4_config, "dhcp_server_identifier"); - if(dhcpserver) - result.dhcpserver = dhcpserver; - } - result.ula = ""; - - // Check if only link-local IPv4 is available (no valid global address found) - if(result.ipaddress.empty()) - { - NMLOG_WARNING("Only link-local IPv4 available on %s, not returning it", interface.c_str()); - // Clear cache for link-local only - if(ethname == interface) - m_ethIPv4Address = IPAddress(); - else if(wifiname == interface) - m_wlanIPv4Address = IPAddress(); - return Core::ERROR_GENERAL; - } - - // Cache the IPv4 address - if(ethname == interface) - m_ethIPv4Address = result; - else if(wifiname == interface) - m_wlanIPv4Address = result; - } - } - if((result.ipaddress.empty() && !(nmUtils::caseInsensitiveCompare(ipversion, "IPV4"))) || nmUtils::caseInsensitiveCompare(ipversion, "IPV6")) - { - std::string ipStr; - const GPtrArray *ipArray = nullptr; - result.ipversion = "IPv6"; - NMIPAddress *ipAddr = nullptr; - ip6_config = nm_active_connection_get_ip6_config(conn); - if(ip6_config) - ipArray = nm_ip_config_get_addresses(ip6_config); - else - NMLOG_WARNING("no IPv6 configurtion on %s", interface.c_str()); - if(ipArray) - { - for (guint i = 0; i < ipArray->len; i++) - { - ipStr.clear(); - ipAddr = static_cast(ipArray->pdata[i]); - if(ipAddr) - { - const char* addr = nm_ip_address_get_address(ipAddr); - if(addr) - ipStr = addr; - } - if(!ipStr.empty()) - { - if (ipStr.compare(0, 5, "fe80:") == 0 || ipStr.compare(0, 6, "fe80::") == 0) - { - result.ula = ipStr; - NMLOG_DEBUG("link-local ip: %s", result.ula.c_str()); - } - else - { - result.prefix = nm_ip_address_get_prefix(ipAddr); - if(result.ipaddress.empty()) // SLAAC mutiple ip not added - result.ipaddress = ipStr; - NMLOG_DEBUG("global ip %s/%d", ipStr.c_str(), result.prefix); - } - } - } - - gateway = nm_ip_config_get_gateway(ip6_config); - if(gateway) - result.gateway= gateway; - dnsArr = (char **)nm_ip_config_get_nameservers(ip6_config); - if(dnsArr) - { - if(dnsArr[0]) - result.primarydns = std::string(dnsArr[0]); - if(dnsArr[1]) - result.secondarydns = std::string(dnsArr[1]); - } - dhcp6_config = nm_active_connection_get_dhcp6_config(conn); - if(dhcp6_config) - { - dhcpserver = nm_dhcp_config_get_one_option (dhcp6_config, "dhcp_server_identifier"); - if(dhcpserver) - result.dhcpserver = dhcpserver; - } - // Cache the IPv6 address - if(ethname == interface) - m_ethIPv6Address = result; - else if(wifiname == interface) - m_wlanIPv6Address = result; - } - } - if(result.ipaddress.empty()) - { - result.autoconfig = true; - if(ipversion.empty()) - result.ipversion = "IPv4"; - } return Core::ERROR_NONE; } diff --git a/tests/l2Test/libnm/l2_test_libnmproxy.cpp b/tests/l2Test/libnm/l2_test_libnmproxy.cpp index e4f9b1ec..ea34fd2d 100644 --- a/tests/l2Test/libnm/l2_test_libnmproxy.cpp +++ b/tests/l2Test/libnm/l2_test_libnmproxy.cpp @@ -515,24 +515,15 @@ TEST_F(NetworkManagerTest, GetIPSettings_unknown_iface) EXPECT_TRUE(response.find("\"success\":false") != std::string::npos); } -TEST_F(NetworkManagerTest, GetIPSettings_invalidDevice) +TEST_F(NetworkManagerTest, GetIPSettings_emptyCache) { - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_device_by_iface(::testing::_, ::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(NULL))); - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("GetIPSettings"), _T("{\"interface\":\"eth0\"}"), response)); - EXPECT_TRUE(response.find("\"success\":false") != std::string::npos); -} - -TEST_F(NetworkManagerTest, GetIPSettings_invalid_state) -{ - EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state(::testing::_)) - .WillOnce(::testing::Return(NM_DEVICE_STATE_UNMANAGED)); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_device_by_iface(::testing::_, ::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100178))); - + /* With no cache populated, GetIPSettings should still succeed but return no IP data */ EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("GetIPSettings"), _T("{\"interface\":\"eth0\"}"), response)); - EXPECT_TRUE(response.find("\"success\":false") != std::string::npos); + EXPECT_TRUE(response.find("\"success\":true") != std::string::npos); + EXPECT_TRUE(response.find("\"interface\":\"eth0\"") != std::string::npos); + EXPECT_TRUE(response.find("\"ipversion\":\"IPv4\"") != std::string::npos); + /* No ipaddress key when cache is empty */ + EXPECT_TRUE(response.find("\"ipaddress\"") == std::string::npos); } TEST_F(NetworkManagerTest, GetIPSettings_interface_Empty) @@ -548,254 +539,91 @@ TEST_F(NetworkManagerTest, GetIPSettings_GetPrimary_failed) EXPECT_EQ(Core::ERROR_GENERAL, NetworkManagerImpl2->GetIPSettings(interface, ipversion, address)); } -TEST_F(NetworkManagerTest, GetIPSettings_Invalid_ActiveConnection) +TEST_F(NetworkManagerTest, GetIPSettings_ipv4_fromCache) { - EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state(::testing::_)) - .WillOnce(::testing::Return(NM_DEVICE_STATE_ACTIVATED)); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_active_connections(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(NULL))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_device_by_iface(::testing::_, ::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100178))); + /* Populate IPv4 cache for eth0, then verify GetIPSettings returns cached data */ + Plugin::IpFamilyCache cache; + cache.valid = true; + cache.globalAddresses["192.168.1.2"] = Plugin::GlobalAddressInfo(24, Plugin::ADDR_GLOBAL); + cache.gateway = "192.168.1.1"; + cache.primarydns = "8.8.8.8"; + cache.secondarydns = "8.8.4.4"; + cache.dhcpserver = "192.168.1.11"; + cache.autoconfig = true; + NetworkManagerImpl->swapIpCache("eth0", "IPv4", cache); EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("GetIPSettings"), _T("{\"interface\":\"eth0\"}"), response)); - EXPECT_TRUE(response.find("\"success\":false") != std::string::npos); -} - -TEST_F(NetworkManagerTest, GetIPSettings_Invalid_Connection) -{ - GPtrArray* dummyActiveConn = g_ptr_array_new(); - NMActiveConnection *nullConnection = static_cast(NULL); - NMActiveConnection *ethActiveConn = static_cast(g_object_new(NM_TYPE_ACTIVE_CONNECTION, NULL)); - g_ptr_array_add(dummyActiveConn, nullConnection); - g_ptr_array_add(dummyActiveConn, ethActiveConn); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_active_connection_get_connection(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(NULL))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state(::testing::_)) - .WillOnce(::testing::Return(NM_DEVICE_STATE_ACTIVATED)); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_active_connections(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(dummyActiveConn))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_device_by_iface(::testing::_, ::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100178))); - - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("GetIPSettings"), _T("{\"interface\":\"eth0\"}"), response)); - EXPECT_TRUE(response.find("\"success\":false") != std::string::npos); - - g_object_unref(ethActiveConn); - g_ptr_array_free(dummyActiveConn, TRUE); -} - -TEST_F(NetworkManagerTest, GetIPSettings_valid_ConnectionSettingsEmpty) -{ - GPtrArray* dummyActiveConn = g_ptr_array_new(); - NMActiveConnection *ethActiveConn = static_cast(g_object_new(NM_TYPE_ACTIVE_CONNECTION, NULL)); - NMRemoteConnection* retConn = static_cast(g_object_new(NM_TYPE_REMOTE_CONNECTION, NULL)); - g_ptr_array_add(dummyActiveConn, ethActiveConn); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_connection_get_setting_connection(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(NULL))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_active_connection_get_connection(::testing::_)) - .WillOnce(::testing::Return(retConn)); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state(::testing::_)) - .WillOnce(::testing::Return(NM_DEVICE_STATE_ACTIVATED)); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_active_connections(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(dummyActiveConn))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_device_by_iface(::testing::_, ::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100178))); - - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("GetIPSettings"), _T("{\"interface\":\"eth0\"}"), response)); - EXPECT_TRUE(response.find("\"success\":false") != std::string::npos); - - g_object_unref(ethActiveConn); - g_ptr_array_free(dummyActiveConn, TRUE); + EXPECT_TRUE(response.find("\"success\":true") != std::string::npos); + EXPECT_TRUE(response.find("\"interface\":\"eth0\"") != std::string::npos); + EXPECT_TRUE(response.find("\"ipversion\":\"IPv4\"") != std::string::npos); + EXPECT_TRUE(response.find("\"autoconfig\":true") != std::string::npos); + EXPECT_TRUE(response.find("\"ipaddress\":\"192.168.1.2\"") != std::string::npos); + EXPECT_TRUE(response.find("\"prefix\":24") != std::string::npos); + EXPECT_TRUE(response.find("\"gateway\":\"192.168.1.1\"") != std::string::npos); + EXPECT_TRUE(response.find("\"primarydns\":\"8.8.8.8\"") != std::string::npos); + EXPECT_TRUE(response.find("\"secondarydns\":\"8.8.4.4\"") != std::string::npos); + EXPECT_TRUE(response.find("\"dhcpserver\":\"192.168.1.11\"") != std::string::npos); } -TEST_F(NetworkManagerTest, GetIPSettings_ipv4_config) +TEST_F(NetworkManagerTest, GetIPSettings_ipv4_autoconfig) { - GPtrArray* dummyActiveConn = g_ptr_array_new(); - NMActiveConnection *ethActiveConn = static_cast(g_object_new(NM_TYPE_ACTIVE_CONNECTION, NULL)); - NMRemoteConnection* retConn = static_cast(g_object_new(NM_TYPE_REMOTE_CONNECTION, NULL)); - g_ptr_array_add(dummyActiveConn, ethActiveConn); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_active_connection_get_ip4_config(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(NULL))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_setting_connection_get_interface_name(::testing::_)) - .WillOnce(::testing::Return("eth0")); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_connection_get_setting_connection(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100173))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_active_connection_get_connection(::testing::_)) - .WillOnce(::testing::Return(retConn)) - .WillOnce(::testing::Return(reinterpret_cast(NULL))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state(::testing::_)) - .WillOnce(::testing::Return(NM_DEVICE_STATE_ACTIVATED)); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_active_connections(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(dummyActiveConn))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_device_by_iface(::testing::_, ::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100178))); + /* Populate IPv4 cache with autoconfig=true but no IP address */ + Plugin::IpFamilyCache cache; + cache.valid = true; + cache.autoconfig = true; + NetworkManagerImpl->swapIpCache("eth0", "IPv4", cache); EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("GetIPSettings"), _T("{\"interface\":\"eth0\"}"), response)); std::string expectedResponse = _T("{\"interface\":\"eth0\",\"ipversion\":\"IPv4\",\"autoconfig\":true,\"success\":true}"); EXPECT_EQ(response, expectedResponse); - - g_object_unref(ethActiveConn); - g_ptr_array_free(dummyActiveConn, TRUE); } -TEST_F(NetworkManagerTest, GetIPSettings_ipv4_configAutoConftrue) +TEST_F(NetworkManagerTest, GetIPSettings_ipv4_staticConfig) { - GPtrArray* dummyActiveConn = g_ptr_array_new(); - NMActiveConnection *ethActiveConn = static_cast(g_object_new(NM_TYPE_ACTIVE_CONNECTION, NULL)); - NMRemoteConnection* retConn = static_cast(g_object_new(NM_TYPE_REMOTE_CONNECTION, NULL)); - g_ptr_array_add(dummyActiveConn, ethActiveConn); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_active_connection_get_ip4_config(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(NULL))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_setting_connection_get_interface_name(::testing::_)) - .WillOnce(::testing::Return("eth0")); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_connection_get_setting_connection(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100173))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_setting_ip_config_get_method(::testing::_)) - .WillOnce(::testing::Return("auto")); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_connection_get_setting_ip4_config(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100173))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_active_connection_get_connection(::testing::_)) - .WillOnce(::testing::Return(retConn)) - .WillOnce(::testing::Return(reinterpret_cast(retConn))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state(::testing::_)) - .WillOnce(::testing::Return(NM_DEVICE_STATE_ACTIVATED)); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_active_connections(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(dummyActiveConn))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_device_by_iface(::testing::_, ::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100178))); + /* Populate IPv4 cache with autoconfig=false (static config) */ + Plugin::IpFamilyCache cache; + cache.valid = true; + cache.autoconfig = false; + cache.globalAddresses["192.168.1.100"] = Plugin::GlobalAddressInfo(24, Plugin::ADDR_GLOBAL); + cache.gateway = "192.168.1.1"; + NetworkManagerImpl->swapIpCache("eth0", "IPv4", cache); EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("GetIPSettings"), _T("{\"interface\":\"eth0\"}"), response)); - std::string expectedResponse = - _T("{\"interface\":\"eth0\",\"ipversion\":\"IPv4\",\"autoconfig\":true,\"success\":true}"); - EXPECT_EQ(response, expectedResponse); - - g_object_unref(ethActiveConn); - g_ptr_array_free(dummyActiveConn, TRUE); + EXPECT_TRUE(response.find("\"success\":true") != std::string::npos); + EXPECT_TRUE(response.find("\"autoconfig\":false") != std::string::npos); + EXPECT_TRUE(response.find("\"ipaddress\":\"192.168.1.100\"") != std::string::npos); } -TEST_F(NetworkManagerTest, GetIPSettings_ipv4_configAutoConfNull) +TEST_F(NetworkManagerTest, GetIPSettings_wlan0_fromCache) { - GPtrArray* dummyActiveConn = g_ptr_array_new(); - NMActiveConnection *ethActiveConn = static_cast(g_object_new(NM_TYPE_ACTIVE_CONNECTION, NULL)); - NMRemoteConnection* retConn = static_cast(g_object_new(NM_TYPE_REMOTE_CONNECTION, NULL)); - g_ptr_array_add(dummyActiveConn, ethActiveConn); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_active_connection_get_ip4_config(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(NULL))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_setting_connection_get_interface_name(::testing::_)) - .WillOnce(::testing::Return("eth0")); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_connection_get_setting_connection(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100173))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_setting_ip_config_get_method(::testing::_)) - .WillOnce(::testing::Return("not auto")); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_connection_get_setting_ip4_config(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100173))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_active_connection_get_connection(::testing::_)) - .WillOnce(::testing::Return(retConn)) - .WillOnce(::testing::Return(reinterpret_cast(retConn))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state(::testing::_)) - .WillOnce(::testing::Return(NM_DEVICE_STATE_ACTIVATED)); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_active_connections(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(dummyActiveConn))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_device_by_iface(::testing::_, ::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100178))); - - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("GetIPSettings"), _T("{\"interface\":\"eth0\"}"), response)); - std::string expectedResponse = - _T("{\"interface\":\"eth0\",\"ipversion\":\"IPv4\",\"autoconfig\":true,\"success\":true}"); - EXPECT_EQ(response, expectedResponse); - - g_object_unref(ethActiveConn); - g_ptr_array_free(dummyActiveConn, TRUE); + /* Populate IPv4 cache for wlan0 */ + Plugin::IpFamilyCache cache; + cache.valid = true; + cache.autoconfig = true; + cache.globalAddresses["10.0.0.5"] = Plugin::GlobalAddressInfo(8, Plugin::ADDR_GLOBAL); + cache.gateway = "10.0.0.1"; + cache.primarydns = "1.1.1.1"; + NetworkManagerImpl->swapIpCache("wlan0", "IPv4", cache); + + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("GetIPSettings"), _T("{\"interface\":\"wlan0\"}"), response)); + EXPECT_TRUE(response.find("\"success\":true") != std::string::npos); + EXPECT_TRUE(response.find("\"interface\":\"wlan0\"") != std::string::npos); + EXPECT_TRUE(response.find("\"ipaddress\":\"10.0.0.5\"") != std::string::npos); } TEST_F(NetworkManagerTest, GetIPSettings_ipv4_config_valid) { - NMActiveConnection *ethActiveConn = static_cast(g_object_new(NM_TYPE_ACTIVE_CONNECTION, NULL)); - NMRemoteConnection* retConn = static_cast(g_object_new(NM_TYPE_REMOTE_CONNECTION, NULL)); - NMIPAddress* ipv4Addr = static_cast(g_object_new(NM_TYPE_REMOTE_CONNECTION, NULL)); - - GPtrArray* dummyActiveConn = g_ptr_array_new(); - GPtrArray* ipvAddr = g_ptr_array_new(); - g_ptr_array_add(dummyActiveConn, ethActiveConn); - g_ptr_array_add(ipvAddr, ipv4Addr); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_dhcp_config_get_one_option(::testing::_, ::testing::_)) - .WillOnce(::testing::Return("192.168.1.11")); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_active_connection_get_dhcp4_config(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100170))); - - const char* fakeDnsServers[] = {"8.8.8.8", "8.8.4.4", nullptr}; - EXPECT_CALL(*p_libnmWrapsImplMock, nm_ip_config_get_nameservers(::testing::_)) - .WillOnce(::testing::Return(fakeDnsServers)); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_ip_config_get_gateway(::testing::_)) - .WillOnce(::testing::Return("192.168.1.0")); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_ip_address_get_address(::testing::_)) - .WillOnce(::testing::Return("192.168.1.2")); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_ip_config_get_addresses(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(ipvAddr))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_active_connection_get_ip4_config(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100171))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_setting_connection_get_interface_name(::testing::_)) - .WillOnce(::testing::Return("eth0")); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_connection_get_setting_connection(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100173))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_active_connection_get_connection(::testing::_)) - .WillOnce(::testing::Return(retConn)) - .WillOnce(::testing::Return(reinterpret_cast(NULL))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state(::testing::_)) - .WillOnce(::testing::Return(NM_DEVICE_STATE_ACTIVATED)); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_active_connections(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(dummyActiveConn))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_device_by_iface(::testing::_, ::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100178))); + /* Populate full IPv4 cache for eth0 and verify all fields */ + Plugin::IpFamilyCache cache; + cache.valid = true; + cache.autoconfig = true; + cache.globalAddresses["192.168.1.2"] = Plugin::GlobalAddressInfo(24, Plugin::ADDR_GLOBAL); + cache.gateway = "192.168.1.0"; + cache.primarydns = "8.8.8.8"; + cache.secondarydns = "8.8.4.4"; + cache.dhcpserver = "192.168.1.11"; + NetworkManagerImpl->swapIpCache("eth0", "IPv4", cache); EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("GetIPSettings"), _T("{\"interface\":\"eth0\"}"), response)); @@ -807,70 +635,21 @@ TEST_F(NetworkManagerTest, GetIPSettings_ipv4_config_valid) EXPECT_TRUE(response.find("\"ula\":\"\"") != std::string::npos); EXPECT_TRUE(response.find("\"dhcpserver\":\"192.168.1.11\"") != std::string::npos); EXPECT_TRUE(response.find("\"gateway\":\"192.168.1.0\"") != std::string::npos); - - g_object_unref(ethActiveConn); - g_object_unref(retConn); - g_object_unref(ipv4Addr); - g_ptr_array_free(dummyActiveConn, TRUE); - g_ptr_array_free(ipvAddr, TRUE); } TEST_F(NetworkManagerTest, GetIPSettings_ipv6_config_valid) { - NMActiveConnection *ethActiveConn = static_cast(g_object_new(NM_TYPE_ACTIVE_CONNECTION, NULL)); - NMRemoteConnection* retConn = static_cast(g_object_new(NM_TYPE_REMOTE_CONNECTION, NULL)); - NMIPAddress* ipv6Addr = static_cast(g_object_new(NM_TYPE_REMOTE_CONNECTION, NULL)); - - GPtrArray* dummyActiveConn = g_ptr_array_new(); - GPtrArray* ipvAddr = g_ptr_array_new(); - g_ptr_array_add(dummyActiveConn, ethActiveConn); - g_ptr_array_add(ipvAddr, ipv6Addr); - g_ptr_array_add(ipvAddr, reinterpret_cast(0x100176)); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_dhcp_config_get_one_option(::testing::_, ::testing::_)) - .WillOnce(::testing::Return("2001:db8::1")); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_active_connection_get_dhcp6_config(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100170))); - - const char* fakeDnsServers[] = {"2001:4860:4860::8888", "2001:4860:4860::8844", nullptr}; - EXPECT_CALL(*p_libnmWrapsImplMock, nm_ip_config_get_nameservers(::testing::_)) - .WillOnce(::testing::Return(fakeDnsServers)); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_ip_config_get_gateway(::testing::_)) - .WillOnce(::testing::Return("2001:4860:4860::1")); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_ip_address_get_prefix(::testing::_)) - .WillOnce(::testing::Return(64)); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_ip_address_get_address(::testing::_)) - .WillOnce(::testing::Return("2001:db8:1:2:3:4:5:6")) - .WillOnce(::testing::Return("fe80::1234:5678:abcd:ef01")); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_ip_config_get_addresses(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(ipvAddr))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_active_connection_get_ip6_config(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100171))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_setting_connection_get_interface_name(::testing::_)) - .WillOnce(::testing::Return("eth0")); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_connection_get_setting_connection(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100173))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_active_connection_get_connection(::testing::_)) - .WillOnce(::testing::Return(retConn)) - .WillOnce(::testing::Return(reinterpret_cast(NULL))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state(::testing::_)) - .WillOnce(::testing::Return(NM_DEVICE_STATE_ACTIVATED)); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_active_connections(::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(dummyActiveConn))); - - EXPECT_CALL(*p_libnmWrapsImplMock, nm_client_get_device_by_iface(::testing::_, ::testing::_)) - .WillOnce(::testing::Return(reinterpret_cast(0x100178))); + /* Populate full IPv6 cache for eth0 and verify all fields */ + Plugin::IpFamilyCache cache; + cache.valid = true; + cache.autoconfig = true; + cache.globalAddresses["2001:db8:1:2:3:4:5:6"] = Plugin::GlobalAddressInfo(64, Plugin::ADDR_GLOBAL); + cache.uniqueLocalAddresses.insert("fd12::1234:5678:abcd:ef01"); + cache.gateway = "2001:4860:4860::1"; + cache.primarydns = "2001:4860:4860::8888"; + cache.secondarydns = "2001:4860:4860::8844"; + cache.dhcpserver = "2001:db8::1"; + NetworkManagerImpl->swapIpCache("eth0", "IPv6", cache); EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("GetIPSettings"), _T("{\"interface\":\"eth0\", \"ipversion\":\"IPv6\"}"), response)); @@ -880,16 +659,10 @@ TEST_F(NetworkManagerTest, GetIPSettings_ipv6_config_valid) EXPECT_TRUE(response.find("\"primarydns\":\"2001:4860:4860::8888\"") != std::string::npos); EXPECT_TRUE(response.find("\"interface\":\"eth0\"") != std::string::npos); EXPECT_TRUE(response.find("\"ipaddress\":\"2001:db8:1:2:3:4:5:6\"") != std::string::npos); - EXPECT_TRUE(response.find("\"ula\":\"fe80::1234:5678:abcd:ef01\"") != std::string::npos); + EXPECT_TRUE(response.find("\"ula\":\"fd12::1234:5678:abcd:ef01\"") != std::string::npos); EXPECT_TRUE(response.find("\"prefix\":64") != std::string::npos); EXPECT_TRUE(response.find("\"dhcpserver\":\"2001:db8::1\"") != std::string::npos); EXPECT_TRUE(response.find("\"gateway\":\"2001:4860:4860::1\"") != std::string::npos); - - g_object_unref(ethActiveConn); - g_object_unref(retConn); - g_object_unref(ipv6Addr); - g_ptr_array_free(dummyActiveConn, TRUE); - g_ptr_array_free(ipvAddr, TRUE); } TEST_F(NetworkManagerTest, SetInterfaceState_deviceFailed_wlan0) diff --git a/tests/l2Test/libnm/l2_test_libnmproxyEvent.cpp b/tests/l2Test/libnm/l2_test_libnmproxyEvent.cpp index c587688a..a86bd07e 100644 --- a/tests/l2Test/libnm/l2_test_libnmproxyEvent.cpp +++ b/tests/l2Test/libnm/l2_test_libnmproxyEvent.cpp @@ -208,31 +208,6 @@ TEST_F(NetworkManagerEventTest, onInterfaceStateChangeCb) WPEFramework::Plugin::GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_DISABLED, "eth0"); } -TEST_F(NetworkManagerEventTest, onAddressChangeCb) -{ - // Test acquiring IPv4 address - WPEFramework::Plugin::GnomeNetworkManagerEvents::onAddressChangeCb("eth0", "192.168.1.100", true, false); - - // Test acquiring IPv6 address - WPEFramework::Plugin::GnomeNetworkManagerEvents::onAddressChangeCb("eth0", "2001:db8::1", true, true); - - // Test acquiring same IPv6 address again (should skip posting) - WPEFramework::Plugin::GnomeNetworkManagerEvents::onAddressChangeCb("eth0", "2001:db8::1", true, true); - - // Test acquiring different IPv6 address - WPEFramework::Plugin::GnomeNetworkManagerEvents::onAddressChangeCb("eth0", "2001:db8::2", true, true); - - // Test losing IPv4 address - WPEFramework::Plugin::GnomeNetworkManagerEvents::onAddressChangeCb("eth0", "", false, false); - - // Test losing IPv6 address - WPEFramework::Plugin::GnomeNetworkManagerEvents::onAddressChangeCb("eth0", "", false, true); - - // Test losing IP on interface with empty cache (should skip posting) - WPEFramework::Plugin::GnomeNetworkManagerEvents::onAddressChangeCb("eth1", "", false, false); - WPEFramework::Plugin::GnomeNetworkManagerEvents::onAddressChangeCb("eth1", "", false, true); -} - TEST_F(NetworkManagerEventTest, onAvailableSSIDsCb) { GPtrArray* fakeDevices = g_ptr_array_new(); @@ -372,9 +347,9 @@ TEST_F(NetworkManagerEventTest, deviceStateChangeCb_disconnected) { NMDevice *wifiDummyDevice = static_cast(g_object_new(NM_TYPE_DEVICE_WIFI, NULL)); EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state(::testing::_)) - .WillOnce(::testing::Return(NM_DEVICE_STATE_DISCONNECTED)); + .WillRepeatedly(::testing::Return(NM_DEVICE_STATE_DISCONNECTED)); EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_iface(::testing::_)) - .WillOnce(::testing::Return("wlan0")); + .WillRepeatedly(::testing::Return("wlan0")); EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state_reason(::testing::_)) .WillOnce(::testing::Return(NM_DEVICE_STATE_REASON_NONE)); WPEFramework::Plugin::GnomeNetworkManagerEvents::deviceStateChangeCb(reinterpret_cast(wifiDummyDevice), nullptr, nullptr); @@ -384,9 +359,9 @@ TEST_F(NetworkManagerEventTest, deviceStateChangeCb_unmanaged) { NMDevice *wifiDummyDevice = static_cast(g_object_new(NM_TYPE_DEVICE_WIFI, NULL)); EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state(::testing::_)) - .WillOnce(::testing::Return(NM_DEVICE_STATE_UNMANAGED)); + .WillRepeatedly(::testing::Return(NM_DEVICE_STATE_UNMANAGED)); EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_iface(::testing::_)) - .WillOnce(::testing::Return("wlan0")); + .WillRepeatedly(::testing::Return("wlan0")); EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state_reason(::testing::_)) .WillOnce(::testing::Return(NM_DEVICE_STATE_REASON_NONE)); WPEFramework::Plugin::GnomeNetworkManagerEvents::deviceStateChangeCb(reinterpret_cast(wifiDummyDevice), nullptr, nullptr); @@ -492,9 +467,9 @@ TEST_F(NetworkManagerEventTest, deviceStateChangeCb_unknown) { NMDevice *wifiDummyDevice = static_cast(g_object_new(NM_TYPE_DEVICE_WIFI, NULL)); EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state(::testing::_)) - .WillOnce(::testing::Return(NM_DEVICE_STATE_UNKNOWN)); + .WillRepeatedly(::testing::Return(NM_DEVICE_STATE_UNKNOWN)); EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_iface(::testing::_)) - .WillOnce(::testing::Return("wlan0")); + .WillRepeatedly(::testing::Return("wlan0")); EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state_reason(::testing::_)) .WillOnce(::testing::Return(NM_DEVICE_STATE_REASON_NONE)); WPEFramework::Plugin::GnomeNetworkManagerEvents::deviceStateChangeCb(reinterpret_cast(wifiDummyDevice), nullptr, nullptr); @@ -504,9 +479,9 @@ TEST_F(NetworkManagerEventTest, deviceStateChangeCb_eth0_unmanaged) { NMDevice *DummyDevice = static_cast(g_object_new(NM_TYPE_DEVICE_ETHERNET, NULL)); EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state(::testing::_)) - .WillOnce(::testing::Return(NM_DEVICE_STATE_UNMANAGED)); + .WillRepeatedly(::testing::Return(NM_DEVICE_STATE_UNMANAGED)); EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_iface(::testing::_)) - .WillOnce(::testing::Return("eth0")); + .WillRepeatedly(::testing::Return("eth0")); EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state_reason(::testing::_)) .WillOnce(::testing::Return(NM_DEVICE_STATE_REASON_NONE)); WPEFramework::Plugin::GnomeNetworkManagerEvents::deviceStateChangeCb(reinterpret_cast(DummyDevice), nullptr, nullptr); @@ -517,9 +492,9 @@ TEST_F(NetworkManagerEventTest, deviceStateChangeCb_eth0_disconnected) { NMDevice *DummyDevice = static_cast(g_object_new(NM_TYPE_DEVICE_ETHERNET, NULL)); EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state(::testing::_)) - .WillOnce(::testing::Return(NM_DEVICE_STATE_DISCONNECTED)); + .WillRepeatedly(::testing::Return(NM_DEVICE_STATE_DISCONNECTED)); EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_iface(::testing::_)) - .WillOnce(::testing::Return("eth0")); + .WillRepeatedly(::testing::Return("eth0")); EXPECT_CALL(*p_libnmWrapsImplMock, nm_device_get_state_reason(::testing::_)) .WillOnce(::testing::Return(NM_DEVICE_STATE_REASON_NONE)); WPEFramework::Plugin::GnomeNetworkManagerEvents::deviceStateChangeCb(reinterpret_cast(DummyDevice), nullptr, nullptr); diff --git a/tests/l2Test/libnm/l2_test_libnmproxyInit.cpp b/tests/l2Test/libnm/l2_test_libnmproxyInit.cpp index e47f24fb..b550b3d6 100644 --- a/tests/l2Test/libnm/l2_test_libnmproxyInit.cpp +++ b/tests/l2Test/libnm/l2_test_libnmproxyInit.cpp @@ -158,7 +158,7 @@ TEST_F(NetworkManagerInitTest, platformInit) EXPECT_EQ(response, _T("{\"success\":false}")); EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("GetIPSettings"), _T("{\"interface\":\"wlan0\"}"), response)); - EXPECT_EQ(response, _T("{\"success\":false}")); + EXPECT_TRUE(response.find("\"success\":true") != std::string::npos); EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("SetHostname"), _T("{\"hostname\":\"test-host\"}"), response)); EXPECT_EQ(response, _T("{\"success\":false}"));