1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/system/gonk/systemlibs.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,291 @@ 1.4 +/* Copyright 2012 Mozilla Foundation and Mozilla contributors 1.5 + * 1.6 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.7 + * you may not use this file except in compliance with the License. 1.8 + * You may obtain a copy of the License at 1.9 + * 1.10 + * http://www.apache.org/licenses/LICENSE-2.0 1.11 + * 1.12 + * Unless required by applicable law or agreed to in writing, software 1.13 + * distributed under the License is distributed on an "AS IS" BASIS, 1.14 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.15 + * See the License for the specific language governing permissions and 1.16 + * limitations under the License. 1.17 + */ 1.18 + 1.19 +if (!this.ctypes) { 1.20 + // We're likely being loaded as a JSM. 1.21 + this.EXPORTED_SYMBOLS = [ "libcutils", "libnetutils", "netHelpers" ]; 1.22 + Components.utils.import("resource://gre/modules/ctypes.jsm"); 1.23 +} 1.24 + 1.25 +const SYSTEM_PROPERTY_KEY_MAX = 32; 1.26 +const SYSTEM_PROPERTY_VALUE_MAX = 92; 1.27 + 1.28 +// We leave this as 'undefined' instead of setting it to 'false'. That 1.29 +// way a file that includes us can have it defined already without us 1.30 +// overriding the value here. 1.31 +let DEBUG; 1.32 + 1.33 +/** 1.34 + * Expose some system-level functions. 1.35 + */ 1.36 +this.libcutils = (function() { 1.37 + let lib; 1.38 + try { 1.39 + lib = ctypes.open("libcutils.so"); 1.40 + } catch(ex) { 1.41 + // Return a fallback option in case libcutils.so isn't present (e.g. 1.42 + // when building Firefox with MOZ_B2G_RIL. 1.43 + if (DEBUG) { 1.44 + dump("Could not load libcutils.so. Using fake propdb.\n"); 1.45 + } 1.46 + let fake_propdb = Object.create(null); 1.47 + return { 1.48 + property_get: function(key, defaultValue) { 1.49 + if (key in fake_propdb) { 1.50 + return fake_propdb[key]; 1.51 + } 1.52 + return defaultValue === undefined ? null : defaultValue; 1.53 + }, 1.54 + property_set: function(key, value) { 1.55 + fake_propdb[key] = value; 1.56 + } 1.57 + }; 1.58 + } 1.59 + 1.60 + let c_property_get = lib.declare("property_get", ctypes.default_abi, 1.61 + ctypes.int, // return value: length 1.62 + ctypes.char.ptr, // key 1.63 + ctypes.char.ptr, // value 1.64 + ctypes.char.ptr); // default 1.65 + let c_property_set = lib.declare("property_set", ctypes.default_abi, 1.66 + ctypes.int, // return value: success 1.67 + ctypes.char.ptr, // key 1.68 + ctypes.char.ptr); // value 1.69 + let c_value_buf = ctypes.char.array(SYSTEM_PROPERTY_VALUE_MAX)(); 1.70 + 1.71 + return { 1.72 + 1.73 + /** 1.74 + * Get a system property. 1.75 + * 1.76 + * @param key 1.77 + * Name of the property 1.78 + * @param defaultValue [optional] 1.79 + * Default value to return if the property isn't set (default: null) 1.80 + */ 1.81 + property_get: function(key, defaultValue) { 1.82 + if (defaultValue === undefined) { 1.83 + defaultValue = null; 1.84 + } 1.85 + c_property_get(key, c_value_buf, defaultValue); 1.86 + return c_value_buf.readString(); 1.87 + }, 1.88 + 1.89 + /** 1.90 + * Set a system property 1.91 + * 1.92 + * @param key 1.93 + * Name of the property 1.94 + * @param value 1.95 + * Value to set the property to. 1.96 + */ 1.97 + property_set: function(key, value) { 1.98 + let rv = c_property_set(key, value); 1.99 + if (rv) { 1.100 + throw Error('libcutils.property_set("' + key + '", "' + value + 1.101 + '") failed with error ' + rv); 1.102 + } 1.103 + } 1.104 + 1.105 + }; 1.106 +})(); 1.107 + 1.108 +/** 1.109 + * Network-related functions from libnetutils. 1.110 + */ 1.111 +this.libnetutils = (function() { 1.112 + let library; 1.113 + try { 1.114 + library = ctypes.open("libnetutils.so"); 1.115 + } catch(ex) { 1.116 + if (DEBUG) { 1.117 + dump("Could not load libnetutils.so!\n"); 1.118 + } 1.119 + // For now we just fake the ctypes library interfacer to return 1.120 + // no-op functions when library.declare() is called. 1.121 + library = { 1.122 + declare: function() { 1.123 + return function fake_libnetutils_function() {}; 1.124 + } 1.125 + }; 1.126 + } 1.127 + 1.128 + let iface = { 1.129 + ifc_enable: library.declare("ifc_enable", ctypes.default_abi, 1.130 + ctypes.int, 1.131 + ctypes.char.ptr), 1.132 + ifc_disable: library.declare("ifc_disable", ctypes.default_abi, 1.133 + ctypes.int, 1.134 + ctypes.char.ptr), 1.135 + ifc_add_host_route: library.declare("ifc_add_host_route", 1.136 + ctypes.default_abi, 1.137 + ctypes.int, 1.138 + ctypes.char.ptr, 1.139 + ctypes.int), 1.140 + ifc_remove_host_routes: library.declare("ifc_remove_host_routes", 1.141 + ctypes.default_abi, 1.142 + ctypes.int, 1.143 + ctypes.char.ptr), 1.144 + ifc_set_default_route: library.declare("ifc_set_default_route", 1.145 + ctypes.default_abi, 1.146 + ctypes.int, 1.147 + ctypes.char.ptr, 1.148 + ctypes.int), 1.149 + ifc_get_default_route: library.declare("ifc_get_default_route", 1.150 + ctypes.default_abi, 1.151 + ctypes.int, 1.152 + ctypes.char.ptr), 1.153 + ifc_remove_default_route: library.declare("ifc_remove_default_route", 1.154 + ctypes.default_abi, 1.155 + ctypes.int, 1.156 + ctypes.char.ptr), 1.157 + ifc_configure: library.declare("ifc_configure", ctypes.default_abi, 1.158 + ctypes.int, 1.159 + ctypes.char.ptr, 1.160 + ctypes.int, 1.161 + ctypes.int, 1.162 + ctypes.int, 1.163 + ctypes.int, 1.164 + ctypes.int), 1.165 + ifc_add_route: library.declare("ifc_add_route", ctypes.default_abi, 1.166 + ctypes.int, // return value 1.167 + ctypes.char.ptr, // ifname 1.168 + ctypes.char.ptr, // dst 1.169 + ctypes.int, // prefix_length 1.170 + ctypes.char.ptr), // gw 1.171 + ifc_remove_route: library.declare("ifc_remove_route", ctypes.default_abi, 1.172 + ctypes.int, // return value 1.173 + ctypes.char.ptr, // ifname 1.174 + ctypes.char.ptr, // dst 1.175 + ctypes.int, // prefix_length 1.176 + ctypes.char.ptr), // gw 1.177 + dhcp_stop: library.declare("dhcp_stop", ctypes.default_abi, 1.178 + ctypes.int, 1.179 + ctypes.char.ptr), 1.180 + dhcp_release_lease: library.declare("dhcp_release_lease", ctypes.default_abi, 1.181 + ctypes.int, 1.182 + ctypes.char.ptr), 1.183 + dhcp_get_errmsg: library.declare("dhcp_get_errmsg", ctypes.default_abi, 1.184 + ctypes.char.ptr), 1.185 + 1.186 + // Constants for ifc_reset_connections. 1.187 + // NOTE: Ignored in versions before ICS. 1.188 + RESET_IPV4_ADDRESSES: 0x01, 1.189 + RESET_IPV6_ADDRESSES: 0x02, 1.190 + }; 1.191 + 1.192 + iface.RESET_ALL_ADDRESSES = iface.RESET_IPV4_ADDRESSES | 1.193 + iface.RESET_IPV6_ADDRESSES; 1.194 + 1.195 + return iface; 1.196 +})(); 1.197 + 1.198 +/** 1.199 + * Helpers for conversions. 1.200 + */ 1.201 +this.netHelpers = { 1.202 + 1.203 + /** 1.204 + * Swap byte orders for 32-bit value 1.205 + */ 1.206 + swap32: function(n) { 1.207 + return (((n >> 24) & 0xFF) << 0) | 1.208 + (((n >> 16) & 0xFF) << 8) | 1.209 + (((n >> 8) & 0xFF) << 16) | 1.210 + (((n >> 0) & 0xFF) << 24); 1.211 + }, 1.212 + 1.213 + /** 1.214 + * Convert network byte order to host byte order 1.215 + * Note: Assume that the system is little endian 1.216 + */ 1.217 + ntohl: function(n) { 1.218 + return this.swap32(n); 1.219 + }, 1.220 + 1.221 + /** 1.222 + * Convert host byte order to network byte order 1.223 + * Note: Assume that the system is little endian 1.224 + */ 1.225 + htonl: function(n) { 1.226 + return this.swap32(n); 1.227 + }, 1.228 + 1.229 + /** 1.230 + * Convert integer representation of an IP address to the string 1.231 + * representation. 1.232 + * 1.233 + * @param ip 1.234 + * IP address in number format. 1.235 + */ 1.236 + ipToString: function(ip) { 1.237 + return ((ip >> 0) & 0xFF) + "." + 1.238 + ((ip >> 8) & 0xFF) + "." + 1.239 + ((ip >> 16) & 0xFF) + "." + 1.240 + ((ip >> 24) & 0xFF); 1.241 + }, 1.242 + 1.243 + /** 1.244 + * Convert string representation of an IP address to the integer 1.245 + * representation (network byte order). 1.246 + * 1.247 + * @param string 1.248 + * String containing the IP address. 1.249 + */ 1.250 + stringToIP: function(string) { 1.251 + if (!string) { 1.252 + return null; 1.253 + } 1.254 + let ip = 0; 1.255 + let start, end = -1; 1.256 + for (let i = 0; i < 4; i++) { 1.257 + start = end + 1; 1.258 + end = string.indexOf(".", start); 1.259 + if (end == -1) { 1.260 + end = string.length; 1.261 + } 1.262 + let num = parseInt(string.slice(start, end), 10); 1.263 + if (isNaN(num)) { 1.264 + return null; 1.265 + } 1.266 + ip |= num << (i * 8); 1.267 + } 1.268 + return ip; 1.269 + }, 1.270 + 1.271 + /** 1.272 + * Make a subnet mask. 1.273 + */ 1.274 + makeMask: function(len) { 1.275 + let mask = 0; 1.276 + for (let i = 0; i < len; ++i) { 1.277 + mask |= (0x80000000 >> i); 1.278 + } 1.279 + return this.ntohl(mask); 1.280 + }, 1.281 + 1.282 + /** 1.283 + * Get Mask length from given mask address 1.284 + */ 1.285 + getMaskLength: function(mask) { 1.286 + let len = 0; 1.287 + let netmask = this.ntohl(mask); 1.288 + while (netmask & 0x80000000) { 1.289 + len++; 1.290 + netmask = netmask << 1; 1.291 + } 1.292 + return len; 1.293 + } 1.294 +};