summaryrefslogtreecommitdiffstats
path: root/chromium/net/dns/mdns_client.cc
blob: 7b1787485cdfe5b3b5b7245d1c3d71cb2e9c7b6f (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
// Copyright 2013 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 "net/dns/mdns_client.h"

#include "net/base/address_family.h"
#include "net/base/net_errors.h"
#include "net/base/network_interfaces.h"
#include "net/dns/mdns_client_impl.h"
#include "net/dns/public/util.h"
#include "net/log/net_log.h"
#include "net/log/net_log_source.h"

namespace net {

namespace {

int Bind(AddressFamily address_family,
         uint32_t interface_index,
         DatagramServerSocket* socket) {
  socket->AllowAddressSharingForMulticast();
  socket->SetMulticastInterface(interface_index);

  int rv = socket->Listen(dns_util::GetMdnsReceiveEndPoint(address_family));
  if (rv < OK)
    return rv;

  return socket->JoinGroup(
      dns_util::GetMdnsGroupEndPoint(address_family).address());
}

}  // namespace

const base::TimeDelta MDnsTransaction::kTransactionTimeout =
    base::TimeDelta::FromSeconds(3);

// static
std::unique_ptr<MDnsSocketFactory> MDnsSocketFactory::CreateDefault() {
  return std::unique_ptr<MDnsSocketFactory>(new MDnsSocketFactoryImpl);
}

// static
std::unique_ptr<MDnsClient> MDnsClient::CreateDefault() {
  return std::unique_ptr<MDnsClient>(new MDnsClientImpl());
}

InterfaceIndexFamilyList GetMDnsInterfacesToBind() {
  NetworkInterfaceList network_list;
  InterfaceIndexFamilyList interfaces;
  if (!GetNetworkList(&network_list, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES))
    return interfaces;
  for (size_t i = 0; i < network_list.size(); ++i) {
    AddressFamily family = GetAddressFamily(network_list[i].address);
    if (family == ADDRESS_FAMILY_IPV4 || family == ADDRESS_FAMILY_IPV6) {
      interfaces.push_back(
          std::make_pair(network_list[i].interface_index, family));
    }
  }
  std::sort(interfaces.begin(), interfaces.end());
  // Interfaces could have multiple addresses. Filter out duplicate entries.
  interfaces.erase(std::unique(interfaces.begin(), interfaces.end()),
                   interfaces.end());
  return interfaces;
}

std::unique_ptr<DatagramServerSocket> CreateAndBindMDnsSocket(
    AddressFamily address_family,
    uint32_t interface_index,
    NetLog* net_log) {
  std::unique_ptr<DatagramServerSocket> socket(
      new UDPServerSocket(net_log, NetLogSource()));

  int rv = Bind(address_family, interface_index, socket.get());
  if (rv != OK) {
    socket.reset();
    VLOG(1) << "MDNS bind failed, address_family=" << address_family
            << ", error=" << rv;
  }
  return socket;
}

}  // namespace net