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

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

mercurial