2012-07-20 02:10:44 -07:00
|
|
|
/* Copyright 2012 Mozilla Foundation and Mozilla contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2012-02-02 10:41:07 -08:00
|
|
|
|
2012-09-27 16:43:54 -07:00
|
|
|
if (!this.ctypes) {
|
|
|
|
// We're likely being loaded as a JSM.
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = [ "libcutils", "libnetutils", "netHelpers" ];
|
2012-09-27 16:43:54 -07:00
|
|
|
Components.utils.import("resource://gre/modules/ctypes.jsm");
|
|
|
|
}
|
|
|
|
|
2012-02-02 10:41:07 -08:00
|
|
|
const SYSTEM_PROPERTY_KEY_MAX = 32;
|
|
|
|
const SYSTEM_PROPERTY_VALUE_MAX = 92;
|
|
|
|
|
2012-04-12 22:58:57 -07:00
|
|
|
// We leave this as 'undefined' instead of setting it to 'false'. That
|
|
|
|
// way a file that includes us can have it defined already without us
|
|
|
|
// overriding the value here.
|
|
|
|
let DEBUG;
|
2012-04-11 10:05:35 -07:00
|
|
|
|
2012-02-02 10:41:07 -08:00
|
|
|
/**
|
|
|
|
* Expose some system-level functions.
|
|
|
|
*/
|
2012-10-31 09:13:28 -07:00
|
|
|
this.libcutils = (function() {
|
2012-02-02 10:41:07 -08:00
|
|
|
let lib;
|
|
|
|
try {
|
|
|
|
lib = ctypes.open("libcutils.so");
|
|
|
|
} catch(ex) {
|
|
|
|
// Return a fallback option in case libcutils.so isn't present (e.g.
|
|
|
|
// when building Firefox with MOZ_B2G_RIL.
|
2012-04-11 10:05:35 -07:00
|
|
|
if (DEBUG) {
|
|
|
|
dump("Could not load libcutils.so. Using fake propdb.\n");
|
|
|
|
}
|
2012-02-02 10:41:07 -08:00
|
|
|
let fake_propdb = Object.create(null);
|
|
|
|
return {
|
2014-01-12 18:44:40 -08:00
|
|
|
property_get: function(key, defaultValue) {
|
2012-02-02 10:41:07 -08:00
|
|
|
if (key in fake_propdb) {
|
|
|
|
return fake_propdb[key];
|
|
|
|
}
|
|
|
|
return defaultValue === undefined ? null : defaultValue;
|
|
|
|
},
|
2014-01-12 18:44:40 -08:00
|
|
|
property_set: function(key, value) {
|
2012-02-02 10:41:07 -08:00
|
|
|
fake_propdb[key] = value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let c_property_get = lib.declare("property_get", ctypes.default_abi,
|
|
|
|
ctypes.int, // return value: length
|
|
|
|
ctypes.char.ptr, // key
|
|
|
|
ctypes.char.ptr, // value
|
|
|
|
ctypes.char.ptr); // default
|
|
|
|
let c_property_set = lib.declare("property_set", ctypes.default_abi,
|
|
|
|
ctypes.int, // return value: success
|
|
|
|
ctypes.char.ptr, // key
|
|
|
|
ctypes.char.ptr); // value
|
|
|
|
let c_value_buf = ctypes.char.array(SYSTEM_PROPERTY_VALUE_MAX)();
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a system property.
|
|
|
|
*
|
|
|
|
* @param key
|
|
|
|
* Name of the property
|
|
|
|
* @param defaultValue [optional]
|
|
|
|
* Default value to return if the property isn't set (default: null)
|
|
|
|
*/
|
2014-01-12 18:44:40 -08:00
|
|
|
property_get: function(key, defaultValue) {
|
2012-02-02 10:41:07 -08:00
|
|
|
if (defaultValue === undefined) {
|
|
|
|
defaultValue = null;
|
|
|
|
}
|
|
|
|
c_property_get(key, c_value_buf, defaultValue);
|
|
|
|
return c_value_buf.readString();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a system property
|
|
|
|
*
|
|
|
|
* @param key
|
|
|
|
* Name of the property
|
|
|
|
* @param value
|
|
|
|
* Value to set the property to.
|
|
|
|
*/
|
2014-01-12 18:44:40 -08:00
|
|
|
property_set: function(key, value) {
|
2012-02-02 10:41:07 -08:00
|
|
|
let rv = c_property_set(key, value);
|
|
|
|
if (rv) {
|
|
|
|
throw Error('libcutils.property_set("' + key + '", "' + value +
|
|
|
|
'") failed with error ' + rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
})();
|
2012-03-12 16:45:57 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Network-related functions from libnetutils.
|
|
|
|
*/
|
2014-01-12 18:44:33 -08:00
|
|
|
this.libnetutils = (function() {
|
2012-03-12 16:45:57 -07:00
|
|
|
let library;
|
|
|
|
try {
|
|
|
|
library = ctypes.open("libnetutils.so");
|
|
|
|
} catch(ex) {
|
2012-04-11 10:05:35 -07:00
|
|
|
if (DEBUG) {
|
|
|
|
dump("Could not load libnetutils.so!\n");
|
|
|
|
}
|
2012-03-12 16:45:57 -07:00
|
|
|
// For now we just fake the ctypes library interfacer to return
|
|
|
|
// no-op functions when library.declare() is called.
|
|
|
|
library = {
|
2014-01-12 18:44:40 -08:00
|
|
|
declare: function() {
|
2012-03-12 16:45:57 -07:00
|
|
|
return function fake_libnetutils_function() {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-03-30 12:04:30 -07:00
|
|
|
let iface = {
|
2012-03-12 16:45:57 -07:00
|
|
|
ifc_enable: library.declare("ifc_enable", ctypes.default_abi,
|
|
|
|
ctypes.int,
|
|
|
|
ctypes.char.ptr),
|
|
|
|
ifc_disable: library.declare("ifc_disable", ctypes.default_abi,
|
|
|
|
ctypes.int,
|
|
|
|
ctypes.char.ptr),
|
|
|
|
ifc_add_host_route: library.declare("ifc_add_host_route",
|
|
|
|
ctypes.default_abi,
|
|
|
|
ctypes.int,
|
|
|
|
ctypes.char.ptr,
|
|
|
|
ctypes.int),
|
|
|
|
ifc_remove_host_routes: library.declare("ifc_remove_host_routes",
|
|
|
|
ctypes.default_abi,
|
|
|
|
ctypes.int,
|
|
|
|
ctypes.char.ptr),
|
|
|
|
ifc_set_default_route: library.declare("ifc_set_default_route",
|
|
|
|
ctypes.default_abi,
|
|
|
|
ctypes.int,
|
|
|
|
ctypes.char.ptr,
|
|
|
|
ctypes.int),
|
|
|
|
ifc_get_default_route: library.declare("ifc_get_default_route",
|
|
|
|
ctypes.default_abi,
|
|
|
|
ctypes.int,
|
|
|
|
ctypes.char.ptr),
|
|
|
|
ifc_remove_default_route: library.declare("ifc_remove_default_route",
|
|
|
|
ctypes.default_abi,
|
|
|
|
ctypes.int,
|
|
|
|
ctypes.char.ptr),
|
|
|
|
ifc_configure: library.declare("ifc_configure", ctypes.default_abi,
|
|
|
|
ctypes.int,
|
|
|
|
ctypes.char.ptr,
|
|
|
|
ctypes.int,
|
|
|
|
ctypes.int,
|
|
|
|
ctypes.int,
|
|
|
|
ctypes.int,
|
|
|
|
ctypes.int),
|
2012-08-30 09:57:33 -07:00
|
|
|
ifc_add_route: library.declare("ifc_add_route", ctypes.default_abi,
|
|
|
|
ctypes.int, // return value
|
|
|
|
ctypes.char.ptr, // ifname
|
|
|
|
ctypes.char.ptr, // dst
|
|
|
|
ctypes.int, // prefix_length
|
|
|
|
ctypes.char.ptr), // gw
|
|
|
|
ifc_remove_route: library.declare("ifc_remove_route", ctypes.default_abi,
|
|
|
|
ctypes.int, // return value
|
|
|
|
ctypes.char.ptr, // ifname
|
|
|
|
ctypes.char.ptr, // dst
|
|
|
|
ctypes.int, // prefix_length
|
|
|
|
ctypes.char.ptr), // gw
|
2012-03-12 16:45:57 -07:00
|
|
|
dhcp_stop: library.declare("dhcp_stop", ctypes.default_abi,
|
|
|
|
ctypes.int,
|
|
|
|
ctypes.char.ptr),
|
|
|
|
dhcp_release_lease: library.declare("dhcp_release_lease", ctypes.default_abi,
|
|
|
|
ctypes.int,
|
|
|
|
ctypes.char.ptr),
|
|
|
|
dhcp_get_errmsg: library.declare("dhcp_get_errmsg", ctypes.default_abi,
|
|
|
|
ctypes.char.ptr),
|
2012-08-30 09:01:34 -07:00
|
|
|
|
|
|
|
// Constants for ifc_reset_connections.
|
|
|
|
// NOTE: Ignored in versions before ICS.
|
|
|
|
RESET_IPV4_ADDRESSES: 0x01,
|
|
|
|
RESET_IPV6_ADDRESSES: 0x02,
|
2012-03-12 16:45:57 -07:00
|
|
|
};
|
2012-03-30 12:04:30 -07:00
|
|
|
|
2012-08-30 09:01:34 -07:00
|
|
|
iface.RESET_ALL_ADDRESSES = iface.RESET_IPV4_ADDRESSES |
|
2013-12-09 23:10:46 -08:00
|
|
|
iface.RESET_IPV6_ADDRESSES;
|
2012-03-30 12:04:30 -07:00
|
|
|
|
|
|
|
return iface;
|
2012-03-12 16:45:57 -07:00
|
|
|
})();
|
2012-03-30 12:04:30 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helpers for conversions.
|
|
|
|
*/
|
2012-10-31 09:13:28 -07:00
|
|
|
this.netHelpers = {
|
2012-03-30 12:04:30 -07:00
|
|
|
|
2012-08-13 18:54:42 -07:00
|
|
|
/**
|
|
|
|
* Swap byte orders for 32-bit value
|
|
|
|
*/
|
2014-01-12 18:44:40 -08:00
|
|
|
swap32: function(n) {
|
2012-08-13 18:54:42 -07:00
|
|
|
return (((n >> 24) & 0xFF) << 0) |
|
|
|
|
(((n >> 16) & 0xFF) << 8) |
|
|
|
|
(((n >> 8) & 0xFF) << 16) |
|
|
|
|
(((n >> 0) & 0xFF) << 24);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert network byte order to host byte order
|
|
|
|
* Note: Assume that the system is little endian
|
|
|
|
*/
|
2014-01-12 18:44:40 -08:00
|
|
|
ntohl: function(n) {
|
2012-08-13 18:54:42 -07:00
|
|
|
return this.swap32(n);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert host byte order to network byte order
|
|
|
|
* Note: Assume that the system is little endian
|
|
|
|
*/
|
2014-01-12 18:44:40 -08:00
|
|
|
htonl: function(n) {
|
2012-08-13 18:54:42 -07:00
|
|
|
return this.swap32(n);
|
|
|
|
},
|
|
|
|
|
2012-03-30 12:04:30 -07:00
|
|
|
/**
|
|
|
|
* Convert integer representation of an IP address to the string
|
|
|
|
* representation.
|
|
|
|
*
|
|
|
|
* @param ip
|
|
|
|
* IP address in number format.
|
|
|
|
*/
|
2014-01-12 18:44:40 -08:00
|
|
|
ipToString: function(ip) {
|
2012-03-30 12:04:30 -07:00
|
|
|
return ((ip >> 0) & 0xFF) + "." +
|
|
|
|
((ip >> 8) & 0xFF) + "." +
|
|
|
|
((ip >> 16) & 0xFF) + "." +
|
|
|
|
((ip >> 24) & 0xFF);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert string representation of an IP address to the integer
|
2013-09-30 01:31:12 -07:00
|
|
|
* representation (network byte order).
|
2012-03-30 12:04:30 -07:00
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
* String containing the IP address.
|
|
|
|
*/
|
2014-01-12 18:44:40 -08:00
|
|
|
stringToIP: function(string) {
|
2013-09-30 01:31:12 -07:00
|
|
|
if (!string) {
|
|
|
|
return null;
|
|
|
|
}
|
2012-03-30 12:04:30 -07:00
|
|
|
let ip = 0;
|
|
|
|
let start, end = -1;
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
start = end + 1;
|
|
|
|
end = string.indexOf(".", start);
|
|
|
|
if (end == -1) {
|
|
|
|
end = string.length;
|
|
|
|
}
|
|
|
|
let num = parseInt(string.slice(start, end), 10);
|
|
|
|
if (isNaN(num)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
ip |= num << (i * 8);
|
|
|
|
}
|
|
|
|
return ip;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a subnet mask.
|
|
|
|
*/
|
2014-01-12 18:44:40 -08:00
|
|
|
makeMask: function(len) {
|
2012-03-30 12:04:30 -07:00
|
|
|
let mask = 0;
|
|
|
|
for (let i = 0; i < len; ++i) {
|
2012-08-13 18:54:42 -07:00
|
|
|
mask |= (0x80000000 >> i);
|
2012-03-30 12:04:30 -07:00
|
|
|
}
|
2012-08-13 18:54:42 -07:00
|
|
|
return this.ntohl(mask);
|
2013-05-02 03:02:33 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Mask length from given mask address
|
|
|
|
*/
|
2014-01-12 18:44:40 -08:00
|
|
|
getMaskLength: function(mask) {
|
2013-05-02 03:02:33 -07:00
|
|
|
let len = 0;
|
|
|
|
let netmask = this.ntohl(mask);
|
|
|
|
while (netmask & 0x80000000) {
|
|
|
|
len++;
|
|
|
|
netmask = netmask << 1;
|
|
|
|
}
|
|
|
|
return len;
|
2012-03-30 12:04:30 -07:00
|
|
|
}
|
|
|
|
};
|