dom/system/gonk/NetworkUtils.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/system/gonk/NetworkUtils.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,396 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef NetworkUtils_h
     1.9 +#define NetworkUtils_h
    1.10 +
    1.11 +#include "nsString.h"
    1.12 +#include "mozilla/dom/NetworkOptionsBinding.h"
    1.13 +#include "mozilla/dom/network/NetUtils.h"
    1.14 +#include "mozilla/ipc/Netd.h"
    1.15 +#include "nsTArray.h"
    1.16 +
    1.17 +class NetworkParams;
    1.18 +class CommandChain;
    1.19 +
    1.20 +using namespace mozilla::dom;
    1.21 +
    1.22 +typedef void (*CommandCallback)(CommandChain*, bool, NetworkResultOptions& aResult);
    1.23 +typedef void (*CommandFunc)(CommandChain*, CommandCallback, NetworkResultOptions& aResult);
    1.24 +typedef void (*MessageCallback)(NetworkResultOptions& aResult);
    1.25 +typedef void (*ErrorCallback)(NetworkParams& aOptions, NetworkResultOptions& aResult);
    1.26 +
    1.27 +class NetworkParams
    1.28 +{
    1.29 +public:
    1.30 +  NetworkParams() {
    1.31 +  }
    1.32 +
    1.33 +  NetworkParams(const NetworkParams& aOther) {
    1.34 +    mIp = aOther.mIp;
    1.35 +    mCmd = aOther.mCmd;
    1.36 +    mDomain = aOther.mDomain;
    1.37 +    mGateway = aOther.mGateway;
    1.38 +    mGateways = aOther.mGateways;
    1.39 +    mHostnames = aOther.mHostnames;
    1.40 +    mId = aOther.mId;
    1.41 +    mIfname = aOther.mIfname;
    1.42 +    mPrefixLength = aOther.mPrefixLength;
    1.43 +    mOldIfname = aOther.mOldIfname;
    1.44 +    mMode = aOther.mMode;
    1.45 +    mReport = aOther.mReport;
    1.46 +    mIsAsync = aOther.mIsAsync;
    1.47 +    mEnabled = aOther.mEnabled;
    1.48 +    mWifictrlinterfacename = aOther.mWifictrlinterfacename;
    1.49 +    mInternalIfname = aOther.mInternalIfname;
    1.50 +    mExternalIfname = aOther.mExternalIfname;
    1.51 +    mEnable = aOther.mEnable;
    1.52 +    mSsid = aOther.mSsid;
    1.53 +    mSecurity = aOther.mSecurity;
    1.54 +    mKey = aOther.mKey;
    1.55 +    mPrefix = aOther.mPrefix;
    1.56 +    mLink = aOther.mLink;
    1.57 +    mInterfaceList = aOther.mInterfaceList;
    1.58 +    mWifiStartIp = aOther.mWifiStartIp;
    1.59 +    mWifiEndIp = aOther.mWifiEndIp;
    1.60 +    mUsbStartIp = aOther.mUsbStartIp;
    1.61 +    mUsbEndIp = aOther.mUsbEndIp;
    1.62 +    mDns1 = aOther.mDns1;
    1.63 +    mDns2 = aOther.mDns2;
    1.64 +    mDnses = aOther.mDnses;
    1.65 +    mRxBytes = aOther.mRxBytes;
    1.66 +    mTxBytes = aOther.mTxBytes;
    1.67 +    mDate = aOther.mDate;
    1.68 +    mStartIp = aOther.mStartIp;
    1.69 +    mEndIp = aOther.mEndIp;
    1.70 +    mServerIp = aOther.mServerIp;
    1.71 +    mMaskLength = aOther.mMaskLength;
    1.72 +    mPreInternalIfname = aOther.mPreInternalIfname;
    1.73 +    mPreExternalIfname = aOther.mPreExternalIfname;
    1.74 +    mCurInternalIfname = aOther.mCurInternalIfname;
    1.75 +    mCurExternalIfname = aOther.mCurExternalIfname;
    1.76 +    mThreshold = aOther.mThreshold;
    1.77 +  }
    1.78 +
    1.79 +  NetworkParams(const NetworkCommandOptions& aOther) {
    1.80 +
    1.81 +#define COPY_SEQUENCE_FIELD(prop, type)                                                      \
    1.82 +    if (aOther.prop.WasPassed()) {                                                           \
    1.83 +      mozilla::dom::Sequence<type > const & currentValue = aOther.prop.InternalValue();      \
    1.84 +      uint32_t length = currentValue.Length();                                               \
    1.85 +      for (uint32_t idx = 0; idx < length; idx++) {                                          \
    1.86 +        prop.AppendElement(currentValue[idx]);                                               \
    1.87 +      }                                                                                      \
    1.88 +    }
    1.89 +
    1.90 +#define COPY_OPT_STRING_FIELD(prop, defaultValue)       \
    1.91 +    if (aOther.prop.WasPassed()) {                      \
    1.92 +      if (aOther.prop.Value().EqualsLiteral("null")) {  \
    1.93 +        prop = defaultValue;                            \
    1.94 +      } else {                                          \
    1.95 +        prop = aOther.prop.Value();                     \
    1.96 +      }                                                 \
    1.97 +    } else {                                            \
    1.98 +      prop = defaultValue;                              \
    1.99 +    }
   1.100 +
   1.101 +#define COPY_OPT_FIELD(prop, defaultValue)            \
   1.102 +    if (aOther.prop.WasPassed()) {                    \
   1.103 +      prop = aOther.prop.Value();                     \
   1.104 +    } else {                                          \
   1.105 +      prop = defaultValue;                            \
   1.106 +    }
   1.107 +
   1.108 +#define COPY_FIELD(prop) prop = aOther.prop;
   1.109 +
   1.110 +    COPY_FIELD(mId)
   1.111 +    COPY_FIELD(mCmd)
   1.112 +    COPY_OPT_STRING_FIELD(mDomain, EmptyString())
   1.113 +    COPY_OPT_STRING_FIELD(mGateway, EmptyString())
   1.114 +    COPY_SEQUENCE_FIELD(mGateways, nsString)
   1.115 +    COPY_SEQUENCE_FIELD(mHostnames, nsString)
   1.116 +    COPY_OPT_STRING_FIELD(mIfname, EmptyString())
   1.117 +    COPY_OPT_STRING_FIELD(mIp, EmptyString())
   1.118 +    COPY_OPT_FIELD(mPrefixLength, 0)
   1.119 +    COPY_OPT_STRING_FIELD(mOldIfname, EmptyString())
   1.120 +    COPY_OPT_STRING_FIELD(mMode, EmptyString())
   1.121 +    COPY_OPT_FIELD(mReport, false)
   1.122 +    COPY_OPT_FIELD(mIsAsync, true)
   1.123 +    COPY_OPT_FIELD(mEnabled, false)
   1.124 +    COPY_OPT_STRING_FIELD(mWifictrlinterfacename, EmptyString())
   1.125 +    COPY_OPT_STRING_FIELD(mInternalIfname, EmptyString())
   1.126 +    COPY_OPT_STRING_FIELD(mExternalIfname, EmptyString())
   1.127 +    COPY_OPT_FIELD(mEnable, false)
   1.128 +    COPY_OPT_STRING_FIELD(mSsid, EmptyString())
   1.129 +    COPY_OPT_STRING_FIELD(mSecurity, EmptyString())
   1.130 +    COPY_OPT_STRING_FIELD(mKey, EmptyString())
   1.131 +    COPY_OPT_STRING_FIELD(mPrefix, EmptyString())
   1.132 +    COPY_OPT_STRING_FIELD(mLink, EmptyString())
   1.133 +    COPY_SEQUENCE_FIELD(mInterfaceList, nsString)
   1.134 +    COPY_OPT_STRING_FIELD(mWifiStartIp, EmptyString())
   1.135 +    COPY_OPT_STRING_FIELD(mWifiEndIp, EmptyString())
   1.136 +    COPY_OPT_STRING_FIELD(mUsbStartIp, EmptyString())
   1.137 +    COPY_OPT_STRING_FIELD(mUsbEndIp, EmptyString())
   1.138 +    COPY_OPT_STRING_FIELD(mDns1, EmptyString())
   1.139 +    COPY_OPT_STRING_FIELD(mDns2, EmptyString())
   1.140 +    COPY_SEQUENCE_FIELD(mDnses, nsString)
   1.141 +    COPY_OPT_FIELD(mRxBytes, -1)
   1.142 +    COPY_OPT_FIELD(mTxBytes, -1)
   1.143 +    COPY_OPT_STRING_FIELD(mDate, EmptyString())
   1.144 +    COPY_OPT_STRING_FIELD(mStartIp, EmptyString())
   1.145 +    COPY_OPT_STRING_FIELD(mEndIp, EmptyString())
   1.146 +    COPY_OPT_STRING_FIELD(mServerIp, EmptyString())
   1.147 +    COPY_OPT_STRING_FIELD(mMaskLength, EmptyString())
   1.148 +    COPY_OPT_STRING_FIELD(mPreInternalIfname, EmptyString())
   1.149 +    COPY_OPT_STRING_FIELD(mPreExternalIfname, EmptyString())
   1.150 +    COPY_OPT_STRING_FIELD(mCurInternalIfname, EmptyString())
   1.151 +    COPY_OPT_STRING_FIELD(mCurExternalIfname, EmptyString())
   1.152 +    COPY_OPT_FIELD(mThreshold, -1)
   1.153 +
   1.154 +#undef COPY_SEQUENCE_FIELD
   1.155 +#undef COPY_OPT_STRING_FIELD
   1.156 +#undef COPY_OPT_FIELD
   1.157 +#undef COPY_FIELD
   1.158 +  }
   1.159 +
   1.160 +  int32_t mId;
   1.161 +  nsString mCmd;
   1.162 +  nsString mDomain;
   1.163 +  nsString mGateway;
   1.164 +  nsTArray<nsString> mGateways;
   1.165 +  nsTArray<nsString> mHostnames;
   1.166 +  nsString mIfname;
   1.167 +  nsString mIp;
   1.168 +  uint32_t mPrefixLength;
   1.169 +  nsString mOldIfname;
   1.170 +  nsString mMode;
   1.171 +  bool mReport;
   1.172 +  bool mIsAsync;
   1.173 +  bool mEnabled;
   1.174 +  nsString mWifictrlinterfacename;
   1.175 +  nsString mInternalIfname;
   1.176 +  nsString mExternalIfname;
   1.177 +  bool mEnable;
   1.178 +  nsString mSsid;
   1.179 +  nsString mSecurity;
   1.180 +  nsString mKey;
   1.181 +  nsString mPrefix;
   1.182 +  nsString mLink;
   1.183 +  nsTArray<nsString> mInterfaceList;
   1.184 +  nsString mWifiStartIp;
   1.185 +  nsString mWifiEndIp;
   1.186 +  nsString mUsbStartIp;
   1.187 +  nsString mUsbEndIp;
   1.188 +  nsString mDns1;
   1.189 +  nsString mDns2;
   1.190 +  nsTArray<nsString> mDnses;
   1.191 +  float mRxBytes;
   1.192 +  float mTxBytes;
   1.193 +  nsString mDate;
   1.194 +  nsString mStartIp;
   1.195 +  nsString mEndIp;
   1.196 +  nsString mServerIp;
   1.197 +  nsString mMaskLength;
   1.198 +  nsString mPreInternalIfname;
   1.199 +  nsString mPreExternalIfname;
   1.200 +  nsString mCurInternalIfname;
   1.201 +  nsString mCurExternalIfname;
   1.202 +  long mThreshold;
   1.203 +};
   1.204 +
   1.205 +// CommandChain store the necessary information to execute command one by one.
   1.206 +// Including :
   1.207 +// 1. Command parameters.
   1.208 +// 2. Command list.
   1.209 +// 3. Error callback function.
   1.210 +// 4. Index of current execution command.
   1.211 +class CommandChain MOZ_FINAL
   1.212 +{
   1.213 +public:
   1.214 +  CommandChain(const NetworkParams& aParams,
   1.215 +               CommandFunc aCmds[],
   1.216 +               uint32_t aLength,
   1.217 +               ErrorCallback aError)
   1.218 +  : mIndex(-1)
   1.219 +  , mParams(aParams)
   1.220 +  , mCommands(aCmds)
   1.221 +  , mLength(aLength)
   1.222 +  , mError(aError) {
   1.223 +  }
   1.224 +
   1.225 +  NetworkParams&
   1.226 +  getParams()
   1.227 +  {
   1.228 +    return mParams;
   1.229 +  };
   1.230 +
   1.231 +  CommandFunc
   1.232 +  getNextCommand()
   1.233 +  {
   1.234 +    mIndex++;
   1.235 +    return mIndex < mLength ? mCommands[mIndex] : nullptr;
   1.236 +  };
   1.237 +
   1.238 +  ErrorCallback
   1.239 +  getErrorCallback() const
   1.240 +  {
   1.241 +    return mError;
   1.242 +  };
   1.243 +
   1.244 +private:
   1.245 +  uint32_t mIndex;
   1.246 +  NetworkParams mParams;
   1.247 +  CommandFunc* mCommands;
   1.248 +  uint32_t mLength;
   1.249 +  ErrorCallback mError;
   1.250 +};
   1.251 +
   1.252 +class NetworkUtils MOZ_FINAL
   1.253 +{
   1.254 +public:
   1.255 +  NetworkUtils(MessageCallback aCallback);
   1.256 +  ~NetworkUtils();
   1.257 +
   1.258 +  void ExecuteCommand(NetworkParams aOptions);
   1.259 +  void onNetdMessage(mozilla::ipc::NetdCommand* aCommand);
   1.260 +
   1.261 +  MessageCallback getMessageCallback() { return mMessageCallback; }
   1.262 +
   1.263 +private:
   1.264 +  /**
   1.265 +   * Commands supported by NetworkUtils.
   1.266 +   */
   1.267 +  bool setDNS(NetworkParams& aOptions);
   1.268 +  bool setDefaultRouteAndDNS(NetworkParams& aOptions);
   1.269 +  bool addHostRoute(NetworkParams& aOptions);
   1.270 +  bool removeDefaultRoute(NetworkParams& aOptions);
   1.271 +  bool removeHostRoute(NetworkParams& aOptions);
   1.272 +  bool removeHostRoutes(NetworkParams& aOptions);
   1.273 +  bool removeNetworkRoute(NetworkParams& aOptions);
   1.274 +  bool addSecondaryRoute(NetworkParams& aOptions);
   1.275 +  bool removeSecondaryRoute(NetworkParams& aOptions);
   1.276 +  bool getNetworkInterfaceStats(NetworkParams& aOptions);
   1.277 +  bool setNetworkInterfaceAlarm(NetworkParams& aOptions);
   1.278 +  bool enableNetworkInterfaceAlarm(NetworkParams& aOptions);
   1.279 +  bool disableNetworkInterfaceAlarm(NetworkParams& aOptions);
   1.280 +  bool setWifiOperationMode(NetworkParams& aOptions);
   1.281 +  bool setDhcpServer(NetworkParams& aOptions);
   1.282 +  bool setWifiTethering(NetworkParams& aOptions);
   1.283 +  bool setUSBTethering(NetworkParams& aOptions);
   1.284 +  bool enableUsbRndis(NetworkParams& aOptions);
   1.285 +  bool updateUpStream(NetworkParams& aOptions);
   1.286 +
   1.287 +  /**
   1.288 +   * function pointer array holds all netd commands should be executed
   1.289 +   * in sequence to accomplish a given command by other module.
   1.290 +   */
   1.291 +  static CommandFunc sWifiEnableChain[];
   1.292 +  static CommandFunc sWifiDisableChain[];
   1.293 +  static CommandFunc sWifiFailChain[];
   1.294 +  static CommandFunc sWifiOperationModeChain[];
   1.295 +  static CommandFunc sUSBEnableChain[];
   1.296 +  static CommandFunc sUSBDisableChain[];
   1.297 +  static CommandFunc sUSBFailChain[];
   1.298 +  static CommandFunc sUpdateUpStreamChain[];
   1.299 +  static CommandFunc sStartDhcpServerChain[];
   1.300 +  static CommandFunc sStopDhcpServerChain[];
   1.301 +  static CommandFunc sNetworkInterfaceStatsChain[];
   1.302 +  static CommandFunc sNetworkInterfaceEnableAlarmChain[];
   1.303 +  static CommandFunc sNetworkInterfaceDisableAlarmChain[];
   1.304 +  static CommandFunc sNetworkInterfaceSetAlarmChain[];
   1.305 +  static CommandFunc sSetDnsChain[];
   1.306 +
   1.307 +  /**
   1.308 +   * Individual netd command stored in command chain.
   1.309 +   */
   1.310 +#define PARAMS CommandChain* aChain, CommandCallback aCallback, NetworkResultOptions& aResult
   1.311 +  static void wifiFirmwareReload(PARAMS);
   1.312 +  static void startAccessPointDriver(PARAMS);
   1.313 +  static void stopAccessPointDriver(PARAMS);
   1.314 +  static void setAccessPoint(PARAMS);
   1.315 +  static void cleanUpStream(PARAMS);
   1.316 +  static void createUpStream(PARAMS);
   1.317 +  static void startSoftAP(PARAMS);
   1.318 +  static void stopSoftAP(PARAMS);
   1.319 +  static void getRxBytes(PARAMS);
   1.320 +  static void getTxBytes(PARAMS);
   1.321 +  static void enableAlarm(PARAMS);
   1.322 +  static void disableAlarm(PARAMS);
   1.323 +  static void setQuota(PARAMS);
   1.324 +  static void removeQuota(PARAMS);
   1.325 +  static void setAlarm(PARAMS);
   1.326 +  static void setInterfaceUp(PARAMS);
   1.327 +  static void tetherInterface(PARAMS);
   1.328 +  static void preTetherInterfaceList(PARAMS);
   1.329 +  static void postTetherInterfaceList(PARAMS);
   1.330 +  static void setIpForwardingEnabled(PARAMS);
   1.331 +  static void tetheringStatus(PARAMS);
   1.332 +  static void stopTethering(PARAMS);
   1.333 +  static void startTethering(PARAMS);
   1.334 +  static void untetherInterface(PARAMS);
   1.335 +  static void setDnsForwarders(PARAMS);
   1.336 +  static void enableNat(PARAMS);
   1.337 +  static void disableNat(PARAMS);
   1.338 +  static void setDefaultInterface(PARAMS);
   1.339 +  static void setInterfaceDns(PARAMS);
   1.340 +  static void wifiTetheringSuccess(PARAMS);
   1.341 +  static void usbTetheringSuccess(PARAMS);
   1.342 +  static void networkInterfaceStatsSuccess(PARAMS);
   1.343 +  static void networkInterfaceAlarmSuccess(PARAMS);
   1.344 +  static void updateUpStreamSuccess(PARAMS);
   1.345 +  static void setDhcpServerSuccess(PARAMS);
   1.346 +  static void wifiOperationModeSuccess(PARAMS);
   1.347 +#undef PARAMS
   1.348 +
   1.349 +  /**
   1.350 +   * Error callback function executed when a command is fail.
   1.351 +   */
   1.352 +#define PARAMS NetworkParams& aOptions, NetworkResultOptions& aResult
   1.353 +  static void wifiTetheringFail(PARAMS);
   1.354 +  static void wifiOperationModeFail(PARAMS);
   1.355 +  static void usbTetheringFail(PARAMS);
   1.356 +  static void updateUpStreamFail(PARAMS);
   1.357 +  static void setDhcpServerFail(PARAMS);
   1.358 +  static void networkInterfaceStatsFail(PARAMS);
   1.359 +  static void networkInterfaceAlarmFail(PARAMS);
   1.360 +  static void setDnsFail(PARAMS);
   1.361 +#undef PARAMS
   1.362 +
   1.363 +  /**
   1.364 +   * Command chain processing functions.
   1.365 +   */
   1.366 +  static void next(CommandChain* aChain, bool aError, NetworkResultOptions& aResult);
   1.367 +  static void nextNetdCommand();
   1.368 +  static void doCommand(const char* aCommand, CommandChain* aChain, CommandCallback aCallback);
   1.369 +
   1.370 +  /**
   1.371 +   * Notify broadcast message to main thread.
   1.372 +   */
   1.373 +  void sendBroadcastMessage(uint32_t code, char* reason);
   1.374 +
   1.375 +  /**
   1.376 +   * Utility functions.
   1.377 +   */
   1.378 +  void checkUsbRndisState(NetworkParams& aOptions);
   1.379 +  void dumpParams(NetworkParams& aOptions, const char* aType);
   1.380 +
   1.381 +  static void escapeQuote(nsCString& aString);
   1.382 +  inline uint32_t netdResponseType(uint32_t code);
   1.383 +  inline bool isBroadcastMessage(uint32_t code);
   1.384 +  inline bool isError(uint32_t code);
   1.385 +  inline bool isComplete(uint32_t code);
   1.386 +  inline bool isProceeding(uint32_t code);
   1.387 +  void Shutdown();
   1.388 +  /**
   1.389 +   * Callback function to send netd result to main thread.
   1.390 +   */
   1.391 +  MessageCallback mMessageCallback;
   1.392 +
   1.393 +  /*
   1.394 +   * Utility class to access libnetutils.
   1.395 +   */
   1.396 +  nsAutoPtr<NetUtils> mNetUtils;
   1.397 +};
   1.398 +
   1.399 +#endif

mercurial