Bug 879225: NetworkManager - extract hostname from various formats. r=gene
authorVicamo Yang <vyang@mozilla.com>
Wed, 05 Jun 2013 17:20:50 +0800
changeset 134134 afb71a02b5c571db38d01dde057acce376ef619d
parent 134077 b61efdb1d7b82834f6dd9bd0f46745683b1498b1
child 134135 307d7abbf1c01e522a1cea8b6ba66421f2c2fcb3
push id29067
push userryanvm@gmail.com
push dateWed, 05 Jun 2013 20:37:20 +0000
treeherdermozilla-inbound@72fbfb2f8e51 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgene
bugs879225
milestone24.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 879225: NetworkManager - extract hostname from various formats. r=gene
dom/system/gonk/NetworkManager.js
--- a/dom/system/gonk/NetworkManager.js
+++ b/dom/system/gonk/NetworkManager.js
@@ -125,16 +125,23 @@ function isError(code) {
   return (type != NETD_COMMAND_PROCEEDING && type != NETD_COMMAND_OKAY);
 }
 
 function isComplete(code) {
   let type = netdResponseType(code);
   return (type != NETD_COMMAND_PROCEEDING);
 }
 
+function defineLazyRegExp(obj, name, pattern) {
+  obj.__defineGetter__(name, function() {
+    delete obj[name];
+    return obj[name] = new RegExp(pattern);
+  });
+}
+
 /**
  * This component watches for network interfaces changing state and then
  * adjusts routes etc. accordingly.
  */
 function NetworkManager() {
   this.networkInterfaces = {};
   Services.obs.addObserver(this, TOPIC_INTERFACE_STATE_CHANGED, true);
   Services.obs.addObserver(this, TOPIC_INTERFACE_REGISTERED, true);
@@ -211,16 +218,20 @@ function NetworkManager() {
     },
 
     handleError: function (aErrorMessage) {
       debug("Error reading the 'tethering.wifi.enabled' setting: " + aErrorMessage);
     }
   });
 
   ppmm.addMessageListener('NetworkInterfaceList:ListInterface', this);
+
+  // Used in resolveHostname().
+  defineLazyRegExp(this, "REGEXP_IPV4", "^\\d{1,3}(?:\\.\\d{1,3}){3}$");
+  defineLazyRegExp(this, "REGEXP_IPV6", "^[\\da-fA-F]{4}(?::[\\da-fA-F]{4}){7}$");
 }
 NetworkManager.prototype = {
   classID:   NETWORKMANAGER_CID,
   classInfo: XPCOMUtils.generateCI({classID: NETWORKMANAGER_CID,
                                     contractID: NETWORKMANAGER_CONTRACTID,
                                     classDescription: "Network Manager",
                                     interfaces: [Ci.nsINetworkManager]}),
   QueryInterface: XPCOMUtils.generateQI([Ci.nsINetworkManager,
@@ -606,27 +617,35 @@ NetworkManager.prototype = {
       hostnames: [network.dns1, network.dns2, network.httpProxyHost]
     };
     this.worker.postMessage(options);
   },
 
   resolveHostname: function resolveHostname(hosts) {
     let retval = [];
 
-    for(var i = 0; i < hosts.length; i++) {
-      let hostname = hosts[i].split('/')[2];
-      if (!hostname) {
+    for (let hostname of hosts) {
+      try {
+        let uri = Services.io.newURI(hostname, null, null);
+        hostname = uri.host;
+      } catch (e) {}
+
+      if (hostname.match(this.REGEXP_IPV4) ||
+          hostname.match(this.REGEXP_IPV6)) {
+        retval.push(hostname);
         continue;
       }
 
-      let hostnameIps = gDNSService.resolve(hostname, 0);
-      while (hostnameIps.hasMore()) {
-        retval.push(hostnameIps.getNextAddrAsString());
-        debug("Found IP at: " + JSON.stringify(retval));
-      }
+      try {
+        let hostnameIps = gDNSService.resolve(hostname, 0);
+        while (hostnameIps.hasMore()) {
+          retval.push(hostnameIps.getNextAddrAsString());
+          debug("Found IP at: " + JSON.stringify(retval));
+        }
+      } catch (e) {}
     }
 
     return retval;
   },
 
   addHostRouteWithResolve: function addHostRouteWithResolve(network, hosts) {
     debug("Going to add host route after dns resolution on " + network.name);
     let options = {