Bug 1043114 - Part 4: Unify add/removeHostRoute and add/removeHostRouteWithResolve (NetworkUtils). r=edgar

This commit is contained in:
Jessica Jong 2014-08-08 02:32:00 -04:00
Родитель c4f7d3bacb
Коммит 339bc7a307
2 изменённых файлов: 100 добавлений и 118 удалений

Просмотреть файл

@ -20,6 +20,8 @@
#include <limits>
#include "mozilla/dom/network/NetUtils.h"
#include <errno.h>
#include <string.h>
#include <sys/types.h> // struct addrinfo
#include <sys/socket.h> // getaddrinfo(), freeaddrinfo()
#include <netdb.h>
@ -76,6 +78,8 @@ static const char* NETD_MESSAGE_DELIMIT = " ";
static const uint32_t BUF_SIZE = 1024;
static const int32_t SUCCESS = 0;
static uint32_t SDK_VERSION;
struct IFProperties {
@ -349,24 +353,6 @@ static int getIpType(const char *aIp) {
return type;
}
/**
* Helper function to find the best match gateway. For now, return
* the gateway that matches the address family passed.
*/
static uint32_t selectGateway(nsTArray<nsString>& gateways, int addrFamily)
{
uint32_t length = gateways.Length();
for (uint32_t i = 0; i < length; i++) {
NS_ConvertUTF16toUTF8 autoGateway(gateways[i]);
if ((getIpType(autoGateway.get()) == AF_INET && addrFamily == AF_INET) ||
(getIpType(autoGateway.get()) == AF_INET6 && addrFamily == AF_INET6)) {
return i;
}
}
return length; // invalid index.
}
static void postMessage(NetworkResultOptions& aResult)
{
MOZ_ASSERT(gNetworkUtils);
@ -1046,7 +1032,7 @@ NetworkUtils::~NetworkUtils()
void NetworkUtils::ExecuteCommand(NetworkParams aOptions)
{
typedef bool (NetworkUtils::*CommandHandler)(NetworkParams&);
typedef int32_t (NetworkUtils::*CommandHandler)(NetworkParams&);
const static struct {
const char* mCommandName;
@ -1095,13 +1081,22 @@ void NetworkUtils::ExecuteCommand(NetworkParams aOptions)
}
// Command matches! Dispatch to the handler.
(this->*handler)(aOptions);
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);
}
@ -1188,7 +1183,7 @@ void NetworkUtils::onNetdMessage(NetdCommand* aCommand)
/**
* Start/Stop DHCP server.
*/
bool NetworkUtils::setDhcpServer(NetworkParams& aOptions)
int32_t NetworkUtils::setDhcpServer(NetworkParams& aOptions)
{
if (aOptions.mEnabled) {
aOptions.mWifiStartIp = aOptions.mStartIp;
@ -1201,13 +1196,13 @@ bool NetworkUtils::setDhcpServer(NetworkParams& aOptions)
} else {
RUN_CHAIN(aOptions, sStopDhcpServerChain, setDhcpServerFail)
}
return true;
return SUCCESS;
}
/**
* Set DNS servers for given network interface.
*/
bool NetworkUtils::setDNS(NetworkParams& aOptions)
int32_t NetworkUtils::setDNS(NetworkParams& aOptions)
{
uint32_t length = aOptions.mDnses.Length();
@ -1241,13 +1236,13 @@ bool NetworkUtils::setDNS(NetworkParams& aOptions)
RUN_CHAIN(aOptions, sSetDnsChain, setDnsFail)
}
return true;
return SUCCESS;
}
/**
* Set default route and DNS servers for given network interface.
*/
bool NetworkUtils::setDefaultRouteAndDNS(NetworkParams& aOptions)
int32_t NetworkUtils::setDefaultRouteAndDNS(NetworkParams& aOptions)
{
NS_ConvertUTF16toUTF8 autoIfname(aOptions.mIfname);
@ -1284,7 +1279,7 @@ bool NetworkUtils::setDefaultRouteAndDNS(NetworkParams& aOptions)
int type = getIpType(gateway);
if (type != AF_INET && type != AF_INET6) {
return false;
return EAFNOSUPPORT;
}
if (type == AF_INET6) {
@ -1295,13 +1290,13 @@ bool NetworkUtils::setDefaultRouteAndDNS(NetworkParams& aOptions)
}
setDNS(aOptions);
return true;
return SUCCESS;
}
/**
* Remove default route for given network interface.
*/
bool NetworkUtils::removeDefaultRoute(NetworkParams& aOptions)
int32_t NetworkUtils::removeDefaultRoute(NetworkParams& aOptions)
{
uint32_t length = aOptions.mGateways.Length();
for (uint32_t i = 0; i < length; i++) {
@ -1309,7 +1304,7 @@ bool NetworkUtils::removeDefaultRoute(NetworkParams& aOptions)
int type = getIpType(autoGateway.get());
if (type != AF_INET && type != AF_INET6) {
return false;
return EAFNOSUPPORT;
}
mNetUtils->do_ifc_remove_route(GET_CHAR(mIfname),
@ -1317,86 +1312,73 @@ bool NetworkUtils::removeDefaultRoute(NetworkParams& aOptions)
0, autoGateway.get());
}
return true;
return SUCCESS;
}
/**
* Add host route for given network interface.
*/
bool NetworkUtils::addHostRoute(NetworkParams& aOptions)
int32_t NetworkUtils::addHostRoute(NetworkParams& aOptions)
{
NS_ConvertUTF16toUTF8 autoIfname(aOptions.mIfname);
NS_ConvertUTF16toUTF8 autoHostname(aOptions.mIp);
NS_ConvertUTF16toUTF8 autoGateway(aOptions.mGateway);
int type, prefix;
uint32_t length = aOptions.mHostnames.Length();
for (uint32_t i = 0; i < length; i++) {
NS_ConvertUTF16toUTF8 autoHostname(aOptions.mHostnames[i]);
type = getIpType(autoHostname.get());
if (type != AF_INET && type != AF_INET6) {
continue;
}
uint32_t index = selectGateway(aOptions.mGateways, type);
if (index >= aOptions.mGateways.Length()) {
continue;
}
NS_ConvertUTF16toUTF8 autoGateway(aOptions.mGateways[index]);
prefix = type == AF_INET ? 32 : 128;
mNetUtils->do_ifc_add_route(autoIfname.get(), autoHostname.get(), prefix,
autoGateway.get());
type = getIpType(autoHostname.get());
if (type != AF_INET && type != AF_INET6) {
return EAFNOSUPPORT;
}
return true;
if (type != getIpType(autoGateway.get())) {
return EINVAL;
}
prefix = type == AF_INET ? 32 : 128;
return mNetUtils->do_ifc_add_route(autoIfname.get(), autoHostname.get(),
prefix, autoGateway.get());
}
/**
* Remove host route for given network interface.
*/
bool NetworkUtils::removeHostRoute(NetworkParams& aOptions)
int32_t NetworkUtils::removeHostRoute(NetworkParams& aOptions)
{
NS_ConvertUTF16toUTF8 autoIfname(aOptions.mIfname);
NS_ConvertUTF16toUTF8 autoHostname(aOptions.mIp);
NS_ConvertUTF16toUTF8 autoGateway(aOptions.mGateway);
int type, prefix;
uint32_t length = aOptions.mHostnames.Length();
for (uint32_t i = 0; i < length; i++) {
NS_ConvertUTF16toUTF8 autoHostname(aOptions.mHostnames[i]);
type = getIpType(autoHostname.get());
if (type != AF_INET && type != AF_INET6) {
continue;
}
uint32_t index = selectGateway(aOptions.mGateways, type);
if (index >= aOptions.mGateways.Length()) {
continue;
}
NS_ConvertUTF16toUTF8 autoGateway(aOptions.mGateways[index]);
prefix = type == AF_INET ? 32 : 128;
mNetUtils->do_ifc_remove_route(autoIfname.get(), autoHostname.get(), prefix,
autoGateway.get());
type = getIpType(autoHostname.get());
if (type != AF_INET && type != AF_INET6) {
return EAFNOSUPPORT;
}
return true;
if (type != getIpType(autoGateway.get())) {
return EINVAL;
}
prefix = type == AF_INET ? 32 : 128;
return mNetUtils->do_ifc_remove_route(autoIfname.get(), autoHostname.get(),
prefix, autoGateway.get());
}
/**
* Remove the routes associated with the named interface.
*/
bool NetworkUtils::removeHostRoutes(NetworkParams& aOptions)
int32_t NetworkUtils::removeHostRoutes(NetworkParams& aOptions)
{
mNetUtils->do_ifc_remove_host_routes(GET_CHAR(mIfname));
return true;
return mNetUtils->do_ifc_remove_host_routes(GET_CHAR(mIfname));
}
bool NetworkUtils::removeNetworkRoute(NetworkParams& aOptions)
int32_t NetworkUtils::removeNetworkRoute(NetworkParams& aOptions)
{
NS_ConvertUTF16toUTF8 autoIfname(aOptions.mIfname);
NS_ConvertUTF16toUTF8 autoIp(aOptions.mIp);
int type = getIpType(autoIp.get());
if (type != AF_INET && type != AF_INET6) {
return false;
return EAFNOSUPPORT;
}
uint32_t prefixLength = GET_FIELD(mPrefixLength);
@ -1405,7 +1387,7 @@ bool NetworkUtils::removeNetworkRoute(NetworkParams& aOptions)
// Calculate subnet.
struct in6_addr in6;
if (inet_pton(AF_INET6, autoIp.get(), &in6) != 1) {
return false;
return EINVAL;
}
uint32_t p, i, p1, mask;
@ -1420,7 +1402,7 @@ bool NetworkUtils::removeNetworkRoute(NetworkParams& aOptions)
char subnetStr[INET6_ADDRSTRLEN];
if (!inet_ntop(AF_INET6, &in6, subnetStr, sizeof subnetStr)) {
return false;
return EINVAL;
}
// Remove default route.
@ -1428,7 +1410,7 @@ bool NetworkUtils::removeNetworkRoute(NetworkParams& aOptions)
// Remove subnet route.
mNetUtils->do_ifc_remove_route(autoIfname.get(), subnetStr, prefixLength, NULL);
return true;
return SUCCESS;
}
/* type == AF_INET */
@ -1442,10 +1424,10 @@ bool NetworkUtils::removeNetworkRoute(NetworkParams& aOptions)
mNetUtils->do_ifc_remove_default_route(autoIfname.get());
mNetUtils->do_ifc_remove_route(autoIfname.get(), dst, prefixLength, gateway);
return true;
return SUCCESS;
}
bool NetworkUtils::addSecondaryRoute(NetworkParams& aOptions)
int32_t NetworkUtils::addSecondaryRoute(NetworkParams& aOptions)
{
char command[MAX_COMMAND_SIZE];
snprintf(command, MAX_COMMAND_SIZE - 1,
@ -1456,10 +1438,10 @@ bool NetworkUtils::addSecondaryRoute(NetworkParams& aOptions)
GET_CHAR(mGateway));
doCommand(command, nullptr, nullptr);
return true;
return SUCCESS;
}
bool NetworkUtils::removeSecondaryRoute(NetworkParams& aOptions)
int32_t NetworkUtils::removeSecondaryRoute(NetworkParams& aOptions)
{
char command[MAX_COMMAND_SIZE];
snprintf(command, MAX_COMMAND_SIZE - 1,
@ -1470,44 +1452,44 @@ bool NetworkUtils::removeSecondaryRoute(NetworkParams& aOptions)
GET_CHAR(mGateway));
doCommand(command, nullptr, nullptr);
return true;
return SUCCESS;
}
bool NetworkUtils::setNetworkInterfaceAlarm(NetworkParams& aOptions)
int32_t NetworkUtils::setNetworkInterfaceAlarm(NetworkParams& aOptions)
{
DEBUG("setNetworkInterfaceAlarms: %s", GET_CHAR(mIfname));
RUN_CHAIN(aOptions, sNetworkInterfaceSetAlarmChain, networkInterfaceAlarmFail);
return true;
return SUCCESS;
}
bool NetworkUtils::enableNetworkInterfaceAlarm(NetworkParams& aOptions)
int32_t NetworkUtils::enableNetworkInterfaceAlarm(NetworkParams& aOptions)
{
DEBUG("enableNetworkInterfaceAlarm: %s", GET_CHAR(mIfname));
RUN_CHAIN(aOptions, sNetworkInterfaceEnableAlarmChain, networkInterfaceAlarmFail);
return true;
return SUCCESS;
}
bool NetworkUtils::disableNetworkInterfaceAlarm(NetworkParams& aOptions)
int32_t NetworkUtils::disableNetworkInterfaceAlarm(NetworkParams& aOptions)
{
DEBUG("disableNetworkInterfaceAlarms: %s", GET_CHAR(mIfname));
RUN_CHAIN(aOptions, sNetworkInterfaceDisableAlarmChain, networkInterfaceAlarmFail);
return true;
return SUCCESS;
}
/**
* handling main thread's reload Wifi firmware request
*/
bool NetworkUtils::setWifiOperationMode(NetworkParams& aOptions)
int32_t NetworkUtils::setWifiOperationMode(NetworkParams& aOptions)
{
DEBUG("setWifiOperationMode: %s %s", GET_CHAR(mIfname), GET_CHAR(mMode));
RUN_CHAIN(aOptions, sWifiOperationModeChain, wifiOperationModeFail);
return true;
return SUCCESS;
}
/**
* handling main thread's enable/disable WiFi Tethering request
*/
bool NetworkUtils::setWifiTethering(NetworkParams& aOptions)
int32_t NetworkUtils::setWifiTethering(NetworkParams& aOptions)
{
bool enable = aOptions.mEnable;
IFProperties interfaceProperties;
@ -1536,10 +1518,10 @@ bool NetworkUtils::setWifiTethering(NetworkParams& aOptions)
GET_CHAR(mInternalIfname), GET_CHAR(mExternalIfname));
RUN_CHAIN(aOptions, sWifiDisableChain, wifiTetheringFail)
}
return true;
return SUCCESS;
}
bool NetworkUtils::setUSBTethering(NetworkParams& aOptions)
int32_t NetworkUtils::setUSBTethering(NetworkParams& aOptions)
{
bool enable = aOptions.mEnable;
IFProperties interfaceProperties;
@ -1568,7 +1550,7 @@ bool NetworkUtils::setUSBTethering(NetworkParams& aOptions)
GET_CHAR(mInternalIfname), GET_CHAR(mExternalIfname));
RUN_CHAIN(aOptions, sUSBDisableChain, usbTetheringFail)
}
return true;
return SUCCESS;
}
void NetworkUtils::escapeQuote(nsCString& aString)
@ -1612,7 +1594,7 @@ void NetworkUtils::checkUsbRndisState(NetworkParams& aOptions)
/**
* Modify usb function's property to turn on USB RNDIS function
*/
bool NetworkUtils::enableUsbRndis(NetworkParams& aOptions)
int32_t NetworkUtils::enableUsbRndis(NetworkParams& aOptions)
{
bool report = aOptions.mReport;
@ -1670,16 +1652,16 @@ bool NetworkUtils::enableUsbRndis(NetworkParams& aOptions)
usleep(USB_FUNCTION_RETRY_INTERVAL * 1000);
checkUsbRndisState(aOptions);
}
return true;
return SUCCESS;
}
/**
* handling upstream interface change event.
*/
bool NetworkUtils::updateUpStream(NetworkParams& aOptions)
int32_t NetworkUtils::updateUpStream(NetworkParams& aOptions)
{
RUN_CHAIN(aOptions, sUpdateUpStreamChain, updateUpStreamFail)
return true;
return SUCCESS;
}
void NetworkUtils::sendBroadcastMessage(uint32_t code, char* reason)

Просмотреть файл

@ -112,7 +112,7 @@ public:
COPY_OPT_STRING_FIELD(mOldIfname, EmptyString())
COPY_OPT_STRING_FIELD(mMode, EmptyString())
COPY_OPT_FIELD(mReport, false)
COPY_OPT_FIELD(mIsAsync, true)
COPY_OPT_FIELD(mIsAsync, false)
COPY_OPT_FIELD(mEnabled, false)
COPY_OPT_STRING_FIELD(mWifictrlinterfacename, EmptyString())
COPY_OPT_STRING_FIELD(mInternalIfname, EmptyString())
@ -250,24 +250,24 @@ private:
/**
* Commands supported by NetworkUtils.
*/
bool setDNS(NetworkParams& aOptions);
bool setDefaultRouteAndDNS(NetworkParams& aOptions);
bool addHostRoute(NetworkParams& aOptions);
bool removeDefaultRoute(NetworkParams& aOptions);
bool removeHostRoute(NetworkParams& aOptions);
bool removeHostRoutes(NetworkParams& aOptions);
bool removeNetworkRoute(NetworkParams& aOptions);
bool addSecondaryRoute(NetworkParams& aOptions);
bool removeSecondaryRoute(NetworkParams& aOptions);
bool setNetworkInterfaceAlarm(NetworkParams& aOptions);
bool enableNetworkInterfaceAlarm(NetworkParams& aOptions);
bool disableNetworkInterfaceAlarm(NetworkParams& aOptions);
bool setWifiOperationMode(NetworkParams& aOptions);
bool setDhcpServer(NetworkParams& aOptions);
bool setWifiTethering(NetworkParams& aOptions);
bool setUSBTethering(NetworkParams& aOptions);
bool enableUsbRndis(NetworkParams& aOptions);
bool updateUpStream(NetworkParams& aOptions);
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);
/**
* function pointer array holds all netd commands should be executed