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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/dial/dial_service.h"
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/location.h"
14 #include "base/logging.h"
15 #include "base/rand_util.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/thread_task_runner_handle.h"
20 #include "base/time/time.h"
21 #include "chrome/browser/extensions/api/dial/dial_device_data.h"
22 #include "components/version_info/version_info.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "net/base/completion_callback.h"
25 #include "net/base/io_buffer.h"
26 #include "net/base/ip_endpoint.h"
27 #include "net/base/net_errors.h"
28 #include "net/base/net_util.h"
29 #include "net/base/network_interfaces.h"
30 #include "net/http/http_response_headers.h"
31 #include "net/http/http_util.h"
33 #if defined(OS_CHROMEOS)
34 #include "chromeos/network/network_state.h"
35 #include "chromeos/network/network_state_handler.h"
36 #include "third_party/cros_system_api/dbus/service_constants.h"
40 using base::TimeDelta
;
41 using content::BrowserThread
;
42 using net::HttpResponseHeaders
;
44 using net::IOBufferWithSize
;
45 using net::IPAddressNumber
;
46 using net::NetworkInterface
;
47 using net::NetworkInterfaceList
;
48 using net::StringIOBuffer
;
51 namespace extensions
{
55 // The total number of requests to make per discovery cycle.
56 const int kDialMaxRequests
= 4;
58 // The interval to wait between successive requests.
59 const int kDialRequestIntervalMillis
= 1000;
61 // The maximum delay a device may wait before responding (MX).
62 const int kDialMaxResponseDelaySecs
= 1;
64 // The maximum time a response is expected after a M-SEARCH request.
65 const int kDialResponseTimeoutSecs
= 2;
67 // The multicast IP address for discovery.
68 const char kDialRequestAddress
[] = "239.255.255.250";
70 // The UDP port number for discovery.
71 const uint16 kDialRequestPort
= 1900;
73 // The DIAL service type as part of the search request.
74 const char kDialSearchType
[] = "urn:dial-multiscreen-org:service:dial:1";
76 // SSDP headers parsed from the response.
77 const char kSsdpLocationHeader
[] = "LOCATION";
78 const char kSsdpCacheControlHeader
[] = "CACHE-CONTROL";
79 const char kSsdpConfigIdHeader
[] = "CONFIGID.UPNP.ORG";
80 const char kSsdpUsnHeader
[] = "USN";
82 // The receive buffer size, in bytes.
83 const int kDialRecvBufferSize
= 1500;
85 // Gets a specific header from |headers| and puts it in |value|.
86 bool GetHeader(HttpResponseHeaders
* headers
, const char* name
,
88 return headers
->EnumerateHeader(NULL
, std::string(name
), value
);
91 // Returns the request string.
92 std::string
BuildRequest() {
93 // Extra line at the end to make UPnP lib happy.
94 std::string
request(base::StringPrintf(
95 "M-SEARCH * HTTP/1.1\r\n"
97 "MAN: \"ssdp:discover\"\r\n"
100 "USER-AGENT: %s/%s %s\r\n"
104 kDialMaxResponseDelaySecs
,
106 version_info::GetProductName().c_str(),
107 version_info::GetVersionNumber().c_str(),
108 version_info::GetOSType().c_str()));
109 // 1500 is a good MTU value for most Ethernet LANs.
110 DCHECK(request
.size() <= 1500);
114 #if !defined(OS_CHROMEOS)
115 void GetNetworkListOnFileThread(
116 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
117 const base::Callback
<void(const NetworkInterfaceList
& networks
)>& cb
) {
118 NetworkInterfaceList list
;
119 bool success
= net::GetNetworkList(
120 &list
, net::INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES
);
122 VLOG(1) << "Could not retrieve network list!";
124 task_runner
->PostTask(FROM_HERE
, base::Bind(cb
, list
));
129 // Finds the IP address of the preferred interface of network type |type|
130 // to bind the socket and inserts the address into |bind_address_list|. This
131 // ChromeOS version can prioritize wifi and ethernet interfaces.
132 void InsertBestBindAddressChromeOS(
133 const chromeos::NetworkTypePattern
& type
,
134 std::vector
<IPAddressNumber
>* bind_address_list
) {
135 const chromeos::NetworkState
* state
= chromeos::NetworkHandler::Get()
136 ->network_state_handler()->ConnectedNetworkByType(type
);
137 IPAddressNumber bind_ip_address
;
139 && net::ParseIPLiteralToNumber(state
->ip_address(), &bind_ip_address
)
140 && bind_ip_address
.size() == net::kIPv4AddressSize
) {
141 VLOG(2) << "Found " << state
->type() << ", " << state
->name() << ": "
142 << state
->ip_address();
143 bind_address_list
->push_back(bind_ip_address
);
146 #endif // !defined(OS_CHROMEOS)
150 DialServiceImpl::DialSocket::DialSocket(
151 const base::Closure
& discovery_request_cb
,
152 const base::Callback
<void(const DialDeviceData
&)>& device_discovered_cb
,
153 const base::Closure
& on_error_cb
)
154 : discovery_request_cb_(discovery_request_cb
),
155 device_discovered_cb_(device_discovered_cb
),
156 on_error_cb_(on_error_cb
),
161 DialServiceImpl::DialSocket::~DialSocket() {
162 DCHECK(thread_checker_
.CalledOnValidThread());
165 bool DialServiceImpl::DialSocket::CreateAndBindSocket(
166 const IPAddressNumber
& bind_ip_address
,
167 net::NetLog
* net_log
,
168 net::NetLog::Source net_log_source
) {
169 DCHECK(thread_checker_
.CalledOnValidThread());
170 DCHECK(!socket_
.get());
171 DCHECK(bind_ip_address
.size() == net::kIPv4AddressSize
);
173 net::RandIntCallback rand_cb
= base::Bind(&base::RandInt
);
174 socket_
.reset(new UDPSocket(net::DatagramSocket::RANDOM_BIND
,
179 // 0 means bind a random port
180 net::IPEndPoint
address(bind_ip_address
, 0);
182 if (socket_
->Open(address
.GetFamily()) != net::OK
||
183 socket_
->SetBroadcast(true) != net::OK
||
184 !CheckResult("Bind", socket_
->Bind(address
))) {
189 DCHECK(socket_
.get());
191 recv_buffer_
= new IOBufferWithSize(kDialRecvBufferSize
);
195 void DialServiceImpl::DialSocket::SendOneRequest(
196 const net::IPEndPoint
& send_address
,
197 const scoped_refptr
<net::StringIOBuffer
>& send_buffer
) {
198 if (!socket_
.get()) {
199 VLOG(1) << "Socket not connected.";
204 VLOG(1) << "Already writing.";
209 int result
= socket_
->SendTo(
210 send_buffer
.get(), send_buffer
->size(), send_address
,
211 base::Bind(&DialServiceImpl::DialSocket::OnSocketWrite
,
212 base::Unretained(this),
213 send_buffer
->size()));
214 bool result_ok
= CheckResult("SendTo", result
);
215 if (result_ok
&& result
> 0) {
216 // Synchronous write.
217 OnSocketWrite(send_buffer
->size(), result
);
221 bool DialServiceImpl::DialSocket::IsClosed() {
222 DCHECK(thread_checker_
.CalledOnValidThread());
223 return !socket_
.get();
226 bool DialServiceImpl::DialSocket::CheckResult(const char* operation
,
228 DCHECK(thread_checker_
.CalledOnValidThread());
229 VLOG(2) << "Operation " << operation
<< " result " << result
;
230 if (result
< net::OK
&& result
!= net::ERR_IO_PENDING
) {
232 std::string
error_str(net::ErrorToString(result
));
233 VLOG(1) << "dial socket error: " << error_str
;
240 void DialServiceImpl::DialSocket::Close() {
241 DCHECK(thread_checker_
.CalledOnValidThread());
247 void DialServiceImpl::DialSocket::OnSocketWrite(int send_buffer_size
,
249 DCHECK(thread_checker_
.CalledOnValidThread());
251 if (!CheckResult("OnSocketWrite", result
))
253 if (result
!= send_buffer_size
) {
254 VLOG(1) << "Sent " << result
<< " chars, expected "
255 << send_buffer_size
<< " chars";
257 discovery_request_cb_
.Run();
260 bool DialServiceImpl::DialSocket::ReadSocket() {
261 DCHECK(thread_checker_
.CalledOnValidThread());
262 if (!socket_
.get()) {
263 VLOG(1) << "Socket not connected.";
268 VLOG(1) << "Already reading.";
272 int result
= net::OK
;
273 bool result_ok
= true;
276 result
= socket_
->RecvFrom(
278 kDialRecvBufferSize
, &recv_address_
,
279 base::Bind(&DialServiceImpl::DialSocket::OnSocketRead
,
280 base::Unretained(this)));
281 result_ok
= CheckResult("RecvFrom", result
);
282 if (result
!= net::ERR_IO_PENDING
)
284 if (result_ok
&& result
> 0) {
286 HandleResponse(result
);
288 } while (result_ok
&& result
!= net::OK
&& result
!= net::ERR_IO_PENDING
);
292 void DialServiceImpl::DialSocket::OnSocketRead(int result
) {
293 DCHECK(thread_checker_
.CalledOnValidThread());
295 if (!CheckResult("OnSocketRead", result
))
298 HandleResponse(result
);
300 // Await next response.
304 void DialServiceImpl::DialSocket::HandleResponse(int bytes_read
) {
305 DCHECK(thread_checker_
.CalledOnValidThread());
306 DCHECK_GT(bytes_read
, 0);
307 if (bytes_read
> kDialRecvBufferSize
) {
308 VLOG(1) << bytes_read
<< " > " << kDialRecvBufferSize
<< "!?";
311 VLOG(2) << "Read " << bytes_read
<< " bytes from "
312 << recv_address_
.ToString();
314 std::string
response(recv_buffer_
->data(), bytes_read
);
315 Time response_time
= Time::Now();
317 // Attempt to parse response, notify observers if successful.
318 DialDeviceData parsed_device
;
319 if (ParseResponse(response
, response_time
, &parsed_device
))
320 device_discovered_cb_
.Run(parsed_device
);
324 bool DialServiceImpl::DialSocket::ParseResponse(
325 const std::string
& response
,
326 const base::Time
& response_time
,
327 DialDeviceData
* device
) {
328 int headers_end
= HttpUtil::LocateEndOfHeaders(response
.c_str(),
330 if (headers_end
< 1) {
331 VLOG(1) << "Headers invalid or empty, ignoring: " << response
;
334 std::string raw_headers
=
335 HttpUtil::AssembleRawHeaders(response
.c_str(), headers_end
);
336 VLOG(3) << "raw_headers: " << raw_headers
<< "\n";
337 scoped_refptr
<HttpResponseHeaders
> headers
=
338 new HttpResponseHeaders(raw_headers
);
340 std::string device_url_str
;
341 if (!GetHeader(headers
.get(), kSsdpLocationHeader
, &device_url_str
) ||
342 device_url_str
.empty()) {
343 VLOG(1) << "No LOCATION header found.";
347 GURL
device_url(device_url_str
);
348 if (!DialDeviceData::IsDeviceDescriptionUrl(device_url
)) {
349 VLOG(1) << "URL " << device_url_str
<< " not valid.";
353 std::string device_id
;
354 if (!GetHeader(headers
.get(), kSsdpUsnHeader
, &device_id
) ||
356 VLOG(1) << "No USN header found.";
360 device
->set_device_id(device_id
);
361 device
->set_device_description_url(device_url
);
362 device
->set_response_time(response_time
);
364 // TODO(mfoltz): Parse the max-age value from the cache control header.
365 // http://crbug.com/165289
366 std::string cache_control
;
367 GetHeader(headers
.get(), kSsdpCacheControlHeader
, &cache_control
);
369 std::string config_id
;
371 if (GetHeader(headers
.get(), kSsdpConfigIdHeader
, &config_id
) &&
372 base::StringToInt(config_id
, &config_id_int
)) {
373 device
->set_config_id(config_id_int
);
375 VLOG(1) << "Malformed or missing " << kSsdpConfigIdHeader
<< ": "
382 DialServiceImpl::DialServiceImpl(net::NetLog
* net_log
)
383 : discovery_active_(false),
384 num_requests_sent_(0),
385 max_requests_(kDialMaxRequests
),
386 finish_delay_(TimeDelta::FromMilliseconds((kDialMaxRequests
- 1) *
387 kDialRequestIntervalMillis
) +
388 TimeDelta::FromSeconds(kDialResponseTimeoutSecs
)),
390 TimeDelta::FromMilliseconds(kDialRequestIntervalMillis
)) {
391 IPAddressNumber address
;
392 bool success
= net::ParseIPLiteralToNumber(kDialRequestAddress
, &address
);
394 send_address_
= net::IPEndPoint(address
, kDialRequestPort
);
395 send_buffer_
= new StringIOBuffer(BuildRequest());
397 net_log_source_
.type
= net::NetLog::SOURCE_UDP_SOCKET
;
398 net_log_source_
.id
= net_log_
->NextID();
401 DialServiceImpl::~DialServiceImpl() {
402 DCHECK(thread_checker_
.CalledOnValidThread());
405 void DialServiceImpl::AddObserver(Observer
* observer
) {
406 DCHECK(thread_checker_
.CalledOnValidThread());
407 observer_list_
.AddObserver(observer
);
410 void DialServiceImpl::RemoveObserver(Observer
* observer
) {
411 DCHECK(thread_checker_
.CalledOnValidThread());
412 observer_list_
.RemoveObserver(observer
);
415 bool DialServiceImpl::HasObserver(const Observer
* observer
) const {
416 DCHECK(thread_checker_
.CalledOnValidThread());
417 return observer_list_
.HasObserver(observer
);
420 bool DialServiceImpl::Discover() {
421 DCHECK(thread_checker_
.CalledOnValidThread());
422 if (discovery_active_
) {
423 VLOG(2) << "Discovery is already active - returning.";
426 discovery_active_
= true;
428 VLOG(2) << "Discovery started.";
434 void DialServiceImpl::StartDiscovery() {
435 DCHECK(thread_checker_
.CalledOnValidThread());
436 DCHECK(discovery_active_
);
437 if (HasOpenSockets()) {
438 VLOG(2) << "Calling StartDiscovery() with open sockets. Returning.";
442 #if defined(OS_CHROMEOS)
443 // The ChromeOS specific version of getting network interfaces does not
444 // require trampolining to another thread, and contains additional interface
445 // information such as interface types (i.e. wifi vs cellular).
446 std::vector
<IPAddressNumber
> chrome_os_address_list
;
447 InsertBestBindAddressChromeOS(chromeos::NetworkTypePattern::Ethernet(),
448 &chrome_os_address_list
);
449 InsertBestBindAddressChromeOS(chromeos::NetworkTypePattern::WiFi(),
450 &chrome_os_address_list
);
451 DiscoverOnAddresses(chrome_os_address_list
);
454 BrowserThread::PostTask(
455 BrowserThread::FILE, FROM_HERE
,
456 base::Bind(&GetNetworkListOnFileThread
,
457 base::ThreadTaskRunnerHandle::Get(),
458 base::Bind(&DialServiceImpl::SendNetworkList
, AsWeakPtr())));
462 void DialServiceImpl::SendNetworkList(const NetworkInterfaceList
& networks
) {
463 DCHECK(thread_checker_
.CalledOnValidThread());
464 typedef std::pair
<uint32
, net::AddressFamily
> InterfaceIndexAddressFamily
;
465 std::set
<InterfaceIndexAddressFamily
> interface_index_addr_family_seen
;
466 std::vector
<IPAddressNumber
> ip_addresses
;
468 // Binds a socket to each IPv4 network interface found. Note that
469 // there may be duplicates in |networks|, so address family + interface index
470 // is used to identify unique interfaces.
471 // TODO(mfoltz): Support IPV6 multicast. http://crbug.com/165286
472 for (NetworkInterfaceList::const_iterator iter
= networks
.begin();
473 iter
!= networks
.end(); ++iter
) {
474 net::AddressFamily addr_family
= net::GetAddressFamily(iter
->address
);
475 VLOG(2) << "Found " << iter
->name
<< ", "
476 << net::IPAddressToString(iter
->address
)
477 << ", address family: " << addr_family
;
478 if (addr_family
== net::ADDRESS_FAMILY_IPV4
) {
479 InterfaceIndexAddressFamily interface_index_addr_family
=
480 std::make_pair(iter
->interface_index
, addr_family
);
481 bool inserted
= interface_index_addr_family_seen
482 .insert(interface_index_addr_family
)
484 // We have not seen this interface before, so add its IP address to the
487 VLOG(2) << "Encountered "
488 << "interface index: " << iter
->interface_index
<< ", "
489 << "address family: " << addr_family
<< " for the first time, "
490 << "adding IP address " << net::IPAddressToString(iter
->address
)
492 ip_addresses
.push_back(iter
->address
);
494 VLOG(2) << "Already encountered "
495 << "interface index: " << iter
->interface_index
<< ", "
496 << "address family: " << addr_family
<< " before, not adding.";
501 DiscoverOnAddresses(ip_addresses
);
504 void DialServiceImpl::DiscoverOnAddresses(
505 const std::vector
<IPAddressNumber
>& ip_addresses
) {
506 if (ip_addresses
.empty()) {
507 VLOG(1) << "Could not find a valid interface to bind. Finishing discovery";
512 // Schedule a timer to finish the discovery process (and close the sockets).
513 if (finish_delay_
> TimeDelta::FromSeconds(0)) {
514 VLOG(2) << "Starting timer to finish discovery.";
515 finish_timer_
.Start(FROM_HERE
,
518 &DialServiceImpl::FinishDiscovery
);
521 for (std::vector
<IPAddressNumber
>::const_iterator iter
= ip_addresses
.begin();
522 iter
!= ip_addresses
.end();
524 BindAndAddSocket(*iter
);
529 void DialServiceImpl::BindAndAddSocket(const IPAddressNumber
& bind_ip_address
) {
530 scoped_ptr
<DialServiceImpl::DialSocket
> dial_socket(CreateDialSocket());
531 if (dial_socket
->CreateAndBindSocket(bind_ip_address
, net_log_
,
533 dial_sockets_
.push_back(dial_socket
.release());
536 scoped_ptr
<DialServiceImpl::DialSocket
> DialServiceImpl::CreateDialSocket() {
537 scoped_ptr
<DialServiceImpl::DialSocket
> dial_socket(
538 new DialServiceImpl::DialSocket(
539 base::Bind(&DialServiceImpl::NotifyOnDiscoveryRequest
, AsWeakPtr()),
540 base::Bind(&DialServiceImpl::NotifyOnDeviceDiscovered
, AsWeakPtr()),
541 base::Bind(&DialServiceImpl::NotifyOnError
, AsWeakPtr())));
542 return dial_socket
.Pass();
545 void DialServiceImpl::SendOneRequest() {
546 DCHECK(thread_checker_
.CalledOnValidThread());
547 if (num_requests_sent_
== max_requests_
) {
548 VLOG(2) << "Reached max requests; stopping request timer.";
549 request_timer_
.Stop();
552 num_requests_sent_
++;
553 VLOG(2) << "Sending request " << num_requests_sent_
<< "/"
555 for (ScopedVector
<DialServiceImpl::DialSocket
>::iterator iter
=
556 dial_sockets_
.begin();
557 iter
!= dial_sockets_
.end();
559 if (!((*iter
)->IsClosed()))
560 (*iter
)->SendOneRequest(send_address_
, send_buffer_
);
564 void DialServiceImpl::NotifyOnDiscoveryRequest() {
565 DCHECK(thread_checker_
.CalledOnValidThread());
566 // If discovery is inactive, no reason to notify observers.
567 if (!discovery_active_
) {
568 VLOG(2) << "Request sent after discovery finished. Ignoring.";
572 VLOG(2) << "Notifying observers of discovery request";
573 FOR_EACH_OBSERVER(Observer
, observer_list_
, OnDiscoveryRequest(this));
574 // If we need to send additional requests, schedule a timer to do so.
575 if (num_requests_sent_
< max_requests_
&& num_requests_sent_
== 1) {
576 VLOG(2) << "Scheduling timer to send additional requests";
577 // TODO(imcheng): Move this to SendOneRequest() once the implications are
579 request_timer_
.Start(FROM_HERE
,
582 &DialServiceImpl::SendOneRequest
);
586 void DialServiceImpl::NotifyOnDeviceDiscovered(
587 const DialDeviceData
& device_data
) {
588 DCHECK(thread_checker_
.CalledOnValidThread());
589 if (!discovery_active_
) {
590 VLOG(2) << "Got response after discovery finished. Ignoring.";
593 FOR_EACH_OBSERVER(Observer
, observer_list_
,
594 OnDeviceDiscovered(this, device_data
));
597 void DialServiceImpl::NotifyOnError() {
598 DCHECK(thread_checker_
.CalledOnValidThread());
599 // TODO(imcheng): Modify upstream so that the device list is not cleared
600 // when it could still potentially discover devices on other sockets.
601 FOR_EACH_OBSERVER(Observer
, observer_list_
,
603 HasOpenSockets() ? DIAL_SERVICE_SOCKET_ERROR
604 : DIAL_SERVICE_NO_INTERFACES
));
607 void DialServiceImpl::FinishDiscovery() {
608 DCHECK(thread_checker_
.CalledOnValidThread());
609 DCHECK(discovery_active_
);
610 VLOG(2) << "Discovery finished.";
611 // Close all open sockets.
612 dial_sockets_
.clear();
613 finish_timer_
.Stop();
614 request_timer_
.Stop();
615 discovery_active_
= false;
616 num_requests_sent_
= 0;
617 FOR_EACH_OBSERVER(Observer
, observer_list_
, OnDiscoveryFinished(this));
620 bool DialServiceImpl::HasOpenSockets() {
621 for (ScopedVector
<DialSocket
>::const_iterator iter
= dial_sockets_
.begin();
622 iter
!= dial_sockets_
.end();
624 if (!((*iter
)->IsClosed()))
630 } // namespace extensions