Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(254)

Side by Side Diff: talk/app/webrtc/java/jni/androidnetworkmonitor_jni.cc

Issue 1556743002: Bind a socket to a network if the network handle is set. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * libjingle 2 * libjingle
3 * Copyright 2015 Google Inc. 3 * Copyright 2015 Google Inc.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met: 6 * modification, are permitted provided that the following conditions are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright notice, 8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer. 9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice, 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation 11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution. 12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products 13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission. 14 * derived from this software without specific prior written permission.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27 27
28 #include "talk/app/webrtc/java/jni/androidnetworkmonitor_jni.h" 28 #include "talk/app/webrtc/java/jni/androidnetworkmonitor_jni.h"
29 29
30 #include <dlfcn.h>
31
32 #include "webrtc/base/bind.h"
30 #include "webrtc/base/common.h" 33 #include "webrtc/base/common.h"
34 #include "webrtc/base/ipaddress.h"
31 #include "talk/app/webrtc/java/jni/classreferenceholder.h" 35 #include "talk/app/webrtc/java/jni/classreferenceholder.h"
32 #include "talk/app/webrtc/java/jni/jni_helpers.h" 36 #include "talk/app/webrtc/java/jni/jni_helpers.h"
33 37
34 namespace webrtc_jni { 38 namespace webrtc_jni {
39
35 jobject AndroidNetworkMonitor::application_context_ = nullptr; 40 jobject AndroidNetworkMonitor::application_context_ = nullptr;
36 41
42 static NetworkType GetNetworkTypeFromJava(JNIEnv* jni, jobject j_network_type) {
43 std::string enum_name =
44 GetJavaEnumName(jni, "org/webrtc/NetworkMonitorAutoDetect$ConnectionType",
45 j_network_type);
46 if (enum_name == "CONNECTION_UNKNOWN") {
47 return NetworkType::NETWORK_UNKNOWN;
48 }
49 if (enum_name == "CONNECTION_ETHERNET") {
50 return NetworkType::NETWORK_ETHERNET;
51 }
52 if (enum_name == "CONNECTION_WIFI") {
53 return NetworkType::NETWORK_WIFI;
54 }
55 if (enum_name == "CONNECTION_4G") {
56 return NetworkType::NETWORK_4G;
57 }
58 if (enum_name == "CONNECTION_3G") {
59 return NetworkType::NETWORK_3G;
60 }
61 if (enum_name == "CONNECTION_2G") {
62 return NetworkType::NETWORK_2G;
63 }
64 if (enum_name == "CONNECTION_BLUETOOTH") {
65 return NetworkType::NETWORK_BLUETOOTH;
66 }
67 if (enum_name == "CONNECTION_NONE") {
68 return NetworkType::NETWORK_NONE;
69 }
70 ASSERT(false);
71 return NetworkType::NETWORK_UNKNOWN;
72 }
73
74 static rtc::IPAddress GetIPAddressFromJava(JNIEnv* jni, jobject j_ip_address) {
75 jclass j_ip_address_class = GetObjectClass(jni, j_ip_address);
76 jfieldID j_address_id = GetFieldID(jni, j_ip_address_class, "address", "[B");
77 jbyteArray j_addresses =
78 static_cast<jbyteArray>(GetObjectField(jni, j_ip_address, j_address_id));
79 size_t address_length = jni->GetArrayLength(j_addresses);
80 jbyte* addr_array = jni->GetByteArrayElements(j_addresses, NULL);
81 CHECK_EXCEPTION(jni) << "Error during NetworkMonitor.GetIPAddressFromJava";
82 if (address_length == 4) {
83 // IP4
84 struct in_addr ip4_addr;
85 memcpy(&ip4_addr.s_addr, addr_array, 4);
86 jni->ReleaseByteArrayElements(j_addresses, addr_array, JNI_ABORT);
87 return rtc::IPAddress(ip4_addr);
88 }
89 // IP6
90 RTC_CHECK(address_length == 16);
91 struct in6_addr ip6_addr;
92 memcpy(ip6_addr.s6_addr, addr_array, address_length);
93 jni->ReleaseByteArrayElements(j_addresses, addr_array, JNI_ABORT);
94 return rtc::IPAddress(ip6_addr);
95 }
96
97 static void GetIPAddressesFromJava(JNIEnv* jni,
98 jobjectArray j_ip_addresses,
99 std::vector<rtc::IPAddress>* ip_addresses) {
100 ip_addresses->clear();
101 size_t num_addresses = jni->GetArrayLength(j_ip_addresses);
102 for (size_t i = 0; i < num_addresses; ++i) {
103 jobject j_ip_address = jni->GetObjectArrayElement(j_ip_addresses, i);
104 rtc::IPAddress ip = GetIPAddressFromJava(jni, j_ip_address);
105
106 ip_addresses->push_back(ip);
107 }
108 CHECK_EXCEPTION(jni) << "Error during NetworkMonitor.GetIPAddressesFromJava";
109 }
110
111 static NetworkInformation GetNetworkInformationFromJava(
112 JNIEnv* jni,
113 jobject j_network_info) {
114 jclass j_network_info_class = GetObjectClass(jni, j_network_info);
115 jfieldID j_interface_name_id =
116 GetFieldID(jni, j_network_info_class, "name", "Ljava/lang/String;");
117 jfieldID j_handle_id = GetFieldID(jni, j_network_info_class, "handle", "I");
118 jfieldID j_type_id =
119 GetFieldID(jni, j_network_info_class, "type",
120 "Lorg/webrtc/NetworkMonitorAutoDetect$ConnectionType;");
121 jfieldID j_ip_addresses_id =
122 GetFieldID(jni, j_network_info_class, "ipAddresses",
123 "[Lorg/webrtc/NetworkMonitorAutoDetect$IPAddress;");
124
125 NetworkInformation network_info;
126 network_info.interface_name = JavaToStdString(
127 jni, GetStringField(jni, j_network_info, j_interface_name_id));
128 network_info.handle =
129 static_cast<NetworkHandle>(GetIntField(jni, j_network_info, j_handle_id));
130 network_info.type = GetNetworkTypeFromJava(
131 jni, GetObjectField(jni, j_network_info, j_type_id));
132 jobjectArray j_ip_addresses = static_cast<jobjectArray>(
133 GetObjectField(jni, j_network_info, j_ip_addresses_id));
134 GetIPAddressesFromJava(jni, j_ip_addresses, &network_info.ip_addresses);
135 CHECK_EXCEPTION(jni) << "Error during NetworkMonitor.GetNetworkInfoFromJava";
136 return network_info;
137 }
138
139 std::string NetworkInformation::ToString() const {
140 std::stringstream ss;
141 ss << "NetInfo[name " << interface_name << "; handle " << handle << "; type "
142 << type << "; address ";
143 for (const rtc::IPAddress address : ip_addresses) {
144 ss << address.ToString() << " ";
145 }
146 ss << "]";
147 return ss.str();
148 }
149
37 // static 150 // static
38 void AndroidNetworkMonitor::SetAndroidContext(JNIEnv* jni, jobject context) { 151 void AndroidNetworkMonitor::SetAndroidContext(JNIEnv* jni, jobject context) {
39 if (application_context_) { 152 if (application_context_) {
40 jni->DeleteGlobalRef(application_context_); 153 jni->DeleteGlobalRef(application_context_);
41 } 154 }
42 application_context_ = NewGlobalRef(jni, context); 155 application_context_ = NewGlobalRef(jni, context);
43 } 156 }
44 157
45 AndroidNetworkMonitor::AndroidNetworkMonitor() 158 AndroidNetworkMonitor::AndroidNetworkMonitor()
46 : j_network_monitor_class_(jni(), 159 : j_network_monitor_class_(jni(),
47 FindClass(jni(), "org/webrtc/NetworkMonitor")), 160 FindClass(jni(), "org/webrtc/NetworkMonitor")),
48 j_network_monitor_( 161 j_network_monitor_(
49 jni(), 162 jni(),
50 jni()->CallStaticObjectMethod( 163 jni()->CallStaticObjectMethod(
51 *j_network_monitor_class_, 164 *j_network_monitor_class_,
52 GetStaticMethodID( 165 GetStaticMethodID(
53 jni(), 166 jni(),
54 *j_network_monitor_class_, 167 *j_network_monitor_class_,
55 "init", 168 "init",
56 "(Landroid/content/Context;)Lorg/webrtc/NetworkMonitor;"), 169 "(Landroid/content/Context;)Lorg/webrtc/NetworkMonitor;"),
57 application_context_)) { 170 application_context_)) {
58 ASSERT(application_context_ != nullptr); 171 ASSERT(application_context_ != nullptr);
59 CHECK_EXCEPTION(jni()) << "Error during NetworkMonitor.init"; 172 CHECK_EXCEPTION(jni()) << "Error during NetworkMonitor.init";
60 } 173 }
61 174
62 void AndroidNetworkMonitor::Start() { 175 void AndroidNetworkMonitor::Start() {
63 RTC_CHECK(thread_checker_.CalledOnValidThread()); 176 RTC_CHECK(thread_checker_.CalledOnValidThread());
177 if (started_) {
178 return;
179 }
180 started_ = true;
181
64 jmethodID m = 182 jmethodID m =
65 GetMethodID(jni(), *j_network_monitor_class_, "startMonitoring", "(J)V"); 183 GetMethodID(jni(), *j_network_monitor_class_, "startMonitoring", "(J)V");
66 jni()->CallVoidMethod(*j_network_monitor_, m, jlongFromPointer(this)); 184 jni()->CallVoidMethod(*j_network_monitor_, m, jlongFromPointer(this));
67 CHECK_EXCEPTION(jni()) << "Error during NetworkMonitor.startMonitoring"; 185 CHECK_EXCEPTION(jni()) << "Error during NetworkMonitor.startMonitoring";
68 } 186 }
69 187
70 void AndroidNetworkMonitor::Stop() { 188 void AndroidNetworkMonitor::Stop() {
71 RTC_CHECK(thread_checker_.CalledOnValidThread()); 189 RTC_CHECK(thread_checker_.CalledOnValidThread());
190 if (!started_) {
191 return;
192 }
193 started_ = false;
194
72 jmethodID m = 195 jmethodID m =
73 GetMethodID(jni(), *j_network_monitor_class_, "stopMonitoring", "(J)V"); 196 GetMethodID(jni(), *j_network_monitor_class_, "stopMonitoring", "(J)V");
74 jni()->CallVoidMethod(*j_network_monitor_, m, jlongFromPointer(this)); 197 jni()->CallVoidMethod(*j_network_monitor_, m, jlongFromPointer(this));
75 CHECK_EXCEPTION(jni()) << "Error during NetworkMonitor.stopMonitoring"; 198 CHECK_EXCEPTION(jni()) << "Error during NetworkMonitor.stopMonitoring";
199
200 network_info_by_address_.clear();
201 }
202
203 int AndroidNetworkMonitor::BindSocketToNetwork(int socket_fd,
204 const rtc::IPAddress& address) {
205 RTC_CHECK(thread_checker_.CalledOnValidThread());
206 auto it = network_info_by_address_.find(address);
207 if (it == network_info_by_address_.end()) {
208 return rtc::ERR_ADDRESS_NOT_FOUND;
209 }
210 // Android prior to Lollipop didn't have support for binding sockets to
211 // networks. However, in that case it should not have reached here because
212 // |network_info_by_address_| should only be populated in Android Lollipop
213 // and above.
214 NetworkInformation network = it->second;
215
216 // NOTE: This does rely on Android implementation details, but
217 // these details are unlikely to change.
218 typedef int (*SetNetworkForSocket)(unsigned netId, int socketFd);
219 static SetNetworkForSocket setNetworkForSocket;
220 // This is not threadsafe, but we are running this only on the worker thread.
221 if (setNetworkForSocket == nullptr) {
222 // Android's netd client library should always be loaded in our address
223 // space as it shims libc functions like connect().
224 const std::string net_library_path = "libnetd_client.so";
225 void* lib = dlopen(net_library_path.c_str(), RTLD_LAZY);
226 if (lib == nullptr) {
227 LOG(LS_ERROR) << "Library " << net_library_path << " not found!";
228 return rtc::ERR_NOT_IMPLEMENTED;
229 }
230 setNetworkForSocket = reinterpret_cast<SetNetworkForSocket>(
231 dlsym(lib, "setNetworkForSocket"));
232 }
233 if (setNetworkForSocket == nullptr) {
234 LOG(LS_ERROR) << "Symbol setNetworkForSocket not found ";
235 return rtc::ERR_NOT_IMPLEMENTED;
236 }
237 int rv = setNetworkForSocket(network.handle, socket_fd);
238 // If |network| has since disconnected, |rv| will be ENONET. Surface this as
239 // ERR_NETWORK_CHANGED, rather than MapSystemError(ENONET) which gives back
240 // the less descriptive ERR_FAILED.
241 if (rv == 0) {
242 return rtc::OK;
243 }
244 if (rv == ENONET) {
245 return rtc::ERR_NETWORK_CHANGED;
246 }
247 return rtc::ERR_FAILED;
248 }
249
250 void AndroidNetworkMonitor::OnNetworkAvailable(
251 const NetworkInformation& network_info) {
252 worker_thread()->Invoke<void>(rtc::Bind(
253 &AndroidNetworkMonitor::OnNetworkAvailable_w, this, network_info));
254 }
255
256 void AndroidNetworkMonitor::OnNetworkAvailable_w(
257 const NetworkInformation& network_info) {
258 LOG(LS_INFO) << "Network available: " << network_info.ToString();
259 for (rtc::IPAddress address : network_info.ip_addresses) {
260 network_info_by_address_[address] = network_info;
261 }
262 }
263
264 rtc::NetworkMonitorInterface*
265 AndroidNetworkMonitorFactory::CreateNetworkMonitor() {
266 return new AndroidNetworkMonitor();
76 } 267 }
77 268
78 JOW(void, NetworkMonitor_nativeNotifyConnectionTypeChanged)( 269 JOW(void, NetworkMonitor_nativeNotifyConnectionTypeChanged)(
79 JNIEnv* jni, jobject j_monitor, jlong j_native_monitor) { 270 JNIEnv* jni, jobject j_monitor, jlong j_native_monitor) {
80 rtc::NetworkMonitorInterface* network_monitor = 271 rtc::NetworkMonitorInterface* network_monitor =
81 reinterpret_cast<rtc::NetworkMonitorInterface*>(j_native_monitor); 272 reinterpret_cast<rtc::NetworkMonitorInterface*>(j_native_monitor);
82 network_monitor->OnNetworksChanged(); 273 network_monitor->OnNetworksChanged();
83 } 274 }
84 275
276 JOW(void, NetworkMonitor_nativeNotifyOfNetworkConnect)(
277 JNIEnv* jni, jobject j_monitor, jlong j_native_monitor,
278 jobject j_network_info) {
279 AndroidNetworkMonitor* network_monitor =
280 reinterpret_cast<AndroidNetworkMonitor*>(j_native_monitor);
281 NetworkInformation network_info =
282 GetNetworkInformationFromJava(jni, j_network_info);
283 network_monitor->OnNetworkAvailable(network_info);
284 }
285
85 } // namespace webrtc_jni 286 } // namespace webrtc_jni
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698