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