File: network_scanner.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (133 lines) | stat: -rw-r--r-- 4,191 bytes parent folder | download | duplicates (6)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/smb_client/discovery/network_scanner.h"

#include <map>
#include <vector>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "chrome/browser/ash/smb_client/discovery/host_locator.h"

namespace ash::smb_client {

namespace {

// Returns true if the host with |host_name| already exists in |host_map|.
bool HostExists(const HostMap& host_map, const Hostname& host_name) {
  return host_map.count(host_name);
}

}  // namespace

RequestInfo::RequestInfo(uint32_t remaining_requests,
                         FindHostsCallback callback)
    : remaining_requests(remaining_requests), callback(std::move(callback)) {}

RequestInfo::RequestInfo(RequestInfo&& other) = default;

RequestInfo::~RequestInfo() = default;

NetworkScanner::NetworkScanner() = default;

NetworkScanner::~NetworkScanner() = default;

void NetworkScanner::FindHostsInNetwork(FindHostsCallback callback) {
  DCHECK(!running_);

  if (locators_.empty()) {
    find_hosts_returned_ = true;
    // Fire the callback immediately if there are no registered HostLocators.
    std::move(callback).Run(false /* success */, HostMap());
    return;
  }

  running_ = true;

  const uint32_t request_id = AddNewRequest(std::move(callback));
  for (const auto& locator : locators_) {
    locator->FindHosts(base::BindOnce(&NetworkScanner::OnHostsFound,
                                      weak_ptr_factory_.GetWeakPtr(),
                                      request_id));
  }
}

void NetworkScanner::RegisterHostLocator(std::unique_ptr<HostLocator> locator) {
  locators_.push_back(std::move(locator));
}

net::IPAddress NetworkScanner::ResolveHost(const std::string& host) const {
  DCHECK(find_hosts_returned_);

  const auto& host_iter = found_hosts_.find(base::ToLowerASCII(host));
  if (host_iter == found_hosts_.end()) {
    return {};
  }

  return host_iter->second;
}

void NetworkScanner::OnHostsFound(uint32_t request_id,
                                  bool success,
                                  const HostMap& host_map) {
  DCHECK_GT(requests_.count(request_id), 0u);

  if (success) {
    AddHostsToResults(request_id, host_map);
  }

  FireCallbackIfFinished(request_id);
}

void NetworkScanner::AddHostsToResults(uint32_t request_id,
                                       const HostMap& new_hosts) {
  auto request_iter = requests_.find(request_id);
  DCHECK(request_iter != requests_.end());

  HostMap& existing_hosts = request_iter->second.hosts_found;
  for (const auto& new_host : new_hosts) {
    const Hostname new_hostname = base::ToLowerASCII(new_host.first);
    const Address& new_ip = new_host.second;

    if (!HostExists(existing_hosts, new_hostname)) {
      existing_hosts.insert(std::pair<Hostname, Address>(new_hostname, new_ip));
    } else if (existing_hosts[new_hostname] != new_ip) {
      LOG(WARNING) << "Different addresses found for host: " << new_hostname;
      LOG(WARNING) << "Existing " << existing_hosts[new_hostname].ToString()
                   << " new " << new_ip.ToString();
    }
  }
}

uint32_t NetworkScanner::AddNewRequest(FindHostsCallback callback) {
  const uint32_t request_id = next_request_id_++;
  requests_.emplace(request_id,
                    RequestInfo(locators_.size(), std::move(callback)));
  return request_id;
}

void NetworkScanner::FireCallbackIfFinished(uint32_t request_id) {
  auto request_iter = requests_.find(request_id);
  DCHECK(request_iter != requests_.end());

  uint32_t& remaining_requests = request_iter->second.remaining_requests;
  DCHECK_GT(remaining_requests, 0u);

  if (--remaining_requests == 0) {
    RequestInfo info = std::move(request_iter->second);
    requests_.erase(request_iter);

    // Save the found hosts for name resolution.
    found_hosts_ = std::move(info.hosts_found);
    find_hosts_returned_ = true;

    running_ = false;
    std::move(info.callback).Run(true /* success */, found_hosts_);
  }
}

}  // namespace ash::smb_client