| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/local_discovery/privet_traffic_detector.h" | 5 #include "chrome/browser/local_discovery/privet_traffic_detector.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "base/sys_byteorder.h" | 8 #include "base/sys_byteorder.h" |
| 9 #include "net/base/dns_util.h" | 9 #include "net/base/dns_util.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 #include "net/dns/dns_protocol.h" | 11 #include "net/dns/dns_protocol.h" |
| 12 #include "net/dns/dns_response.h" | 12 #include "net/dns/dns_response.h" |
| 13 #include "net/dns/mdns_client.h" | 13 #include "net/dns/mdns_client.h" |
| 14 #include "net/log/net_log.h" | 14 #include "net/log/net_log.h" |
| 15 #include "net/udp/datagram_server_socket.h" | 15 #include "net/udp/datagram_server_socket.h" |
| 16 #include "net/udp/udp_server_socket.h" | 16 #include "net/udp/udp_server_socket.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 const int kMaxRestartAttempts = 10; | 20 const int kMaxRestartAttempts = 10; |
| 21 const char kPrivetDeviceTypeDnsString[] = "\x07_privet"; | 21 const char kPrivetDeviceTypeDnsString[] = "\x07_privet"; |
| 22 | 22 |
| 23 void GetNetworkListOnFileThread( | 23 void GetNetworkListOnFileThread( |
| 24 const base::Callback<void(const net::NetworkInterfaceList&)> callback) { | 24 const base::Callback<void(const net::NetworkInterfaceList&)> callback) { |
| 25 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 25 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); |
| 26 net::NetworkInterfaceList networks; | 26 net::NetworkInterfaceList networks; |
| 27 if (!GetNetworkList(&networks, net::INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)) | 27 if (!GetNetworkList(&networks, net::INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)) |
| 28 return; | 28 return; |
| 29 | 29 |
| 30 net::NetworkInterfaceList ip4_networks; | 30 net::NetworkInterfaceList ip4_networks; |
| 31 for (size_t i = 0; i < networks.size(); ++i) { | 31 for (size_t i = 0; i < networks.size(); ++i) { |
| 32 net::AddressFamily address_family = | 32 net::AddressFamily address_family = |
| 33 net::GetAddressFamily(networks[i].address); | 33 net::GetAddressFamily(networks[i].address); |
| 34 if (address_family == net::ADDRESS_FAMILY_IPV4 && | 34 if (address_family == net::ADDRESS_FAMILY_IPV4 && |
| 35 networks[i].prefix_length >= 24) { | 35 networks[i].prefix_length >= 24) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 | 69 |
| 70 void PrivetTrafficDetector::Start() { | 70 void PrivetTrafficDetector::Start() { |
| 71 content::BrowserThread::PostTask( | 71 content::BrowserThread::PostTask( |
| 72 content::BrowserThread::IO, | 72 content::BrowserThread::IO, |
| 73 FROM_HERE, | 73 FROM_HERE, |
| 74 base::Bind(&PrivetTrafficDetector::StartOnIOThread, | 74 base::Bind(&PrivetTrafficDetector::StartOnIOThread, |
| 75 weak_ptr_factory_.GetWeakPtr())); | 75 weak_ptr_factory_.GetWeakPtr())); |
| 76 } | 76 } |
| 77 | 77 |
| 78 PrivetTrafficDetector::~PrivetTrafficDetector() { | 78 PrivetTrafficDetector::~PrivetTrafficDetector() { |
| 79 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 79 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 80 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | 80 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
| 81 } | 81 } |
| 82 | 82 |
| 83 void PrivetTrafficDetector::StartOnIOThread() { | 83 void PrivetTrafficDetector::StartOnIOThread() { |
| 84 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 84 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 85 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); | 85 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); |
| 86 ScheduleRestart(); | 86 ScheduleRestart(); |
| 87 } | 87 } |
| 88 | 88 |
| 89 void PrivetTrafficDetector::OnNetworkChanged( | 89 void PrivetTrafficDetector::OnNetworkChanged( |
| 90 net::NetworkChangeNotifier::ConnectionType type) { | 90 net::NetworkChangeNotifier::ConnectionType type) { |
| 91 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 91 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 92 restart_attempts_ = kMaxRestartAttempts; | 92 restart_attempts_ = kMaxRestartAttempts; |
| 93 if (type != net::NetworkChangeNotifier::CONNECTION_NONE) | 93 if (type != net::NetworkChangeNotifier::CONNECTION_NONE) |
| 94 ScheduleRestart(); | 94 ScheduleRestart(); |
| 95 } | 95 } |
| 96 | 96 |
| 97 void PrivetTrafficDetector::ScheduleRestart() { | 97 void PrivetTrafficDetector::ScheduleRestart() { |
| 98 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 98 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 99 socket_.reset(); | 99 socket_.reset(); |
| 100 weak_ptr_factory_.InvalidateWeakPtrs(); | 100 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 101 content::BrowserThread::PostDelayedTask( | 101 content::BrowserThread::PostDelayedTask( |
| 102 content::BrowserThread::FILE, | 102 content::BrowserThread::FILE, |
| 103 FROM_HERE, | 103 FROM_HERE, |
| 104 base::Bind(&GetNetworkListOnFileThread, | 104 base::Bind(&GetNetworkListOnFileThread, |
| 105 base::Bind(&PrivetTrafficDetector::Restart, | 105 base::Bind(&PrivetTrafficDetector::Restart, |
| 106 weak_ptr_factory_.GetWeakPtr())), | 106 weak_ptr_factory_.GetWeakPtr())), |
| 107 base::TimeDelta::FromSeconds(3)); | 107 base::TimeDelta::FromSeconds(3)); |
| 108 } | 108 } |
| 109 | 109 |
| 110 void PrivetTrafficDetector::Restart(const net::NetworkInterfaceList& networks) { | 110 void PrivetTrafficDetector::Restart(const net::NetworkInterfaceList& networks) { |
| 111 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 111 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 112 networks_ = networks; | 112 networks_ = networks; |
| 113 if (Bind() < net::OK || DoLoop(0) < net::OK) { | 113 if (Bind() < net::OK || DoLoop(0) < net::OK) { |
| 114 if ((restart_attempts_--) > 0) | 114 if ((restart_attempts_--) > 0) |
| 115 ScheduleRestart(); | 115 ScheduleRestart(); |
| 116 } else { | 116 } else { |
| 117 // Reset on success. | 117 // Reset on success. |
| 118 restart_attempts_ = kMaxRestartAttempts; | 118 restart_attempts_ = kMaxRestartAttempts; |
| 119 } | 119 } |
| 120 } | 120 } |
| 121 | 121 |
| 122 int PrivetTrafficDetector::Bind() { | 122 int PrivetTrafficDetector::Bind() { |
| 123 if (!start_time_.is_null()) { | 123 if (!start_time_.is_null()) { |
| 124 base::TimeDelta time_delta = base::Time::Now() - start_time_; | 124 base::TimeDelta time_delta = base::Time::Now() - start_time_; |
| 125 UMA_HISTOGRAM_LONG_TIMES("LocalDiscovery.DetectorRestartTime", time_delta); | 125 UMA_HISTOGRAM_LONG_TIMES("LocalDiscovery.DetectorRestartTime", time_delta); |
| 126 } | 126 } |
| 127 start_time_ = base::Time::Now(); | 127 start_time_ = base::Time::Now(); |
| 128 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 128 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 129 socket_.reset(new net::UDPServerSocket(NULL, net::NetLog::Source())); | 129 socket_.reset(new net::UDPServerSocket(NULL, net::NetLog::Source())); |
| 130 net::IPEndPoint multicast_addr = net::GetMDnsIPEndPoint(address_family_); | 130 net::IPEndPoint multicast_addr = net::GetMDnsIPEndPoint(address_family_); |
| 131 net::IPAddressNumber address_any(multicast_addr.address().size()); | 131 net::IPAddressNumber address_any(multicast_addr.address().size()); |
| 132 net::IPEndPoint bind_endpoint(address_any, multicast_addr.port()); | 132 net::IPEndPoint bind_endpoint(address_any, multicast_addr.port()); |
| 133 socket_->AllowAddressReuse(); | 133 socket_->AllowAddressReuse(); |
| 134 int rv = socket_->Listen(bind_endpoint); | 134 int rv = socket_->Listen(bind_endpoint); |
| 135 if (rv < net::OK) | 135 if (rv < net::OK) |
| 136 return rv; | 136 return rv; |
| 137 socket_->SetMulticastLoopbackMode(false); | 137 socket_->SetMulticastLoopbackMode(false); |
| 138 return socket_->JoinGroup(multicast_addr.address()); | 138 return socket_->JoinGroup(multicast_addr.address()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 163 return false; | 163 return false; |
| 164 const char* substring_begin = kPrivetDeviceTypeDnsString; | 164 const char* substring_begin = kPrivetDeviceTypeDnsString; |
| 165 const char* substring_end = substring_begin + | 165 const char* substring_end = substring_begin + |
| 166 arraysize(kPrivetDeviceTypeDnsString) - 1; | 166 arraysize(kPrivetDeviceTypeDnsString) - 1; |
| 167 // Check for expected substring, any Privet device must include this. | 167 // Check for expected substring, any Privet device must include this. |
| 168 return std::search(buffer_begin, buffer_end, substring_begin, | 168 return std::search(buffer_begin, buffer_end, substring_begin, |
| 169 substring_end) != buffer_end; | 169 substring_end) != buffer_end; |
| 170 } | 170 } |
| 171 | 171 |
| 172 int PrivetTrafficDetector::DoLoop(int rv) { | 172 int PrivetTrafficDetector::DoLoop(int rv) { |
| 173 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 173 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 174 do { | 174 do { |
| 175 if (IsPrivetPacket(rv)) { | 175 if (IsPrivetPacket(rv)) { |
| 176 socket_.reset(); | 176 socket_.reset(); |
| 177 callback_runner_->PostTask(FROM_HERE, on_traffic_detected_); | 177 callback_runner_->PostTask(FROM_HERE, on_traffic_detected_); |
| 178 base::TimeDelta time_delta = base::Time::Now() - start_time_; | 178 base::TimeDelta time_delta = base::Time::Now() - start_time_; |
| 179 UMA_HISTOGRAM_LONG_TIMES("LocalDiscovery.DetectorTriggerTime", | 179 UMA_HISTOGRAM_LONG_TIMES("LocalDiscovery.DetectorTriggerTime", |
| 180 time_delta); | 180 time_delta); |
| 181 return net::OK; | 181 return net::OK; |
| 182 } | 182 } |
| 183 | 183 |
| 184 rv = socket_->RecvFrom( | 184 rv = socket_->RecvFrom( |
| 185 io_buffer_.get(), | 185 io_buffer_.get(), |
| 186 io_buffer_->size(), | 186 io_buffer_->size(), |
| 187 &recv_addr_, | 187 &recv_addr_, |
| 188 base::Bind(base::IgnoreResult(&PrivetTrafficDetector::DoLoop), | 188 base::Bind(base::IgnoreResult(&PrivetTrafficDetector::DoLoop), |
| 189 base::Unretained(this))); | 189 base::Unretained(this))); |
| 190 } while (rv > 0); | 190 } while (rv > 0); |
| 191 | 191 |
| 192 if (rv != net::ERR_IO_PENDING) | 192 if (rv != net::ERR_IO_PENDING) |
| 193 return rv; | 193 return rv; |
| 194 | 194 |
| 195 return net::OK; | 195 return net::OK; |
| 196 } | 196 } |
| 197 | 197 |
| 198 } // namespace local_discovery | 198 } // namespace local_discovery |
| OLD | NEW |