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

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

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

Powered by Google App Engine
This is Rietveld 408576698