OSDN Git Service

Make netd track and notify IP address changes.
authorLorenzo Colitti <lorenzo@google.com>
Thu, 1 Aug 2013 20:57:47 +0000 (05:57 +0900)
committerLorenzo Colitti <lorenzo@google.com>
Fri, 9 Aug 2013 03:43:30 +0000 (12:43 +0900)
Subscribe netd's netlink socket to listen to IPv4 and IPv6
address changes (and ND opts, which we'll need for IPv6 DNS
later), and make NetlinkHandler notify the system of address
changes.

Bug: 10232006
Change-Id: Ib9dfd58635dce389980d8ee9529a17661a02320a

NetlinkHandler.cpp
NetlinkHandler.h
NetlinkManager.cpp
ResponseCode.h

index 6621a9a..753b92b 100644 (file)
@@ -52,7 +52,7 @@ void NetlinkHandler::onEvent(NetlinkEvent *evt) {
         return;
     }
 
-    if (!strcmp(subsys, "net")) {
+    if (!strcmp(subsys, "interface")) {
         int action = evt->getAction();
         const char *iface = evt->findParam("INTERFACE");
 
@@ -69,6 +69,16 @@ void NetlinkHandler::onEvent(NetlinkEvent *evt) {
             notifyInterfaceLinkChanged(iface, false);
         }
 
+    } else if (!strcmp(subsys, "address")) {
+        const char *address = evt->findParam("ADDRESS");
+        const char *iface = evt->findParam("IFACE");
+        const char *flags = evt->findParam("FLAGS");
+        const char *scope = evt->findParam("SCOPE");
+
+        if (iface && flags && scope) {
+            notifyAddressChanged(evt->getAction(), address, iface, flags, scope);
+        }
+
     } else if (!strcmp(subsys, "qlog")) {
         const char *alertName = evt->findParam("ALERT_NAME");
         const char *iface = evt->findParam("INTERFACE");
@@ -145,3 +155,15 @@ void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
     mNm->getBroadcaster()->sendBroadcast(
         ResponseCode::InterfaceClassActivity, msg, false);
 }
+
+void NetlinkHandler::notifyAddressChanged(int action, const char *addr,
+                                          const char *iface, const char *flags,
+                                          const char *scope) {
+    char msg[255];
+    snprintf(msg, sizeof(msg), "Address %s %s %s %s %s",
+             (action == NetlinkEvent::NlActionAdd) ? "updated" : "removed",
+             addr, iface, flags, scope);
+
+    mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceAddressChange,
+            msg, false);
+}
index 50bface..0cbf339 100644 (file)
@@ -39,5 +39,7 @@ protected:
     void notifyInterfaceLinkChanged(const char *name, bool isUp);
     void notifyQuotaLimitReached(const char *name, const char *iface);
     void notifyInterfaceClassActivity(const char *name, bool isActive);
+    void notifyAddressChanged(int action, const char *addr, const char *iface,
+                              const char *flags, const char *scope);
 };
 #endif
index 8e2bc69..32578a1 100644 (file)
@@ -101,7 +101,11 @@ int NetlinkManager::start() {
         return -1;
     }
 
-    if ((mRouteHandler = setupSocket(&mRouteSock, NETLINK_ROUTE, RTMGRP_LINK,
+    if ((mRouteHandler = setupSocket(&mRouteSock, NETLINK_ROUTE,
+                                     RTMGRP_LINK |
+                                     RTMGRP_IPV4_IFADDR |
+                                     RTMGRP_IPV6_IFADDR |
+                                     (1 << (RTNLGRP_ND_USEROPT - 1)),
          NetlinkListener::NETLINK_FORMAT_BINARY)) == NULL) {
         return -1;
     }
index c5792c7..46b842d 100644 (file)
@@ -75,5 +75,6 @@ public:
     static const int ServiceGetAddrInfoFailed       = 611;
     static const int ServiceGetAddrInfoSuccess      = 612;
     static const int InterfaceClassActivity         = 613;
+    static const int InterfaceAddressChange         = 614;
 };
 #endif