dom/network/src/NetUtils.cpp

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 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "NetUtils.h"
michael@0 6 #include <dlfcn.h>
michael@0 7 #include <errno.h>
michael@0 8 #include <cutils/properties.h>
michael@0 9 #include "prinit.h"
michael@0 10 #include "mozilla/Assertions.h"
michael@0 11 #include "nsDebug.h"
michael@0 12
michael@0 13 static void* sNetUtilsLib;
michael@0 14 static PRCallOnceType sInitNetUtilsLib;
michael@0 15
michael@0 16 static PRStatus
michael@0 17 InitNetUtilsLib()
michael@0 18 {
michael@0 19 sNetUtilsLib = dlopen("/system/lib/libnetutils.so", RTLD_LAZY);
michael@0 20 // We might fail to open the hardware lib. That's OK.
michael@0 21 return PR_SUCCESS;
michael@0 22 }
michael@0 23
michael@0 24 static void*
michael@0 25 GetNetUtilsLibHandle()
michael@0 26 {
michael@0 27 PR_CallOnce(&sInitNetUtilsLib, InitNetUtilsLib);
michael@0 28 return sNetUtilsLib;
michael@0 29 }
michael@0 30
michael@0 31 // static
michael@0 32 void*
michael@0 33 NetUtils::GetSharedLibrary()
michael@0 34 {
michael@0 35 void* netLib = GetNetUtilsLibHandle();
michael@0 36 if (!netLib) {
michael@0 37 NS_WARNING("No /system/lib/libnetutils.so");
michael@0 38 }
michael@0 39 return netLib;
michael@0 40 }
michael@0 41
michael@0 42 // static
michael@0 43 int32_t
michael@0 44 NetUtils::SdkVersion()
michael@0 45 {
michael@0 46 char propVersion[PROPERTY_VALUE_MAX];
michael@0 47 property_get("ro.build.version.sdk", propVersion, "0");
michael@0 48 int32_t version = strtol(propVersion, nullptr, 10);
michael@0 49 return version;
michael@0 50 }
michael@0 51
michael@0 52 DEFINE_DLFUNC(ifc_enable, int32_t, const char*)
michael@0 53 DEFINE_DLFUNC(ifc_disable, int32_t, const char*)
michael@0 54 DEFINE_DLFUNC(ifc_configure, int32_t, const char*, in_addr_t, uint32_t,
michael@0 55 in_addr_t, in_addr_t, in_addr_t)
michael@0 56 DEFINE_DLFUNC(ifc_reset_connections, int32_t, const char*, const int32_t)
michael@0 57 DEFINE_DLFUNC(ifc_set_default_route, int32_t, const char*, in_addr_t)
michael@0 58 DEFINE_DLFUNC(ifc_add_route, int32_t, const char*, const char*, uint32_t, const char*)
michael@0 59 DEFINE_DLFUNC(ifc_remove_route, int32_t, const char*, const char*, uint32_t, const char*)
michael@0 60 DEFINE_DLFUNC(ifc_remove_host_routes, int32_t, const char*)
michael@0 61 DEFINE_DLFUNC(ifc_remove_default_route, int32_t, const char*)
michael@0 62 DEFINE_DLFUNC(dhcp_stop, int32_t, const char*)
michael@0 63
michael@0 64 int32_t NetUtils::do_ifc_enable(const char *ifname)
michael@0 65 {
michael@0 66 USE_DLFUNC(ifc_enable)
michael@0 67 return ifc_enable(ifname);
michael@0 68 }
michael@0 69
michael@0 70 int32_t NetUtils::do_ifc_disable(const char *ifname)
michael@0 71 {
michael@0 72 USE_DLFUNC(ifc_disable)
michael@0 73 return ifc_disable(ifname);
michael@0 74 }
michael@0 75
michael@0 76 int32_t NetUtils::do_ifc_configure(const char *ifname,
michael@0 77 in_addr_t address,
michael@0 78 uint32_t prefixLength,
michael@0 79 in_addr_t gateway,
michael@0 80 in_addr_t dns1,
michael@0 81 in_addr_t dns2)
michael@0 82 {
michael@0 83 USE_DLFUNC(ifc_configure)
michael@0 84 int32_t ret = ifc_configure(ifname, address, prefixLength, gateway, dns1, dns2);
michael@0 85 return ret;
michael@0 86 }
michael@0 87
michael@0 88 int32_t NetUtils::do_ifc_reset_connections(const char *ifname,
michael@0 89 const int32_t resetMask)
michael@0 90 {
michael@0 91 USE_DLFUNC(ifc_reset_connections)
michael@0 92 return ifc_reset_connections(ifname, resetMask);
michael@0 93 }
michael@0 94
michael@0 95 int32_t NetUtils::do_ifc_set_default_route(const char *ifname,
michael@0 96 in_addr_t gateway)
michael@0 97 {
michael@0 98 USE_DLFUNC(ifc_set_default_route)
michael@0 99 return ifc_set_default_route(ifname, gateway);
michael@0 100 }
michael@0 101
michael@0 102 int32_t NetUtils::do_ifc_add_route(const char *ifname,
michael@0 103 const char *dst,
michael@0 104 uint32_t prefixLength,
michael@0 105 const char *gateway)
michael@0 106 {
michael@0 107 USE_DLFUNC(ifc_add_route)
michael@0 108 return ifc_add_route(ifname, dst, prefixLength, gateway);
michael@0 109 }
michael@0 110
michael@0 111 int32_t NetUtils::do_ifc_remove_route(const char *ifname,
michael@0 112 const char *dst,
michael@0 113 uint32_t prefixLength,
michael@0 114 const char *gateway)
michael@0 115 {
michael@0 116 USE_DLFUNC(ifc_remove_route)
michael@0 117 return ifc_remove_route(ifname, dst, prefixLength, gateway);
michael@0 118 }
michael@0 119
michael@0 120 int32_t NetUtils::do_ifc_remove_host_routes(const char *ifname)
michael@0 121 {
michael@0 122 USE_DLFUNC(ifc_remove_host_routes)
michael@0 123 return ifc_remove_host_routes(ifname);
michael@0 124 }
michael@0 125
michael@0 126 int32_t NetUtils::do_ifc_remove_default_route(const char *ifname)
michael@0 127 {
michael@0 128 USE_DLFUNC(ifc_remove_default_route)
michael@0 129 return ifc_remove_default_route(ifname);
michael@0 130 }
michael@0 131
michael@0 132 int32_t NetUtils::do_dhcp_stop(const char *ifname)
michael@0 133 {
michael@0 134 USE_DLFUNC(dhcp_stop)
michael@0 135 return dhcp_stop(ifname);
michael@0 136 }
michael@0 137
michael@0 138 int32_t NetUtils::do_dhcp_do_request(const char *ifname,
michael@0 139 char *ipaddr,
michael@0 140 char *gateway,
michael@0 141 uint32_t *prefixLength,
michael@0 142 char *dns1,
michael@0 143 char *dns2,
michael@0 144 char *server,
michael@0 145 uint32_t *lease,
michael@0 146 char* vendorinfo)
michael@0 147 {
michael@0 148 int32_t ret = -1;
michael@0 149 uint32_t sdkVersion = SdkVersion();
michael@0 150
michael@0 151 if (sdkVersion == 15) {
michael@0 152 // ICS
michael@0 153 // http://androidxref.com/4.0.4/xref/system/core/libnetutils/dhcp_utils.c#149
michael@0 154 DEFINE_DLFUNC(dhcp_do_request, int32_t, const char*, char*, char*, uint32_t*, char*, char*, char*, uint32_t*)
michael@0 155 USE_DLFUNC(dhcp_do_request)
michael@0 156 vendorinfo[0] = '\0';
michael@0 157
michael@0 158 ret = dhcp_do_request(ifname, ipaddr, gateway, prefixLength, dns1, dns2,
michael@0 159 server, lease);
michael@0 160 } else if (sdkVersion == 16 || sdkVersion == 17) {
michael@0 161 // JB 4.1 and 4.2
michael@0 162 // http://androidxref.com/4.1.2/xref/system/core/libnetutils/dhcp_utils.c#175
michael@0 163 // http://androidxref.com/4.2.2_r1/xref/system/core/include/netutils/dhcp.h#26
michael@0 164 DEFINE_DLFUNC(dhcp_do_request, int32_t, const char*, char*, char*, uint32_t*, char*, char*, char*, uint32_t*, char*)
michael@0 165 USE_DLFUNC(dhcp_do_request)
michael@0 166 ret = dhcp_do_request(ifname, ipaddr, gateway, prefixLength, dns1, dns2,
michael@0 167 server, lease, vendorinfo);
michael@0 168 } else if (sdkVersion == 18) {
michael@0 169 // JB 4.3
michael@0 170 // http://androidxref.com/4.3_r2.1/xref/system/core/libnetutils/dhcp_utils.c#181
michael@0 171 DEFINE_DLFUNC(dhcp_do_request, int32_t, const char*, char*, char*, uint32_t*, char**, char*, uint32_t*, char*, char*)
michael@0 172 USE_DLFUNC(dhcp_do_request)
michael@0 173 char *dns[3] = {dns1, dns2, nullptr};
michael@0 174 char domains[PROPERTY_VALUE_MAX];
michael@0 175 ret = dhcp_do_request(ifname, ipaddr, gateway, prefixLength, dns,
michael@0 176 server, lease, vendorinfo, domains);
michael@0 177 } else if (sdkVersion == 19) {
michael@0 178 // JB 4.4
michael@0 179 // http://androidxref.com/4.4_r1/xref/system/core/libnetutils/dhcp_utils.c#18
michael@0 180 DEFINE_DLFUNC(dhcp_do_request, int32_t, const char*, char*, char*, uint32_t*, char**, char*, uint32_t*, char*, char*, char*)
michael@0 181 USE_DLFUNC(dhcp_do_request)
michael@0 182 char *dns[3] = {dns1, dns2, nullptr};
michael@0 183 char domains[PROPERTY_VALUE_MAX];
michael@0 184 char mtu[PROPERTY_VALUE_MAX];
michael@0 185 ret = dhcp_do_request(ifname, ipaddr, gateway, prefixLength, dns, server, lease, vendorinfo, domains, mtu);
michael@0 186 } else {
michael@0 187 NS_WARNING("Unable to perform do_dhcp_request: unsupported sdk version!");
michael@0 188 }
michael@0 189 return ret;
michael@0 190 }

mercurial