From 20f2ebf6b22c2c6e6a07c9712571b645d0340038 Mon Sep 17 00:00:00 2001 From: Vicamo Yang Date: Wed, 5 Jun 2013 17:20:50 +0800 Subject: [PATCH] Bug 879225: NetworkManager - extract hostname from various formats. r=gene --- dom/system/gonk/NetworkManager.js | 35 ++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/dom/system/gonk/NetworkManager.js b/dom/system/gonk/NetworkManager.js index 3affc853603f..fa7c4290b15a 100644 --- a/dom/system/gonk/NetworkManager.js +++ b/dom/system/gonk/NetworkManager.js @@ -130,6 +130,13 @@ function isComplete(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. @@ -216,6 +223,10 @@ function NetworkManager() { }); 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, @@ -611,17 +622,25 @@ NetworkManager.prototype = { 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;