Bug 1038531 - Unify NetworkWorker. r=mrbkap r=fabrice

This commit is contained in:
Henry Chang 2014-08-18 11:02:56 +08:00
parent 78da0cea96
commit 4dbc8e4fd0
14 changed files with 543 additions and 471 deletions

View File

@ -592,7 +592,7 @@ NetworkManager.prototype = {
// The override was just set, so reconfigure the network.
if (this.active != this._overriddenActive) {
this.active = this._overriddenActive;
gNetworkService.setDefaultRouteAndDNS(this.active, oldActive);
this._setDefaultRouteAndDNS(this.active, oldActive);
Services.obs.notifyObservers(this.active, TOPIC_ACTIVE_CHANGED, null);
}
return;
@ -603,7 +603,7 @@ NetworkManager.prototype = {
this.active.state == Ci.nsINetworkInterface.NETWORK_STATE_CONNECTED &&
this.active.type == this._preferredNetworkType) {
debug("Active network is already our preferred type.");
gNetworkService.setDefaultRouteAndDNS(this.active, oldActive);
this._setDefaultRouteAndDNS(this.active, oldActive);
return;
}
@ -642,7 +642,7 @@ NetworkManager.prototype = {
gNetworkService.setDNS(this.active);
} else {
#endif // MOZ_B2G_RIL
gNetworkService.setDefaultRouteAndDNS(this.active, oldActive);
this._setDefaultRouteAndDNS(this.active, oldActive);
#ifdef MOZ_B2G_RIL
}
#endif
@ -1249,7 +1249,15 @@ NetworkManager.prototype = {
this.wantConnectionEvent = null;
callback.call(this);
}
},
_setDefaultRouteAndDNS: function(network, oldInterface) {
gNetworkService.setDefaultRoute(network, oldInterface, function(success) {
gNetworkService.setDNS(network, function(result) {
gNetworkService.setNetworkProxy(network);
});
});
},
};
let CaptivePortalDetectionHelper = (function() {

View File

@ -188,7 +188,6 @@ NetworkService.prototype = {
};
params.report = true;
params.isAsync = true;
this.controlMessage(params, function(result) {
if (!isError(result.resultCode)) {
@ -210,7 +209,6 @@ NetworkService.prototype = {
};
params.report = true;
params.isAsync = true;
this.controlMessage(params, function(result) {
if (!isError(result.resultCode)) {
@ -230,7 +228,6 @@ NetworkService.prototype = {
};
params.report = true;
params.isAsync = true;
this.controlMessage(params, function(result) {
callback(result);
@ -247,7 +244,6 @@ NetworkService.prototype = {
};
params.report = true;
params.isAsync = true;
this.controlMessage(params, function(result) {
if (isError(result.resultCode)) {
@ -277,8 +273,8 @@ NetworkService.prototype = {
}
},
setDNS: function(networkInterface) {
if(DEBUG) debug("Going DNS to " + networkInterface.name);
setDNS: function(networkInterface, callback) {
if (DEBUG) debug("Going DNS to " + networkInterface.name);
let dnses = networkInterface.getDnses();
let options = {
cmd: "setDNS",
@ -286,23 +282,23 @@ NetworkService.prototype = {
domain: "mozilla." + networkInterface.name + ".doman",
dnses: dnses
};
this.controlMessage(options);
this.controlMessage(options, function(result) {
callback.setDnsResult(result.success ? null : result.reason);
});
},
setDefaultRouteAndDNS: function(network, oldInterface) {
if(DEBUG) debug("Going to change route and DNS to " + network.name);
setDefaultRoute: function(network, oldInterface, callback) {
if (DEBUG) debug("Going to change default route to " + network.name);
let gateways = network.getGateways();
let dnses = network.getDnses();
let options = {
cmd: "setDefaultRouteAndDNS",
cmd: "setDefaultRoute",
ifname: network.name,
oldIfname: (oldInterface && oldInterface !== network) ? oldInterface.name : null,
gateways: gateways,
domain: "mozilla." + network.name + ".doman",
dnses: dnses
gateways: gateways
};
this.controlMessage(options);
this.setNetworkProxy(network);
this.controlMessage(options, function(result) {
callback.nativeCommandResult(!result.error);
});
},
removeDefaultRoute: function(network) {
@ -415,7 +411,6 @@ NetworkService.prototype = {
}
config.cmd = "setDhcpServer";
config.isAsync = true;
config.enabled = enabled;
this.controlMessage(config, function setDhcpServerResult(response) {
@ -437,7 +432,6 @@ NetworkService.prototype = {
config.cmd = "setWifiTethering";
// The callback function in controlMessage may not be fired immediately.
config.isAsync = true;
this.controlMessage(config, function setWifiTetheringResult(data) {
let code = data.resultCode;
let reason = data.resultReason;
@ -458,7 +452,6 @@ NetworkService.prototype = {
setUSBTethering: function(enable, config, callback) {
config.cmd = "setUSBTethering";
// The callback function in controlMessage may not be fired immediately.
config.isAsync = true;
this.controlMessage(config, function setUsbTetheringResult(data) {
let code = data.resultCode;
let reason = data.resultReason;
@ -491,7 +484,6 @@ NetworkService.prototype = {
}
// The callback function in controlMessage may not be fired immediately.
params.isAsync = true;
//this._usbTetheringAction = TETHERING_STATE_ONGOING;
this.controlMessage(params, function(data) {
callback.enableUsbRndisResult(data.result, data.enable);
@ -501,7 +493,6 @@ NetworkService.prototype = {
updateUpStream: function(previous, current, callback) {
let params = {
cmd: "updateUpStream",
isAsync: true,
preInternalIfname: previous.internalIfname,
preExternalIfname: previous.externalIfname,
curInternalIfname: current.internalIfname,
@ -516,6 +507,66 @@ NetworkService.prototype = {
});
},
configureInterface: function(config, callback) {
let params = {
cmd: "configureInterface",
ifname: config.ifname,
ipaddr: config.ipaddr,
mask: config.mask,
gateway_long: config.gateway,
dns1_long: config.dns1,
dns2_long: config.dns2,
};
this.controlMessage(params, function(result) {
callback.nativeCommandResult(!result.error);
});
},
dhcpRequest: function(interfaceName, callback) {
let params = {
cmd: "dhcpRequest",
ifname: interfaceName
};
this.controlMessage(params, function(result) {
callback.dhcpRequestResult(!result.error, result.error ? null : result);
});
},
enableInterface: function(interfaceName, callback) {
let params = {
cmd: "enableInterface",
ifname: interfaceName
};
this.controlMessage(params, function(result) {
callback.nativeCommandResult(!result.error);
});
},
disableInterface: function(interfaceName, callback) {
let params = {
cmd: "disableInterface",
ifname: interfaceName
};
this.controlMessage(params, function(result) {
callback.nativeCommandResult(!result.error);
});
},
resetConnections: function(interfaceName, callback) {
let params = {
cmd: "resetConnections",
ifname: interfaceName
};
this.controlMessage(params, function(result) {
callback.nativeCommandResult(!result.error);
});
},
shutdown: false,
observe: function observe(aSubject, aTopic, aData) {

View File

@ -101,7 +101,14 @@ typedef Tuple3<NetdCommand*, CommandChain*, CommandCallback> QueueData;
#define GET_CURRENT_CALLBACK (gCommandQueue.IsEmpty() ? nullptr : gCommandQueue[0].c)
#define GET_CURRENT_COMMAND (gCommandQueue.IsEmpty() ? nullptr : gCommandQueue[0].a->mData)
#define CNT_OF_ARRAY(a) (sizeof(a) / sizeof(a[0]))
// A macro for native function call return value check.
// For native function call, non-zero return value means failure.
#define RETURN_IF_FAILED(rv) do { \
if (SUCCESS != rv) { \
return rv; \
} \
} while (0);
static NetworkUtils* gNetworkUtils;
static nsTArray<QueueData> gCommandQueue;
@ -393,6 +400,40 @@ void NetworkUtils::next(CommandChain* aChain, bool aError, NetworkResultOptions&
(*f)(aChain, next, aResult);
}
CommandResult::CommandResult(int32_t aResultCode)
: mIsPending(false)
{
// This is usually not a netd command. We treat the return code
// typical linux convention, which uses 0 to indicate success.
mResult.mError = (aResultCode == SUCCESS ? false : true);
mResult.mResultCode = aResultCode;
if (aResultCode != SUCCESS) {
// The returned value is sometimes negative, make sure we pass a positive
// error number to strerror.
enum { STRERROR_R_BUF_SIZE = 1024, };
char strerrorBuf[STRERROR_R_BUF_SIZE];
strerror_r(abs(aResultCode), strerrorBuf, STRERROR_R_BUF_SIZE);
mResult.mReason = NS_ConvertUTF8toUTF16(strerrorBuf);
}
mResult.mRet = true;
}
CommandResult::CommandResult(const mozilla::dom::NetworkResultOptions& aResult)
: mResult(aResult)
, mIsPending(false)
{
}
CommandResult::CommandResult(const Pending&)
: mIsPending(true)
{
}
bool CommandResult::isPending() const
{
return mIsPending;
}
/**
* Send command to netd.
*/
@ -1030,14 +1071,19 @@ NetworkUtils::~NetworkUtils()
#define GET_CHAR(prop) NS_ConvertUTF16toUTF8(aOptions.prop).get()
#define GET_FIELD(prop) aOptions.prop
// Hoist this type definition to global to avoid template
// instantiation error on gcc 4.4 used by ICS emulator.
typedef CommandResult (NetworkUtils::*CommandHandler)(NetworkParams&);
struct CommandHandlerEntry
{
const char* mCommandName;
CommandHandler mCommandHandler;
};
void NetworkUtils::ExecuteCommand(NetworkParams aOptions)
{
typedef int32_t (NetworkUtils::*CommandHandler)(NetworkParams&);
const static struct {
const char* mCommandName;
CommandHandler mCommandHandler;
} COMMAND_HANDLER_TABLE[] = {
const static CommandHandlerEntry
COMMAND_HANDLER_TABLE[] = {
// For command 'testCommand', BUILD_ENTRY(testCommand) will generate
// {"testCommand", NetworkUtils::testCommand}
@ -1045,7 +1091,7 @@ void NetworkUtils::ExecuteCommand(NetworkParams aOptions)
BUILD_ENTRY(removeNetworkRoute),
BUILD_ENTRY(setDNS),
BUILD_ENTRY(setDefaultRouteAndDNS),
BUILD_ENTRY(setDefaultRoute),
BUILD_ENTRY(removeDefaultRoute),
BUILD_ENTRY(addHostRoute),
BUILD_ENTRY(removeHostRoute),
@ -1061,13 +1107,18 @@ void NetworkUtils::ExecuteCommand(NetworkParams aOptions)
BUILD_ENTRY(setUSBTethering),
BUILD_ENTRY(enableUsbRndis),
BUILD_ENTRY(updateUpStream),
BUILD_ENTRY(configureInterface),
BUILD_ENTRY(dhcpRequest),
BUILD_ENTRY(enableInterface),
BUILD_ENTRY(disableInterface),
BUILD_ENTRY(resetConnections),
#undef BUILD_ENTRY
};
// Loop until we find the command name which matches aOptions.mCmd.
CommandHandler handler = nullptr;
for (size_t i = 0; i < CNT_OF_ARRAY(COMMAND_HANDLER_TABLE); i++) {
for (size_t i = 0; i < mozilla::ArrayLength(COMMAND_HANDLER_TABLE); i++) {
if (aOptions.mCmd.EqualsASCII(COMMAND_HANDLER_TABLE[i].mCommandName)) {
handler = COMMAND_HANDLER_TABLE[i].mCommandHandler;
break;
@ -1080,25 +1131,19 @@ void NetworkUtils::ExecuteCommand(NetworkParams aOptions)
return;
}
// Command matches! Dispatch to the handler.
int32_t ret = 0;
ret = (this->*handler)(aOptions);
if (!aOptions.mIsAsync) {
// The requested command is synchronous, which implies the actual result
// from netd is not important to the client. So, just notify the
// registered callback.
NetworkResultOptions result;
result.mError = ret == SUCCESS ? false : true;
result.mResultCode = ret;
if (ret != SUCCESS) {
// The returned value is sometimes negative, make sure we pass a positive
// error number to strerror.
result.mReason = NS_ConvertUTF8toUTF16(strerror(abs(ret)));
}
result.mRet = true;
postMessage(aOptions, result);
// The handler would return one of the following 3 values
// to be wrapped to CommandResult:
//
// 1) |int32_t| for mostly synchronous native function calls.
// 2) |NetworkResultOptions| to populate additional results. (e.g. dhcpRequest)
// 3) |CommandResult::Pending| to indicate the result is not
// obtained yet.
//
// If the handler returns "Pending", the handler should take the
// responsibility for posting result to main thread.
CommandResult commandResult = (this->*handler)(aOptions);
if (!commandResult.isPending()) {
postMessage(aOptions, commandResult.mResult);
}
}
@ -1183,7 +1228,7 @@ void NetworkUtils::onNetdMessage(NetdCommand* aCommand)
/**
* Start/Stop DHCP server.
*/
int32_t NetworkUtils::setDhcpServer(NetworkParams& aOptions)
CommandResult NetworkUtils::setDhcpServer(NetworkParams& aOptions)
{
if (aOptions.mEnabled) {
aOptions.mWifiStartIp = aOptions.mStartIp;
@ -1196,13 +1241,13 @@ int32_t NetworkUtils::setDhcpServer(NetworkParams& aOptions)
} else {
RUN_CHAIN(aOptions, sStopDhcpServerChain, setDhcpServerFail)
}
return SUCCESS;
return CommandResult::Pending();
}
/**
* Set DNS servers for given network interface.
*/
int32_t NetworkUtils::setDNS(NetworkParams& aOptions)
CommandResult NetworkUtils::setDNS(NetworkParams& aOptions)
{
uint32_t length = aOptions.mDnses.Length();
@ -1234,23 +1279,116 @@ int32_t NetworkUtils::setDNS(NetworkParams& aOptions)
// DNS needs to be set through netd since JellyBean (4.3).
if (SDK_VERSION >= 18) {
RUN_CHAIN(aOptions, sSetDnsChain, setDnsFail)
return CommandResult::Pending();
}
return SUCCESS;
}
CommandResult NetworkUtils::configureInterface(NetworkParams& aOptions)
{
NS_ConvertUTF16toUTF8 autoIfname(aOptions.mIfname);
return mNetUtils->do_ifc_configure(
autoIfname.get(),
aOptions.mIpaddr,
aOptions.mMask,
aOptions.mGateway_long,
aOptions.mDns1_long,
aOptions.mDns2_long
);
}
CommandResult NetworkUtils::dhcpRequest(NetworkParams& aOptions) {
mozilla::dom::NetworkResultOptions result;
NS_ConvertUTF16toUTF8 autoIfname(aOptions.mIfname);
char ipaddr[PROPERTY_VALUE_MAX];
char gateway[PROPERTY_VALUE_MAX];
uint32_t prefixLength;
char dns1[PROPERTY_VALUE_MAX];
char dns2[PROPERTY_VALUE_MAX];
char server[PROPERTY_VALUE_MAX];
uint32_t lease;
char vendorinfo[PROPERTY_VALUE_MAX];
int32_t ret = mNetUtils->do_dhcp_do_request(autoIfname.get(),
ipaddr,
gateway,
&prefixLength,
dns1,
dns2,
server,
&lease,
vendorinfo);
RETURN_IF_FAILED(ret);
result.mIpaddr_str = NS_ConvertUTF8toUTF16(ipaddr);
result.mGateway_str = NS_ConvertUTF8toUTF16(gateway);
result.mDns1_str = NS_ConvertUTF8toUTF16(dns1);
result.mDns2_str = NS_ConvertUTF8toUTF16(dns2);
result.mServer_str = NS_ConvertUTF8toUTF16(server);
result.mVendor_str = NS_ConvertUTF8toUTF16(vendorinfo);
result.mLease = lease;
result.mMask = makeMask(prefixLength);
uint32_t inet4; // only support IPv4 for now.
#define INET_PTON(var, field) \
PR_BEGIN_MACRO \
inet_pton(AF_INET, var, &inet4); \
result.field = inet4; \
PR_END_MACRO
INET_PTON(ipaddr, mIpaddr);
INET_PTON(gateway, mGateway);
if (dns1[0] != '\0') {
INET_PTON(dns1, mDns1);
}
if (dns2[0] != '\0') {
INET_PTON(dns2, mDns2);
}
INET_PTON(server, mServer);
char inet_str[64];
if (inet_ntop(AF_INET, &result.mMask, inet_str, sizeof(inet_str))) {
result.mMask_str = NS_ConvertUTF8toUTF16(inet_str);
}
return result;
}
CommandResult NetworkUtils::enableInterface(NetworkParams& aOptions) {
return mNetUtils->do_ifc_enable(
NS_ConvertUTF16toUTF8(aOptions.mIfname).get());
}
CommandResult NetworkUtils::disableInterface(NetworkParams& aOptions) {
return mNetUtils->do_ifc_disable(
NS_ConvertUTF16toUTF8(aOptions.mIfname).get());
}
CommandResult NetworkUtils::resetConnections(NetworkParams& aOptions) {
NS_ConvertUTF16toUTF8 autoIfname(aOptions.mIfname);
return mNetUtils->do_ifc_reset_connections(
NS_ConvertUTF16toUTF8(aOptions.mIfname).get(),
RESET_ALL_ADDRESSES);
}
/**
* Set default route and DNS servers for given network interface.
*/
int32_t NetworkUtils::setDefaultRouteAndDNS(NetworkParams& aOptions)
CommandResult NetworkUtils::setDefaultRoute(NetworkParams& aOptions)
{
NS_ConvertUTF16toUTF8 autoIfname(aOptions.mIfname);
if (!aOptions.mOldIfname.IsEmpty()) {
// Remove IPv4's default route.
mNetUtils->do_ifc_remove_default_route(GET_CHAR(mOldIfname));
RETURN_IF_FAILED(mNetUtils->do_ifc_remove_default_route(GET_CHAR(mOldIfname)));
// Remove IPv6's default route.
mNetUtils->do_ifc_remove_route(GET_CHAR(mOldIfname), "::", 0, NULL);
RETURN_IF_FAILED(mNetUtils->do_ifc_remove_route(GET_CHAR(mOldIfname), "::", 0, NULL));
}
uint32_t length = aOptions.mGateways.Length();
@ -1264,9 +1402,9 @@ int32_t NetworkUtils::setDefaultRouteAndDNS(NetworkParams& aOptions)
}
if (type == AF_INET6) {
mNetUtils->do_ifc_add_route(autoIfname.get(), "::", 0, autoGateway.get());
RETURN_IF_FAILED(mNetUtils->do_ifc_add_route(autoIfname.get(), "::", 0, autoGateway.get()));
} else { /* type == AF_INET */
mNetUtils->do_ifc_set_default_route(autoIfname.get(), inet_addr(autoGateway.get()));
RETURN_IF_FAILED(mNetUtils->do_ifc_set_default_route(autoIfname.get(), inet_addr(autoGateway.get())));
}
}
} else {
@ -1283,20 +1421,19 @@ int32_t NetworkUtils::setDefaultRouteAndDNS(NetworkParams& aOptions)
}
if (type == AF_INET6) {
mNetUtils->do_ifc_add_route(autoIfname.get(), "::", 0, gateway);
RETURN_IF_FAILED(mNetUtils->do_ifc_add_route(autoIfname.get(), "::", 0, gateway));
} else { /* type == AF_INET */
mNetUtils->do_ifc_set_default_route(autoIfname.get(), inet_addr(gateway));
RETURN_IF_FAILED(mNetUtils->do_ifc_set_default_route(autoIfname.get(), inet_addr(gateway)));
}
}
setDNS(aOptions);
return SUCCESS;
}
/**
* Remove default route for given network interface.
*/
int32_t NetworkUtils::removeDefaultRoute(NetworkParams& aOptions)
CommandResult NetworkUtils::removeDefaultRoute(NetworkParams& aOptions)
{
uint32_t length = aOptions.mGateways.Length();
for (uint32_t i = 0; i < length; i++) {
@ -1307,9 +1444,9 @@ int32_t NetworkUtils::removeDefaultRoute(NetworkParams& aOptions)
return EAFNOSUPPORT;
}
mNetUtils->do_ifc_remove_route(GET_CHAR(mIfname),
type == AF_INET ? "0.0.0.0" : "::",
0, autoGateway.get());
RETURN_IF_FAILED(mNetUtils->do_ifc_remove_route(GET_CHAR(mIfname),
type == AF_INET ? "0.0.0.0" : "::",
0, autoGateway.get()));
}
return SUCCESS;
@ -1318,7 +1455,7 @@ int32_t NetworkUtils::removeDefaultRoute(NetworkParams& aOptions)
/**
* Add host route for given network interface.
*/
int32_t NetworkUtils::addHostRoute(NetworkParams& aOptions)
CommandResult NetworkUtils::addHostRoute(NetworkParams& aOptions)
{
NS_ConvertUTF16toUTF8 autoIfname(aOptions.mIfname);
NS_ConvertUTF16toUTF8 autoHostname(aOptions.mIp);
@ -1342,7 +1479,7 @@ int32_t NetworkUtils::addHostRoute(NetworkParams& aOptions)
/**
* Remove host route for given network interface.
*/
int32_t NetworkUtils::removeHostRoute(NetworkParams& aOptions)
CommandResult NetworkUtils::removeHostRoute(NetworkParams& aOptions)
{
NS_ConvertUTF16toUTF8 autoIfname(aOptions.mIfname);
NS_ConvertUTF16toUTF8 autoHostname(aOptions.mIp);
@ -1366,12 +1503,12 @@ int32_t NetworkUtils::removeHostRoute(NetworkParams& aOptions)
/**
* Remove the routes associated with the named interface.
*/
int32_t NetworkUtils::removeHostRoutes(NetworkParams& aOptions)
CommandResult NetworkUtils::removeHostRoutes(NetworkParams& aOptions)
{
return mNetUtils->do_ifc_remove_host_routes(GET_CHAR(mIfname));
}
int32_t NetworkUtils::removeNetworkRoute(NetworkParams& aOptions)
CommandResult NetworkUtils::removeNetworkRoute(NetworkParams& aOptions)
{
NS_ConvertUTF16toUTF8 autoIfname(aOptions.mIfname);
NS_ConvertUTF16toUTF8 autoIp(aOptions.mIp);
@ -1406,10 +1543,10 @@ int32_t NetworkUtils::removeNetworkRoute(NetworkParams& aOptions)
}
// Remove default route.
mNetUtils->do_ifc_remove_route(autoIfname.get(), "::", 0, NULL);
RETURN_IF_FAILED(mNetUtils->do_ifc_remove_route(autoIfname.get(), "::", 0, NULL));
// Remove subnet route.
mNetUtils->do_ifc_remove_route(autoIfname.get(), subnetStr, prefixLength, NULL);
RETURN_IF_FAILED(mNetUtils->do_ifc_remove_route(autoIfname.get(), subnetStr, prefixLength, NULL));
return SUCCESS;
}
@ -1422,12 +1559,12 @@ int32_t NetworkUtils::removeNetworkRoute(NetworkParams& aOptions)
addr.s_addr = subnet;
const char* dst = inet_ntoa(addr);
mNetUtils->do_ifc_remove_default_route(autoIfname.get());
mNetUtils->do_ifc_remove_route(autoIfname.get(), dst, prefixLength, gateway);
RETURN_IF_FAILED(mNetUtils->do_ifc_remove_default_route(autoIfname.get()));
RETURN_IF_FAILED(mNetUtils->do_ifc_remove_route(autoIfname.get(), dst, prefixLength, gateway));
return SUCCESS;
}
int32_t NetworkUtils::addSecondaryRoute(NetworkParams& aOptions)
CommandResult NetworkUtils::addSecondaryRoute(NetworkParams& aOptions)
{
char command[MAX_COMMAND_SIZE];
snprintf(command, MAX_COMMAND_SIZE - 1,
@ -1441,7 +1578,7 @@ int32_t NetworkUtils::addSecondaryRoute(NetworkParams& aOptions)
return SUCCESS;
}
int32_t NetworkUtils::removeSecondaryRoute(NetworkParams& aOptions)
CommandResult NetworkUtils::removeSecondaryRoute(NetworkParams& aOptions)
{
char command[MAX_COMMAND_SIZE];
snprintf(command, MAX_COMMAND_SIZE - 1,
@ -1455,41 +1592,41 @@ int32_t NetworkUtils::removeSecondaryRoute(NetworkParams& aOptions)
return SUCCESS;
}
int32_t NetworkUtils::setNetworkInterfaceAlarm(NetworkParams& aOptions)
CommandResult NetworkUtils::setNetworkInterfaceAlarm(NetworkParams& aOptions)
{
DEBUG("setNetworkInterfaceAlarms: %s", GET_CHAR(mIfname));
RUN_CHAIN(aOptions, sNetworkInterfaceSetAlarmChain, networkInterfaceAlarmFail);
return SUCCESS;
return CommandResult::Pending();
}
int32_t NetworkUtils::enableNetworkInterfaceAlarm(NetworkParams& aOptions)
CommandResult NetworkUtils::enableNetworkInterfaceAlarm(NetworkParams& aOptions)
{
DEBUG("enableNetworkInterfaceAlarm: %s", GET_CHAR(mIfname));
RUN_CHAIN(aOptions, sNetworkInterfaceEnableAlarmChain, networkInterfaceAlarmFail);
return SUCCESS;
return CommandResult::Pending();
}
int32_t NetworkUtils::disableNetworkInterfaceAlarm(NetworkParams& aOptions)
CommandResult NetworkUtils::disableNetworkInterfaceAlarm(NetworkParams& aOptions)
{
DEBUG("disableNetworkInterfaceAlarms: %s", GET_CHAR(mIfname));
RUN_CHAIN(aOptions, sNetworkInterfaceDisableAlarmChain, networkInterfaceAlarmFail);
return SUCCESS;
return CommandResult::Pending();
}
/**
* handling main thread's reload Wifi firmware request
*/
int32_t NetworkUtils::setWifiOperationMode(NetworkParams& aOptions)
CommandResult NetworkUtils::setWifiOperationMode(NetworkParams& aOptions)
{
DEBUG("setWifiOperationMode: %s %s", GET_CHAR(mIfname), GET_CHAR(mMode));
RUN_CHAIN(aOptions, sWifiOperationModeChain, wifiOperationModeFail);
return SUCCESS;
return CommandResult::Pending();
}
/**
* handling main thread's enable/disable WiFi Tethering request
*/
int32_t NetworkUtils::setWifiTethering(NetworkParams& aOptions)
CommandResult NetworkUtils::setWifiTethering(NetworkParams& aOptions)
{
bool enable = aOptions.mEnable;
IFProperties interfaceProperties;
@ -1518,10 +1655,10 @@ int32_t NetworkUtils::setWifiTethering(NetworkParams& aOptions)
GET_CHAR(mInternalIfname), GET_CHAR(mExternalIfname));
RUN_CHAIN(aOptions, sWifiDisableChain, wifiTetheringFail)
}
return SUCCESS;
return CommandResult::Pending();
}
int32_t NetworkUtils::setUSBTethering(NetworkParams& aOptions)
CommandResult NetworkUtils::setUSBTethering(NetworkParams& aOptions)
{
bool enable = aOptions.mEnable;
IFProperties interfaceProperties;
@ -1550,7 +1687,7 @@ int32_t NetworkUtils::setUSBTethering(NetworkParams& aOptions)
GET_CHAR(mInternalIfname), GET_CHAR(mExternalIfname));
RUN_CHAIN(aOptions, sUSBDisableChain, usbTetheringFail)
}
return SUCCESS;
return CommandResult::Pending();
}
void NetworkUtils::escapeQuote(nsCString& aString)
@ -1559,7 +1696,7 @@ void NetworkUtils::escapeQuote(nsCString& aString)
aString.ReplaceSubstring("\"", "\\\"");
}
void NetworkUtils::checkUsbRndisState(NetworkParams& aOptions)
CommandResult NetworkUtils::checkUsbRndisState(NetworkParams& aOptions)
{
static uint32_t retry = 0;
@ -1574,27 +1711,25 @@ void NetworkUtils::checkUsbRndisState(NetworkParams& aOptions)
NetworkResultOptions result;
result.mEnable = aOptions.mEnable;
result.mResult = true;
postMessage(aOptions, result);
retry = 0;
return;
return result;
}
if (retry < USB_FUNCTION_RETRY_TIMES) {
retry++;
usleep(USB_FUNCTION_RETRY_INTERVAL * 1000);
checkUsbRndisState(aOptions);
return;
return checkUsbRndisState(aOptions);
}
NetworkResultOptions result;
result.mResult = false;
postMessage(aOptions, result);
retry = 0;
return result;
}
/**
* Modify usb function's property to turn on USB RNDIS function
*/
int32_t NetworkUtils::enableUsbRndis(NetworkParams& aOptions)
CommandResult NetworkUtils::enableUsbRndis(NetworkParams& aOptions)
{
bool report = aOptions.mReport;
@ -1650,7 +1785,7 @@ int32_t NetworkUtils::enableUsbRndis(NetworkParams& aOptions)
// Trigger the timer to check usb state and report the result to NetworkManager.
if (report) {
usleep(USB_FUNCTION_RETRY_INTERVAL * 1000);
checkUsbRndisState(aOptions);
return checkUsbRndisState(aOptions);
}
return SUCCESS;
}
@ -1658,10 +1793,10 @@ int32_t NetworkUtils::enableUsbRndis(NetworkParams& aOptions)
/**
* handling upstream interface change event.
*/
int32_t NetworkUtils::updateUpStream(NetworkParams& aOptions)
CommandResult NetworkUtils::updateUpStream(NetworkParams& aOptions)
{
RUN_CHAIN(aOptions, sUpdateUpStreamChain, updateUpStreamFail)
return SUCCESS;
return CommandResult::Pending();
}
void NetworkUtils::sendBroadcastMessage(uint32_t code, char* reason)

View File

@ -28,48 +28,6 @@ public:
NetworkParams() {
}
NetworkParams(const NetworkParams& aOther) {
mIp = aOther.mIp;
mCmd = aOther.mCmd;
mDomain = aOther.mDomain;
mGateway = aOther.mGateway;
mGateways = aOther.mGateways;
mId = aOther.mId;
mIfname = aOther.mIfname;
mPrefixLength = aOther.mPrefixLength;
mOldIfname = aOther.mOldIfname;
mMode = aOther.mMode;
mReport = aOther.mReport;
mIsAsync = aOther.mIsAsync;
mEnabled = aOther.mEnabled;
mWifictrlinterfacename = aOther.mWifictrlinterfacename;
mInternalIfname = aOther.mInternalIfname;
mExternalIfname = aOther.mExternalIfname;
mEnable = aOther.mEnable;
mSsid = aOther.mSsid;
mSecurity = aOther.mSecurity;
mKey = aOther.mKey;
mPrefix = aOther.mPrefix;
mLink = aOther.mLink;
mInterfaceList = aOther.mInterfaceList;
mWifiStartIp = aOther.mWifiStartIp;
mWifiEndIp = aOther.mWifiEndIp;
mUsbStartIp = aOther.mUsbStartIp;
mUsbEndIp = aOther.mUsbEndIp;
mDns1 = aOther.mDns1;
mDns2 = aOther.mDns2;
mDnses = aOther.mDnses;
mStartIp = aOther.mStartIp;
mEndIp = aOther.mEndIp;
mServerIp = aOther.mServerIp;
mMaskLength = aOther.mMaskLength;
mPreInternalIfname = aOther.mPreInternalIfname;
mPreExternalIfname = aOther.mPreExternalIfname;
mCurInternalIfname = aOther.mCurInternalIfname;
mCurExternalIfname = aOther.mCurExternalIfname;
mThreshold = aOther.mThreshold;
}
NetworkParams(const mozilla::dom::NetworkCommandOptions& aOther) {
#define COPY_SEQUENCE_FIELD(prop, type) \
@ -112,7 +70,6 @@ public:
COPY_OPT_STRING_FIELD(mOldIfname, EmptyString())
COPY_OPT_STRING_FIELD(mMode, EmptyString())
COPY_OPT_FIELD(mReport, false)
COPY_OPT_FIELD(mIsAsync, false)
COPY_OPT_FIELD(mEnabled, false)
COPY_OPT_STRING_FIELD(mWifictrlinterfacename, EmptyString())
COPY_OPT_STRING_FIELD(mInternalIfname, EmptyString())
@ -140,6 +97,11 @@ public:
COPY_OPT_STRING_FIELD(mCurInternalIfname, EmptyString())
COPY_OPT_STRING_FIELD(mCurExternalIfname, EmptyString())
COPY_OPT_FIELD(mThreshold, -1)
COPY_OPT_FIELD(mIpaddr, 0)
COPY_OPT_FIELD(mMask, 0)
COPY_OPT_FIELD(mGateway_long, 0)
COPY_OPT_FIELD(mDns1_long, 0)
COPY_OPT_FIELD(mDns2_long, 0)
#undef COPY_SEQUENCE_FIELD
#undef COPY_OPT_STRING_FIELD
@ -158,7 +120,6 @@ public:
nsString mOldIfname;
nsString mMode;
bool mReport;
bool mIsAsync;
bool mEnabled;
nsString mWifictrlinterfacename;
nsString mInternalIfname;
@ -186,6 +147,11 @@ public:
nsString mCurInternalIfname;
nsString mCurExternalIfname;
long mThreshold;
long mIpaddr;
long mMask;
long mGateway_long;
long mDns1_long;
long mDns2_long;
};
// CommandChain store the necessary information to execute command one by one.
@ -235,6 +201,25 @@ private:
ErrorCallback mError;
};
// A helper class to easily construct a resolved
// or a pending result for command execution.
class CommandResult
{
public:
struct Pending {};
public:
CommandResult(int32_t aResultCode);
CommandResult(const mozilla::dom::NetworkResultOptions& aResult);
CommandResult(const Pending&);
bool isPending() const;
mozilla::dom::NetworkResultOptions mResult;
private:
bool mIsPending;
};
class NetworkUtils MOZ_FINAL
{
public:
@ -250,24 +235,29 @@ private:
/**
* Commands supported by NetworkUtils.
*/
int32_t setDNS(NetworkParams& aOptions);
int32_t setDefaultRouteAndDNS(NetworkParams& aOptions);
int32_t addHostRoute(NetworkParams& aOptions);
int32_t removeDefaultRoute(NetworkParams& aOptions);
int32_t removeHostRoute(NetworkParams& aOptions);
int32_t removeHostRoutes(NetworkParams& aOptions);
int32_t removeNetworkRoute(NetworkParams& aOptions);
int32_t addSecondaryRoute(NetworkParams& aOptions);
int32_t removeSecondaryRoute(NetworkParams& aOptions);
int32_t setNetworkInterfaceAlarm(NetworkParams& aOptions);
int32_t enableNetworkInterfaceAlarm(NetworkParams& aOptions);
int32_t disableNetworkInterfaceAlarm(NetworkParams& aOptions);
int32_t setWifiOperationMode(NetworkParams& aOptions);
int32_t setDhcpServer(NetworkParams& aOptions);
int32_t setWifiTethering(NetworkParams& aOptions);
int32_t setUSBTethering(NetworkParams& aOptions);
int32_t enableUsbRndis(NetworkParams& aOptions);
int32_t updateUpStream(NetworkParams& aOptions);
CommandResult configureInterface(NetworkParams& aOptions);
CommandResult dhcpRequest(NetworkParams& aOptions);
CommandResult enableInterface(NetworkParams& aOptions);
CommandResult disableInterface(NetworkParams& aOptions);
CommandResult resetConnections(NetworkParams& aOptions);
CommandResult setDefaultRoute(NetworkParams& aOptions);
CommandResult addHostRoute(NetworkParams& aOptions);
CommandResult removeDefaultRoute(NetworkParams& aOptions);
CommandResult removeHostRoute(NetworkParams& aOptions);
CommandResult removeHostRoutes(NetworkParams& aOptions);
CommandResult removeNetworkRoute(NetworkParams& aOptions);
CommandResult setDNS(NetworkParams& aOptions);
CommandResult addSecondaryRoute(NetworkParams& aOptions);
CommandResult removeSecondaryRoute(NetworkParams& aOptions);
CommandResult setNetworkInterfaceAlarm(NetworkParams& aOptions);
CommandResult enableNetworkInterfaceAlarm(NetworkParams& aOptions);
CommandResult disableNetworkInterfaceAlarm(NetworkParams& aOptions);
CommandResult setWifiOperationMode(NetworkParams& aOptions);
CommandResult setDhcpServer(NetworkParams& aOptions);
CommandResult setWifiTethering(NetworkParams& aOptions);
CommandResult setUSBTethering(NetworkParams& aOptions);
CommandResult enableUsbRndis(NetworkParams& aOptions);
CommandResult updateUpStream(NetworkParams& aOptions);
/**
* function pointer array holds all netd commands should be executed
@ -360,7 +350,7 @@ private:
/**
* Utility functions.
*/
void checkUsbRndisState(NetworkParams& aOptions);
CommandResult checkUsbRndisState(NetworkParams& aOptions);
void dumpParams(NetworkParams& aOptions, const char* aType);
static void escapeQuote(nsCString& aString);

View File

@ -33,24 +33,9 @@ class NetworkResultDispatcher : public nsRunnable
{
public:
NetworkResultDispatcher(const NetworkResultOptions& aResult)
: mResult(aResult)
{
MOZ_ASSERT(!NS_IsMainThread());
#define COPY_FIELD(prop) mResult.prop = aResult.prop;
COPY_FIELD(mId)
COPY_FIELD(mRet)
COPY_FIELD(mBroadcast)
COPY_FIELD(mTopic)
COPY_FIELD(mReason)
COPY_FIELD(mResultCode)
COPY_FIELD(mResultReason)
COPY_FIELD(mError)
COPY_FIELD(mEnable)
COPY_FIELD(mResult)
COPY_FIELD(mSuccess)
COPY_FIELD(mCurExternalIfname)
COPY_FIELD(mCurInternalIfname)
#undef COPY_FIELD
}
NS_IMETHOD Run()

View File

@ -101,10 +101,65 @@ interface nsIUpdateUpStreamCallback : nsISupports
void updateUpStreamResult(in boolean success, in DOMString externalIfname);
};
[scriptable, function, uuid(eedca6c0-1310-11e4-9191-0800200c9a66)]
interface nsISetDnsCallback : nsISupports
{
/**
* Callback function used to report the result of setting DNS server.
*
* @param error
* An error message if the operation wasn't successful,
* or `null` if it was.
*/
void setDnsResult(in jsval error);
};
[scriptable, function, uuid(5d0e1a60-1187-11e4-9191-0800200c9a66)]
interface nsINativeCommandCallback : nsISupports
{
/**
* Callback function used to report the result of a network native command.
*
* @param success
* Boolean to indicate the operation is successful or not.
*/
void nativeCommandResult(in boolean success);
};
[scriptable, function, uuid(694abb80-1187-11e4-9191-0800200c9a66)]
interface nsIDhcpRequestCallback : nsISupports
{
/**
* Callback function used to report the result of DHCP client request.
*
* @param success
* Boolean to indicate the operation is successful or not.
*
* @param dhcpInfo
* An object to represent the successful DHCP request:
*
* - gateway_str: string
* - dns1_str: string
* - dns2_str: string
* - mask_str: string
* - server_str: string
* - vendor_str: string
* - lease: long
* - mask: long
* - ipaddr: long
* - gateway: long
* - dns1: long
* - dns2: long
* - server: long
*/
void dhcpRequestResult(in boolean success, in jsval dhcpInfo);
};
/**
* Provide network services.
*/
[scriptable, uuid(ddb38428-0cf2-4c6a-a3c9-5e2f00fc54db)]
[scriptable, uuid(9f1d78e0-1314-11e4-9191-0800200c9a66)]
interface nsINetworkService : nsISupports
{
/**
@ -229,19 +284,26 @@ interface nsINetworkService : nsISupports
*
* @param networkInterface
* The network interface which contains the DNS we want to set.
*
* @param callback
* Callback to notify the result of setting DNS server.
*/
void setDNS(in nsINetworkInterface networkInterface);
void setDNS(in nsINetworkInterface networkInterface,
in nsISetDnsCallback callback);
/**
* Set default route and DNS.
* Set default route.
*
* @param networkInterface
* The network interface we want to set to the default route and dns.
* The network interface we want to set to the default route.
* @param oldInterface
* The previous network interface.
* @param callback
* Callback to notify the result of setting default route.
*/
void setDefaultRouteAndDNS(in nsINetworkInterface networkInterface,
in nsINetworkInterface oldInterface);
void setDefaultRoute(in nsINetworkInterface networkInterface,
in nsINetworkInterface oldInterface,
in nsINativeCommandCallback callback);
/**
* Remove default route.
@ -341,4 +403,71 @@ interface nsINetworkService : nsISupports
void updateUpStream(in jsval previous,
in jsval current,
in nsIUpdateUpStreamCallback callback);
/**
* Configure a network interface.
*
* @param config
* An object containing the detail that we want to configure the interface:
*
* - ifname: string
* - ipaddr: long
* - mask: long
* - gateway: long
* - dns1: long
* - dns2: long
*
* @param callback
* Callback to notify the result of configurating network interface.
*/
void configureInterface(in jsval config,
in nsINativeCommandCallback callback);
/**
* Issue a DHCP client request.
*
* @param networkInterface
* The network interface which we wnat to do the DHCP request on.
*
* @param callback
* Callback to notify the result of the DHCP request.
*/
void dhcpRequest(in DOMString interfaceName,
in nsIDhcpRequestCallback callback);
/**
* Enable a network interface.
*
* @param networkInterface
* The network interface name which we want to enable.
*
* @param callback
* Callback to notify the result of disabling network interface.
*/
void enableInterface(in DOMString interfaceName,
in nsINativeCommandCallback callback);
/**
* Disable a network interface.
*
* @param networkInterface
* The network interface name which we want to disable.
*
* @param callback
* Callback to notify the result of disabling network interface.
*/
void disableInterface(in DOMString interfaceName,
in nsINativeCommandCallback callback);
/**
* Reset all connections
*
* @param networkInterface
* The network interface name which we want to reset.
*
* @param callback
* Callback to notify the result of resetting connections.
*/
void resetConnections(in DOMString interfaceName,
in nsINativeCommandCallback callback);
};

View File

@ -22,7 +22,6 @@ dictionary NetworkCommandOptions
sequence<DOMString> gateways; // for "setDefaultRouteAndDNS", "removeDefaultRoute".
DOMString mode; // for "setWifiOperationMode".
boolean report; // for "setWifiOperationMode".
boolean isAsync; // for "setWifiOperationMode".
boolean enabled; // for "setDhcpServer".
DOMString wifictrlinterfacename; // for "setWifiTethering".
DOMString internalIfname; // for "setWifiTethering".
@ -50,6 +49,12 @@ dictionary NetworkCommandOptions
DOMString preExternalIfname; // for "updateUpStream".
DOMString curInternalIfname; // for "updateUpStream".
DOMString curExternalIfname; // for "updateUpStream".
long ipaddr; // for "ifc_configure".
long mask; // for "ifc_configure".
long gateway_long; // for "ifc_configure".
long dns1_long; // for "ifc_configure".
long dns2_long; // for "ifc_configure".
};
/**
@ -73,4 +78,22 @@ dictionary NetworkResultOptions
boolean success = false; // for "setDhcpServer".
DOMString curExternalIfname = ""; // for "updateUpStream".
DOMString curInternalIfname = ""; // for "updateUpStream".
DOMString reply = ""; // for "command".
DOMString route = ""; // for "ifc_get_default_route".
DOMString ipaddr_str = ""; // The following are for the result of
// dhcp_do_request.
DOMString gateway_str = "";
DOMString dns1_str = "";
DOMString dns2_str = "";
DOMString mask_str = "";
DOMString server_str = "";
DOMString vendor_str = "";
long lease = 0;
long mask = 0;
long ipaddr = 0;
long gateway = 0;
long dns1 = 0;
long dns2 = 0;
long server = 0;
};

View File

@ -10,22 +10,6 @@ dictionary WifiCommandOptions
long id = 0; // opaque id.
DOMString cmd = ""; // the command name.
DOMString request; // for "command"
DOMString ifname; // for "ifc_reset_connections", "ifc_enable",
// "ifc_disable", "ifc_remove_host_routes",
// "ifc_remove_default_route", "dhcp_stop",
// "dhcp_release_lease", "ifc_get_default_route",
// "ifc_add_host_route", "ifc_set_default_route",
// "ifc_configure", "dhcp_do_request",
// "dhcp_do_request_renew".
long route; // for "ifc_add_host_route", "ifc_set_default_route".
long ipaddr; // for "ifc_configure".
long mask; // for "ifc_configure".
long gateway; // for "ifc_configure".
long dns1; // for "ifc_configure".
long dns2; // for "ifc_configure".
DOMString key; // for "property_get", "property_set".
DOMString value; // for "property_set".
DOMString defaultValue; // for "property_get".
};
/**
@ -37,24 +21,6 @@ dictionary WifiResultOptions
long status = 0; // the return status of the command.
// Used by most commands.
DOMString reply = ""; // for "command".
DOMString route = ""; // for "ifc_get_default_route".
DOMString error = ""; // for "dhcp_get_errmsg".
DOMString value = ""; // for "property_get".
DOMString ipaddr_str = ""; // The following are for the result of
// dhcp_do_request.
DOMString gateway_str = "";
DOMString dns1_str = "";
DOMString dns2_str = "";
DOMString mask_str = "";
DOMString server_str = "";
DOMString vendor_str = "";
long lease = 0;
long mask = 0;
long ipaddr = 0;
long gateway = 0;
long dns1 = 0;
long dns2 = 0;
long server = 0;
};

View File

@ -30,23 +30,8 @@ this.WifiNetUtil = function(controlMessage) {
var util = {};
util.configureInterface = function(cfg, callback) {
let message = { cmd: "ifc_configure",
ifname: cfg.ifname,
ipaddr: cfg.ipaddr,
mask: cfg.mask,
gateway: cfg.gateway,
dns1: cfg.dns1,
dns2: cfg.dns2 };
controlMessage(message, function(data) {
callback(!data.status);
});
};
util.runDhcp = function (ifname, callback) {
controlMessage({ cmd: "dhcp_do_request", ifname: ifname }, function(data) {
var dhcpInfo = data.status ? null : data;
gNetworkService.dhcpRequest(ifname, function(success, dhcpInfo) {
util.runIpConfig(ifname, dhcpInfo, callback);
});
};
@ -62,18 +47,6 @@ this.WifiNetUtil = function(controlMessage) {
stopProcess(dhcpService, processName, callback);
};
util.enableInterface = function (ifname, callback) {
controlMessage({ cmd: "ifc_enable", ifname: ifname }, function (data) {
callback(!data.status);
});
};
util.disableInterface = function (ifname, callback) {
controlMessage({ cmd: "ifc_disable", ifname: ifname }, function (data) {
callback(!data.status);
});
};
util.startDhcpServer = function (config, callback) {
gNetworkService.setDhcpServer(true, config, function (error) {
callback(!error);
@ -86,60 +59,6 @@ this.WifiNetUtil = function(controlMessage) {
});
};
util.addHostRoute = function (ifname, route, callback) {
controlMessage({ cmd: "ifc_add_host_route", ifname: ifname, route: route }, function(data) {
callback(!data.status);
});
};
util.removeHostRoutes = function (ifname, callback) {
controlMessage({ cmd: "ifc_remove_host_routes", ifname: ifname }, function(data) {
callback(!data.status);
});
};
util.setDefaultRoute = function (ifname, route, callback) {
controlMessage({ cmd: "ifc_set_default_route", ifname: ifname, route: route }, function(data) {
callback(!data.status);
});
};
util.getDefaultRoute = function (ifname, callback) {
controlMessage({ cmd: "ifc_get_default_route", ifname: ifname }, function(data) {
callback(!data.route);
});
};
util.removeDefaultRoute = function (ifname, callback) {
controlMessage({ cmd: "ifc_remove_default_route", ifname: ifname }, function(data) {
callback(!data.status);
});
};
util.resetConnections = function (ifname, callback) {
controlMessage({ cmd: "ifc_reset_connections", ifname: ifname }, function(data) {
callback(!data.status);
});
};
util.releaseDhcpLease = function (ifname, callback) {
controlMessage({ cmd: "dhcp_release_lease", ifname: ifname }, function(data) {
callback(!data.status);
});
};
util.getDhcpError = function (callback) {
controlMessage({ cmd: "dhcp_get_errmsg" }, function(data) {
callback(data.error);
});
};
util.runDhcpRenew = function (ifname, callback) {
controlMessage({ cmd: "dhcp_do_request", ifname: ifname }, function(data) {
callback(data.status ? null : data);
});
};
util.runIpConfig = function (name, data, callback) {
if (!data) {
debug("IP config failed to run");

View File

@ -21,6 +21,10 @@ XPCOMUtils.defineLazyServiceGetter(this, "gNetworkManager",
"@mozilla.org/network/manager;1",
"nsINetworkManager");
XPCOMUtils.defineLazyServiceGetter(this, "gNetworkService",
"@mozilla.org/network/service;1",
"nsINetworkService");
this.EXPORTED_SYMBOLS = ["WifiP2pManager"];
const EVENT_IGNORED = -1;
@ -647,7 +651,7 @@ function P2pStateMachine(aP2pCommand, aNetUtil) {
// Step 4: Enable p2p0 net interface. wpa_supplicant may have
// already done it for us.
aNetUtil.enableInterface(P2P_INTERFACE_NAME, function (success) {
gNetworkService.enableInterface(P2P_INTERFACE_NAME, function (success) {
onSuccess();
});
});
@ -1318,7 +1322,7 @@ function P2pStateMachine(aP2pCommand, aNetUtil) {
debug('P2P function disabled');
aP2pCommand.closeSupplicantConnection(function (status) {
debug('Supplicant connection closed');
aNetUtil.disableInterface(P2P_INTERFACE_NAME, function (success){
gNetworkService.disableInterface(P2P_INTERFACE_NAME, function (success){
debug('Disabled interface: ' + P2P_INTERFACE_NAME);
_onDisabled(true);
_sm.gotoState(stateDisabled);

View File

@ -89,36 +89,10 @@ class WifiResultDispatcher : public nsRunnable
{
public:
WifiResultDispatcher(WifiResultOptions& aResult, const nsACString& aInterface)
: mInterface(aInterface)
: mResult(aResult)
, mInterface(aInterface)
{
MOZ_ASSERT(!NS_IsMainThread());
// XXX: is there a better way to copy webidl dictionnaries?
// the copy constructor is private.
#define COPY_FIELD(prop) mResult.prop = aResult.prop;
COPY_FIELD(mId)
COPY_FIELD(mStatus)
COPY_FIELD(mReply)
COPY_FIELD(mRoute)
COPY_FIELD(mError)
COPY_FIELD(mValue)
COPY_FIELD(mIpaddr_str)
COPY_FIELD(mGateway_str)
COPY_FIELD(mDns1_str)
COPY_FIELD(mDns2_str)
COPY_FIELD(mMask_str)
COPY_FIELD(mServer_str)
COPY_FIELD(mVendor_str)
COPY_FIELD(mLease)
COPY_FIELD(mMask)
COPY_FIELD(mIpaddr)
COPY_FIELD(mGateway)
COPY_FIELD(mDns1)
COPY_FIELD(mDns2)
COPY_FIELD(mServer)
#undef COPY_FIELD
}
NS_IMETHOD Run()

View File

@ -8,7 +8,6 @@
#include <cutils/properties.h>
#include "prinit.h"
#include "js/CharacterEncoding.h"
#include "mozilla/dom/network/NetUtils.h"
using namespace mozilla::dom;
@ -380,14 +379,17 @@ public:
// Concrete class to use to access the wpa supplicant.
WpaSupplicant::WpaSupplicant()
{
if (NetUtils::SdkVersion() < 16) {
char propVersion[PROPERTY_VALUE_MAX];
property_get("ro.build.version.sdk", propVersion, "0");
mSdkVersion = strtol(propVersion, nullptr, 10);
if (mSdkVersion < 16) {
mImpl = new ICSWpaSupplicantImpl();
} else if (NetUtils::SdkVersion() < 19) {
} else if (mSdkVersion < 19) {
mImpl = new JBWpaSupplicantImpl();
} else {
mImpl = new KKWpaSupplicantImpl();
}
mNetUtils = new NetUtils();
mWifiHotspotUtils = new WifiHotspotUtils();
};
@ -418,9 +420,6 @@ bool WpaSupplicant::ExecuteCommand(CommandOptions aOptions,
const nsCString& aInterface)
{
CHECK_HWLIB(false)
if (!mNetUtils->GetSharedLibrary()) {
return false;
}
if (!mWifiHotspotUtils->GetSharedLibrary()) {
return false;
@ -455,81 +454,6 @@ bool WpaSupplicant::ExecuteCommand(CommandOptions aOptions,
aResult.mStatus = mImpl->do_wifi_stop_supplicant(0);
} else if (aOptions.mCmd.EqualsLiteral("connect_to_supplicant")) {
aResult.mStatus = mImpl->do_wifi_connect_to_supplicant(aInterface.get());
} else if (aOptions.mCmd.EqualsLiteral("ifc_enable")) {
aResult.mStatus = mNetUtils->do_ifc_enable(GET_CHAR(mIfname));
} else if (aOptions.mCmd.EqualsLiteral("ifc_disable")) {
aResult.mStatus = mNetUtils->do_ifc_disable(GET_CHAR(mIfname));
} else if (aOptions.mCmd.EqualsLiteral("ifc_configure")) {
aResult.mStatus = mNetUtils->do_ifc_configure(
GET_CHAR(mIfname), aOptions.mIpaddr, aOptions.mMask,
aOptions.mGateway, aOptions.mDns1, aOptions.mDns2
);
} else if (aOptions.mCmd.EqualsLiteral("ifc_reset_connections")) {
aResult.mStatus = mNetUtils->do_ifc_reset_connections(
GET_CHAR(mIfname), RESET_ALL_ADDRESSES
);
} else if (aOptions.mCmd.EqualsLiteral("dhcp_stop")) {
aResult.mStatus = mNetUtils->do_dhcp_stop(GET_CHAR(mIfname));
} else if (aOptions.mCmd.EqualsLiteral("dhcp_do_request")) {
char ipaddr[PROPERTY_VALUE_MAX];
char gateway[PROPERTY_VALUE_MAX];
uint32_t prefixLength;
char dns1[PROPERTY_VALUE_MAX];
char dns2[PROPERTY_VALUE_MAX];
char server[PROPERTY_VALUE_MAX];
uint32_t lease;
char vendorinfo[PROPERTY_VALUE_MAX];
aResult.mStatus =
mNetUtils->do_dhcp_do_request(GET_CHAR(mIfname),
ipaddr,
gateway,
&prefixLength,
dns1,
dns2,
server,
&lease,
vendorinfo);
if (aResult.mStatus == -1) {
// Early return since we failed.
return true;
}
aResult.mIpaddr_str = NS_ConvertUTF8toUTF16(ipaddr);
aResult.mGateway_str = NS_ConvertUTF8toUTF16(gateway);
aResult.mDns1_str = NS_ConvertUTF8toUTF16(dns1);
aResult.mDns2_str = NS_ConvertUTF8toUTF16(dns2);
aResult.mServer_str = NS_ConvertUTF8toUTF16(server);
aResult.mVendor_str = NS_ConvertUTF8toUTF16(vendorinfo);
aResult.mLease = lease;
aResult.mMask = MakeMask(prefixLength);
uint32_t inet4; // only support IPv4 for now.
#define INET_PTON(var, field) \
PR_BEGIN_MACRO \
inet_pton(AF_INET, var, &inet4); \
aResult.field = inet4; \
PR_END_MACRO
INET_PTON(ipaddr, mIpaddr);
INET_PTON(gateway, mGateway);
if (dns1[0] != '\0') {
INET_PTON(dns1, mDns1);
}
if (dns2[0] != '\0') {
INET_PTON(dns2, mDns2);
}
INET_PTON(server, mServer);
//aResult.mask_str = netHelpers.ipToString(obj.mask);
char inet_str[64];
if (inet_ntop(AF_INET, &aResult.mMask, inet_str, sizeof(inet_str))) {
aResult.mMask_str = NS_ConvertUTF8toUTF16(inet_str);
}
} else if (aOptions.mCmd.EqualsLiteral("hostapd_command")) {
size_t len = BUFFER_SIZE - 1;
char buffer[BUFFER_SIZE];
@ -595,7 +519,7 @@ WpaSupplicant::CheckBuffer(char* buffer, int32_t length,
return;
}
if (NetUtils::SdkVersion() < 18) {
if (mSdkVersion < 18) {
buffer[length] = 0;
LossyConvertUTF8toUTF16(buffer, length, aEvent);
return;

View File

@ -13,29 +13,12 @@
#include "nsString.h"
#include "nsAutoPtr.h"
#include "mozilla/dom/WifiOptionsBinding.h"
#include "mozilla/dom/network/NetUtils.h"
#include "WifiHotspotUtils.h"
// Needed to add a copy constructor to WifiCommandOptions.
struct CommandOptions
{
public:
CommandOptions(const CommandOptions& aOther) {
mId = aOther.mId;
mCmd = aOther.mCmd;
mRequest = aOther.mRequest;
mIfname = aOther.mIfname;
mRoute = aOther.mRoute;
mIpaddr = aOther.mIpaddr;
mMask = aOther.mMask;
mGateway = aOther.mGateway;
mDns1 = aOther.mDns1;
mDns2 = aOther.mDns2;
mKey = aOther.mKey;
mValue = aOther.mValue;
mDefaultValue = aOther.mDefaultValue;
}
CommandOptions(const mozilla::dom::WifiCommandOptions& aOther) {
#define COPY_OPT_FIELD(prop, defaultValue) \
@ -49,16 +32,6 @@ public:
COPY_FIELD(mId)
COPY_FIELD(mCmd)
COPY_OPT_FIELD(mRequest, EmptyString())
COPY_OPT_FIELD(mIfname, EmptyString())
COPY_OPT_FIELD(mIpaddr, 0)
COPY_OPT_FIELD(mRoute, 0)
COPY_OPT_FIELD(mMask, 0)
COPY_OPT_FIELD(mGateway, 0)
COPY_OPT_FIELD(mDns1, 0)
COPY_OPT_FIELD(mDns2, 0)
COPY_OPT_FIELD(mKey, EmptyString())
COPY_OPT_FIELD(mValue, EmptyString())
COPY_OPT_FIELD(mDefaultValue, EmptyString())
#undef COPY_OPT_FIELD
#undef COPY_FIELD
@ -66,18 +39,8 @@ public:
// All the fields, not Optional<> anymore to get copy constructors.
nsString mCmd;
nsString mDefaultValue;
int32_t mDns1;
int32_t mDns2;
int32_t mGateway;
int32_t mId;
nsString mIfname;
int32_t mIpaddr;
nsString mKey;
int32_t mMask;
nsString mRequest;
int32_t mRoute;
nsString mValue;
};
// Abstract class that exposes libhardware_legacy functions we need for
@ -130,9 +93,10 @@ public:
private:
nsAutoPtr<WpaSupplicantImpl> mImpl;
nsAutoPtr<NetUtils> mNetUtils;
nsAutoPtr<WifiHotspotUtils> mWifiHotspotUtils;
uint32_t mSdkVersion;
protected:
void CheckBuffer(char* buffer, int32_t length, nsAString& aEvent);
uint32_t MakeMask(uint32_t len);

View File

@ -415,8 +415,8 @@ var WifiManager = (function() {
// and routing table is changed but still cannot connect to network
// so the workaround here is disable interface the enable again to
// trigger network reconnect with static ip.
netUtil.disableInterface(manager.ifname, function (ok) {
netUtil.enableInterface(manager.ifname, function (ok) {
gNetworkService.disableInterface(manager.ifname, function (ok) {
gNetworkService.enableInterface(manager.ifname, function (ok) {
});
});
}
@ -442,12 +442,12 @@ var WifiManager = (function() {
}
// Set ip, mask length, gateway, dns to network interface
netUtil.configureInterface( { ifname: ifname,
ipaddr: staticIpInfo.ipaddr,
mask: staticIpInfo.maskLength,
gateway: staticIpInfo.gateway,
dns1: staticIpInfo.dns1,
dns2: staticIpInfo.dns2 }, function (data) {
gNetworkService.configureInterface( { ifname: ifname,
ipaddr: staticIpInfo.ipaddr,
mask: staticIpInfo.maskLength,
gateway: staticIpInfo.gateway,
dns1: staticIpInfo.dns1,
dns2: staticIpInfo.dns2 }, function (data) {
netUtil.runIpConfig(ifname, staticIpInfo, function(data) {
dhcpInfo = data.info;
notify("networkconnected", data);
@ -587,17 +587,17 @@ var WifiManager = (function() {
manager.connectionDropped = function(callback) {
// Reset network interface when connection drop
netUtil.configureInterface( { ifname: manager.ifname,
ipaddr: 0,
mask: 0,
gateway: 0,
dns1: 0,
dns2: 0 }, function (data) {
gNetworkService.configureInterface( { ifname: manager.ifname,
ipaddr: 0,
mask: 0,
gateway: 0,
dns1: 0,
dns2: 0 }, function (data) {
});
// If we got disconnected, kill the DHCP client in preparation for
// reconnection.
netUtil.resetConnections(manager.ifname, function() {
gNetworkService.resetConnections(manager.ifname, function() {
netUtil.stopDhcp(manager.ifname, function() {
callback();
});
@ -889,7 +889,7 @@ var WifiManager = (function() {
}
suppressEvents = true;
wifiCommand.killSupplicant(function() {
netUtil.disableInterface(manager.ifname, function (ok) {
gNetworkService.disableInterface(manager.ifname, function (ok) {
suppressEvents = false;
callback();
});
@ -996,7 +996,7 @@ var WifiManager = (function() {
}
manager.supplicantStarted = true;
netUtil.enableInterface(manager.ifname, function (ok) {
gNetworkService.enableInterface(manager.ifname, function (ok) {
callback(ok ? 0 : -1);
});
});
@ -1040,7 +1040,7 @@ var WifiManager = (function() {
wifiCommand.closeSupplicantConnection(function() {
manager.connectToSupplicant = false;
manager.state = "UNINITIALIZED";
netUtil.disableInterface(manager.ifname, function (ok) {
gNetworkService.disableInterface(manager.ifname, function (ok) {
unloadDriver(WIFI_FIRMWARE_STATION, callback);
});
});