Bug 1579424 - P3 Use ifname and ip as a fail over, r=michal
authorJunior Hsu <juhsu@mozilla.com>
Tue, 12 Nov 2019 16:05:08 +0000
changeset 502269 1d6992a1811e20062d4813630fe7a4e23c37c142
parent 502268 500126439162e6a839f8c0bc0d8355ad0f5386e2
child 502270 bc41fd81784b182e71b5b13b9ec5cf29cbc8e88e
push id114172
push userdluca@mozilla.com
push dateTue, 19 Nov 2019 11:31:10 +0000
treeherdermozilla-inbound@b5c5ba07d3db [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmichal
bugs1579424
milestone72.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1579424 - P3 Use ifname and ip as a fail over, r=michal Differential Revision: https://phabricator.services.mozilla.com/D48805
netwerk/system/mac/nsNetworkLinkService.mm
--- a/netwerk/system/mac/nsNetworkLinkService.mm
+++ b/netwerk/system/mac/nsNetworkLinkService.mm
@@ -483,35 +483,58 @@ static bool ipv4NetworkId(SHA1Sum* aSHA1
     if (!gateway) {
       continue;
     }
     if (gateway->sa_family == AF_INET) {
       sockin = reinterpret_cast<struct sockaddr_in*>(gateway);
       inet_ntop(AF_INET, &sockin->sin_addr.s_addr, ip, sizeof(ip) - 1);
       char mac[18];
 
+      // TODO: cache the arp table instead of multiple system call.
       if (scanArp(ip, mac, sizeof(mac))) {
         hash.AppendElement(nsCString(mac));
       } else {
-        // TODO: fail over to ip and interface name
+        // Can't find a real MAC address. This might be a VPN gateway.
+        char buf[IFNAMSIZ] = {0};
+        char* ifName = if_indextoname(rtm->rtm_index, buf);
+        if (!ifName) {
+          LOG(("ipv4NetworkId: AF_INET if_indextoname failed"));
+          continue;
+        }
+
+        hash.AppendElement(nsCString(ifName));
+        hash.AppendElement(nsCString(ip));
       }
     } else if (gateway->sa_family == AF_LINK) {
       char buf[64];
       struct sockaddr_dl* sockdl = reinterpret_cast<struct sockaddr_dl*>(gateway);
       if (getMac(sockdl, buf, sizeof(buf))) {
         hash.AppendElement(nsCString(buf));
       } else {
-        // TODO: fail over to interface name
+        char buf[IFNAMSIZ] = {0};
+        char* ifName = if_indextoname(rtm->rtm_index, buf);
+        if (!ifName) {
+          LOG(("ipv4NetworkId: AF_LINK if_indextoname failed"));
+          continue;
+        }
+
+        hash.AppendElement(nsCString(ifName));
       }
     }
   }
 
+  // We didn't get any valid hash key to generate network ID.
+  if (hash.IsEmpty()) {
+    LOG(("ipv4NetworkId: No valid hash key"));
+    return false;
+  }
+
   hash.Sort();
   for (uint32_t i = 0; i < hash.Length(); ++i) {
-    LOG(("Hashing string for network id: %s", hash[i].get()));
+    LOG(("ipv4NetworkId: Hashing string for network id: %s", hash[i].get()));
     aSHA1->update(hash[i].get(), hash[i].Length());
   }
 
   return true;
 }
 
 //
 // Sort and hash the prefixes and netmasks