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

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: Minor edits. Fix one typo in variable name. 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/logging.h"
12 #include "base/win/windows_version.h"
13 #include "net/base/address_list.h"
14 #include "net/base/ip_endpoint.h"
15 #include "net/base/winsock_init.h"
16
17 namespace net {
18
19 namespace {
20
21 // If |ipe| has IPv4-mapped IPv6 address, converts it to IPv4.
mmenke 2012/07/30 15:37:00 nit: "and leaves all other addresses unmodified."
22 void ConvertIPv4MappedToIPv4(IPEndPoint* ipe) {
mmenke 2012/07/30 15:37:00 May want to put this in net_util.cc. I don't see
23 DCHECK_EQ(AF_INET6, ipe->GetFamily());
24 const IPAddressNumber& address = ipe->address();
25 const unsigned char prefix[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF };
26 if (std::equal(address.begin(), address.begin() + arraysize(prefix),
27 prefix)) {
28 *ipe = IPEndPoint(
29 IPAddressNumber(address.begin() + arraysize(prefix), address.end()),
30 ipe->port());
31 }
32 }
33
34 class AddressSorterWin : public AddressSorter {
35 public:
36 AddressSorterWin() : socket_(INVALID_SOCKET) {
37 EnsureWinsockInit();
38 socket_ = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
39 DCHECK_NE(INVALID_SOCKET, socket_);
40 }
41
42 virtual ~AddressSorterWin() {
43 closesocket(socket_);
44 }
45
46 virtual bool Sort(AddressList* list) const OVERRIDE {
47 size_t heap_size = sizeof(SOCKET_ADDRESS_LIST) +
48 list->size() * (sizeof(SOCKET_ADDRESS) + sizeof(SOCKADDR_STORAGE));
49 scoped_ptr_malloc<SOCKET_ADDRESS_LIST> heap_in(
50 reinterpret_cast<SOCKET_ADDRESS_LIST*>(malloc(heap_size)));
51
52 SOCKET_ADDRESS_LIST* sort_list = heap_in.get();
53 sort_list->iAddressCount = list->size();
54 SOCKADDR_STORAGE* storage = reinterpret_cast<SOCKADDR_STORAGE*>(
55 sort_list->Address + sort_list->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 sort_list->Address[i].lpSockaddr = addr;
70 sort_list->Address[i].iSockaddrLength = addr_len;
71 }
72
73 DWORD result_size = 0;
74 int result = WSAIoctl(socket_, SIO_ADDRESS_LIST_SORT, sort_list, heap_size,
cbentzel 2012/07/30 15:53:43 Could this be blocking the IO thread?
szym 2012/07/30 16:28:45 Yes, but no more than GetAdaptersAddresses. The wo
mmenke1 2012/07/30 17:19:06 If I recall correctly, eroman has said previously
75 sort_list, heap_size, &result_size, NULL, NULL);
mmenke 2012/07/30 15:37:00 Wonder if this ever eats addresses depending on lo
szym 2012/07/30 16:28:45 I'll need to test it, but either way I wouldn't te
76 if (result == SOCKET_ERROR) {
77 LOG(ERROR) << "SIO_ADDRESS_LIST_SORT failed" << WSAGetLastError();
78 return false; // Unsorted.
79 }
80 list->clear();
81 for (int i = 0; i < sort_list->iAddressCount; ++i) {
82 IPEndPoint ipe;
83 ipe.FromSockAddr(sort_list->Address[i].lpSockaddr,
84 sort_list->Address[i].iSockaddrLength);
85 // Unmap V4MAPPED IPv6 addresses so that Happy Eyeballs works properly.
86 ConvertIPv4MappedToIPv4(&ipe);
87 list->push_back(ipe);
88 }
89 return true;
90 }
91
92 private:
93 // The socket to run WSAIoctl on.
94 SOCKET socket_;
95
96 DISALLOW_COPY_AND_ASSIGN(AddressSorterWin);
97 };
98
99 // Wrapper for AddressSorterWin which does not sort IPv4 or IPv4-mapped
100 // addresses but always puts them at the end of the list. Needed because the
101 // SIO_ADDRESS_LIST_SORT does not support IPv4 addresses on Windows XP.
102 class AddressSorterWinXP : public AddressSorter {
103 public:
104 AddressSorterWinXP() {}
105 virtual ~AddressSorterWinXP() {}
106
107 virtual bool Sort(AddressList* list) const OVERRIDE {
108 AddressList list_ipv4;
109 AddressList list_ipv6;
110 for (size_t i = 0; i < list->size(); ++i) {
111 IPEndPoint ipe = (*list)[i];
112 if (ipe.GetFamily() == AF_INET6)
113 ConvertIPv4MappedToIPv4(&ipe);
114 if (ipe.GetFamily() == AF_INET) {
115 list_ipv4.push_back(ipe);
116 } else {
117 list_ipv6.push_back(ipe);
118 }
119 }
120 if (!list_ipv6.empty() && !sorter_.Sort(&list_ipv6))
121 return false;
122 list->clear();
123 list->insert(list->end(), list_ipv6.begin(), list_ipv6.end());
124 list->insert(list->end(), list_ipv4.begin(), list_ipv4.end());
125 return true;
126 }
127
128 private:
129 AddressSorterWin sorter_;
130
131 DISALLOW_COPY_AND_ASSIGN(AddressSorterWinXP);
132 };
133
134 } // namespace
135
136 // static
137 scoped_ptr<AddressSorter> AddressSorter::CreateAddressSorter() {
138 if (base::win::GetVersion() < base::win::VERSION_VISTA)
139 return scoped_ptr<AddressSorter>(new AddressSorterWinXP());
140 return scoped_ptr<AddressSorter>(new AddressSorterWin());
141 }
142
143 } // namespace net
144
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698