Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/udp/udp_socket_posix.h" | 5 #include "net/udp/udp_socket_posix.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <net/if.h> | 9 #include <net/if.h> |
| 10 #include <netdb.h> | 10 #include <netdb.h> |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 339 DCHECK(!is_connected()); | 339 DCHECK(!is_connected()); |
| 340 if (network == NetworkChangeNotifier::kInvalidNetworkHandle) | 340 if (network == NetworkChangeNotifier::kInvalidNetworkHandle) |
| 341 return ERR_INVALID_ARGUMENT; | 341 return ERR_INVALID_ARGUMENT; |
| 342 #if defined(OS_ANDROID) | 342 #if defined(OS_ANDROID) |
| 343 // Android prior to Lollipop didn't have support for binding sockets to | 343 // Android prior to Lollipop didn't have support for binding sockets to |
| 344 // networks. | 344 // networks. |
| 345 if (base::android::BuildInfo::GetInstance()->sdk_int() < | 345 if (base::android::BuildInfo::GetInstance()->sdk_int() < |
| 346 base::android::SDK_VERSION_LOLLIPOP) { | 346 base::android::SDK_VERSION_LOLLIPOP) { |
| 347 return ERR_NOT_IMPLEMENTED; | 347 return ERR_NOT_IMPLEMENTED; |
| 348 } | 348 } |
| 349 // NOTE(pauljensen): This does rely on Android implementation details, but | 349 int rv; |
| 350 // these details are unlikely to change. | 350 // On Android M and newer releases use supported NDK API. On Android L use |
| 351 typedef int (*SetNetworkForSocket)(unsigned netId, int socketFd); | 351 // setNetworkForSocket from libnetd_client.so. |
| 352 static SetNetworkForSocket setNetworkForSocket; | 352 if (base::android::BuildInfo::GetInstance()->sdk_int() >= |
| 353 // This is racy, but all racers should come out with the same answer so it | 353 base::android::SDK_VERSION_MARSHMALLOW) { |
| 354 // shouldn't matter. | 354 // See declaration of android_setsocknetwork() here: |
| 355 if (!setNetworkForSocket) { | 355 // http://androidxref.com/6.0.0_r1/xref/development/ndk/platforms/android-M/ include/android/multinetwork.h#65 |
| 356 // Android's netd client library should always be loaded in our address | 356 // Function cannot be called directly as it will cause app to fail to load |
| 357 // space as it shims socket() which was used to create |socket_|. | 357 // on pre-marshmallow devices. |
| 358 base::FilePath file(base::GetNativeLibraryName("netd_client")); | 358 typedef int (*MarshmallowSetNetworkForSocket)(int64_t netId, int socketFd); |
| 359 // Use RTLD_NOW to match Android's prior loading of the library: | 359 static MarshmallowSetNetworkForSocket marshmallowSetNetworkForSocket; |
| 360 // http://androidxref.com/6.0.0_r5/xref/bionic/libc/bionic/NetdClient.cpp#37 | 360 // This is racy, but all racers should come out with the same answer so it |
|
xunjieli
2016/07/20 17:55:49
One last question: why is this racy? If all socket
pauljensen
2016/07/20 18:02:16
There are multiple network threads but only one st
xunjieli
2016/07/20 18:08:03
Ah, i see. I always forget Cronet can have multipl
| |
| 361 // Use RTLD_NOLOAD to assert that the library is already loaded and | 361 // shouldn't matter. |
| 362 // avoid doing any disk IO. | 362 if (!marshmallowSetNetworkForSocket) { |
| 363 void* dl = dlopen(file.value().c_str(), RTLD_NOW | RTLD_NOLOAD); | 363 base::FilePath file(base::GetNativeLibraryName("android")); |
| 364 setNetworkForSocket = | 364 void* dl = dlopen(file.value().c_str(), RTLD_NOW); |
| 365 reinterpret_cast<SetNetworkForSocket>(dlsym(dl, "setNetworkForSocket")); | 365 marshmallowSetNetworkForSocket = |
| 366 reinterpret_cast<MarshmallowSetNetworkForSocket>( | |
| 367 dlsym(dl, "android_setsocknetwork")); | |
| 368 } | |
| 369 if (!marshmallowSetNetworkForSocket) | |
| 370 return ERR_NOT_IMPLEMENTED; | |
| 371 rv = marshmallowSetNetworkForSocket(network, socket_); | |
| 372 if (rv) | |
| 373 rv = errno; | |
| 374 } else { | |
| 375 // NOTE(pauljensen): This does rely on Android implementation details, but | |
| 376 // they won't change because Lollipop is already released. | |
| 377 typedef int (*LollipopSetNetworkForSocket)(unsigned netId, int socketFd); | |
| 378 static LollipopSetNetworkForSocket lollipopSetNetworkForSocket; | |
| 379 // This is racy, but all racers should come out with the same answer so it | |
| 380 // shouldn't matter. | |
| 381 if (!lollipopSetNetworkForSocket) { | |
| 382 // Android's netd client library should always be loaded in our address | |
| 383 // space as it shims socket() which was used to create |socket_|. | |
| 384 base::FilePath file(base::GetNativeLibraryName("netd_client")); | |
| 385 // Use RTLD_NOW to match Android's prior loading of the library: | |
| 386 // http://androidxref.com/6.0.0_r5/xref/bionic/libc/bionic/NetdClient.cpp# 37 | |
| 387 // Use RTLD_NOLOAD to assert that the library is already loaded and | |
| 388 // avoid doing any disk IO. | |
| 389 void* dl = dlopen(file.value().c_str(), RTLD_NOW | RTLD_NOLOAD); | |
| 390 lollipopSetNetworkForSocket = | |
| 391 reinterpret_cast<LollipopSetNetworkForSocket>( | |
| 392 dlsym(dl, "setNetworkForSocket")); | |
| 393 } | |
| 394 if (!lollipopSetNetworkForSocket) | |
| 395 return ERR_NOT_IMPLEMENTED; | |
| 396 rv = -lollipopSetNetworkForSocket(network, socket_); | |
| 366 } | 397 } |
| 367 if (!setNetworkForSocket) | |
| 368 return ERR_NOT_IMPLEMENTED; | |
| 369 int rv = setNetworkForSocket(network, socket_); | |
| 370 // If |network| has since disconnected, |rv| will be ENONET. Surface this as | 398 // If |network| has since disconnected, |rv| will be ENONET. Surface this as |
| 371 // ERR_NETWORK_CHANGED, rather than MapSystemError(ENONET) which gives back | 399 // ERR_NETWORK_CHANGED, rather than MapSystemError(ENONET) which gives back |
| 372 // the less descriptive ERR_FAILED. | 400 // the less descriptive ERR_FAILED. |
| 373 if (rv == ENONET) | 401 if (rv == ENONET) |
| 374 return ERR_NETWORK_CHANGED; | 402 return ERR_NETWORK_CHANGED; |
| 375 if (rv == 0) | 403 if (rv == 0) |
| 376 bound_network_ = network; | 404 bound_network_ = network; |
| 377 return MapSystemError(rv); | 405 return MapSystemError(rv); |
| 378 #else | 406 #else |
| 379 NOTIMPLEMENTED(); | 407 NOTIMPLEMENTED(); |
| (...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 813 return MapSystemError(errno); | 841 return MapSystemError(errno); |
| 814 | 842 |
| 815 return OK; | 843 return OK; |
| 816 } | 844 } |
| 817 | 845 |
| 818 void UDPSocketPosix::DetachFromThread() { | 846 void UDPSocketPosix::DetachFromThread() { |
| 819 base::NonThreadSafe::DetachFromThread(); | 847 base::NonThreadSafe::DetachFromThread(); |
| 820 } | 848 } |
| 821 | 849 |
| 822 } // namespace net | 850 } // namespace net |
| OLD | NEW |