2014-06-24 22:12:07 -07:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
2013-09-29 20:00:04 -07:00
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/systemlibs.js");
|
|
|
|
|
2014-01-14 02:23:42 -08:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gNetworkService",
|
|
|
|
"@mozilla.org/network/service;1",
|
|
|
|
"nsINetworkService");
|
2013-09-29 20:00:04 -07:00
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["WifiNetUtil"];
|
|
|
|
|
|
|
|
const DHCP_PROP = "init.svc.dhcpcd";
|
|
|
|
const DHCP = "dhcpcd";
|
2014-01-14 02:23:42 -08:00
|
|
|
const DEBUG = false;
|
2013-09-29 20:00:04 -07:00
|
|
|
|
|
|
|
this.WifiNetUtil = function(controlMessage) {
|
2014-01-14 02:23:42 -08:00
|
|
|
function debug(msg) {
|
|
|
|
if (DEBUG) {
|
|
|
|
dump('-------------- NetUtil: ' + msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-29 20:00:04 -07:00
|
|
|
var util = {};
|
|
|
|
|
|
|
|
util.runDhcp = function (ifname, callback) {
|
2014-08-17 20:02:56 -07:00
|
|
|
gNetworkService.dhcpRequest(ifname, function(success, dhcpInfo) {
|
2013-09-29 20:00:04 -07:00
|
|
|
util.runIpConfig(ifname, dhcpInfo, callback);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
util.stopDhcp = function (ifname, callback) {
|
|
|
|
// This function does exactly what dhcp_stop does. Unforunately, if we call
|
|
|
|
// this function twice before the previous callback is returned. We may block
|
|
|
|
// our self waiting for the callback. It slows down the wifi startup procedure.
|
|
|
|
// Therefore, we have to roll our own version here.
|
|
|
|
let dhcpService = DHCP_PROP + "_" + ifname;
|
|
|
|
let suffix = (ifname.substr(0, 3) === "p2p") ? "p2p" : ifname;
|
|
|
|
let processName = DHCP + "_" + suffix;
|
|
|
|
stopProcess(dhcpService, processName, callback);
|
|
|
|
};
|
|
|
|
|
2014-01-14 02:23:42 -08:00
|
|
|
util.startDhcpServer = function (config, callback) {
|
|
|
|
gNetworkService.setDhcpServer(true, config, function (error) {
|
2013-09-29 20:00:04 -07:00
|
|
|
callback(!error);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
util.stopDhcpServer = function (callback) {
|
2014-01-14 02:23:42 -08:00
|
|
|
gNetworkService.setDhcpServer(false, null, function (error) {
|
2013-09-29 20:00:04 -07:00
|
|
|
callback(!error);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
util.runIpConfig = function (name, data, callback) {
|
|
|
|
if (!data) {
|
2014-01-14 02:23:42 -08:00
|
|
|
debug("IP config failed to run");
|
2013-09-29 20:00:04 -07:00
|
|
|
callback({ info: data });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setProperty("net." + name + ".dns1", ipToString(data.dns1),
|
|
|
|
function(ok) {
|
|
|
|
if (!ok) {
|
2014-01-14 02:23:42 -08:00
|
|
|
debug("Unable to set net.<ifname>.dns1");
|
2013-09-29 20:00:04 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
setProperty("net." + name + ".dns2", ipToString(data.dns2),
|
|
|
|
function(ok) {
|
|
|
|
if (!ok) {
|
2014-01-14 02:23:42 -08:00
|
|
|
debug("Unable to set net.<ifname>.dns2");
|
2013-09-29 20:00:04 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
setProperty("net." + name + ".gw", ipToString(data.gateway),
|
|
|
|
function(ok) {
|
|
|
|
if (!ok) {
|
2014-01-14 02:23:42 -08:00
|
|
|
debug("Unable to set net.<ifname>.gw");
|
2013-09-29 20:00:04 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
callback({ info: data });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
// Helper functions.
|
|
|
|
//--------------------------------------------------
|
|
|
|
|
|
|
|
function stopProcess(service, process, callback) {
|
|
|
|
var count = 0;
|
|
|
|
var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
function tick() {
|
|
|
|
let result = libcutils.property_get(service);
|
|
|
|
if (result === null) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (result === "stopped" || ++count >= 5) {
|
|
|
|
// Either we succeeded or ran out of time.
|
|
|
|
timer = null;
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else it's still running, continue waiting.
|
|
|
|
timer.initWithCallback(tick, 1000, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
setProperty("ctl.stop", process, tick);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper around libcutils.property_set that returns true if setting the
|
|
|
|
// value was successful.
|
|
|
|
// Note that the callback is not called asynchronously.
|
|
|
|
function setProperty(key, value, callback) {
|
|
|
|
let ok = true;
|
|
|
|
try {
|
|
|
|
libcutils.property_set(key, value);
|
|
|
|
} catch(e) {
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
callback(ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ipToString(n) {
|
|
|
|
return String((n >> 0) & 0xFF) + "." +
|
|
|
|
((n >> 8) & 0xFF) + "." +
|
|
|
|
((n >> 16) & 0xFF) + "." +
|
|
|
|
((n >> 24) & 0xFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
return util;
|
|
|
|
};
|