network_manager: Keep interface info around

This will allow us to rebuild objects if needed and doesn't take much
more memory to store.

Change-Id: Ifcc8b2b62472167e9cebbead4b6f9c6565c06f0f
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/network_manager.cpp b/src/network_manager.cpp
index 82eb2c5..8dc14e6 100644
--- a/src/network_manager.cpp
+++ b/src/network_manager.cpp
@@ -117,7 +117,7 @@
         bus, (this->objPath / "dhcp").str, *this);
 }
 
-void Manager::createInterface(const UndiscoveredInfo& info, bool enabled)
+void Manager::createInterface(const AllIntfInfo& info, bool enabled)
 {
     if (auto it = interfacesByIdx.find(info.intf.idx);
         it != interfacesByIdx.end())
@@ -189,15 +189,20 @@
         }
     }
 
-    auto it = systemdNetworkdEnabled.find(info.idx);
-    if (it != systemdNetworkdEnabled.end())
+    auto infoIt = intfInfo.find(info.idx);
+    if (infoIt != intfInfo.end())
     {
-        createInterface({info}, it->second);
+        infoIt->second.intf = info;
     }
     else
     {
-        undiscoveredIntfInfo.insert_or_assign(
-            info.idx, UndiscoveredInfo{std::move(info)});
+        infoIt = std::get<0>(intfInfo.emplace(info.idx, AllIntfInfo{info}));
+    }
+
+    if (auto it = systemdNetworkdEnabled.find(info.idx);
+        it != systemdNetworkdEnabled.end())
+    {
+        createInterface(infoIt->second, it->second);
     }
 }
 
@@ -233,13 +238,13 @@
     }
     else
     {
-        undiscoveredIntfInfo.erase(info.idx);
         ignoredIntf.erase(info.idx);
     }
     if (nit != interfaces.end())
     {
         interfaces.erase(nit);
     }
+    intfInfo.erase(info.idx);
 }
 
 void Manager::addAddress(const AddressInfo& info)
@@ -248,14 +253,14 @@
     {
         return;
     }
-    if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end())
-    {
-        it->second->addAddr(info);
-    }
-    else if (auto it = undiscoveredIntfInfo.find(info.ifidx);
-             it != undiscoveredIntfInfo.end())
+    if (auto it = intfInfo.find(info.ifidx); it != intfInfo.end())
     {
         it->second.addrs.insert_or_assign(info.ifaddr, info);
+        if (auto it = interfacesByIdx.find(info.ifidx);
+            it != interfacesByIdx.end())
+        {
+            it->second->addAddr(info);
+        }
     }
     else if (!ignoredIntf.contains(info.ifidx))
     {
@@ -269,11 +274,10 @@
     if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end())
     {
         it->second->addrs.erase(info.ifaddr);
-    }
-    else if (auto it = undiscoveredIntfInfo.find(info.ifidx);
-             it != undiscoveredIntfInfo.end())
-    {
-        it->second.addrs.erase(info.ifaddr);
+        if (auto it = intfInfo.find(info.ifidx); it != intfInfo.end())
+        {
+            it->second.addrs.erase(info.ifaddr);
+        }
     }
 }
 
@@ -283,14 +287,14 @@
     {
         return;
     }
-    if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end())
-    {
-        it->second->addStaticNeigh(info);
-    }
-    else if (auto it = undiscoveredIntfInfo.find(info.ifidx);
-             it != undiscoveredIntfInfo.end())
+    if (auto it = intfInfo.find(info.ifidx); it != intfInfo.end())
     {
         it->second.staticNeighs.insert_or_assign(*info.addr, info);
+        if (auto it = interfacesByIdx.find(info.ifidx);
+            it != interfacesByIdx.end())
+        {
+            it->second->addStaticNeigh(info);
+        }
     }
     else if (!ignoredIntf.contains(info.ifidx))
     {
@@ -305,42 +309,20 @@
     {
         return;
     }
-    if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end())
-    {
-        it->second->staticNeighbors.erase(*info.addr);
-    }
-    else if (auto it = undiscoveredIntfInfo.find(info.ifidx);
-             it != undiscoveredIntfInfo.end())
+    if (auto it = intfInfo.find(info.ifidx); it != intfInfo.end())
     {
         it->second.staticNeighs.erase(*info.addr);
+        if (auto it = interfacesByIdx.find(info.ifidx);
+            it != interfacesByIdx.end())
+        {
+            it->second->staticNeighbors.erase(*info.addr);
+        }
     }
 }
 
 void Manager::addDefGw(unsigned ifidx, InAddrAny addr)
 {
-    if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
-    {
-        std::visit(
-            [&](auto addr) {
-                if constexpr (std::is_same_v<in_addr, decltype(addr)>)
-                {
-                    it->second->EthernetInterfaceIntf::defaultGateway(
-                        std::to_string(addr));
-                }
-                else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
-                {
-                    it->second->EthernetInterfaceIntf::defaultGateway6(
-                        std::to_string(addr));
-                }
-                else
-                {
-                    static_assert(!std::is_same_v<void, decltype(addr)>);
-                }
-            },
-            addr);
-    }
-    else if (auto it = undiscoveredIntfInfo.find(ifidx);
-             it != undiscoveredIntfInfo.end())
+    if (auto it = intfInfo.find(ifidx); it != intfInfo.end())
     {
         std::visit(
             [&](auto addr) {
@@ -358,6 +340,27 @@
                 }
             },
             addr);
+        if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
+        {
+            std::visit(
+                [&](auto addr) {
+                    if constexpr (std::is_same_v<in_addr, decltype(addr)>)
+                    {
+                        it->second->EthernetInterfaceIntf::defaultGateway(
+                            std::to_string(addr));
+                    }
+                    else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
+                    {
+                        it->second->EthernetInterfaceIntf::defaultGateway6(
+                            std::to_string(addr));
+                    }
+                    else
+                    {
+                        static_assert(!std::is_same_v<void, decltype(addr)>);
+                    }
+                },
+                addr);
+        }
     }
     else if (!ignoredIntf.contains(ifidx))
     {
@@ -368,33 +371,7 @@
 
 void Manager::removeDefGw(unsigned ifidx, InAddrAny addr)
 {
-    if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
-    {
-        std::visit(
-            [&](auto addr) {
-                if constexpr (std::is_same_v<in_addr, decltype(addr)>)
-                {
-                    if (it->second->defaultGateway() == std::to_string(addr))
-                    {
-                        it->second->EthernetInterfaceIntf::defaultGateway("");
-                    }
-                }
-                else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
-                {
-                    if (it->second->defaultGateway6() == std::to_string(addr))
-                    {
-                        it->second->EthernetInterfaceIntf::defaultGateway6("");
-                    }
-                }
-                else
-                {
-                    static_assert(!std::is_same_v<void, decltype(addr)>);
-                }
-            },
-            addr);
-    }
-    else if (auto it = undiscoveredIntfInfo.find(ifidx);
-             it != undiscoveredIntfInfo.end())
+    if (auto it = intfInfo.find(ifidx); it != intfInfo.end())
     {
         std::visit(
             [&](auto addr) {
@@ -418,6 +395,35 @@
                 }
             },
             addr);
+        if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
+        {
+            std::visit(
+                [&](auto addr) {
+                    if constexpr (std::is_same_v<in_addr, decltype(addr)>)
+                    {
+                        if (it->second->defaultGateway() ==
+                            std::to_string(addr))
+                        {
+                            it->second->EthernetInterfaceIntf::defaultGateway(
+                                "");
+                        }
+                    }
+                    else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
+                    {
+                        if (it->second->defaultGateway6() ==
+                            std::to_string(addr))
+                        {
+                            it->second->EthernetInterfaceIntf::defaultGateway6(
+                                "");
+                        }
+                    }
+                    else
+                    {
+                        static_assert(!std::is_same_v<void, decltype(addr)>);
+                    }
+                },
+                addr);
+        }
     }
 }
 
@@ -540,18 +546,14 @@
     {
         bool managed = state != "unmanaged";
         systemdNetworkdEnabled.insert_or_assign(ifidx, managed);
-        if (auto it = undiscoveredIntfInfo.find(ifidx);
-            it != undiscoveredIntfInfo.end())
-        {
-            auto info = std::move(it->second);
-            undiscoveredIntfInfo.erase(it);
-            createInterface(info, managed);
-        }
-        else if (auto it = interfacesByIdx.find(ifidx);
-                 it != interfacesByIdx.end())
+        if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
         {
             it->second->EthernetInterfaceIntf::nicEnabled(managed);
         }
+        else if (auto it = intfInfo.find(ifidx); it != intfInfo.end())
+        {
+            createInterface(it->second, managed);
+        }
     }
 }