dom/system/gonk/systemlibs.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Copyright 2012 Mozilla Foundation and Mozilla contributors
michael@0 2 *
michael@0 3 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 4 * you may not use this file except in compliance with the License.
michael@0 5 * You may obtain a copy of the License at
michael@0 6 *
michael@0 7 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 8 *
michael@0 9 * Unless required by applicable law or agreed to in writing, software
michael@0 10 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 12 * See the License for the specific language governing permissions and
michael@0 13 * limitations under the License.
michael@0 14 */
michael@0 15
michael@0 16 if (!this.ctypes) {
michael@0 17 // We're likely being loaded as a JSM.
michael@0 18 this.EXPORTED_SYMBOLS = [ "libcutils", "libnetutils", "netHelpers" ];
michael@0 19 Components.utils.import("resource://gre/modules/ctypes.jsm");
michael@0 20 }
michael@0 21
michael@0 22 const SYSTEM_PROPERTY_KEY_MAX = 32;
michael@0 23 const SYSTEM_PROPERTY_VALUE_MAX = 92;
michael@0 24
michael@0 25 // We leave this as 'undefined' instead of setting it to 'false'. That
michael@0 26 // way a file that includes us can have it defined already without us
michael@0 27 // overriding the value here.
michael@0 28 let DEBUG;
michael@0 29
michael@0 30 /**
michael@0 31 * Expose some system-level functions.
michael@0 32 */
michael@0 33 this.libcutils = (function() {
michael@0 34 let lib;
michael@0 35 try {
michael@0 36 lib = ctypes.open("libcutils.so");
michael@0 37 } catch(ex) {
michael@0 38 // Return a fallback option in case libcutils.so isn't present (e.g.
michael@0 39 // when building Firefox with MOZ_B2G_RIL.
michael@0 40 if (DEBUG) {
michael@0 41 dump("Could not load libcutils.so. Using fake propdb.\n");
michael@0 42 }
michael@0 43 let fake_propdb = Object.create(null);
michael@0 44 return {
michael@0 45 property_get: function(key, defaultValue) {
michael@0 46 if (key in fake_propdb) {
michael@0 47 return fake_propdb[key];
michael@0 48 }
michael@0 49 return defaultValue === undefined ? null : defaultValue;
michael@0 50 },
michael@0 51 property_set: function(key, value) {
michael@0 52 fake_propdb[key] = value;
michael@0 53 }
michael@0 54 };
michael@0 55 }
michael@0 56
michael@0 57 let c_property_get = lib.declare("property_get", ctypes.default_abi,
michael@0 58 ctypes.int, // return value: length
michael@0 59 ctypes.char.ptr, // key
michael@0 60 ctypes.char.ptr, // value
michael@0 61 ctypes.char.ptr); // default
michael@0 62 let c_property_set = lib.declare("property_set", ctypes.default_abi,
michael@0 63 ctypes.int, // return value: success
michael@0 64 ctypes.char.ptr, // key
michael@0 65 ctypes.char.ptr); // value
michael@0 66 let c_value_buf = ctypes.char.array(SYSTEM_PROPERTY_VALUE_MAX)();
michael@0 67
michael@0 68 return {
michael@0 69
michael@0 70 /**
michael@0 71 * Get a system property.
michael@0 72 *
michael@0 73 * @param key
michael@0 74 * Name of the property
michael@0 75 * @param defaultValue [optional]
michael@0 76 * Default value to return if the property isn't set (default: null)
michael@0 77 */
michael@0 78 property_get: function(key, defaultValue) {
michael@0 79 if (defaultValue === undefined) {
michael@0 80 defaultValue = null;
michael@0 81 }
michael@0 82 c_property_get(key, c_value_buf, defaultValue);
michael@0 83 return c_value_buf.readString();
michael@0 84 },
michael@0 85
michael@0 86 /**
michael@0 87 * Set a system property
michael@0 88 *
michael@0 89 * @param key
michael@0 90 * Name of the property
michael@0 91 * @param value
michael@0 92 * Value to set the property to.
michael@0 93 */
michael@0 94 property_set: function(key, value) {
michael@0 95 let rv = c_property_set(key, value);
michael@0 96 if (rv) {
michael@0 97 throw Error('libcutils.property_set("' + key + '", "' + value +
michael@0 98 '") failed with error ' + rv);
michael@0 99 }
michael@0 100 }
michael@0 101
michael@0 102 };
michael@0 103 })();
michael@0 104
michael@0 105 /**
michael@0 106 * Network-related functions from libnetutils.
michael@0 107 */
michael@0 108 this.libnetutils = (function() {
michael@0 109 let library;
michael@0 110 try {
michael@0 111 library = ctypes.open("libnetutils.so");
michael@0 112 } catch(ex) {
michael@0 113 if (DEBUG) {
michael@0 114 dump("Could not load libnetutils.so!\n");
michael@0 115 }
michael@0 116 // For now we just fake the ctypes library interfacer to return
michael@0 117 // no-op functions when library.declare() is called.
michael@0 118 library = {
michael@0 119 declare: function() {
michael@0 120 return function fake_libnetutils_function() {};
michael@0 121 }
michael@0 122 };
michael@0 123 }
michael@0 124
michael@0 125 let iface = {
michael@0 126 ifc_enable: library.declare("ifc_enable", ctypes.default_abi,
michael@0 127 ctypes.int,
michael@0 128 ctypes.char.ptr),
michael@0 129 ifc_disable: library.declare("ifc_disable", ctypes.default_abi,
michael@0 130 ctypes.int,
michael@0 131 ctypes.char.ptr),
michael@0 132 ifc_add_host_route: library.declare("ifc_add_host_route",
michael@0 133 ctypes.default_abi,
michael@0 134 ctypes.int,
michael@0 135 ctypes.char.ptr,
michael@0 136 ctypes.int),
michael@0 137 ifc_remove_host_routes: library.declare("ifc_remove_host_routes",
michael@0 138 ctypes.default_abi,
michael@0 139 ctypes.int,
michael@0 140 ctypes.char.ptr),
michael@0 141 ifc_set_default_route: library.declare("ifc_set_default_route",
michael@0 142 ctypes.default_abi,
michael@0 143 ctypes.int,
michael@0 144 ctypes.char.ptr,
michael@0 145 ctypes.int),
michael@0 146 ifc_get_default_route: library.declare("ifc_get_default_route",
michael@0 147 ctypes.default_abi,
michael@0 148 ctypes.int,
michael@0 149 ctypes.char.ptr),
michael@0 150 ifc_remove_default_route: library.declare("ifc_remove_default_route",
michael@0 151 ctypes.default_abi,
michael@0 152 ctypes.int,
michael@0 153 ctypes.char.ptr),
michael@0 154 ifc_configure: library.declare("ifc_configure", ctypes.default_abi,
michael@0 155 ctypes.int,
michael@0 156 ctypes.char.ptr,
michael@0 157 ctypes.int,
michael@0 158 ctypes.int,
michael@0 159 ctypes.int,
michael@0 160 ctypes.int,
michael@0 161 ctypes.int),
michael@0 162 ifc_add_route: library.declare("ifc_add_route", ctypes.default_abi,
michael@0 163 ctypes.int, // return value
michael@0 164 ctypes.char.ptr, // ifname
michael@0 165 ctypes.char.ptr, // dst
michael@0 166 ctypes.int, // prefix_length
michael@0 167 ctypes.char.ptr), // gw
michael@0 168 ifc_remove_route: library.declare("ifc_remove_route", ctypes.default_abi,
michael@0 169 ctypes.int, // return value
michael@0 170 ctypes.char.ptr, // ifname
michael@0 171 ctypes.char.ptr, // dst
michael@0 172 ctypes.int, // prefix_length
michael@0 173 ctypes.char.ptr), // gw
michael@0 174 dhcp_stop: library.declare("dhcp_stop", ctypes.default_abi,
michael@0 175 ctypes.int,
michael@0 176 ctypes.char.ptr),
michael@0 177 dhcp_release_lease: library.declare("dhcp_release_lease", ctypes.default_abi,
michael@0 178 ctypes.int,
michael@0 179 ctypes.char.ptr),
michael@0 180 dhcp_get_errmsg: library.declare("dhcp_get_errmsg", ctypes.default_abi,
michael@0 181 ctypes.char.ptr),
michael@0 182
michael@0 183 // Constants for ifc_reset_connections.
michael@0 184 // NOTE: Ignored in versions before ICS.
michael@0 185 RESET_IPV4_ADDRESSES: 0x01,
michael@0 186 RESET_IPV6_ADDRESSES: 0x02,
michael@0 187 };
michael@0 188
michael@0 189 iface.RESET_ALL_ADDRESSES = iface.RESET_IPV4_ADDRESSES |
michael@0 190 iface.RESET_IPV6_ADDRESSES;
michael@0 191
michael@0 192 return iface;
michael@0 193 })();
michael@0 194
michael@0 195 /**
michael@0 196 * Helpers for conversions.
michael@0 197 */
michael@0 198 this.netHelpers = {
michael@0 199
michael@0 200 /**
michael@0 201 * Swap byte orders for 32-bit value
michael@0 202 */
michael@0 203 swap32: function(n) {
michael@0 204 return (((n >> 24) & 0xFF) << 0) |
michael@0 205 (((n >> 16) & 0xFF) << 8) |
michael@0 206 (((n >> 8) & 0xFF) << 16) |
michael@0 207 (((n >> 0) & 0xFF) << 24);
michael@0 208 },
michael@0 209
michael@0 210 /**
michael@0 211 * Convert network byte order to host byte order
michael@0 212 * Note: Assume that the system is little endian
michael@0 213 */
michael@0 214 ntohl: function(n) {
michael@0 215 return this.swap32(n);
michael@0 216 },
michael@0 217
michael@0 218 /**
michael@0 219 * Convert host byte order to network byte order
michael@0 220 * Note: Assume that the system is little endian
michael@0 221 */
michael@0 222 htonl: function(n) {
michael@0 223 return this.swap32(n);
michael@0 224 },
michael@0 225
michael@0 226 /**
michael@0 227 * Convert integer representation of an IP address to the string
michael@0 228 * representation.
michael@0 229 *
michael@0 230 * @param ip
michael@0 231 * IP address in number format.
michael@0 232 */
michael@0 233 ipToString: function(ip) {
michael@0 234 return ((ip >> 0) & 0xFF) + "." +
michael@0 235 ((ip >> 8) & 0xFF) + "." +
michael@0 236 ((ip >> 16) & 0xFF) + "." +
michael@0 237 ((ip >> 24) & 0xFF);
michael@0 238 },
michael@0 239
michael@0 240 /**
michael@0 241 * Convert string representation of an IP address to the integer
michael@0 242 * representation (network byte order).
michael@0 243 *
michael@0 244 * @param string
michael@0 245 * String containing the IP address.
michael@0 246 */
michael@0 247 stringToIP: function(string) {
michael@0 248 if (!string) {
michael@0 249 return null;
michael@0 250 }
michael@0 251 let ip = 0;
michael@0 252 let start, end = -1;
michael@0 253 for (let i = 0; i < 4; i++) {
michael@0 254 start = end + 1;
michael@0 255 end = string.indexOf(".", start);
michael@0 256 if (end == -1) {
michael@0 257 end = string.length;
michael@0 258 }
michael@0 259 let num = parseInt(string.slice(start, end), 10);
michael@0 260 if (isNaN(num)) {
michael@0 261 return null;
michael@0 262 }
michael@0 263 ip |= num << (i * 8);
michael@0 264 }
michael@0 265 return ip;
michael@0 266 },
michael@0 267
michael@0 268 /**
michael@0 269 * Make a subnet mask.
michael@0 270 */
michael@0 271 makeMask: function(len) {
michael@0 272 let mask = 0;
michael@0 273 for (let i = 0; i < len; ++i) {
michael@0 274 mask |= (0x80000000 >> i);
michael@0 275 }
michael@0 276 return this.ntohl(mask);
michael@0 277 },
michael@0 278
michael@0 279 /**
michael@0 280 * Get Mask length from given mask address
michael@0 281 */
michael@0 282 getMaskLength: function(mask) {
michael@0 283 let len = 0;
michael@0 284 let netmask = this.ntohl(mask);
michael@0 285 while (netmask & 0x80000000) {
michael@0 286 len++;
michael@0 287 netmask = netmask << 1;
michael@0 288 }
michael@0 289 return len;
michael@0 290 }
michael@0 291 };

mercurial