| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/dns/address_sorter.h" | 5 #include "net/dns/address_sorter.h" |
| 6 | 6 |
| 7 #include <winsock2.h> | 7 #include <winsock2.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 std::unique_ptr<SOCKET_ADDRESS_LIST, base::FreeDeleter> input_buffer_; | 133 std::unique_ptr<SOCKET_ADDRESS_LIST, base::FreeDeleter> input_buffer_; |
| 134 std::unique_ptr<SOCKET_ADDRESS_LIST, base::FreeDeleter> output_buffer_; | 134 std::unique_ptr<SOCKET_ADDRESS_LIST, base::FreeDeleter> output_buffer_; |
| 135 bool success_; | 135 bool success_; |
| 136 | 136 |
| 137 DISALLOW_COPY_AND_ASSIGN(Job); | 137 DISALLOW_COPY_AND_ASSIGN(Job); |
| 138 }; | 138 }; |
| 139 | 139 |
| 140 DISALLOW_COPY_AND_ASSIGN(AddressSorterWin); | 140 DISALLOW_COPY_AND_ASSIGN(AddressSorterWin); |
| 141 }; | 141 }; |
| 142 | 142 |
| 143 // Merges |list_ipv4| and |list_ipv6| before passing it to |callback|, but | |
| 144 // only if |success| is true. | |
| 145 void MergeResults(const AddressSorter::CallbackType& callback, | |
| 146 const AddressList& list_ipv4, | |
| 147 bool success, | |
| 148 const AddressList& list_ipv6) { | |
| 149 if (!success) { | |
| 150 callback.Run(false, AddressList()); | |
| 151 return; | |
| 152 } | |
| 153 AddressList list; | |
| 154 list.insert(list.end(), list_ipv6.begin(), list_ipv6.end()); | |
| 155 list.insert(list.end(), list_ipv4.begin(), list_ipv4.end()); | |
| 156 callback.Run(true, list); | |
| 157 } | |
| 158 | |
| 159 // Wrapper for AddressSorterWin which does not sort IPv4 or IPv4-mapped | |
| 160 // addresses but always puts them at the end of the list. Needed because the | |
| 161 // SIO_ADDRESS_LIST_SORT does not support IPv4 addresses on Windows XP. | |
| 162 class AddressSorterWinXP : public AddressSorter { | |
| 163 public: | |
| 164 AddressSorterWinXP() {} | |
| 165 ~AddressSorterWinXP() override {} | |
| 166 | |
| 167 // AddressSorter: | |
| 168 void Sort(const AddressList& list, | |
| 169 const CallbackType& callback) const override { | |
| 170 AddressList list_ipv4; | |
| 171 AddressList list_ipv6; | |
| 172 for (size_t i = 0; i < list.size(); ++i) { | |
| 173 const IPEndPoint& ipe = list[i]; | |
| 174 if (ipe.GetFamily() == ADDRESS_FAMILY_IPV4) { | |
| 175 list_ipv4.push_back(ipe); | |
| 176 } else { | |
| 177 list_ipv6.push_back(ipe); | |
| 178 } | |
| 179 } | |
| 180 if (!list_ipv6.empty()) { | |
| 181 sorter_.Sort(list_ipv6, base::Bind(&MergeResults, callback, list_ipv4)); | |
| 182 } else { | |
| 183 NOTREACHED() << "Should not be called with IPv4-only addresses."; | |
| 184 callback.Run(true, list); | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 private: | |
| 189 AddressSorterWin sorter_; | |
| 190 | |
| 191 DISALLOW_COPY_AND_ASSIGN(AddressSorterWinXP); | |
| 192 }; | |
| 193 | |
| 194 } // namespace | 143 } // namespace |
| 195 | 144 |
| 196 // static | 145 // static |
| 197 std::unique_ptr<AddressSorter> AddressSorter::CreateAddressSorter() { | 146 std::unique_ptr<AddressSorter> AddressSorter::CreateAddressSorter() { |
| 198 if (base::win::GetVersion() < base::win::VERSION_VISTA) | |
| 199 return std::unique_ptr<AddressSorter>(new AddressSorterWinXP()); | |
| 200 return std::unique_ptr<AddressSorter>(new AddressSorterWin()); | 147 return std::unique_ptr<AddressSorter>(new AddressSorterWin()); |
| 201 } | 148 } |
| 202 | 149 |
| 203 } // namespace net | 150 } // namespace net |
| 204 | 151 |
| OLD | NEW |