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

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

Powered by Google App Engine
This is Rietveld 408576698