1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/network/src/NetUtils.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,190 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "NetUtils.h" 1.9 +#include <dlfcn.h> 1.10 +#include <errno.h> 1.11 +#include <cutils/properties.h> 1.12 +#include "prinit.h" 1.13 +#include "mozilla/Assertions.h" 1.14 +#include "nsDebug.h" 1.15 + 1.16 +static void* sNetUtilsLib; 1.17 +static PRCallOnceType sInitNetUtilsLib; 1.18 + 1.19 +static PRStatus 1.20 +InitNetUtilsLib() 1.21 +{ 1.22 + sNetUtilsLib = dlopen("/system/lib/libnetutils.so", RTLD_LAZY); 1.23 + // We might fail to open the hardware lib. That's OK. 1.24 + return PR_SUCCESS; 1.25 +} 1.26 + 1.27 +static void* 1.28 +GetNetUtilsLibHandle() 1.29 +{ 1.30 + PR_CallOnce(&sInitNetUtilsLib, InitNetUtilsLib); 1.31 + return sNetUtilsLib; 1.32 +} 1.33 + 1.34 +// static 1.35 +void* 1.36 +NetUtils::GetSharedLibrary() 1.37 +{ 1.38 + void* netLib = GetNetUtilsLibHandle(); 1.39 + if (!netLib) { 1.40 + NS_WARNING("No /system/lib/libnetutils.so"); 1.41 + } 1.42 + return netLib; 1.43 +} 1.44 + 1.45 +// static 1.46 +int32_t 1.47 +NetUtils::SdkVersion() 1.48 +{ 1.49 + char propVersion[PROPERTY_VALUE_MAX]; 1.50 + property_get("ro.build.version.sdk", propVersion, "0"); 1.51 + int32_t version = strtol(propVersion, nullptr, 10); 1.52 + return version; 1.53 +} 1.54 + 1.55 +DEFINE_DLFUNC(ifc_enable, int32_t, const char*) 1.56 +DEFINE_DLFUNC(ifc_disable, int32_t, const char*) 1.57 +DEFINE_DLFUNC(ifc_configure, int32_t, const char*, in_addr_t, uint32_t, 1.58 + in_addr_t, in_addr_t, in_addr_t) 1.59 +DEFINE_DLFUNC(ifc_reset_connections, int32_t, const char*, const int32_t) 1.60 +DEFINE_DLFUNC(ifc_set_default_route, int32_t, const char*, in_addr_t) 1.61 +DEFINE_DLFUNC(ifc_add_route, int32_t, const char*, const char*, uint32_t, const char*) 1.62 +DEFINE_DLFUNC(ifc_remove_route, int32_t, const char*, const char*, uint32_t, const char*) 1.63 +DEFINE_DLFUNC(ifc_remove_host_routes, int32_t, const char*) 1.64 +DEFINE_DLFUNC(ifc_remove_default_route, int32_t, const char*) 1.65 +DEFINE_DLFUNC(dhcp_stop, int32_t, const char*) 1.66 + 1.67 +int32_t NetUtils::do_ifc_enable(const char *ifname) 1.68 +{ 1.69 + USE_DLFUNC(ifc_enable) 1.70 + return ifc_enable(ifname); 1.71 +} 1.72 + 1.73 +int32_t NetUtils::do_ifc_disable(const char *ifname) 1.74 +{ 1.75 + USE_DLFUNC(ifc_disable) 1.76 + return ifc_disable(ifname); 1.77 +} 1.78 + 1.79 +int32_t NetUtils::do_ifc_configure(const char *ifname, 1.80 + in_addr_t address, 1.81 + uint32_t prefixLength, 1.82 + in_addr_t gateway, 1.83 + in_addr_t dns1, 1.84 + in_addr_t dns2) 1.85 +{ 1.86 + USE_DLFUNC(ifc_configure) 1.87 + int32_t ret = ifc_configure(ifname, address, prefixLength, gateway, dns1, dns2); 1.88 + return ret; 1.89 +} 1.90 + 1.91 +int32_t NetUtils::do_ifc_reset_connections(const char *ifname, 1.92 + const int32_t resetMask) 1.93 +{ 1.94 + USE_DLFUNC(ifc_reset_connections) 1.95 + return ifc_reset_connections(ifname, resetMask); 1.96 +} 1.97 + 1.98 +int32_t NetUtils::do_ifc_set_default_route(const char *ifname, 1.99 + in_addr_t gateway) 1.100 +{ 1.101 + USE_DLFUNC(ifc_set_default_route) 1.102 + return ifc_set_default_route(ifname, gateway); 1.103 +} 1.104 + 1.105 +int32_t NetUtils::do_ifc_add_route(const char *ifname, 1.106 + const char *dst, 1.107 + uint32_t prefixLength, 1.108 + const char *gateway) 1.109 +{ 1.110 + USE_DLFUNC(ifc_add_route) 1.111 + return ifc_add_route(ifname, dst, prefixLength, gateway); 1.112 +} 1.113 + 1.114 +int32_t NetUtils::do_ifc_remove_route(const char *ifname, 1.115 + const char *dst, 1.116 + uint32_t prefixLength, 1.117 + const char *gateway) 1.118 +{ 1.119 + USE_DLFUNC(ifc_remove_route) 1.120 + return ifc_remove_route(ifname, dst, prefixLength, gateway); 1.121 +} 1.122 + 1.123 +int32_t NetUtils::do_ifc_remove_host_routes(const char *ifname) 1.124 +{ 1.125 + USE_DLFUNC(ifc_remove_host_routes) 1.126 + return ifc_remove_host_routes(ifname); 1.127 +} 1.128 + 1.129 +int32_t NetUtils::do_ifc_remove_default_route(const char *ifname) 1.130 +{ 1.131 + USE_DLFUNC(ifc_remove_default_route) 1.132 + return ifc_remove_default_route(ifname); 1.133 +} 1.134 + 1.135 +int32_t NetUtils::do_dhcp_stop(const char *ifname) 1.136 +{ 1.137 + USE_DLFUNC(dhcp_stop) 1.138 + return dhcp_stop(ifname); 1.139 +} 1.140 + 1.141 +int32_t NetUtils::do_dhcp_do_request(const char *ifname, 1.142 + char *ipaddr, 1.143 + char *gateway, 1.144 + uint32_t *prefixLength, 1.145 + char *dns1, 1.146 + char *dns2, 1.147 + char *server, 1.148 + uint32_t *lease, 1.149 + char* vendorinfo) 1.150 +{ 1.151 + int32_t ret = -1; 1.152 + uint32_t sdkVersion = SdkVersion(); 1.153 + 1.154 + if (sdkVersion == 15) { 1.155 + // ICS 1.156 + // http://androidxref.com/4.0.4/xref/system/core/libnetutils/dhcp_utils.c#149 1.157 + DEFINE_DLFUNC(dhcp_do_request, int32_t, const char*, char*, char*, uint32_t*, char*, char*, char*, uint32_t*) 1.158 + USE_DLFUNC(dhcp_do_request) 1.159 + vendorinfo[0] = '\0'; 1.160 + 1.161 + ret = dhcp_do_request(ifname, ipaddr, gateway, prefixLength, dns1, dns2, 1.162 + server, lease); 1.163 + } else if (sdkVersion == 16 || sdkVersion == 17) { 1.164 + // JB 4.1 and 4.2 1.165 + // http://androidxref.com/4.1.2/xref/system/core/libnetutils/dhcp_utils.c#175 1.166 + // http://androidxref.com/4.2.2_r1/xref/system/core/include/netutils/dhcp.h#26 1.167 + DEFINE_DLFUNC(dhcp_do_request, int32_t, const char*, char*, char*, uint32_t*, char*, char*, char*, uint32_t*, char*) 1.168 + USE_DLFUNC(dhcp_do_request) 1.169 + ret = dhcp_do_request(ifname, ipaddr, gateway, prefixLength, dns1, dns2, 1.170 + server, lease, vendorinfo); 1.171 + } else if (sdkVersion == 18) { 1.172 + // JB 4.3 1.173 + // http://androidxref.com/4.3_r2.1/xref/system/core/libnetutils/dhcp_utils.c#181 1.174 + DEFINE_DLFUNC(dhcp_do_request, int32_t, const char*, char*, char*, uint32_t*, char**, char*, uint32_t*, char*, char*) 1.175 + USE_DLFUNC(dhcp_do_request) 1.176 + char *dns[3] = {dns1, dns2, nullptr}; 1.177 + char domains[PROPERTY_VALUE_MAX]; 1.178 + ret = dhcp_do_request(ifname, ipaddr, gateway, prefixLength, dns, 1.179 + server, lease, vendorinfo, domains); 1.180 + } else if (sdkVersion == 19) { 1.181 + // JB 4.4 1.182 + // http://androidxref.com/4.4_r1/xref/system/core/libnetutils/dhcp_utils.c#18 1.183 + DEFINE_DLFUNC(dhcp_do_request, int32_t, const char*, char*, char*, uint32_t*, char**, char*, uint32_t*, char*, char*, char*) 1.184 + USE_DLFUNC(dhcp_do_request) 1.185 + char *dns[3] = {dns1, dns2, nullptr}; 1.186 + char domains[PROPERTY_VALUE_MAX]; 1.187 + char mtu[PROPERTY_VALUE_MAX]; 1.188 + ret = dhcp_do_request(ifname, ipaddr, gateway, prefixLength, dns, server, lease, vendorinfo, domains, mtu); 1.189 + } else { 1.190 + NS_WARNING("Unable to perform do_dhcp_request: unsupported sdk version!"); 1.191 + } 1.192 + return ret; 1.193 +}