Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | "use strict"; |
michael@0 | 8 | |
michael@0 | 9 | const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
michael@0 | 10 | |
michael@0 | 11 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 12 | Cu.import("resource://gre/modules/systemlibs.js"); |
michael@0 | 13 | |
michael@0 | 14 | XPCOMUtils.defineLazyServiceGetter(this, "gNetworkService", |
michael@0 | 15 | "@mozilla.org/network/service;1", |
michael@0 | 16 | "nsINetworkService"); |
michael@0 | 17 | |
michael@0 | 18 | this.EXPORTED_SYMBOLS = ["WifiNetUtil"]; |
michael@0 | 19 | |
michael@0 | 20 | const DHCP_PROP = "init.svc.dhcpcd"; |
michael@0 | 21 | const DHCP = "dhcpcd"; |
michael@0 | 22 | const DEBUG = false; |
michael@0 | 23 | |
michael@0 | 24 | this.WifiNetUtil = function(controlMessage) { |
michael@0 | 25 | function debug(msg) { |
michael@0 | 26 | if (DEBUG) { |
michael@0 | 27 | dump('-------------- NetUtil: ' + msg); |
michael@0 | 28 | } |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | var util = {}; |
michael@0 | 32 | |
michael@0 | 33 | util.configureInterface = function(cfg, callback) { |
michael@0 | 34 | let message = { cmd: "ifc_configure", |
michael@0 | 35 | ifname: cfg.ifname, |
michael@0 | 36 | ipaddr: cfg.ipaddr, |
michael@0 | 37 | mask: cfg.mask, |
michael@0 | 38 | gateway: cfg.gateway, |
michael@0 | 39 | dns1: cfg.dns1, |
michael@0 | 40 | dns2: cfg.dns2 }; |
michael@0 | 41 | |
michael@0 | 42 | controlMessage(message, function(data) { |
michael@0 | 43 | callback(!data.status); |
michael@0 | 44 | }); |
michael@0 | 45 | }; |
michael@0 | 46 | |
michael@0 | 47 | util.runDhcp = function (ifname, callback) { |
michael@0 | 48 | controlMessage({ cmd: "dhcp_do_request", ifname: ifname }, function(data) { |
michael@0 | 49 | var dhcpInfo = data.status ? null : data; |
michael@0 | 50 | util.runIpConfig(ifname, dhcpInfo, callback); |
michael@0 | 51 | }); |
michael@0 | 52 | }; |
michael@0 | 53 | |
michael@0 | 54 | util.stopDhcp = function (ifname, callback) { |
michael@0 | 55 | // This function does exactly what dhcp_stop does. Unforunately, if we call |
michael@0 | 56 | // this function twice before the previous callback is returned. We may block |
michael@0 | 57 | // our self waiting for the callback. It slows down the wifi startup procedure. |
michael@0 | 58 | // Therefore, we have to roll our own version here. |
michael@0 | 59 | let dhcpService = DHCP_PROP + "_" + ifname; |
michael@0 | 60 | let suffix = (ifname.substr(0, 3) === "p2p") ? "p2p" : ifname; |
michael@0 | 61 | let processName = DHCP + "_" + suffix; |
michael@0 | 62 | stopProcess(dhcpService, processName, callback); |
michael@0 | 63 | }; |
michael@0 | 64 | |
michael@0 | 65 | util.enableInterface = function (ifname, callback) { |
michael@0 | 66 | controlMessage({ cmd: "ifc_enable", ifname: ifname }, function (data) { |
michael@0 | 67 | callback(!data.status); |
michael@0 | 68 | }); |
michael@0 | 69 | }; |
michael@0 | 70 | |
michael@0 | 71 | util.disableInterface = function (ifname, callback) { |
michael@0 | 72 | controlMessage({ cmd: "ifc_disable", ifname: ifname }, function (data) { |
michael@0 | 73 | callback(!data.status); |
michael@0 | 74 | }); |
michael@0 | 75 | }; |
michael@0 | 76 | |
michael@0 | 77 | util.startDhcpServer = function (config, callback) { |
michael@0 | 78 | gNetworkService.setDhcpServer(true, config, function (error) { |
michael@0 | 79 | callback(!error); |
michael@0 | 80 | }); |
michael@0 | 81 | }; |
michael@0 | 82 | |
michael@0 | 83 | util.stopDhcpServer = function (callback) { |
michael@0 | 84 | gNetworkService.setDhcpServer(false, null, function (error) { |
michael@0 | 85 | callback(!error); |
michael@0 | 86 | }); |
michael@0 | 87 | }; |
michael@0 | 88 | |
michael@0 | 89 | util.addHostRoute = function (ifname, route, callback) { |
michael@0 | 90 | controlMessage({ cmd: "ifc_add_host_route", ifname: ifname, route: route }, function(data) { |
michael@0 | 91 | callback(!data.status); |
michael@0 | 92 | }); |
michael@0 | 93 | }; |
michael@0 | 94 | |
michael@0 | 95 | util.removeHostRoutes = function (ifname, callback) { |
michael@0 | 96 | controlMessage({ cmd: "ifc_remove_host_routes", ifname: ifname }, function(data) { |
michael@0 | 97 | callback(!data.status); |
michael@0 | 98 | }); |
michael@0 | 99 | }; |
michael@0 | 100 | |
michael@0 | 101 | util.setDefaultRoute = function (ifname, route, callback) { |
michael@0 | 102 | controlMessage({ cmd: "ifc_set_default_route", ifname: ifname, route: route }, function(data) { |
michael@0 | 103 | callback(!data.status); |
michael@0 | 104 | }); |
michael@0 | 105 | }; |
michael@0 | 106 | |
michael@0 | 107 | util.getDefaultRoute = function (ifname, callback) { |
michael@0 | 108 | controlMessage({ cmd: "ifc_get_default_route", ifname: ifname }, function(data) { |
michael@0 | 109 | callback(!data.route); |
michael@0 | 110 | }); |
michael@0 | 111 | }; |
michael@0 | 112 | |
michael@0 | 113 | util.removeDefaultRoute = function (ifname, callback) { |
michael@0 | 114 | controlMessage({ cmd: "ifc_remove_default_route", ifname: ifname }, function(data) { |
michael@0 | 115 | callback(!data.status); |
michael@0 | 116 | }); |
michael@0 | 117 | }; |
michael@0 | 118 | |
michael@0 | 119 | util.resetConnections = function (ifname, callback) { |
michael@0 | 120 | controlMessage({ cmd: "ifc_reset_connections", ifname: ifname }, function(data) { |
michael@0 | 121 | callback(!data.status); |
michael@0 | 122 | }); |
michael@0 | 123 | }; |
michael@0 | 124 | |
michael@0 | 125 | util.releaseDhcpLease = function (ifname, callback) { |
michael@0 | 126 | controlMessage({ cmd: "dhcp_release_lease", ifname: ifname }, function(data) { |
michael@0 | 127 | callback(!data.status); |
michael@0 | 128 | }); |
michael@0 | 129 | }; |
michael@0 | 130 | |
michael@0 | 131 | util.getDhcpError = function (callback) { |
michael@0 | 132 | controlMessage({ cmd: "dhcp_get_errmsg" }, function(data) { |
michael@0 | 133 | callback(data.error); |
michael@0 | 134 | }); |
michael@0 | 135 | }; |
michael@0 | 136 | |
michael@0 | 137 | util.runDhcpRenew = function (ifname, callback) { |
michael@0 | 138 | controlMessage({ cmd: "dhcp_do_request", ifname: ifname }, function(data) { |
michael@0 | 139 | callback(data.status ? null : data); |
michael@0 | 140 | }); |
michael@0 | 141 | }; |
michael@0 | 142 | |
michael@0 | 143 | util.runIpConfig = function (name, data, callback) { |
michael@0 | 144 | if (!data) { |
michael@0 | 145 | debug("IP config failed to run"); |
michael@0 | 146 | callback({ info: data }); |
michael@0 | 147 | return; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | setProperty("net." + name + ".dns1", ipToString(data.dns1), |
michael@0 | 151 | function(ok) { |
michael@0 | 152 | if (!ok) { |
michael@0 | 153 | debug("Unable to set net.<ifname>.dns1"); |
michael@0 | 154 | return; |
michael@0 | 155 | } |
michael@0 | 156 | setProperty("net." + name + ".dns2", ipToString(data.dns2), |
michael@0 | 157 | function(ok) { |
michael@0 | 158 | if (!ok) { |
michael@0 | 159 | debug("Unable to set net.<ifname>.dns2"); |
michael@0 | 160 | return; |
michael@0 | 161 | } |
michael@0 | 162 | setProperty("net." + name + ".gw", ipToString(data.gateway), |
michael@0 | 163 | function(ok) { |
michael@0 | 164 | if (!ok) { |
michael@0 | 165 | debug("Unable to set net.<ifname>.gw"); |
michael@0 | 166 | return; |
michael@0 | 167 | } |
michael@0 | 168 | callback({ info: data }); |
michael@0 | 169 | }); |
michael@0 | 170 | }); |
michael@0 | 171 | }); |
michael@0 | 172 | }; |
michael@0 | 173 | |
michael@0 | 174 | //-------------------------------------------------- |
michael@0 | 175 | // Helper functions. |
michael@0 | 176 | //-------------------------------------------------- |
michael@0 | 177 | |
michael@0 | 178 | function stopProcess(service, process, callback) { |
michael@0 | 179 | var count = 0; |
michael@0 | 180 | var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
michael@0 | 181 | function tick() { |
michael@0 | 182 | let result = libcutils.property_get(service); |
michael@0 | 183 | if (result === null) { |
michael@0 | 184 | callback(); |
michael@0 | 185 | return; |
michael@0 | 186 | } |
michael@0 | 187 | if (result === "stopped" || ++count >= 5) { |
michael@0 | 188 | // Either we succeeded or ran out of time. |
michael@0 | 189 | timer = null; |
michael@0 | 190 | callback(); |
michael@0 | 191 | return; |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | // Else it's still running, continue waiting. |
michael@0 | 195 | timer.initWithCallback(tick, 1000, Ci.nsITimer.TYPE_ONE_SHOT); |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | setProperty("ctl.stop", process, tick); |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | // Wrapper around libcutils.property_set that returns true if setting the |
michael@0 | 202 | // value was successful. |
michael@0 | 203 | // Note that the callback is not called asynchronously. |
michael@0 | 204 | function setProperty(key, value, callback) { |
michael@0 | 205 | let ok = true; |
michael@0 | 206 | try { |
michael@0 | 207 | libcutils.property_set(key, value); |
michael@0 | 208 | } catch(e) { |
michael@0 | 209 | ok = false; |
michael@0 | 210 | } |
michael@0 | 211 | callback(ok); |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | function ipToString(n) { |
michael@0 | 215 | return String((n >> 0) & 0xFF) + "." + |
michael@0 | 216 | ((n >> 8) & 0xFF) + "." + |
michael@0 | 217 | ((n >> 16) & 0xFF) + "." + |
michael@0 | 218 | ((n >> 24) & 0xFF); |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | return util; |
michael@0 | 222 | }; |