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/dns/address_sorter.h" | 5 #include "net/dns/address_sorter.h" |
6 | 6 |
7 #include <winsock2.h> | 7 #include <winsock2.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/threading/worker_pool.h" | 14 #include "base/threading/worker_pool.h" |
15 #include "base/win/windows_version.h" | 15 #include "base/win/windows_version.h" |
16 #include "net/base/address_list.h" | 16 #include "net/base/address_list.h" |
17 #include "net/base/ip_endpoint.h" | 17 #include "net/base/ip_endpoint.h" |
18 #include "net/base/winsock_init.h" | 18 #include "net/base/winsock_init.h" |
19 | 19 |
20 namespace net { | 20 namespace net { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 class AddressSorterWin : public AddressSorter { | 24 class AddressSorterWin : public AddressSorter { |
25 public: | 25 public: |
26 AddressSorterWin() { | 26 AddressSorterWin() { EnsureWinsockInit(); } |
27 EnsureWinsockInit(); | |
28 } | |
29 | 27 |
30 virtual ~AddressSorterWin() {} | 28 virtual ~AddressSorterWin() {} |
31 | 29 |
32 // AddressSorter: | 30 // AddressSorter: |
33 virtual void Sort(const AddressList& list, | 31 virtual void Sort(const AddressList& list, |
34 const CallbackType& callback) const OVERRIDE { | 32 const CallbackType& callback) const OVERRIDE { |
35 DCHECK(!list.empty()); | 33 DCHECK(!list.empty()); |
36 scoped_refptr<Job> job = new Job(list, callback); | 34 scoped_refptr<Job> job = new Job(list, callback); |
37 } | 35 } |
38 | 36 |
39 private: | 37 private: |
40 // Executes the SIO_ADDRESS_LIST_SORT ioctl on the WorkerPool, and | 38 // Executes the SIO_ADDRESS_LIST_SORT ioctl on the WorkerPool, and |
41 // performs the necessary conversions to/from AddressList. | 39 // performs the necessary conversions to/from AddressList. |
42 class Job : public base::RefCountedThreadSafe<Job> { | 40 class Job : public base::RefCountedThreadSafe<Job> { |
43 public: | 41 public: |
44 Job(const AddressList& list, const CallbackType& callback) | 42 Job(const AddressList& list, const CallbackType& callback) |
45 : callback_(callback), | 43 : callback_(callback), |
46 buffer_size_(sizeof(SOCKET_ADDRESS_LIST) + | 44 buffer_size_(sizeof(SOCKET_ADDRESS_LIST) + |
47 list.size() * (sizeof(SOCKET_ADDRESS) + | 45 list.size() * |
48 sizeof(SOCKADDR_STORAGE))), | 46 (sizeof(SOCKET_ADDRESS) + sizeof(SOCKADDR_STORAGE))), |
49 input_buffer_(reinterpret_cast<SOCKET_ADDRESS_LIST*>( | 47 input_buffer_( |
50 malloc(buffer_size_))), | 48 reinterpret_cast<SOCKET_ADDRESS_LIST*>(malloc(buffer_size_))), |
51 output_buffer_(reinterpret_cast<SOCKET_ADDRESS_LIST*>( | 49 output_buffer_( |
52 malloc(buffer_size_))), | 50 reinterpret_cast<SOCKET_ADDRESS_LIST*>(malloc(buffer_size_))), |
53 success_(false) { | 51 success_(false) { |
54 input_buffer_->iAddressCount = list.size(); | 52 input_buffer_->iAddressCount = list.size(); |
55 SOCKADDR_STORAGE* storage = reinterpret_cast<SOCKADDR_STORAGE*>( | 53 SOCKADDR_STORAGE* storage = reinterpret_cast<SOCKADDR_STORAGE*>( |
56 input_buffer_->Address + input_buffer_->iAddressCount); | 54 input_buffer_->Address + input_buffer_->iAddressCount); |
57 | 55 |
58 for (size_t i = 0; i < list.size(); ++i) { | 56 for (size_t i = 0; i < list.size(); ++i) { |
59 IPEndPoint ipe = list[i]; | 57 IPEndPoint ipe = list[i]; |
60 // Addresses must be sockaddr_in6. | 58 // Addresses must be sockaddr_in6. |
61 if (ipe.GetFamily() == ADDRESS_FAMILY_IPV4) { | 59 if (ipe.GetFamily() == ADDRESS_FAMILY_IPV4) { |
62 ipe = IPEndPoint(ConvertIPv4NumberToIPv6Number(ipe.address()), | 60 ipe = IPEndPoint(ConvertIPv4NumberToIPv6Number(ipe.address()), |
63 ipe.port()); | 61 ipe.port()); |
64 } | 62 } |
65 | 63 |
66 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(storage + i); | 64 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(storage + i); |
67 socklen_t addr_len = sizeof(SOCKADDR_STORAGE); | 65 socklen_t addr_len = sizeof(SOCKADDR_STORAGE); |
68 bool result = ipe.ToSockAddr(addr, &addr_len); | 66 bool result = ipe.ToSockAddr(addr, &addr_len); |
69 DCHECK(result); | 67 DCHECK(result); |
70 input_buffer_->Address[i].lpSockaddr = addr; | 68 input_buffer_->Address[i].lpSockaddr = addr; |
71 input_buffer_->Address[i].iSockaddrLength = addr_len; | 69 input_buffer_->Address[i].iSockaddrLength = addr_len; |
72 } | 70 } |
73 | 71 |
74 if (!base::WorkerPool::PostTaskAndReply( | 72 if (!base::WorkerPool::PostTaskAndReply( |
75 FROM_HERE, | 73 FROM_HERE, |
76 base::Bind(&Job::Run, this), | 74 base::Bind(&Job::Run, this), |
77 base::Bind(&Job::OnComplete, this), | 75 base::Bind(&Job::OnComplete, this), |
78 false /* task is slow */)) { | 76 false /* task is slow */)) { |
79 LOG(ERROR) << "WorkerPool::PostTaskAndReply failed"; | 77 LOG(ERROR) << "WorkerPool::PostTaskAndReply failed"; |
80 OnComplete(); | 78 OnComplete(); |
81 } | 79 } |
82 } | 80 } |
83 | 81 |
84 private: | 82 private: |
85 friend class base::RefCountedThreadSafe<Job>; | 83 friend class base::RefCountedThreadSafe<Job>; |
86 ~Job() {} | 84 ~Job() {} |
87 | 85 |
88 // Executed on the WorkerPool. | 86 // Executed on the WorkerPool. |
89 void Run() { | 87 void Run() { |
90 SOCKET sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); | 88 SOCKET sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); |
91 if (sock == INVALID_SOCKET) | 89 if (sock == INVALID_SOCKET) |
92 return; | 90 return; |
93 DWORD result_size = 0; | 91 DWORD result_size = 0; |
94 int result = WSAIoctl(sock, SIO_ADDRESS_LIST_SORT, input_buffer_.get(), | 92 int result = WSAIoctl(sock, |
95 buffer_size_, output_buffer_.get(), buffer_size_, | 93 SIO_ADDRESS_LIST_SORT, |
96 &result_size, NULL, NULL); | 94 input_buffer_.get(), |
| 95 buffer_size_, |
| 96 output_buffer_.get(), |
| 97 buffer_size_, |
| 98 &result_size, |
| 99 NULL, |
| 100 NULL); |
97 if (result == SOCKET_ERROR) { | 101 if (result == SOCKET_ERROR) { |
98 LOG(ERROR) << "SIO_ADDRESS_LIST_SORT failed " << WSAGetLastError(); | 102 LOG(ERROR) << "SIO_ADDRESS_LIST_SORT failed " << WSAGetLastError(); |
99 } else { | 103 } else { |
100 success_ = true; | 104 success_ = true; |
101 } | 105 } |
102 closesocket(sock); | 106 closesocket(sock); |
103 } | 107 } |
104 | 108 |
105 // Executed on the calling thread. | 109 // Executed on the calling thread. |
106 void OnComplete() { | 110 void OnComplete() { |
107 AddressList list; | 111 AddressList list; |
108 if (success_) { | 112 if (success_) { |
109 list.reserve(output_buffer_->iAddressCount); | 113 list.reserve(output_buffer_->iAddressCount); |
110 for (int i = 0; i < output_buffer_->iAddressCount; ++i) { | 114 for (int i = 0; i < output_buffer_->iAddressCount; ++i) { |
111 IPEndPoint ipe; | 115 IPEndPoint ipe; |
112 ipe.FromSockAddr(output_buffer_->Address[i].lpSockaddr, | 116 ipe.FromSockAddr(output_buffer_->Address[i].lpSockaddr, |
113 output_buffer_->Address[i].iSockaddrLength); | 117 output_buffer_->Address[i].iSockaddrLength); |
114 // Unmap V4MAPPED IPv6 addresses so that Happy Eyeballs works. | 118 // Unmap V4MAPPED IPv6 addresses so that Happy Eyeballs works. |
115 if (IsIPv4Mapped(ipe.address())) { | 119 if (IsIPv4Mapped(ipe.address())) { |
116 ipe = IPEndPoint(ConvertIPv4MappedToIPv4(ipe.address()), | 120 ipe = |
117 ipe.port()); | 121 IPEndPoint(ConvertIPv4MappedToIPv4(ipe.address()), ipe.port()); |
118 } | 122 } |
119 list.push_back(ipe); | 123 list.push_back(ipe); |
120 } | 124 } |
121 } | 125 } |
122 callback_.Run(success_, list); | 126 callback_.Run(success_, list); |
123 } | 127 } |
124 | 128 |
125 const CallbackType callback_; | 129 const CallbackType callback_; |
126 const size_t buffer_size_; | 130 const size_t buffer_size_; |
127 scoped_ptr<SOCKET_ADDRESS_LIST, base::FreeDeleter> input_buffer_; | 131 scoped_ptr<SOCKET_ADDRESS_LIST, base::FreeDeleter> input_buffer_; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 } // namespace | 192 } // namespace |
189 | 193 |
190 // static | 194 // static |
191 scoped_ptr<AddressSorter> AddressSorter::CreateAddressSorter() { | 195 scoped_ptr<AddressSorter> AddressSorter::CreateAddressSorter() { |
192 if (base::win::GetVersion() < base::win::VERSION_VISTA) | 196 if (base::win::GetVersion() < base::win::VERSION_VISTA) |
193 return scoped_ptr<AddressSorter>(new AddressSorterWinXP()); | 197 return scoped_ptr<AddressSorter>(new AddressSorterWinXP()); |
194 return scoped_ptr<AddressSorter>(new AddressSorterWin()); | 198 return scoped_ptr<AddressSorter>(new AddressSorterWin()); |
195 } | 199 } |
196 | 200 |
197 } // namespace net | 201 } // namespace net |
198 | |
OLD | NEW |