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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 AddressSorterPosix::AddressScope GetIPv6MulticastScope( | 75 AddressSorterPosix::AddressScope GetIPv6MulticastScope( |
76 const IPAddressNumber& address) { | 76 const IPAddressNumber& address) { |
77 DCHECK_EQ(kIPv6AddressSize, address.size()); | 77 DCHECK_EQ(kIPv6AddressSize, address.size()); |
78 return static_cast<AddressSorterPosix::AddressScope>(address[1] & 0x0F); | 78 return static_cast<AddressSorterPosix::AddressScope>(address[1] & 0x0F); |
79 } | 79 } |
80 | 80 |
81 bool IsIPv6Loopback(const IPAddressNumber& address) { | 81 bool IsIPv6Loopback(const IPAddressNumber& address) { |
82 DCHECK_EQ(kIPv6AddressSize, address.size()); | 82 DCHECK_EQ(kIPv6AddressSize, address.size()); |
83 // IN6_IS_ADDR_LOOPBACK | 83 // IN6_IS_ADDR_LOOPBACK |
84 unsigned char kLoopback[kIPv6AddressSize] = { | 84 unsigned char kLoopback[kIPv6AddressSize] = { |
85 0, 0, 0, 0, 0, 0, 0, 0, | 85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
86 0, 0, 0, 0, 0, 0, 0, 1, | |
87 }; | 86 }; |
88 return address == IPAddressNumber(kLoopback, kLoopback + kIPv6AddressSize); | 87 return address == IPAddressNumber(kLoopback, kLoopback + kIPv6AddressSize); |
89 } | 88 } |
90 | 89 |
91 bool IsIPv6LinkLocal(const IPAddressNumber& address) { | 90 bool IsIPv6LinkLocal(const IPAddressNumber& address) { |
92 DCHECK_EQ(kIPv6AddressSize, address.size()); | 91 DCHECK_EQ(kIPv6AddressSize, address.size()); |
93 // IN6_IS_ADDR_LINKLOCAL | 92 // IN6_IS_ADDR_LINKLOCAL |
94 return (address[0] == 0xFE) && ((address[1] & 0xC0) == 0x80); | 93 return (address[0] == 0xFE) && ((address[1] & 0xC0) == 0x80); |
95 } | 94 } |
96 | 95 |
(...skipping 20 matching lines...) Expand all Loading... |
117 return static_cast<AddressSorterPosix::AddressScope>( | 116 return static_cast<AddressSorterPosix::AddressScope>( |
118 GetPolicyValue(ipv4_scope_table, address)); | 117 GetPolicyValue(ipv4_scope_table, address)); |
119 } else { | 118 } else { |
120 NOTREACHED(); | 119 NOTREACHED(); |
121 return AddressSorterPosix::SCOPE_NODELOCAL; | 120 return AddressSorterPosix::SCOPE_NODELOCAL; |
122 } | 121 } |
123 } | 122 } |
124 | 123 |
125 // Default policy table. RFC 3484, Section 2.1. | 124 // Default policy table. RFC 3484, Section 2.1. |
126 AddressSorterPosix::PolicyEntry kDefaultPrecedenceTable[] = { | 125 AddressSorterPosix::PolicyEntry kDefaultPrecedenceTable[] = { |
127 // ::1/128 -- loopback | 126 // ::1/128 -- loopback |
128 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 128, 50 }, | 127 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 128, 50}, |
129 // ::/0 -- any | 128 // ::/0 -- any |
130 { { }, 0, 40 }, | 129 {{}, 0, 40}, |
131 // ::ffff:0:0/96 -- IPv4 mapped | 130 // ::ffff:0:0/96 -- IPv4 mapped |
132 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF }, 96, 35 }, | 131 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF}, 96, 35}, |
133 // 2002::/16 -- 6to4 | 132 // 2002::/16 -- 6to4 |
134 { { 0x20, 0x02, }, 16, 30 }, | 133 {{ |
135 // 2001::/32 -- Teredo | 134 0x20, 0x02, |
136 { { 0x20, 0x01, 0, 0 }, 32, 5 }, | 135 }, |
137 // fc00::/7 -- unique local address | 136 16, |
138 { { 0xFC }, 7, 3 }, | 137 30}, |
139 // ::/96 -- IPv4 compatible | 138 // 2001::/32 -- Teredo |
140 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 96, 1 }, | 139 {{0x20, 0x01, 0, 0}, 32, 5}, |
141 // fec0::/10 -- site-local expanded scope | 140 // fc00::/7 -- unique local address |
142 { { 0xFE, 0xC0 }, 10, 1 }, | 141 {{0xFC}, 7, 3}, |
143 // 3ffe::/16 -- 6bone | 142 // ::/96 -- IPv4 compatible |
144 { { 0x3F, 0xFE }, 16, 1 }, | 143 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 96, 1}, |
| 144 // fec0::/10 -- site-local expanded scope |
| 145 {{0xFE, 0xC0}, 10, 1}, |
| 146 // 3ffe::/16 -- 6bone |
| 147 {{0x3F, 0xFE}, 16, 1}, |
145 }; | 148 }; |
146 | 149 |
147 AddressSorterPosix::PolicyEntry kDefaultLabelTable[] = { | 150 AddressSorterPosix::PolicyEntry kDefaultLabelTable[] = { |
148 // ::1/128 -- loopback | 151 // ::1/128 -- loopback |
149 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 128, 0 }, | 152 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 128, 0}, |
150 // ::/0 -- any | 153 // ::/0 -- any |
151 { { }, 0, 1 }, | 154 {{}, 0, 1}, |
152 // ::ffff:0:0/96 -- IPv4 mapped | 155 // ::ffff:0:0/96 -- IPv4 mapped |
153 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF }, 96, 4 }, | 156 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF}, 96, 4}, |
154 // 2002::/16 -- 6to4 | 157 // 2002::/16 -- 6to4 |
155 { { 0x20, 0x02, }, 16, 2 }, | 158 {{ |
156 // 2001::/32 -- Teredo | 159 0x20, 0x02, |
157 { { 0x20, 0x01, 0, 0 }, 32, 5 }, | 160 }, |
158 // fc00::/7 -- unique local address | 161 16, |
159 { { 0xFC }, 7, 13 }, | 162 2}, |
160 // ::/96 -- IPv4 compatible | 163 // 2001::/32 -- Teredo |
161 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 96, 3 }, | 164 {{0x20, 0x01, 0, 0}, 32, 5}, |
162 // fec0::/10 -- site-local expanded scope | 165 // fc00::/7 -- unique local address |
163 { { 0xFE, 0xC0 }, 10, 11 }, | 166 {{0xFC}, 7, 13}, |
164 // 3ffe::/16 -- 6bone | 167 // ::/96 -- IPv4 compatible |
165 { { 0x3F, 0xFE }, 16, 12 }, | 168 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 96, 3}, |
| 169 // fec0::/10 -- site-local expanded scope |
| 170 {{0xFE, 0xC0}, 10, 11}, |
| 171 // 3ffe::/16 -- 6bone |
| 172 {{0x3F, 0xFE}, 16, 12}, |
166 }; | 173 }; |
167 | 174 |
168 // Default mapping of IPv4 addresses to scope. | 175 // Default mapping of IPv4 addresses to scope. |
169 AddressSorterPosix::PolicyEntry kDefaultIPv4ScopeTable[] = { | 176 AddressSorterPosix::PolicyEntry kDefaultIPv4ScopeTable[] = { |
170 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0x7F }, 104, | 177 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0x7F}, |
171 AddressSorterPosix::SCOPE_LINKLOCAL }, | 178 104, |
172 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xA9, 0xFE }, 112, | 179 AddressSorterPosix::SCOPE_LINKLOCAL}, |
173 AddressSorterPosix::SCOPE_LINKLOCAL }, | 180 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xA9, 0xFE}, |
174 { { }, 0, AddressSorterPosix::SCOPE_GLOBAL }, | 181 112, |
| 182 AddressSorterPosix::SCOPE_LINKLOCAL}, |
| 183 {{}, 0, AddressSorterPosix::SCOPE_GLOBAL}, |
175 }; | 184 }; |
176 | 185 |
177 struct DestinationInfo { | 186 struct DestinationInfo { |
178 IPAddressNumber address; | 187 IPAddressNumber address; |
179 AddressSorterPosix::AddressScope scope; | 188 AddressSorterPosix::AddressScope scope; |
180 unsigned precedence; | 189 unsigned precedence; |
181 unsigned label; | 190 unsigned label; |
182 const AddressSorterPosix::SourceAddressInfo* src; | 191 const AddressSorterPosix::SourceAddressInfo* src; |
183 unsigned common_prefix_length; | 192 unsigned common_prefix_length; |
184 }; | 193 }; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 // stable_sort takes care of that. | 243 // stable_sort takes care of that. |
235 return false; | 244 return false; |
236 } | 245 } |
237 | 246 |
238 } // namespace | 247 } // namespace |
239 | 248 |
240 AddressSorterPosix::AddressSorterPosix(ClientSocketFactory* socket_factory) | 249 AddressSorterPosix::AddressSorterPosix(ClientSocketFactory* socket_factory) |
241 : socket_factory_(socket_factory), | 250 : socket_factory_(socket_factory), |
242 precedence_table_(LoadPolicy(kDefaultPrecedenceTable, | 251 precedence_table_(LoadPolicy(kDefaultPrecedenceTable, |
243 arraysize(kDefaultPrecedenceTable))), | 252 arraysize(kDefaultPrecedenceTable))), |
244 label_table_(LoadPolicy(kDefaultLabelTable, | 253 label_table_( |
245 arraysize(kDefaultLabelTable))), | 254 LoadPolicy(kDefaultLabelTable, arraysize(kDefaultLabelTable))), |
246 ipv4_scope_table_(LoadPolicy(kDefaultIPv4ScopeTable, | 255 ipv4_scope_table_(LoadPolicy(kDefaultIPv4ScopeTable, |
247 arraysize(kDefaultIPv4ScopeTable))) { | 256 arraysize(kDefaultIPv4ScopeTable))) { |
248 NetworkChangeNotifier::AddIPAddressObserver(this); | 257 NetworkChangeNotifier::AddIPAddressObserver(this); |
249 OnIPAddressChanged(); | 258 OnIPAddressChanged(); |
250 } | 259 } |
251 | 260 |
252 AddressSorterPosix::~AddressSorterPosix() { | 261 AddressSorterPosix::~AddressSorterPosix() { |
253 NetworkChangeNotifier::RemoveIPAddressObserver(this); | 262 NetworkChangeNotifier::RemoveIPAddressObserver(this); |
254 } | 263 } |
255 | 264 |
256 void AddressSorterPosix::Sort(const AddressList& list, | 265 void AddressSorterPosix::Sort(const AddressList& list, |
257 const CallbackType& callback) const { | 266 const CallbackType& callback) const { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 | 301 |
293 SourceAddressInfo& src_info = source_map_[src.address()]; | 302 SourceAddressInfo& src_info = source_map_[src.address()]; |
294 if (src_info.scope == SCOPE_UNDEFINED) { | 303 if (src_info.scope == SCOPE_UNDEFINED) { |
295 // If |source_info_| is out of date, |src| might be missing, but we still | 304 // If |source_info_| is out of date, |src| might be missing, but we still |
296 // want to sort, even though the HostCache will be cleared soon. | 305 // want to sort, even though the HostCache will be cleared soon. |
297 FillPolicy(src.address(), &src_info); | 306 FillPolicy(src.address(), &src_info); |
298 } | 307 } |
299 info->src = &src_info; | 308 info->src = &src_info; |
300 | 309 |
301 if (info->address.size() == src.address().size()) { | 310 if (info->address.size() == src.address().size()) { |
302 info->common_prefix_length = std::min( | 311 info->common_prefix_length = |
303 CommonPrefixLength(info->address, src.address()), | 312 std::min(CommonPrefixLength(info->address, src.address()), |
304 info->src->prefix_length); | 313 info->src->prefix_length); |
305 } | 314 } |
306 sort_list.push_back(info.release()); | 315 sort_list.push_back(info.release()); |
307 } | 316 } |
308 | 317 |
309 std::stable_sort(sort_list.begin(), sort_list.end(), CompareDestinations); | 318 std::stable_sort(sort_list.begin(), sort_list.end(), CompareDestinations); |
310 | 319 |
311 AddressList result; | 320 AddressList result; |
312 for (size_t i = 0; i < sort_list.size(); ++i) | 321 for (size_t i = 0; i < sort_list.size(); ++i) |
313 result.push_back(IPEndPoint(sort_list[i]->address, 0 /* port */)); | 322 result.push_back(IPEndPoint(sort_list[i]->address, 0 /* port */)); |
314 | 323 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 info->label = GetPolicyValue(label_table_, address); | 400 info->label = GetPolicyValue(label_table_, address); |
392 } | 401 } |
393 | 402 |
394 // static | 403 // static |
395 scoped_ptr<AddressSorter> AddressSorter::CreateAddressSorter() { | 404 scoped_ptr<AddressSorter> AddressSorter::CreateAddressSorter() { |
396 return scoped_ptr<AddressSorter>( | 405 return scoped_ptr<AddressSorter>( |
397 new AddressSorterPosix(ClientSocketFactory::GetDefaultFactory())); | 406 new AddressSorterPosix(ClientSocketFactory::GetDefaultFactory())); |
398 } | 407 } |
399 | 408 |
400 } // namespace net | 409 } // namespace net |
401 | |
OLD | NEW |