Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: net/dns/address_sorter_win.cc

Issue 10442098: [net/dns] Resolve AF_UNSPEC on dual-stacked systems. Sort addresses according to RFC3484. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: updated needs_sort Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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,
mmenke 2012/08/14 15:56:12 nit: // AddressSorter:
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 scoped_ptr_malloc<SOCKET_ADDRESS_LIST> input_buffer_;
126 scoped_ptr_malloc<SOCKET_ADDRESS_LIST> output_buffer_;
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,
mmenke 2012/08/14 15:56:12 nit: // AddressSorter:
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698