Bug 961598 - support DNS reslover. r=vchang
authorJohn Shih <jshih@mozilla.com>
Thu, 20 Feb 2014 19:04:51 +0800
changeset 170998 480d41979c72ead1dadb35b2d64a5f3338b52e3e
parent 170967 9ce9bdf1ffe79dee4ce679c4440cd2289cde3a5f
child 170999 62cef6077cb4760e98fdc5557a55ccb1c397bdbb
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersvchang
bugs961598
milestone30.0a1
Bug 961598 - support DNS reslover. r=vchang
dom/system/gonk/NetworkService.js
dom/system/gonk/NetworkUtils.cpp
dom/system/gonk/NetworkUtils.h
dom/webidl/NetworkOptions.webidl
--- a/dom/system/gonk/NetworkService.js
+++ b/dom/system/gonk/NetworkService.js
@@ -278,29 +278,31 @@ NetworkService.prototype = {
     this.controlMessage(options, function() {});
   },
 
   setDNS: function(networkInterface) {
     if(DEBUG) debug("Going DNS to " + networkInterface.name);
     let options = {
       cmd: "setDNS",
       ifname: networkInterface.name,
+      domain: "mozilla." + networkInterface.name + ".doman",
       dns1_str: networkInterface.dns1,
       dns2_str: networkInterface.dns2
     };
     this.controlMessage(options, function() {});
   },
 
   setDefaultRouteAndDNS: function(network, oldInterface) {
     if(DEBUG) debug("Going to change route and DNS to " + network.name);
     let options = {
       cmd: "setDefaultRouteAndDNS",
       ifname: network.name,
       oldIfname: (oldInterface && oldInterface !== network) ? oldInterface.name : null,
       gateway_str: network.gateway,
+      domain: "mozilla." + network.name + ".doman",
       dns1_str: network.dns1,
       dns2_str: network.dns2
     };
     this.controlMessage(options, function() {});
     this.setNetworkProxy(network);
   },
 
   removeDefaultRoute: function(ifname) {
--- a/dom/system/gonk/NetworkUtils.cpp
+++ b/dom/system/gonk/NetworkUtils.cpp
@@ -200,16 +200,21 @@ CommandFunc NetworkUtils::sNetworkInterf
   NetworkUtils::networkInterfaceAlarmSuccess
 };
 
 CommandFunc NetworkUtils::sNetworkInterfaceSetAlarmChain[] = {
   NetworkUtils::setAlarm,
   NetworkUtils::networkInterfaceAlarmSuccess
 };
 
+CommandFunc NetworkUtils::sSetDnsChain[] = {
+  NetworkUtils::setDefaultInterface,
+  NetworkUtils::setInterfaceDns
+};
+
 /**
  * Helper function to get the bit length from given mask.
  */
 static uint32_t getMaskLength(const uint32_t mask)
 {
   uint32_t netmask = ntohl(mask);
   uint32_t len = 0;
   while (netmask & 0x80000000) {
@@ -733,16 +738,36 @@ void NetworkUtils::disableNat(CommandCha
                               NetworkResultOptions& aResult)
 {
   char command[MAX_COMMAND_SIZE];
   snprintf(command, MAX_COMMAND_SIZE - 1, "nat disable %s %s 0", GET_CHAR(mInternalIfname), GET_CHAR(mExternalIfname));
 
   doCommand(command, aChain, aCallback);
 }
 
+void NetworkUtils::setDefaultInterface(CommandChain* aChain,
+                                       CommandCallback aCallback,
+                                       NetworkResultOptions& aResult)
+{
+  char command[MAX_COMMAND_SIZE];
+  snprintf(command, MAX_COMMAND_SIZE - 1, "resolver setdefaultif %s", GET_CHAR(mIfname));
+
+  doCommand(command, aChain, aCallback);
+}
+
+void NetworkUtils::setInterfaceDns(CommandChain* aChain,
+                                   CommandCallback aCallback,
+                                   NetworkResultOptions& aResult)
+{
+  char command[MAX_COMMAND_SIZE];
+  snprintf(command, MAX_COMMAND_SIZE - 1, "resolver setifdns %s %s %s %s", GET_CHAR(mIfname), GET_CHAR(mDomain), GET_CHAR(mDns1_str), GET_CHAR(mDns2_str));
+
+  doCommand(command, aChain, aCallback);
+}
+
 #undef GET_CHAR
 #undef GET_FIELD
 
 /*
  * Netd command success/fail function
  */
 #define ASSIGN_FIELD(prop)  aResult.prop = aChain->getParams().prop;
 #define ASSIGN_FIELD_VALUE(prop, value)  aResult.prop = value;
@@ -863,16 +888,21 @@ void NetworkUtils::wifiOperationModeFail
 
 void NetworkUtils::wifiOperationModeSuccess(CommandChain* aChain,
                                             CommandCallback aCallback,
                                             NetworkResultOptions& aResult)
 {
   postMessage(aChain->getParams(), aResult);
 }
 
+void NetworkUtils::setDnsFail(NetworkParams& aOptions, NetworkResultOptions& aResult)
+{
+  postMessage(aOptions, aResult);
+}
+
 #undef ASSIGN_FIELD
 #undef ASSIGN_FIELD_VALUE
 
 NetworkUtils::NetworkUtils(MessageCallback aCallback)
  : mMessageCallback(aCallback)
 {
   mNetUtils = new NetUtils();
 
@@ -1049,16 +1079,21 @@ bool NetworkUtils::setDNS(NetworkParams&
   // Bump the DNS change property.
   char dnschange[PROPERTY_VALUE_MAX];
   property_get("net.dnschange", dnschange, "0");
 
   char num[PROPERTY_VALUE_MAX];
   snprintf(num, PROPERTY_VALUE_MAX - 1, "%d", atoi(dnschange) + 1);
   property_set("net.dnschange", num);
 
+  // DNS needs to be set through netd since JellyBean (4.3).
+  if (SDK_VERSION >= 18) {
+    RUN_CHAIN(aOptions, sSetDnsChain, setDnsFail)
+  }
+
   return true;
 }
 
 /**
  * Set default route and DNS servers for given network interface.
  */
 bool NetworkUtils::setDefaultRouteAndDNS(NetworkParams& aOptions)
 {
--- a/dom/system/gonk/NetworkUtils.h
+++ b/dom/system/gonk/NetworkUtils.h
@@ -25,16 +25,17 @@ class NetworkParams
 {
 public:
   NetworkParams() {
   }
 
   NetworkParams(const NetworkParams& aOther) {
     mIp = aOther.mIp;
     mCmd = aOther.mCmd;
+    mDomain = aOther.mDomain;
     mDns1_str = aOther.mDns1_str;
     mDns2_str = aOther.mDns2_str;
     mGateway = aOther.mGateway;
     mGateway_str = aOther.mGateway_str;
     mHostnames = aOther.mHostnames;
     mId = aOther.mId;
     mIfname = aOther.mIfname;
     mNetmask = aOther.mNetmask;
@@ -101,16 +102,17 @@ public:
     } else {                                          \
       prop = defaultValue;                            \
     }
 
 #define COPY_FIELD(prop) prop = aOther.prop;
 
     COPY_FIELD(mId)
     COPY_FIELD(mCmd)
+    COPY_OPT_STRING_FIELD(mDomain, EmptyString())
     COPY_OPT_STRING_FIELD(mDns1_str, EmptyString())
     COPY_OPT_STRING_FIELD(mDns2_str, EmptyString())
     COPY_OPT_STRING_FIELD(mGateway, EmptyString())
     COPY_OPT_STRING_FIELD(mGateway_str, EmptyString())
     COPY_SEQUENCE_FIELD(mHostnames, nsString)
     COPY_OPT_STRING_FIELD(mIfname, EmptyString())
     COPY_OPT_STRING_FIELD(mIp, EmptyString())
     COPY_OPT_STRING_FIELD(mNetmask, EmptyString())
@@ -151,16 +153,17 @@ public:
 #undef COPY_SEQUENCE_FIELD
 #undef COPY_OPT_STRING_FIELD
 #undef COPY_OPT_FIELD
 #undef COPY_FIELD
   }
 
   int32_t mId;
   nsString mCmd;
+  nsString mDomain;
   nsString mDns1_str;
   nsString mDns2_str;
   nsString mGateway;
   nsString mGateway_str;
   nsTArray<nsString> mHostnames;
   nsString mIfname;
   nsString mIp;
   nsString mNetmask;
@@ -294,16 +297,17 @@ private:
   static CommandFunc sUSBFailChain[];
   static CommandFunc sUpdateUpStreamChain[];
   static CommandFunc sStartDhcpServerChain[];
   static CommandFunc sStopDhcpServerChain[];
   static CommandFunc sNetworkInterfaceStatsChain[];
   static CommandFunc sNetworkInterfaceEnableAlarmChain[];
   static CommandFunc sNetworkInterfaceDisableAlarmChain[];
   static CommandFunc sNetworkInterfaceSetAlarmChain[];
+  static CommandFunc sSetDnsChain[];
 
   /**
    * Individual netd command stored in command chain.
    */
 #define PARAMS CommandChain* aChain, CommandCallback aCallback, NetworkResultOptions& aResult
   static void wifiFirmwareReload(PARAMS);
   static void startAccessPointDriver(PARAMS);
   static void stopAccessPointDriver(PARAMS);
@@ -326,16 +330,18 @@ private:
   static void setIpForwardingEnabled(PARAMS);
   static void tetheringStatus(PARAMS);
   static void stopTethering(PARAMS);
   static void startTethering(PARAMS);
   static void untetherInterface(PARAMS);
   static void setDnsForwarders(PARAMS);
   static void enableNat(PARAMS);
   static void disableNat(PARAMS);
+  static void setDefaultInterface(PARAMS);
+  static void setInterfaceDns(PARAMS);
   static void wifiTetheringSuccess(PARAMS);
   static void usbTetheringSuccess(PARAMS);
   static void networkInterfaceStatsSuccess(PARAMS);
   static void networkInterfaceAlarmSuccess(PARAMS);
   static void updateUpStreamSuccess(PARAMS);
   static void setDhcpServerSuccess(PARAMS);
   static void wifiOperationModeSuccess(PARAMS);
 #undef PARAMS
@@ -346,16 +352,17 @@ private:
 #define PARAMS NetworkParams& aOptions, NetworkResultOptions& aResult
   static void wifiTetheringFail(PARAMS);
   static void wifiOperationModeFail(PARAMS);
   static void usbTetheringFail(PARAMS);
   static void updateUpStreamFail(PARAMS);
   static void setDhcpServerFail(PARAMS);
   static void networkInterfaceStatsFail(PARAMS);
   static void networkInterfaceAlarmFail(PARAMS);
+  static void setDnsFail(PARAMS);
 #undef PARAMS
 
   /**
    * Command chain processing functions.
    */
   static void next(CommandChain* aChain, bool aError, NetworkResultOptions& aResult);
   static void nextNetdCommand();
   static void doCommand(const char* aCommand, CommandChain* aChain, CommandCallback aCallback);
--- a/dom/webidl/NetworkOptions.webidl
+++ b/dom/webidl/NetworkOptions.webidl
@@ -10,16 +10,17 @@ dictionary NetworkCommandOptions
   long id = 0;                        // opaque id.
   DOMString cmd = "";                 // the command name.
   DOMString ifname;                   // for "removeNetworkRoute", "setDNS",
                                       //     "setDefaultRouteAndDNS", "removeDefaultRoute"
                                       //     "addHostRoute", "removeHostRoute"
                                       //     "removeHostRoutes".
   DOMString ip;                       // for "removeNetworkRoute", "setWifiTethering".
   DOMString netmask;                  // for "removeNetworkRoute".
+  DOMString domain;                   // for "setDNS"
   DOMString dns1_str;                 // for "setDNS", "setDefaultRouteAndDNS".
   DOMString dns2_str;                 // for "setDNS", "setDefaultRouteAndDNS".
   DOMString oldIfname;                // for "setDefaultRouteAndDNS".
   DOMString gateway_str;              // for "setDefaultRouteAndDNS".
   DOMString gateway;                  // for "addHostRoute", "removeHostRoute".
   sequence<DOMString> hostnames;      // for "addHostRoute", "removeHostRoute".
   DOMString mode;                     // for "setWifiOperationMode".
   boolean report;                     // for "setWifiOperationMode".