summaryrefslogtreecommitdiffstats
path: root/chrome/browser/local_discovery/endpoint_resolver.cc
blob: d0ffaa72cecb196d969aaa51fc904c8a10aa2f0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/local_discovery/endpoint_resolver.h"

#include "base/command_line.h"
#include "base/debug/dump_without_crashing.h"
#include "build/build_config.h"
#include "chrome/browser/local_discovery/service_discovery_shared_client.h"
#include "chrome/common/chrome_switches.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"

namespace local_discovery {

EndpointResolver::EndpointResolver() {
  service_discovery_client_ = ServiceDiscoverySharedClient::GetInstance();
}

EndpointResolver::~EndpointResolver() {}

void EndpointResolver::Start(const std::string& service_name,
                             const ResultCallback& callback) {
  service_resolver_ = service_discovery_client_->CreateServiceResolver(
      service_name, base::Bind(&EndpointResolver::ServiceResolveComplete,
                               base::Unretained(this), callback));
  service_resolver_->StartResolving();
}

void EndpointResolver::ServiceResolveComplete(
    const ResultCallback& callback,
    ServiceResolver::RequestStatus result,
    const ServiceDescription& description) {
  if (result != ServiceResolver::STATUS_SUCCESS)
    return callback.Run(net::IPEndPoint());

  Start(description.address, callback);
}

void EndpointResolver::Start(const net::HostPortPair& address,
                             const ResultCallback& callback) {
#if defined(OS_MACOSX)
  net::IPAddress ip_address;
  if (!ip_address.AssignFromIPLiteral(address.host())) {
    NOTREACHED() << address.ToString();
    // Unexpected, but could be a reason for crbug.com/513505
    base::debug::DumpWithoutCrashing();
    return callback.Run(net::IPEndPoint());
  }

  // OSX already has IP there.
  callback.Run(net::IPEndPoint(ip_address, address.port()));
#else   // OS_MACOSX
  net::AddressFamily address_family = net::ADDRESS_FAMILY_UNSPECIFIED;
  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kPrivetIPv6Only)) {
    address_family = net::ADDRESS_FAMILY_IPV6;
  }

  domain_resolver_ = service_discovery_client_->CreateLocalDomainResolver(
      address.host(), address_family,
      base::Bind(&EndpointResolver::DomainResolveComplete,
                 base::Unretained(this), address.port(), callback));
  domain_resolver_->Start();
#endif  // OS_MACOSX
}

void EndpointResolver::DomainResolveComplete(
    uint16_t port,
    const ResultCallback& callback,
    bool success,
    const net::IPAddress& address_ipv4,
    const net::IPAddress& address_ipv6) {
  if (!success)
    return callback.Run(net::IPEndPoint());

  net::IPAddress address = address_ipv4;
  if (!address.IsValid())
    address = address_ipv6;

  DCHECK(address.IsValid());

  callback.Run(net::IPEndPoint(address, port));
}

}  // namespace local_discovery