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 14 matching lines...) Expand all Loading... |
25 #include "net/base/ip_address.h" | 25 #include "net/base/ip_address.h" |
26 #include "net/base/ip_endpoint.h" | 26 #include "net/base/ip_endpoint.h" |
27 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
28 #include "net/base/network_activity_monitor.h" | 28 #include "net/base/network_activity_monitor.h" |
29 #include "net/base/sockaddr_storage.h" | 29 #include "net/base/sockaddr_storage.h" |
30 #include "net/log/net_log.h" | 30 #include "net/log/net_log.h" |
31 #include "net/socket/socket_descriptor.h" | 31 #include "net/socket/socket_descriptor.h" |
32 #include "net/udp/udp_net_log_parameters.h" | 32 #include "net/udp/udp_net_log_parameters.h" |
33 | 33 |
34 #if defined(OS_ANDROID) | 34 #if defined(OS_ANDROID) |
| 35 #include <dlfcn.h> |
| 36 // This was added in Lollipop to dlfcn.h |
| 37 #define RTLD_NOLOAD 4 |
35 #include "base/android/build_info.h" | 38 #include "base/android/build_info.h" |
36 #include "base/native_library.h" | 39 #include "base/native_library.h" |
37 #include "base/strings/utf_string_conversions.h" | 40 #include "base/strings/utf_string_conversions.h" |
38 #endif | 41 #endif |
39 | 42 |
40 namespace net { | 43 namespace net { |
41 | 44 |
42 namespace { | 45 namespace { |
43 | 46 |
44 const int kBindRetries = 10; | 47 const int kBindRetries = 10; |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 return ERR_NOT_IMPLEMENTED; | 347 return ERR_NOT_IMPLEMENTED; |
345 } | 348 } |
346 // NOTE(pauljensen): This does rely on Android implementation details, but | 349 // NOTE(pauljensen): This does rely on Android implementation details, but |
347 // these details are unlikely to change. | 350 // these details are unlikely to change. |
348 typedef int (*SetNetworkForSocket)(unsigned netId, int socketFd); | 351 typedef int (*SetNetworkForSocket)(unsigned netId, int socketFd); |
349 static SetNetworkForSocket setNetworkForSocket; | 352 static SetNetworkForSocket setNetworkForSocket; |
350 // This is racy, but all racers should come out with the same answer so it | 353 // This is racy, but all racers should come out with the same answer so it |
351 // shouldn't matter. | 354 // shouldn't matter. |
352 if (!setNetworkForSocket) { | 355 if (!setNetworkForSocket) { |
353 // Android's netd client library should always be loaded in our address | 356 // Android's netd client library should always be loaded in our address |
354 // space as it shims libc functions like connect(). | 357 // space as it shims socket() which was used to create |socket_|. |
355 base::FilePath file(base::GetNativeLibraryName("netd_client")); | 358 base::FilePath file(base::GetNativeLibraryName("netd_client")); |
356 base::NativeLibrary lib = base::LoadNativeLibrary(file, nullptr); | 359 // Use RTLD_NOW to match Android's prior loading of the library: |
357 setNetworkForSocket = reinterpret_cast<SetNetworkForSocket>( | 360 // http://androidxref.com/6.0.0_r5/xref/bionic/libc/bionic/NetdClient.cpp#37 |
358 base::GetFunctionPointerFromNativeLibrary(lib, "setNetworkForSocket")); | 361 // Use RTLD_NOLOAD to assert that the library is already loaded and |
| 362 // avoid doing any disk IO. |
| 363 void* dl = dlopen(file.value().c_str(), RTLD_NOW | RTLD_NOLOAD); |
| 364 setNetworkForSocket = |
| 365 reinterpret_cast<SetNetworkForSocket>(dlsym(dl, "setNetworkForSocket")); |
359 } | 366 } |
360 if (!setNetworkForSocket) | 367 if (!setNetworkForSocket) |
361 return ERR_NOT_IMPLEMENTED; | 368 return ERR_NOT_IMPLEMENTED; |
362 int rv = setNetworkForSocket(network, socket_); | 369 int rv = setNetworkForSocket(network, socket_); |
363 // If |network| has since disconnected, |rv| will be ENONET. Surface this as | 370 // If |network| has since disconnected, |rv| will be ENONET. Surface this as |
364 // ERR_NETWORK_CHANGED, rather than MapSystemError(ENONET) which gives back | 371 // ERR_NETWORK_CHANGED, rather than MapSystemError(ENONET) which gives back |
365 // the less descriptive ERR_FAILED. | 372 // the less descriptive ERR_FAILED. |
366 if (rv == ENONET) | 373 if (rv == ENONET) |
367 return ERR_NETWORK_CHANGED; | 374 return ERR_NETWORK_CHANGED; |
368 if (rv == 0) | 375 if (rv == 0) |
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
806 return MapSystemError(errno); | 813 return MapSystemError(errno); |
807 | 814 |
808 return OK; | 815 return OK; |
809 } | 816 } |
810 | 817 |
811 void UDPSocketPosix::DetachFromThread() { | 818 void UDPSocketPosix::DetachFromThread() { |
812 base::NonThreadSafe::DetachFromThread(); | 819 base::NonThreadSafe::DetachFromThread(); |
813 } | 820 } |
814 | 821 |
815 } // namespace net | 822 } // namespace net |
OLD | NEW |