File: network_scanner.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (110 lines) | stat: -rw-r--r-- 4,367 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
// 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.

#ifndef CHROME_BROWSER_ASH_SMB_CLIENT_DISCOVERY_NETWORK_SCANNER_H_
#define CHROME_BROWSER_ASH_SMB_CLIENT_DISCOVERY_NETWORK_SCANNER_H_

#include <map>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/ash/smb_client/discovery/host_locator.h"
#include "net/base/ip_address.h"

namespace ash::smb_client {

// Holds the number of in-flight requests and the callback to call once all the
// HostLocators are finished. Also holds the hosts found from the HostLocators
// that have already returned.
struct RequestInfo {
  uint32_t remaining_requests;
  FindHostsCallback callback;
  HostMap hosts_found;

  RequestInfo(uint32_t remaining_requests, FindHostsCallback callback);
  RequestInfo(RequestInfo&& other);

  RequestInfo(const RequestInfo&) = delete;
  RequestInfo& operator=(const RequestInfo&) = delete;

  ~RequestInfo();
};

// NetworkScanner discovers SMB hosts in the local network by querying
// registered HostLocators and aggregating their results. RegisterHostLocator is
// used to register HostLocators that are responsible for finding hosts.
// FindHostsInNetwork is called to get a list of discoverable hosts in the
// network. ResolveHost is used to get the IP address of a given host.
class NetworkScanner {
 public:
  NetworkScanner();

  NetworkScanner(const NetworkScanner&) = delete;
  NetworkScanner& operator=(const NetworkScanner&) = delete;

  ~NetworkScanner();

  // Query the registered HostLocators and return all the hosts found.
  // |callback| is called once all the HostLocators have responded with their
  // results. If there are no locators, the callback is fired immediately with
  // an empty result and success set to false. Once this call has returned, the
  // hosts found are cached locally and are resolvable individually through
  // ResolveHost().
  void FindHostsInNetwork(FindHostsCallback callback);

  // Registeres a |locator| to be queried when FindHostsInNetwork() is called.
  void RegisterHostLocator(std::unique_ptr<HostLocator> locator);

  // Resolves |host| to an address using the cached results of
  // FindHostsInNetwork(). FindHostsInNetwork() has to be called beforehand. If
  // no address is found, this returns an invalid IPAddress.
  net::IPAddress ResolveHost(const std::string& host) const;

 private:
  // Callback handler for HostLocator::FindHosts().
  void OnHostsFound(uint32_t request_id, bool success, const HostMap& host_map);

  // Adds |host_map| hosts to current results. The host will not be added if the
  // hostname already exists in results, and if the IP address does not match,
  // it will be logged.
  void AddHostsToResults(uint32_t request_id, const HostMap& host_map);

  // Adds a new request to track and saves |callback| to be called when the
  // request is finished. Returns the request id.
  uint32_t AddNewRequest(FindHostsCallback callback);

  // Called after a HostLocator returns with results and decrements the count of
  // requests in RequestInfo for |request_id|. Fires the callback for if
  // there are no more requests and deletes the corresponding RequestInfo.
  void FireCallbackIfFinished(uint32_t request_id);

  std::vector<std::unique_ptr<HostLocator>> locators_;

  // Used for tracking in-flight requests to HostLocators. The key is the
  // request id, and the value is the RequestInfo struct.
  std::map<uint32_t, RequestInfo> requests_;

  uint32_t next_request_id_ = 0;

  // Hosts that are found from FindHostsInNetwork(). This is cached for name
  // resolution when calling ResolveHost().
  HostMap found_hosts_;

  // True if FindHostsInNetwork() has been called and returned results
  // regardless if any hosts are found.
  bool find_hosts_returned_ = false;

  // True if FindHostsInNetwork() has been called and is waiting for
  // FindHostsCallback to be invoked. This is to prevent multiple calls of
  // FindHostsInNetwork() from concurrently executing. Used only for DCHECKing
  // if FindHostsInNetwork() is already running.
  bool running_ = false;

  base::WeakPtrFactory<NetworkScanner> weak_ptr_factory_{this};
};

}  // namespace ash::smb_client

#endif  // CHROME_BROWSER_ASH_SMB_CLIENT_DISCOVERY_NETWORK_SCANNER_H_