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_posix.h" | 5 #include "net/dns/address_sorter_posix.h" |
6 | 6 |
7 #include <netinet/in.h> | 7 #include <netinet/in.h> |
8 | 8 |
9 #if defined(OS_MACOSX) || defined(OS_BSD) | 9 #if defined(OS_MACOSX) || defined(OS_BSD) |
10 #include <sys/socket.h> // Must be included before ifaddrs.h. | 10 #include <sys/socket.h> // Must be included before ifaddrs.h. |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 | 168 |
169 // Default mapping of IPv4 addresses to scope. | 169 // Default mapping of IPv4 addresses to scope. |
170 AddressSorterPosix::PolicyEntry kDefaultIPv4ScopeTable[] = { | 170 AddressSorterPosix::PolicyEntry kDefaultIPv4ScopeTable[] = { |
171 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0x7F }, 104, | 171 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0x7F }, 104, |
172 AddressSorterPosix::SCOPE_LINKLOCAL }, | 172 AddressSorterPosix::SCOPE_LINKLOCAL }, |
173 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xA9, 0xFE }, 112, | 173 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xA9, 0xFE }, 112, |
174 AddressSorterPosix::SCOPE_LINKLOCAL }, | 174 AddressSorterPosix::SCOPE_LINKLOCAL }, |
175 { { }, 0, AddressSorterPosix::SCOPE_GLOBAL }, | 175 { { }, 0, AddressSorterPosix::SCOPE_GLOBAL }, |
176 }; | 176 }; |
177 | 177 |
178 // Returns number of matching initial bits between the addresses |a1| and |a2|. | |
179 unsigned CommonPrefixLength(const IPAddressNumber& a1, | |
180 const IPAddressNumber& a2) { | |
181 DCHECK_EQ(a1.size(), a2.size()); | |
182 for (size_t i = 0; i < a1.size(); ++i) { | |
183 unsigned diff = a1[i] ^ a2[i]; | |
184 if (!diff) | |
185 continue; | |
186 for (unsigned j = 0; j < CHAR_BIT; ++j) { | |
187 if (diff & (1 << (CHAR_BIT - 1))) | |
188 return i * CHAR_BIT + j; | |
189 diff <<= 1; | |
190 } | |
191 NOTREACHED(); | |
192 } | |
193 return a1.size() * CHAR_BIT; | |
194 } | |
195 | |
196 // Computes the number of leading 1-bits in |mask|. | |
197 unsigned MaskPrefixLength(const IPAddressNumber& mask) { | |
198 IPAddressNumber all_ones(mask.size(), 0xFF); | |
199 return CommonPrefixLength(mask, all_ones); | |
200 } | |
201 | |
202 struct DestinationInfo { | 178 struct DestinationInfo { |
203 IPAddressNumber address; | 179 IPAddressNumber address; |
204 AddressSorterPosix::AddressScope scope; | 180 AddressSorterPosix::AddressScope scope; |
205 unsigned precedence; | 181 unsigned precedence; |
206 unsigned label; | 182 unsigned label; |
207 const AddressSorterPosix::SourceAddressInfo* src; | 183 const AddressSorterPosix::SourceAddressInfo* src; |
208 unsigned common_prefix_length; | 184 unsigned common_prefix_length; |
209 }; | 185 }; |
210 | 186 |
211 // Returns true iff |dst_a| should precede |dst_b| in the address list. | 187 // Returns true iff |dst_a| should precede |dst_b| in the address list. |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 } | 393 } |
418 | 394 |
419 // static | 395 // static |
420 scoped_ptr<AddressSorter> AddressSorter::CreateAddressSorter() { | 396 scoped_ptr<AddressSorter> AddressSorter::CreateAddressSorter() { |
421 return scoped_ptr<AddressSorter>( | 397 return scoped_ptr<AddressSorter>( |
422 new AddressSorterPosix(ClientSocketFactory::GetDefaultFactory())); | 398 new AddressSorterPosix(ClientSocketFactory::GetDefaultFactory())); |
423 } | 399 } |
424 | 400 |
425 } // namespace net | 401 } // namespace net |
426 | 402 |
OLD | NEW |