| 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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #if defined(OS_MACOSX) || defined(OS_BSD) | 10 #if defined(OS_MACOSX) || defined(OS_BSD) |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 NetworkChangeNotifier::RemoveIPAddressObserver(this); | 256 NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 257 } | 257 } |
| 258 | 258 |
| 259 void AddressSorterPosix::Sort(const AddressList& list, | 259 void AddressSorterPosix::Sort(const AddressList& list, |
| 260 const CallbackType& callback) const { | 260 const CallbackType& callback) const { |
| 261 DCHECK(CalledOnValidThread()); | 261 DCHECK(CalledOnValidThread()); |
| 262 std::vector<scoped_ptr<DestinationInfo>> sort_list; | 262 std::vector<scoped_ptr<DestinationInfo>> sort_list; |
| 263 | 263 |
| 264 for (size_t i = 0; i < list.size(); ++i) { | 264 for (size_t i = 0; i < list.size(); ++i) { |
| 265 scoped_ptr<DestinationInfo> info(new DestinationInfo()); | 265 scoped_ptr<DestinationInfo> info(new DestinationInfo()); |
| 266 info->address = list[i].address(); | 266 info->address = list[i].address().bytes(); |
| 267 info->scope = GetScope(ipv4_scope_table_, info->address); | 267 info->scope = GetScope(ipv4_scope_table_, info->address); |
| 268 info->precedence = GetPolicyValue(precedence_table_, info->address); | 268 info->precedence = GetPolicyValue(precedence_table_, info->address); |
| 269 info->label = GetPolicyValue(label_table_, info->address); | 269 info->label = GetPolicyValue(label_table_, info->address); |
| 270 | 270 |
| 271 // Each socket can only be bound once. | 271 // Each socket can only be bound once. |
| 272 scoped_ptr<DatagramClientSocket> socket( | 272 scoped_ptr<DatagramClientSocket> socket( |
| 273 socket_factory_->CreateDatagramClientSocket( | 273 socket_factory_->CreateDatagramClientSocket( |
| 274 DatagramSocket::DEFAULT_BIND, | 274 DatagramSocket::DEFAULT_BIND, |
| 275 RandIntCallback(), | 275 RandIntCallback(), |
| 276 NULL /* NetLog */, | 276 NULL /* NetLog */, |
| 277 NetLog::Source())); | 277 NetLog::Source())); |
| 278 | 278 |
| 279 // Even though no packets are sent, cannot use port 0 in Connect. | 279 // Even though no packets are sent, cannot use port 0 in Connect. |
| 280 IPEndPoint dest(info->address, 80 /* port */); | 280 IPEndPoint dest(info->address, 80 /* port */); |
| 281 int rv = socket->Connect(dest); | 281 int rv = socket->Connect(dest); |
| 282 if (rv != OK) { | 282 if (rv != OK) { |
| 283 VLOG(1) << "Could not connect to " << dest.ToStringWithoutPort() | 283 VLOG(1) << "Could not connect to " << dest.ToStringWithoutPort() |
| 284 << " reason " << rv; | 284 << " reason " << rv; |
| 285 continue; | 285 continue; |
| 286 } | 286 } |
| 287 // Filter out unusable destinations. | 287 // Filter out unusable destinations. |
| 288 IPEndPoint src; | 288 IPEndPoint src; |
| 289 rv = socket->GetLocalAddress(&src); | 289 rv = socket->GetLocalAddress(&src); |
| 290 if (rv != OK) { | 290 if (rv != OK) { |
| 291 LOG(WARNING) << "Could not get local address for " | 291 LOG(WARNING) << "Could not get local address for " |
| 292 << dest.ToStringWithoutPort() << " reason " << rv; | 292 << dest.ToStringWithoutPort() << " reason " << rv; |
| 293 continue; | 293 continue; |
| 294 } | 294 } |
| 295 | 295 |
| 296 SourceAddressInfo& src_info = source_map_[src.address()]; | 296 SourceAddressInfo& src_info = source_map_[src.address().bytes()]; |
| 297 if (src_info.scope == SCOPE_UNDEFINED) { | 297 if (src_info.scope == SCOPE_UNDEFINED) { |
| 298 // If |source_info_| is out of date, |src| might be missing, but we still | 298 // If |source_info_| is out of date, |src| might be missing, but we still |
| 299 // want to sort, even though the HostCache will be cleared soon. | 299 // want to sort, even though the HostCache will be cleared soon. |
| 300 FillPolicy(src.address(), &src_info); | 300 FillPolicy(src.address().bytes(), &src_info); |
| 301 } | 301 } |
| 302 info->src = &src_info; | 302 info->src = &src_info; |
| 303 | 303 |
| 304 if (info->address.size() == src.address().size()) { | 304 if (info->address.size() == src.address().size()) { |
| 305 info->common_prefix_length = std::min( | 305 info->common_prefix_length = |
| 306 CommonPrefixLength(info->address, src.address()), | 306 std::min(CommonPrefixLength(info->address, src.address().bytes()), |
| 307 info->src->prefix_length); | 307 info->src->prefix_length); |
| 308 } | 308 } |
| 309 sort_list.push_back(std::move(info)); | 309 sort_list.push_back(std::move(info)); |
| 310 } | 310 } |
| 311 | 311 |
| 312 std::stable_sort(sort_list.begin(), sort_list.end(), CompareDestinations); | 312 std::stable_sort(sort_list.begin(), sort_list.end(), CompareDestinations); |
| 313 | 313 |
| 314 AddressList result; | 314 AddressList result; |
| 315 for (size_t i = 0; i < sort_list.size(); ++i) | 315 for (size_t i = 0; i < sort_list.size(); ++i) |
| 316 result.push_back(IPEndPoint(sort_list[i]->address, 0 /* port */)); | 316 result.push_back(IPEndPoint(sort_list[i]->address, 0 /* port */)); |
| 317 | 317 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 if (rv < 0) { | 350 if (rv < 0) { |
| 351 LOG(WARNING) << "getifaddrs failed " << rv; | 351 LOG(WARNING) << "getifaddrs failed " << rv; |
| 352 close(ioctl_socket); | 352 close(ioctl_socket); |
| 353 return; | 353 return; |
| 354 } | 354 } |
| 355 | 355 |
| 356 for (struct ifaddrs* ifa = addrs; ifa != NULL; ifa = ifa->ifa_next) { | 356 for (struct ifaddrs* ifa = addrs; ifa != NULL; ifa = ifa->ifa_next) { |
| 357 IPEndPoint src; | 357 IPEndPoint src; |
| 358 if (!src.FromSockAddr(ifa->ifa_addr, ifa->ifa_addr->sa_len)) | 358 if (!src.FromSockAddr(ifa->ifa_addr, ifa->ifa_addr->sa_len)) |
| 359 continue; | 359 continue; |
| 360 SourceAddressInfo& info = source_map_[src.address()]; | 360 SourceAddressInfo& info = source_map_[src.address().bytes()]; |
| 361 // Note: no known way to fill in |native| and |home|. | 361 // Note: no known way to fill in |native| and |home|. |
| 362 info.native = info.home = info.deprecated = false; | 362 info.native = info.home = info.deprecated = false; |
| 363 if (ifa->ifa_addr->sa_family == AF_INET6) { | 363 if (ifa->ifa_addr->sa_family == AF_INET6) { |
| 364 struct in6_ifreq ifr = {}; | 364 struct in6_ifreq ifr = {}; |
| 365 strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name) - 1); | 365 strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name) - 1); |
| 366 DCHECK_LE(ifa->ifa_addr->sa_len, sizeof(ifr.ifr_ifru.ifru_addr)); | 366 DCHECK_LE(ifa->ifa_addr->sa_len, sizeof(ifr.ifr_ifru.ifru_addr)); |
| 367 memcpy(&ifr.ifr_ifru.ifru_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len); | 367 memcpy(&ifr.ifr_ifru.ifru_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len); |
| 368 int rv = ioctl(ioctl_socket, SIOCGIFAFLAG_IN6, &ifr); | 368 int rv = ioctl(ioctl_socket, SIOCGIFAFLAG_IN6, &ifr); |
| 369 if (rv >= 0) { | 369 if (rv >= 0) { |
| 370 info.deprecated = ifr.ifr_ifru.ifru_flags & IN6_IFF_DEPRECATED; | 370 info.deprecated = ifr.ifr_ifru.ifru_flags & IN6_IFF_DEPRECATED; |
| 371 } else { | 371 } else { |
| 372 LOG(WARNING) << "SIOCGIFAFLAG_IN6 failed " << rv; | 372 LOG(WARNING) << "SIOCGIFAFLAG_IN6 failed " << rv; |
| 373 } | 373 } |
| 374 } | 374 } |
| 375 if (ifa->ifa_netmask) { | 375 if (ifa->ifa_netmask) { |
| 376 IPEndPoint netmask; | 376 IPEndPoint netmask; |
| 377 if (netmask.FromSockAddr(ifa->ifa_netmask, ifa->ifa_addr->sa_len)) { | 377 if (netmask.FromSockAddr(ifa->ifa_netmask, ifa->ifa_addr->sa_len)) { |
| 378 info.prefix_length = MaskPrefixLength(netmask.address()); | 378 info.prefix_length = MaskPrefixLength(netmask.address().bytes()); |
| 379 } else { | 379 } else { |
| 380 LOG(WARNING) << "FromSockAddr failed on netmask"; | 380 LOG(WARNING) << "FromSockAddr failed on netmask"; |
| 381 } | 381 } |
| 382 } | 382 } |
| 383 FillPolicy(src.address(), &info); | 383 FillPolicy(src.address().bytes(), &info); |
| 384 } | 384 } |
| 385 freeifaddrs(addrs); | 385 freeifaddrs(addrs); |
| 386 close(ioctl_socket); | 386 close(ioctl_socket); |
| 387 #endif | 387 #endif |
| 388 } | 388 } |
| 389 | 389 |
| 390 void AddressSorterPosix::FillPolicy(const IPAddressNumber& address, | 390 void AddressSorterPosix::FillPolicy(const IPAddressNumber& address, |
| 391 SourceAddressInfo* info) const { | 391 SourceAddressInfo* info) const { |
| 392 DCHECK(CalledOnValidThread()); | 392 DCHECK(CalledOnValidThread()); |
| 393 info->scope = GetScope(ipv4_scope_table_, address); | 393 info->scope = GetScope(ipv4_scope_table_, address); |
| 394 info->label = GetPolicyValue(label_table_, address); | 394 info->label = GetPolicyValue(label_table_, address); |
| 395 } | 395 } |
| 396 | 396 |
| 397 // static | 397 // static |
| 398 scoped_ptr<AddressSorter> AddressSorter::CreateAddressSorter() { | 398 scoped_ptr<AddressSorter> AddressSorter::CreateAddressSorter() { |
| 399 return scoped_ptr<AddressSorter>( | 399 return scoped_ptr<AddressSorter>( |
| 400 new AddressSorterPosix(ClientSocketFactory::GetDefaultFactory())); | 400 new AddressSorterPosix(ClientSocketFactory::GetDefaultFactory())); |
| 401 } | 401 } |
| 402 | 402 |
| 403 } // namespace net | 403 } // namespace net |
| 404 | 404 |
| OLD | NEW |