| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 #include "webrtc/api/java/jni/androidnetworkmonitor_jni.h" | |
| 12 | |
| 13 #include <dlfcn.h> | |
| 14 | |
| 15 #include "webrtc/api/java/jni/classreferenceholder.h" | |
| 16 #include "webrtc/api/java/jni/jni_helpers.h" | |
| 17 #include "webrtc/base/bind.h" | |
| 18 #include "webrtc/base/common.h" | |
| 19 #include "webrtc/base/ipaddress.h" | |
| 20 | |
| 21 namespace webrtc_jni { | |
| 22 | |
| 23 jobject AndroidNetworkMonitor::application_context_ = nullptr; | |
| 24 | |
| 25 static NetworkType GetNetworkTypeFromJava(JNIEnv* jni, jobject j_network_type) { | |
| 26 std::string enum_name = | |
| 27 GetJavaEnumName(jni, "org/webrtc/NetworkMonitorAutoDetect$ConnectionType", | |
| 28 j_network_type); | |
| 29 if (enum_name == "CONNECTION_UNKNOWN") { | |
| 30 return NetworkType::NETWORK_UNKNOWN; | |
| 31 } | |
| 32 if (enum_name == "CONNECTION_ETHERNET") { | |
| 33 return NetworkType::NETWORK_ETHERNET; | |
| 34 } | |
| 35 if (enum_name == "CONNECTION_WIFI") { | |
| 36 return NetworkType::NETWORK_WIFI; | |
| 37 } | |
| 38 if (enum_name == "CONNECTION_4G") { | |
| 39 return NetworkType::NETWORK_4G; | |
| 40 } | |
| 41 if (enum_name == "CONNECTION_3G") { | |
| 42 return NetworkType::NETWORK_3G; | |
| 43 } | |
| 44 if (enum_name == "CONNECTION_2G") { | |
| 45 return NetworkType::NETWORK_2G; | |
| 46 } | |
| 47 if (enum_name == "CONNECTION_BLUETOOTH") { | |
| 48 return NetworkType::NETWORK_BLUETOOTH; | |
| 49 } | |
| 50 if (enum_name == "CONNECTION_NONE") { | |
| 51 return NetworkType::NETWORK_NONE; | |
| 52 } | |
| 53 ASSERT(false); | |
| 54 return NetworkType::NETWORK_UNKNOWN; | |
| 55 } | |
| 56 | |
| 57 static rtc::AdapterType AdapterTypeFromNetworkType(NetworkType network_type) { | |
| 58 switch (network_type) { | |
| 59 case NETWORK_UNKNOWN: | |
| 60 RTC_DCHECK(false) << "Unknown network type"; | |
| 61 return rtc::ADAPTER_TYPE_UNKNOWN; | |
| 62 case NETWORK_ETHERNET: | |
| 63 return rtc::ADAPTER_TYPE_ETHERNET; | |
| 64 case NETWORK_WIFI: | |
| 65 return rtc::ADAPTER_TYPE_WIFI; | |
| 66 case NETWORK_4G: | |
| 67 case NETWORK_3G: | |
| 68 case NETWORK_2G: | |
| 69 return rtc::ADAPTER_TYPE_CELLULAR; | |
| 70 case NETWORK_BLUETOOTH: | |
| 71 // There is no corresponding mapping for bluetooth networks. | |
| 72 // Map it to VPN for now. | |
| 73 return rtc::ADAPTER_TYPE_VPN; | |
| 74 default: | |
| 75 RTC_DCHECK(false) << "Invalid network type " << network_type; | |
| 76 return rtc::ADAPTER_TYPE_UNKNOWN; | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 static rtc::IPAddress GetIPAddressFromJava(JNIEnv* jni, jobject j_ip_address) { | |
| 81 jclass j_ip_address_class = GetObjectClass(jni, j_ip_address); | |
| 82 jfieldID j_address_id = GetFieldID(jni, j_ip_address_class, "address", "[B"); | |
| 83 jbyteArray j_addresses = | |
| 84 static_cast<jbyteArray>(GetObjectField(jni, j_ip_address, j_address_id)); | |
| 85 size_t address_length = jni->GetArrayLength(j_addresses); | |
| 86 jbyte* addr_array = jni->GetByteArrayElements(j_addresses, nullptr); | |
| 87 CHECK_EXCEPTION(jni) << "Error during GetIPAddressFromJava"; | |
| 88 if (address_length == 4) { | |
| 89 // IP4 | |
| 90 struct in_addr ip4_addr; | |
| 91 memcpy(&ip4_addr.s_addr, addr_array, 4); | |
| 92 jni->ReleaseByteArrayElements(j_addresses, addr_array, JNI_ABORT); | |
| 93 return rtc::IPAddress(ip4_addr); | |
| 94 } | |
| 95 // IP6 | |
| 96 RTC_CHECK(address_length == 16); | |
| 97 struct in6_addr ip6_addr; | |
| 98 memcpy(ip6_addr.s6_addr, addr_array, address_length); | |
| 99 jni->ReleaseByteArrayElements(j_addresses, addr_array, JNI_ABORT); | |
| 100 return rtc::IPAddress(ip6_addr); | |
| 101 } | |
| 102 | |
| 103 static void GetIPAddressesFromJava(JNIEnv* jni, | |
| 104 jobjectArray j_ip_addresses, | |
| 105 std::vector<rtc::IPAddress>* ip_addresses) { | |
| 106 ip_addresses->clear(); | |
| 107 size_t num_addresses = jni->GetArrayLength(j_ip_addresses); | |
| 108 CHECK_EXCEPTION(jni) << "Error during GetArrayLength"; | |
| 109 for (size_t i = 0; i < num_addresses; ++i) { | |
| 110 jobject j_ip_address = jni->GetObjectArrayElement(j_ip_addresses, i); | |
| 111 CHECK_EXCEPTION(jni) << "Error during GetObjectArrayElement"; | |
| 112 rtc::IPAddress ip = GetIPAddressFromJava(jni, j_ip_address); | |
| 113 ip_addresses->push_back(ip); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 static NetworkInformation GetNetworkInformationFromJava( | |
| 118 JNIEnv* jni, | |
| 119 jobject j_network_info) { | |
| 120 jclass j_network_info_class = GetObjectClass(jni, j_network_info); | |
| 121 jfieldID j_interface_name_id = | |
| 122 GetFieldID(jni, j_network_info_class, "name", "Ljava/lang/String;"); | |
| 123 jfieldID j_handle_id = GetFieldID(jni, j_network_info_class, "handle", "I"); | |
| 124 jfieldID j_type_id = | |
| 125 GetFieldID(jni, j_network_info_class, "type", | |
| 126 "Lorg/webrtc/NetworkMonitorAutoDetect$ConnectionType;"); | |
| 127 jfieldID j_ip_addresses_id = | |
| 128 GetFieldID(jni, j_network_info_class, "ipAddresses", | |
| 129 "[Lorg/webrtc/NetworkMonitorAutoDetect$IPAddress;"); | |
| 130 | |
| 131 NetworkInformation network_info; | |
| 132 network_info.interface_name = JavaToStdString( | |
| 133 jni, GetStringField(jni, j_network_info, j_interface_name_id)); | |
| 134 network_info.handle = | |
| 135 static_cast<NetworkHandle>(GetIntField(jni, j_network_info, j_handle_id)); | |
| 136 network_info.type = GetNetworkTypeFromJava( | |
| 137 jni, GetObjectField(jni, j_network_info, j_type_id)); | |
| 138 jobjectArray j_ip_addresses = static_cast<jobjectArray>( | |
| 139 GetObjectField(jni, j_network_info, j_ip_addresses_id)); | |
| 140 GetIPAddressesFromJava(jni, j_ip_addresses, &network_info.ip_addresses); | |
| 141 return network_info; | |
| 142 } | |
| 143 | |
| 144 std::string NetworkInformation::ToString() const { | |
| 145 std::stringstream ss; | |
| 146 ss << "NetInfo[name " << interface_name << "; handle " << handle << "; type " | |
| 147 << type << "; address"; | |
| 148 for (const rtc::IPAddress address : ip_addresses) { | |
| 149 ss << " " << address.ToString(); | |
| 150 } | |
| 151 ss << "]"; | |
| 152 return ss.str(); | |
| 153 } | |
| 154 | |
| 155 // static | |
| 156 void AndroidNetworkMonitor::SetAndroidContext(JNIEnv* jni, jobject context) { | |
| 157 if (application_context_) { | |
| 158 jni->DeleteGlobalRef(application_context_); | |
| 159 } | |
| 160 application_context_ = NewGlobalRef(jni, context); | |
| 161 } | |
| 162 | |
| 163 AndroidNetworkMonitor::AndroidNetworkMonitor() | |
| 164 : j_network_monitor_class_(jni(), | |
| 165 FindClass(jni(), "org/webrtc/NetworkMonitor")), | |
| 166 j_network_monitor_( | |
| 167 jni(), | |
| 168 jni()->CallStaticObjectMethod( | |
| 169 *j_network_monitor_class_, | |
| 170 GetStaticMethodID( | |
| 171 jni(), | |
| 172 *j_network_monitor_class_, | |
| 173 "init", | |
| 174 "(Landroid/content/Context;)Lorg/webrtc/NetworkMonitor;"), | |
| 175 application_context_)) { | |
| 176 ASSERT(application_context_ != nullptr); | |
| 177 CHECK_EXCEPTION(jni()) << "Error during NetworkMonitor.init"; | |
| 178 } | |
| 179 | |
| 180 void AndroidNetworkMonitor::Start() { | |
| 181 RTC_CHECK(thread_checker_.CalledOnValidThread()); | |
| 182 if (started_) { | |
| 183 return; | |
| 184 } | |
| 185 started_ = true; | |
| 186 | |
| 187 // This is kind of magic behavior, but doing this allows the SocketServer to | |
| 188 // use this as a NetworkBinder to bind sockets on a particular network when | |
| 189 // it creates sockets. | |
| 190 worker_thread()->socketserver()->set_network_binder(this); | |
| 191 | |
| 192 jmethodID m = | |
| 193 GetMethodID(jni(), *j_network_monitor_class_, "startMonitoring", "(J)V"); | |
| 194 jni()->CallVoidMethod(*j_network_monitor_, m, jlongFromPointer(this)); | |
| 195 CHECK_EXCEPTION(jni()) << "Error during CallVoidMethod"; | |
| 196 } | |
| 197 | |
| 198 void AndroidNetworkMonitor::Stop() { | |
| 199 RTC_CHECK(thread_checker_.CalledOnValidThread()); | |
| 200 if (!started_) { | |
| 201 return; | |
| 202 } | |
| 203 started_ = false; | |
| 204 | |
| 205 // Once the network monitor stops, it will clear all network information and | |
| 206 // it won't find the network handle to bind anyway. | |
| 207 if (worker_thread()->socketserver()->network_binder() == this) { | |
| 208 worker_thread()->socketserver()->set_network_binder(nullptr); | |
| 209 } | |
| 210 | |
| 211 jmethodID m = | |
| 212 GetMethodID(jni(), *j_network_monitor_class_, "stopMonitoring", "(J)V"); | |
| 213 jni()->CallVoidMethod(*j_network_monitor_, m, jlongFromPointer(this)); | |
| 214 CHECK_EXCEPTION(jni()) << "Error during NetworkMonitor.stopMonitoring"; | |
| 215 | |
| 216 network_handle_by_address_.clear(); | |
| 217 network_info_by_handle_.clear(); | |
| 218 } | |
| 219 | |
| 220 int AndroidNetworkMonitor::BindSocketToNetwork(int socket_fd, | |
| 221 const rtc::IPAddress& address) { | |
| 222 RTC_CHECK(thread_checker_.CalledOnValidThread()); | |
| 223 // Android prior to Lollipop didn't have support for binding sockets to | |
| 224 // networks. However, in that case it should not have reached here because | |
| 225 // |network_handle_by_address_| should only be populated in Android Lollipop | |
| 226 // and above. | |
| 227 // TODO(honghaiz): Add a check for Android version here so that it won't try | |
| 228 // to look for handle if the Android version is before Lollipop. | |
| 229 auto iter = network_handle_by_address_.find(address); | |
| 230 if (iter == network_handle_by_address_.end()) { | |
| 231 return rtc::NETWORK_BIND_ADDRESS_NOT_FOUND; | |
| 232 } | |
| 233 NetworkHandle network_handle = iter->second; | |
| 234 | |
| 235 // NOTE: This does rely on Android implementation details, but | |
| 236 // these details are unlikely to change. | |
| 237 typedef int (*SetNetworkForSocket)(unsigned netId, int socketFd); | |
| 238 static SetNetworkForSocket setNetworkForSocket; | |
| 239 // This is not threadsafe, but we are running this only on the worker thread. | |
| 240 if (setNetworkForSocket == nullptr) { | |
| 241 // Android's netd client library should always be loaded in our address | |
| 242 // space as it shims libc functions like connect(). | |
| 243 const std::string net_library_path = "libnetd_client.so"; | |
| 244 void* lib = dlopen(net_library_path.c_str(), RTLD_LAZY); | |
| 245 if (lib == nullptr) { | |
| 246 LOG(LS_ERROR) << "Library " << net_library_path << " not found!"; | |
| 247 return rtc::NETWORK_BIND_NOT_IMPLEMENTED; | |
| 248 } | |
| 249 setNetworkForSocket = reinterpret_cast<SetNetworkForSocket>( | |
| 250 dlsym(lib, "setNetworkForSocket")); | |
| 251 } | |
| 252 if (setNetworkForSocket == nullptr) { | |
| 253 LOG(LS_ERROR) << "Symbol setNetworkForSocket not found "; | |
| 254 return rtc::NETWORK_BIND_NOT_IMPLEMENTED; | |
| 255 } | |
| 256 int rv = setNetworkForSocket(network_handle, socket_fd); | |
| 257 // If |network| has since disconnected, |rv| will be ENONET. Surface this as | |
| 258 // ERR_NETWORK_CHANGED, rather than MapSystemError(ENONET) which gives back | |
| 259 // the less descriptive ERR_FAILED. | |
| 260 if (rv == 0) { | |
| 261 return rtc::NETWORK_BIND_SUCCESS; | |
| 262 } | |
| 263 if (rv == ENONET) { | |
| 264 return rtc::NETWORK_BIND_NETWORK_CHANGED; | |
| 265 } | |
| 266 return rtc::NETWORK_BIND_FAILURE; | |
| 267 } | |
| 268 | |
| 269 void AndroidNetworkMonitor::OnNetworkConnected( | |
| 270 const NetworkInformation& network_info) { | |
| 271 worker_thread()->Invoke<void>( | |
| 272 RTC_FROM_HERE, rtc::Bind(&AndroidNetworkMonitor::OnNetworkConnected_w, | |
| 273 this, network_info)); | |
| 274 // Fire SignalNetworksChanged to update the list of networks. | |
| 275 OnNetworksChanged(); | |
| 276 } | |
| 277 | |
| 278 void AndroidNetworkMonitor::OnNetworkConnected_w( | |
| 279 const NetworkInformation& network_info) { | |
| 280 LOG(LS_INFO) << "Network connected: " << network_info.ToString(); | |
| 281 adapter_type_by_name_[network_info.interface_name] = | |
| 282 AdapterTypeFromNetworkType(network_info.type); | |
| 283 network_info_by_handle_[network_info.handle] = network_info; | |
| 284 for (const rtc::IPAddress& address : network_info.ip_addresses) { | |
| 285 network_handle_by_address_[address] = network_info.handle; | |
| 286 } | |
| 287 } | |
| 288 | |
| 289 void AndroidNetworkMonitor::OnNetworkDisconnected(NetworkHandle handle) { | |
| 290 LOG(LS_INFO) << "Network disconnected for handle " << handle; | |
| 291 worker_thread()->Invoke<void>( | |
| 292 RTC_FROM_HERE, | |
| 293 rtc::Bind(&AndroidNetworkMonitor::OnNetworkDisconnected_w, this, handle)); | |
| 294 } | |
| 295 | |
| 296 void AndroidNetworkMonitor::OnNetworkDisconnected_w(NetworkHandle handle) { | |
| 297 auto iter = network_info_by_handle_.find(handle); | |
| 298 if (iter != network_info_by_handle_.end()) { | |
| 299 for (const rtc::IPAddress& address : iter->second.ip_addresses) { | |
| 300 network_handle_by_address_.erase(address); | |
| 301 } | |
| 302 network_info_by_handle_.erase(iter); | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 void AndroidNetworkMonitor::SetNetworkInfos( | |
| 307 const std::vector<NetworkInformation>& network_infos) { | |
| 308 RTC_CHECK(thread_checker_.CalledOnValidThread()); | |
| 309 network_handle_by_address_.clear(); | |
| 310 network_info_by_handle_.clear(); | |
| 311 LOG(LS_INFO) << "Android network monitor found " << network_infos.size() | |
| 312 << " networks"; | |
| 313 for (NetworkInformation network : network_infos) { | |
| 314 OnNetworkConnected_w(network); | |
| 315 } | |
| 316 } | |
| 317 | |
| 318 rtc::AdapterType AndroidNetworkMonitor::GetAdapterType( | |
| 319 const std::string& if_name) { | |
| 320 auto iter = adapter_type_by_name_.find(if_name); | |
| 321 rtc::AdapterType type = (iter == adapter_type_by_name_.end()) | |
| 322 ? rtc::ADAPTER_TYPE_UNKNOWN | |
| 323 : iter->second; | |
| 324 if (type == rtc::ADAPTER_TYPE_UNKNOWN) { | |
| 325 LOG(LS_WARNING) << "Get an unknown type for the interface " << if_name; | |
| 326 } | |
| 327 return type; | |
| 328 } | |
| 329 | |
| 330 rtc::NetworkMonitorInterface* | |
| 331 AndroidNetworkMonitorFactory::CreateNetworkMonitor() { | |
| 332 return new AndroidNetworkMonitor(); | |
| 333 } | |
| 334 | |
| 335 JOW(void, NetworkMonitor_nativeNotifyConnectionTypeChanged)( | |
| 336 JNIEnv* jni, jobject j_monitor, jlong j_native_monitor) { | |
| 337 rtc::NetworkMonitorInterface* network_monitor = | |
| 338 reinterpret_cast<rtc::NetworkMonitorInterface*>(j_native_monitor); | |
| 339 network_monitor->OnNetworksChanged(); | |
| 340 } | |
| 341 | |
| 342 JOW(void, NetworkMonitor_nativeNotifyOfActiveNetworkList)( | |
| 343 JNIEnv* jni, jobject j_monitor, jlong j_native_monitor, | |
| 344 jobjectArray j_network_infos) { | |
| 345 AndroidNetworkMonitor* network_monitor = | |
| 346 reinterpret_cast<AndroidNetworkMonitor*>(j_native_monitor); | |
| 347 std::vector<NetworkInformation> network_infos; | |
| 348 size_t num_networks = jni->GetArrayLength(j_network_infos); | |
| 349 for (size_t i = 0; i < num_networks; ++i) { | |
| 350 jobject j_network_info = jni->GetObjectArrayElement(j_network_infos, i); | |
| 351 CHECK_EXCEPTION(jni) << "Error during GetObjectArrayElement"; | |
| 352 network_infos.push_back(GetNetworkInformationFromJava(jni, j_network_info)); | |
| 353 } | |
| 354 network_monitor->SetNetworkInfos(network_infos); | |
| 355 } | |
| 356 | |
| 357 JOW(void, NetworkMonitor_nativeNotifyOfNetworkConnect)( | |
| 358 JNIEnv* jni, jobject j_monitor, jlong j_native_monitor, | |
| 359 jobject j_network_info) { | |
| 360 AndroidNetworkMonitor* network_monitor = | |
| 361 reinterpret_cast<AndroidNetworkMonitor*>(j_native_monitor); | |
| 362 NetworkInformation network_info = | |
| 363 GetNetworkInformationFromJava(jni, j_network_info); | |
| 364 network_monitor->OnNetworkConnected(network_info); | |
| 365 } | |
| 366 | |
| 367 JOW(void, NetworkMonitor_nativeNotifyOfNetworkDisconnect)( | |
| 368 JNIEnv* jni, jobject j_monitor, jlong j_native_monitor, | |
| 369 jint network_handle) { | |
| 370 AndroidNetworkMonitor* network_monitor = | |
| 371 reinterpret_cast<AndroidNetworkMonitor*>(j_native_monitor); | |
| 372 network_monitor->OnNetworkDisconnected( | |
| 373 static_cast<NetworkHandle>(network_handle)); | |
| 374 } | |
| 375 | |
| 376 } // namespace webrtc_jni | |
| OLD | NEW |