OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/dns/address_sorter.h" | |
6 | |
7 #include <winsock2.h> | |
8 | |
9 #include <algorithm> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/location.h" | |
13 #include "base/logging.h" | |
14 #include "base/threading/worker_pool.h" | |
15 #include "base/win/windows_version.h" | |
16 #include "net/base/address_list.h" | |
17 #include "net/base/ip_endpoint.h" | |
18 #include "net/base/winsock_init.h" | |
19 | |
20 namespace net { | |
21 | |
22 namespace { | |
23 | |
24 class AddressSorterWin : public AddressSorter { | |
25 public: | |
26 AddressSorterWin() { | |
27 EnsureWinsockInit(); | |
28 } | |
29 | |
30 virtual ~AddressSorterWin() {} | |
31 | |
32 virtual void Sort(const AddressList& list, | |
33 const CallbackType& callback) const OVERRIDE { | |
34 DCHECK(!list.empty()); | |
35 scoped_refptr<Job> job = new Job(list, callback); | |
36 } | |
37 | |
38 private: | |
39 // Executes the SIO_ADDRESS_LIST_SORT ioctl on the WorkerPool, and | |
40 // performs the necessary conversions to/from AddressList. | |
41 class Job : public base::RefCountedThreadSafe<Job> { | |
42 public: | |
43 Job(const AddressList& list, const CallbackType& callback) | |
44 : callback_(callback), | |
45 buffer_size_(sizeof(SOCKET_ADDRESS_LIST) + | |
46 list.size() * (sizeof(SOCKET_ADDRESS) + | |
47 sizeof(SOCKADDR_STORAGE))), | |
48 input_buffer_(reinterpret_cast<SOCKET_ADDRESS_LIST*>( | |
49 malloc(buffer_size_))), | |
50 output_buffer_(reinterpret_cast<SOCKET_ADDRESS_LIST*>( | |
51 malloc(buffer_size_))), | |
52 success_(false) { | |
53 input_buffer_->iAddressCount = list.size(); | |
54 SOCKADDR_STORAGE* storage = reinterpret_cast<SOCKADDR_STORAGE*>( | |
55 input_buffer_->Address + input_buffer_->iAddressCount); | |
56 | |
57 for (size_t i = 0; i < list.size(); ++i) { | |
58 IPEndPoint ipe = list[i]; | |
59 // Addresses must be sockaddr_in6. | |
60 if (ipe.GetFamily() == AF_INET) { | |
61 ipe = IPEndPoint(ConvertIPv4NumberToIPv6Number(ipe.address()), | |
62 ipe.port()); | |
63 } | |
64 | |
65 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(storage + i); | |
66 socklen_t addr_len = sizeof(SOCKADDR_STORAGE); | |
67 bool result = ipe.ToSockAddr(addr, &addr_len); | |
68 DCHECK(result); | |
69 input_buffer_->Address[i].lpSockaddr = addr; | |
70 input_buffer_->Address[i].iSockaddrLength = addr_len; | |
71 } | |
72 | |
73 if (!base::WorkerPool::PostTaskAndReply( | |
74 FROM_HERE, | |
75 base::Bind(&Job::Run, this), | |
76 base::Bind(&Job::OnComplete, this), | |
77 false /* task is slow */)) { | |
78 LOG(ERROR) << "WorkerPool::PostTaskAndReply failed"; | |
79 OnComplete(); | |
80 } | |
81 } | |
82 | |
83 private: | |
84 friend class base::RefCountedThreadSafe<Job>; | |
85 ~Job() {} | |
86 | |
87 // Executed on the WorkerPool. | |
88 void Run() { | |
89 SOCKET sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); | |
90 DCHECK_NE(INVALID_SOCKET, sock); | |
91 DWORD result_size = 0; | |
92 int result = WSAIoctl(sock, SIO_ADDRESS_LIST_SORT, input_buffer_.get(), | |
93 buffer_size_, output_buffer_.get(), buffer_size_, | |
94 &result_size, NULL, NULL); | |
95 if (result == SOCKET_ERROR) { | |
96 LOG(ERROR) << "SIO_ADDRESS_LIST_SORT failed " << WSAGetLastError(); | |
97 } else { | |
98 success_ = true; | |
99 } | |
100 closesocket(sock); | |
101 } | |
102 | |
103 // Executed on the calling thread. | |
104 void OnComplete() { | |
105 AddressList list; | |
106 if (success_) { | |
107 list.reserve(output_buffer_->iAddressCount); | |
108 for (int i = 0; i < output_buffer_->iAddressCount; ++i) { | |
109 IPEndPoint ipe; | |
110 ipe.FromSockAddr(output_buffer_->Address[i].lpSockaddr, | |
111 output_buffer_->Address[i].iSockaddrLength); | |
112 // Unmap V4MAPPED IPv6 addresses so that Happy Eyeballs works. | |
113 if (IsIPv4Mapped(ipe.address())) { | |
114 ipe = IPEndPoint(ConvertIPv4MappedToIPv4(ipe.address()), | |
115 ipe.port()); | |
116 } | |
117 list.push_back(ipe); | |
118 } | |
119 } | |
120 callback_.Run(success_, list); | |
121 } | |
122 | |
123 const CallbackType callback_; | |
124 const size_t buffer_size_; | |
125 const scoped_ptr_malloc<SOCKET_ADDRESS_LIST> input_buffer_; | |
126 const scoped_ptr_malloc<SOCKET_ADDRESS_LIST> output_buffer_; | |
mmenke
2012/08/13 20:11:50
Think having input_buffer_ and output_buffer_ cons
szym
2012/08/13 20:28:26
I definitely don't want to make Run const. This is
| |
127 bool success_; | |
128 | |
129 DISALLOW_COPY_AND_ASSIGN(Job); | |
130 }; | |
131 | |
132 DISALLOW_COPY_AND_ASSIGN(AddressSorterWin); | |
133 }; | |
134 | |
135 // Merges |list_ipv4| and |list_ipv6| before passing it to |callback|, but | |
136 // only if |success| is true. | |
137 void MergeResults(const AddressSorter::CallbackType& callback, | |
138 const AddressList& list_ipv4, | |
139 bool success, | |
140 const AddressList& list_ipv6) { | |
141 if (!success) { | |
142 callback.Run(false, AddressList()); | |
143 return; | |
144 } | |
145 AddressList list; | |
146 list.insert(list.end(), list_ipv6.begin(), list_ipv6.end()); | |
147 list.insert(list.end(), list_ipv4.begin(), list_ipv4.end()); | |
148 callback.Run(true, list); | |
149 } | |
150 | |
151 // Wrapper for AddressSorterWin which does not sort IPv4 or IPv4-mapped | |
152 // addresses but always puts them at the end of the list. Needed because the | |
153 // SIO_ADDRESS_LIST_SORT does not support IPv4 addresses on Windows XP. | |
154 class AddressSorterWinXP : public AddressSorter { | |
155 public: | |
156 AddressSorterWinXP() {} | |
157 virtual ~AddressSorterWinXP() {} | |
158 | |
159 virtual void Sort(const AddressList& list, | |
160 const CallbackType& callback) const OVERRIDE { | |
161 AddressList list_ipv4; | |
162 AddressList list_ipv6; | |
163 for (size_t i = 0; i < list.size(); ++i) { | |
164 const IPEndPoint& ipe = list[i]; | |
165 if (ipe.GetFamily() == AF_INET) { | |
166 list_ipv4.push_back(ipe); | |
167 } else { | |
168 list_ipv6.push_back(ipe); | |
169 } | |
170 } | |
171 if (!list_ipv6.empty()) { | |
172 sorter_.Sort(list_ipv6, base::Bind(&MergeResults, callback, list_ipv4)); | |
173 } else { | |
174 NOTREACHED() << "Should not be called with IPv4-only addresses."; | |
175 callback.Run(true, list); | |
176 } | |
177 } | |
178 | |
179 private: | |
180 AddressSorterWin sorter_; | |
181 | |
182 DISALLOW_COPY_AND_ASSIGN(AddressSorterWinXP); | |
183 }; | |
184 | |
185 } // namespace | |
186 | |
187 // static | |
188 scoped_ptr<AddressSorter> AddressSorter::CreateAddressSorter() { | |
189 if (base::win::GetVersion() < base::win::VERSION_VISTA) | |
190 return scoped_ptr<AddressSorter>(new AddressSorterWinXP()); | |
191 return scoped_ptr<AddressSorter>(new AddressSorterWin()); | |
192 } | |
193 | |
194 } // namespace net | |
195 | |
OLD | NEW |