Bug 879225 - NetworkManager - extract hostname from various formats. r=gene, a=leo+
authorVicamo Yang <vyang@mozilla.com>
Wed, 05 Jun 2013 17:20:50 +0800
changeset 119575 7b5b8b85cc94ea0c07b4fc235570ecf58cc50f32
parent 119574 96cc0e2b19a1f9f1132fb177617fe07cafe16c65
child 119576 7efc09447a20073ef40a2b5b32d099a1fc1a6077
push id18
push userryanvm@gmail.com
push dateMon, 10 Jun 2013 17:53:19 +0000
reviewersgene, leo
bugs879225
milestone18.0
Bug 879225 - NetworkManager - extract hostname from various formats. r=gene, a=leo+
dom/system/gonk/NetworkManager.js
--- a/dom/system/gonk/NetworkManager.js
+++ b/dom/system/gonk/NetworkManager.js
@@ -121,16 +121,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);
@@ -205,16 +212,20 @@ function NetworkManager() {
         settingsLock.set(SETTINGS_WIFI_ENABLED, aResult, null);
       });
     },
 
     handleError: function (aErrorMessage) {
       debug("Error reading the 'tethering.wifi.enabled' setting: " + aErrorMessage);
     }
   });
+
+  // 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,
@@ -544,27 +555,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 = {