rtnetlink_server: Don't refresh for addresses

We can dynamically add and remove addresses without having to recreate
all of the objects.

Change-Id: Ic510263cc6ba5d2ec228ce94fa83cf5fe4664c6d
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/rtnetlink_server.cpp b/src/rtnetlink_server.cpp
index 880eba1..2a0ab38 100644
--- a/src/rtnetlink_server.cpp
+++ b/src/rtnetlink_server.cpp
@@ -36,8 +36,6 @@
     {
         case RTM_NEWLINK:
         case RTM_DELLINK:
-        case RTM_NEWADDR:
-        case RTM_DELADDR:
             return true;
         case RTM_NEWNEIGH:
         case RTM_DELNEIGH:
@@ -103,6 +101,26 @@
         std::get<InAddrAny>(*ret));
 }
 
+static void addrhandler(Manager& m, bool n, std::string_view data)
+{
+    auto info = netlink::addrFromRtm(data);
+    auto it = m.interfacesByIdx.find(info.ifidx);
+    if (it == m.interfacesByIdx.end())
+    {
+        auto msg = fmt::format("Interface `{}` not found for addr", info.ifidx);
+        log<level::ERR>(msg.c_str(), entry("IFIDX=%u", info.ifidx));
+        return;
+    }
+    if (n)
+    {
+        it->second->addAddr(info);
+    }
+    else
+    {
+        it->second->addrs.erase(info.ifaddr);
+    }
+}
+
 static void handler(Manager& m, const nlmsghdr& hdr, std::string_view data)
 {
     if (shouldRefresh(hdr, data) && !refreshObjectTimer->isEnabled())
@@ -117,6 +135,12 @@
         case RTM_DELROUTE:
             rthandler(m, false, data);
             break;
+        case RTM_NEWADDR:
+            addrhandler(m, true, data);
+            break;
+        case RTM_DELADDR:
+            addrhandler(m, false, data);
+            break;
     }
 }