dom/system/gonk/NetworkUtils.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #ifndef NetworkUtils_h
     6 #define NetworkUtils_h
     8 #include "nsString.h"
     9 #include "mozilla/dom/NetworkOptionsBinding.h"
    10 #include "mozilla/dom/network/NetUtils.h"
    11 #include "mozilla/ipc/Netd.h"
    12 #include "nsTArray.h"
    14 class NetworkParams;
    15 class CommandChain;
    17 using namespace mozilla::dom;
    19 typedef void (*CommandCallback)(CommandChain*, bool, NetworkResultOptions& aResult);
    20 typedef void (*CommandFunc)(CommandChain*, CommandCallback, NetworkResultOptions& aResult);
    21 typedef void (*MessageCallback)(NetworkResultOptions& aResult);
    22 typedef void (*ErrorCallback)(NetworkParams& aOptions, NetworkResultOptions& aResult);
    24 class NetworkParams
    25 {
    26 public:
    27   NetworkParams() {
    28   }
    30   NetworkParams(const NetworkParams& aOther) {
    31     mIp = aOther.mIp;
    32     mCmd = aOther.mCmd;
    33     mDomain = aOther.mDomain;
    34     mGateway = aOther.mGateway;
    35     mGateways = aOther.mGateways;
    36     mHostnames = aOther.mHostnames;
    37     mId = aOther.mId;
    38     mIfname = aOther.mIfname;
    39     mPrefixLength = aOther.mPrefixLength;
    40     mOldIfname = aOther.mOldIfname;
    41     mMode = aOther.mMode;
    42     mReport = aOther.mReport;
    43     mIsAsync = aOther.mIsAsync;
    44     mEnabled = aOther.mEnabled;
    45     mWifictrlinterfacename = aOther.mWifictrlinterfacename;
    46     mInternalIfname = aOther.mInternalIfname;
    47     mExternalIfname = aOther.mExternalIfname;
    48     mEnable = aOther.mEnable;
    49     mSsid = aOther.mSsid;
    50     mSecurity = aOther.mSecurity;
    51     mKey = aOther.mKey;
    52     mPrefix = aOther.mPrefix;
    53     mLink = aOther.mLink;
    54     mInterfaceList = aOther.mInterfaceList;
    55     mWifiStartIp = aOther.mWifiStartIp;
    56     mWifiEndIp = aOther.mWifiEndIp;
    57     mUsbStartIp = aOther.mUsbStartIp;
    58     mUsbEndIp = aOther.mUsbEndIp;
    59     mDns1 = aOther.mDns1;
    60     mDns2 = aOther.mDns2;
    61     mDnses = aOther.mDnses;
    62     mRxBytes = aOther.mRxBytes;
    63     mTxBytes = aOther.mTxBytes;
    64     mDate = aOther.mDate;
    65     mStartIp = aOther.mStartIp;
    66     mEndIp = aOther.mEndIp;
    67     mServerIp = aOther.mServerIp;
    68     mMaskLength = aOther.mMaskLength;
    69     mPreInternalIfname = aOther.mPreInternalIfname;
    70     mPreExternalIfname = aOther.mPreExternalIfname;
    71     mCurInternalIfname = aOther.mCurInternalIfname;
    72     mCurExternalIfname = aOther.mCurExternalIfname;
    73     mThreshold = aOther.mThreshold;
    74   }
    76   NetworkParams(const NetworkCommandOptions& aOther) {
    78 #define COPY_SEQUENCE_FIELD(prop, type)                                                      \
    79     if (aOther.prop.WasPassed()) {                                                           \
    80       mozilla::dom::Sequence<type > const & currentValue = aOther.prop.InternalValue();      \
    81       uint32_t length = currentValue.Length();                                               \
    82       for (uint32_t idx = 0; idx < length; idx++) {                                          \
    83         prop.AppendElement(currentValue[idx]);                                               \
    84       }                                                                                      \
    85     }
    87 #define COPY_OPT_STRING_FIELD(prop, defaultValue)       \
    88     if (aOther.prop.WasPassed()) {                      \
    89       if (aOther.prop.Value().EqualsLiteral("null")) {  \
    90         prop = defaultValue;                            \
    91       } else {                                          \
    92         prop = aOther.prop.Value();                     \
    93       }                                                 \
    94     } else {                                            \
    95       prop = defaultValue;                              \
    96     }
    98 #define COPY_OPT_FIELD(prop, defaultValue)            \
    99     if (aOther.prop.WasPassed()) {                    \
   100       prop = aOther.prop.Value();                     \
   101     } else {                                          \
   102       prop = defaultValue;                            \
   103     }
   105 #define COPY_FIELD(prop) prop = aOther.prop;
   107     COPY_FIELD(mId)
   108     COPY_FIELD(mCmd)
   109     COPY_OPT_STRING_FIELD(mDomain, EmptyString())
   110     COPY_OPT_STRING_FIELD(mGateway, EmptyString())
   111     COPY_SEQUENCE_FIELD(mGateways, nsString)
   112     COPY_SEQUENCE_FIELD(mHostnames, nsString)
   113     COPY_OPT_STRING_FIELD(mIfname, EmptyString())
   114     COPY_OPT_STRING_FIELD(mIp, EmptyString())
   115     COPY_OPT_FIELD(mPrefixLength, 0)
   116     COPY_OPT_STRING_FIELD(mOldIfname, EmptyString())
   117     COPY_OPT_STRING_FIELD(mMode, EmptyString())
   118     COPY_OPT_FIELD(mReport, false)
   119     COPY_OPT_FIELD(mIsAsync, true)
   120     COPY_OPT_FIELD(mEnabled, false)
   121     COPY_OPT_STRING_FIELD(mWifictrlinterfacename, EmptyString())
   122     COPY_OPT_STRING_FIELD(mInternalIfname, EmptyString())
   123     COPY_OPT_STRING_FIELD(mExternalIfname, EmptyString())
   124     COPY_OPT_FIELD(mEnable, false)
   125     COPY_OPT_STRING_FIELD(mSsid, EmptyString())
   126     COPY_OPT_STRING_FIELD(mSecurity, EmptyString())
   127     COPY_OPT_STRING_FIELD(mKey, EmptyString())
   128     COPY_OPT_STRING_FIELD(mPrefix, EmptyString())
   129     COPY_OPT_STRING_FIELD(mLink, EmptyString())
   130     COPY_SEQUENCE_FIELD(mInterfaceList, nsString)
   131     COPY_OPT_STRING_FIELD(mWifiStartIp, EmptyString())
   132     COPY_OPT_STRING_FIELD(mWifiEndIp, EmptyString())
   133     COPY_OPT_STRING_FIELD(mUsbStartIp, EmptyString())
   134     COPY_OPT_STRING_FIELD(mUsbEndIp, EmptyString())
   135     COPY_OPT_STRING_FIELD(mDns1, EmptyString())
   136     COPY_OPT_STRING_FIELD(mDns2, EmptyString())
   137     COPY_SEQUENCE_FIELD(mDnses, nsString)
   138     COPY_OPT_FIELD(mRxBytes, -1)
   139     COPY_OPT_FIELD(mTxBytes, -1)
   140     COPY_OPT_STRING_FIELD(mDate, EmptyString())
   141     COPY_OPT_STRING_FIELD(mStartIp, EmptyString())
   142     COPY_OPT_STRING_FIELD(mEndIp, EmptyString())
   143     COPY_OPT_STRING_FIELD(mServerIp, EmptyString())
   144     COPY_OPT_STRING_FIELD(mMaskLength, EmptyString())
   145     COPY_OPT_STRING_FIELD(mPreInternalIfname, EmptyString())
   146     COPY_OPT_STRING_FIELD(mPreExternalIfname, EmptyString())
   147     COPY_OPT_STRING_FIELD(mCurInternalIfname, EmptyString())
   148     COPY_OPT_STRING_FIELD(mCurExternalIfname, EmptyString())
   149     COPY_OPT_FIELD(mThreshold, -1)
   151 #undef COPY_SEQUENCE_FIELD
   152 #undef COPY_OPT_STRING_FIELD
   153 #undef COPY_OPT_FIELD
   154 #undef COPY_FIELD
   155   }
   157   int32_t mId;
   158   nsString mCmd;
   159   nsString mDomain;
   160   nsString mGateway;
   161   nsTArray<nsString> mGateways;
   162   nsTArray<nsString> mHostnames;
   163   nsString mIfname;
   164   nsString mIp;
   165   uint32_t mPrefixLength;
   166   nsString mOldIfname;
   167   nsString mMode;
   168   bool mReport;
   169   bool mIsAsync;
   170   bool mEnabled;
   171   nsString mWifictrlinterfacename;
   172   nsString mInternalIfname;
   173   nsString mExternalIfname;
   174   bool mEnable;
   175   nsString mSsid;
   176   nsString mSecurity;
   177   nsString mKey;
   178   nsString mPrefix;
   179   nsString mLink;
   180   nsTArray<nsString> mInterfaceList;
   181   nsString mWifiStartIp;
   182   nsString mWifiEndIp;
   183   nsString mUsbStartIp;
   184   nsString mUsbEndIp;
   185   nsString mDns1;
   186   nsString mDns2;
   187   nsTArray<nsString> mDnses;
   188   float mRxBytes;
   189   float mTxBytes;
   190   nsString mDate;
   191   nsString mStartIp;
   192   nsString mEndIp;
   193   nsString mServerIp;
   194   nsString mMaskLength;
   195   nsString mPreInternalIfname;
   196   nsString mPreExternalIfname;
   197   nsString mCurInternalIfname;
   198   nsString mCurExternalIfname;
   199   long mThreshold;
   200 };
   202 // CommandChain store the necessary information to execute command one by one.
   203 // Including :
   204 // 1. Command parameters.
   205 // 2. Command list.
   206 // 3. Error callback function.
   207 // 4. Index of current execution command.
   208 class CommandChain MOZ_FINAL
   209 {
   210 public:
   211   CommandChain(const NetworkParams& aParams,
   212                CommandFunc aCmds[],
   213                uint32_t aLength,
   214                ErrorCallback aError)
   215   : mIndex(-1)
   216   , mParams(aParams)
   217   , mCommands(aCmds)
   218   , mLength(aLength)
   219   , mError(aError) {
   220   }
   222   NetworkParams&
   223   getParams()
   224   {
   225     return mParams;
   226   };
   228   CommandFunc
   229   getNextCommand()
   230   {
   231     mIndex++;
   232     return mIndex < mLength ? mCommands[mIndex] : nullptr;
   233   };
   235   ErrorCallback
   236   getErrorCallback() const
   237   {
   238     return mError;
   239   };
   241 private:
   242   uint32_t mIndex;
   243   NetworkParams mParams;
   244   CommandFunc* mCommands;
   245   uint32_t mLength;
   246   ErrorCallback mError;
   247 };
   249 class NetworkUtils MOZ_FINAL
   250 {
   251 public:
   252   NetworkUtils(MessageCallback aCallback);
   253   ~NetworkUtils();
   255   void ExecuteCommand(NetworkParams aOptions);
   256   void onNetdMessage(mozilla::ipc::NetdCommand* aCommand);
   258   MessageCallback getMessageCallback() { return mMessageCallback; }
   260 private:
   261   /**
   262    * Commands supported by NetworkUtils.
   263    */
   264   bool setDNS(NetworkParams& aOptions);
   265   bool setDefaultRouteAndDNS(NetworkParams& aOptions);
   266   bool addHostRoute(NetworkParams& aOptions);
   267   bool removeDefaultRoute(NetworkParams& aOptions);
   268   bool removeHostRoute(NetworkParams& aOptions);
   269   bool removeHostRoutes(NetworkParams& aOptions);
   270   bool removeNetworkRoute(NetworkParams& aOptions);
   271   bool addSecondaryRoute(NetworkParams& aOptions);
   272   bool removeSecondaryRoute(NetworkParams& aOptions);
   273   bool getNetworkInterfaceStats(NetworkParams& aOptions);
   274   bool setNetworkInterfaceAlarm(NetworkParams& aOptions);
   275   bool enableNetworkInterfaceAlarm(NetworkParams& aOptions);
   276   bool disableNetworkInterfaceAlarm(NetworkParams& aOptions);
   277   bool setWifiOperationMode(NetworkParams& aOptions);
   278   bool setDhcpServer(NetworkParams& aOptions);
   279   bool setWifiTethering(NetworkParams& aOptions);
   280   bool setUSBTethering(NetworkParams& aOptions);
   281   bool enableUsbRndis(NetworkParams& aOptions);
   282   bool updateUpStream(NetworkParams& aOptions);
   284   /**
   285    * function pointer array holds all netd commands should be executed
   286    * in sequence to accomplish a given command by other module.
   287    */
   288   static CommandFunc sWifiEnableChain[];
   289   static CommandFunc sWifiDisableChain[];
   290   static CommandFunc sWifiFailChain[];
   291   static CommandFunc sWifiOperationModeChain[];
   292   static CommandFunc sUSBEnableChain[];
   293   static CommandFunc sUSBDisableChain[];
   294   static CommandFunc sUSBFailChain[];
   295   static CommandFunc sUpdateUpStreamChain[];
   296   static CommandFunc sStartDhcpServerChain[];
   297   static CommandFunc sStopDhcpServerChain[];
   298   static CommandFunc sNetworkInterfaceStatsChain[];
   299   static CommandFunc sNetworkInterfaceEnableAlarmChain[];
   300   static CommandFunc sNetworkInterfaceDisableAlarmChain[];
   301   static CommandFunc sNetworkInterfaceSetAlarmChain[];
   302   static CommandFunc sSetDnsChain[];
   304   /**
   305    * Individual netd command stored in command chain.
   306    */
   307 #define PARAMS CommandChain* aChain, CommandCallback aCallback, NetworkResultOptions& aResult
   308   static void wifiFirmwareReload(PARAMS);
   309   static void startAccessPointDriver(PARAMS);
   310   static void stopAccessPointDriver(PARAMS);
   311   static void setAccessPoint(PARAMS);
   312   static void cleanUpStream(PARAMS);
   313   static void createUpStream(PARAMS);
   314   static void startSoftAP(PARAMS);
   315   static void stopSoftAP(PARAMS);
   316   static void getRxBytes(PARAMS);
   317   static void getTxBytes(PARAMS);
   318   static void enableAlarm(PARAMS);
   319   static void disableAlarm(PARAMS);
   320   static void setQuota(PARAMS);
   321   static void removeQuota(PARAMS);
   322   static void setAlarm(PARAMS);
   323   static void setInterfaceUp(PARAMS);
   324   static void tetherInterface(PARAMS);
   325   static void preTetherInterfaceList(PARAMS);
   326   static void postTetherInterfaceList(PARAMS);
   327   static void setIpForwardingEnabled(PARAMS);
   328   static void tetheringStatus(PARAMS);
   329   static void stopTethering(PARAMS);
   330   static void startTethering(PARAMS);
   331   static void untetherInterface(PARAMS);
   332   static void setDnsForwarders(PARAMS);
   333   static void enableNat(PARAMS);
   334   static void disableNat(PARAMS);
   335   static void setDefaultInterface(PARAMS);
   336   static void setInterfaceDns(PARAMS);
   337   static void wifiTetheringSuccess(PARAMS);
   338   static void usbTetheringSuccess(PARAMS);
   339   static void networkInterfaceStatsSuccess(PARAMS);
   340   static void networkInterfaceAlarmSuccess(PARAMS);
   341   static void updateUpStreamSuccess(PARAMS);
   342   static void setDhcpServerSuccess(PARAMS);
   343   static void wifiOperationModeSuccess(PARAMS);
   344 #undef PARAMS
   346   /**
   347    * Error callback function executed when a command is fail.
   348    */
   349 #define PARAMS NetworkParams& aOptions, NetworkResultOptions& aResult
   350   static void wifiTetheringFail(PARAMS);
   351   static void wifiOperationModeFail(PARAMS);
   352   static void usbTetheringFail(PARAMS);
   353   static void updateUpStreamFail(PARAMS);
   354   static void setDhcpServerFail(PARAMS);
   355   static void networkInterfaceStatsFail(PARAMS);
   356   static void networkInterfaceAlarmFail(PARAMS);
   357   static void setDnsFail(PARAMS);
   358 #undef PARAMS
   360   /**
   361    * Command chain processing functions.
   362    */
   363   static void next(CommandChain* aChain, bool aError, NetworkResultOptions& aResult);
   364   static void nextNetdCommand();
   365   static void doCommand(const char* aCommand, CommandChain* aChain, CommandCallback aCallback);
   367   /**
   368    * Notify broadcast message to main thread.
   369    */
   370   void sendBroadcastMessage(uint32_t code, char* reason);
   372   /**
   373    * Utility functions.
   374    */
   375   void checkUsbRndisState(NetworkParams& aOptions);
   376   void dumpParams(NetworkParams& aOptions, const char* aType);
   378   static void escapeQuote(nsCString& aString);
   379   inline uint32_t netdResponseType(uint32_t code);
   380   inline bool isBroadcastMessage(uint32_t code);
   381   inline bool isError(uint32_t code);
   382   inline bool isComplete(uint32_t code);
   383   inline bool isProceeding(uint32_t code);
   384   void Shutdown();
   385   /**
   386    * Callback function to send netd result to main thread.
   387    */
   388   MessageCallback mMessageCallback;
   390   /*
   391    * Utility class to access libnetutils.
   392    */
   393   nsAutoPtr<NetUtils> mNetUtils;
   394 };
   396 #endif

mercurial