File: netbios_host_locator.h

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 (118 lines) | stat: -rw-r--r-- 4,857 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
// 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_NETBIOS_HOST_LOCATOR_H_
#define CHROME_BROWSER_ASH_SMB_CLIENT_DISCOVERY_NETBIOS_HOST_LOCATOR_H_

#include <list>
#include <string>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/timer/timer.h"
#include "chrome/browser/ash/smb_client/discovery/host_locator.h"
#include "chrome/browser/ash/smb_client/discovery/netbios_client_interface.h"
#include "chromeos/ash/components/dbus/smbprovider/smb_provider_client.h"
#include "net/base/network_interfaces.h"

namespace ash::smb_client {

// Calculates the broadcast address of a network interface.
net::IPAddress CalculateBroadcastAddress(
    const net::NetworkInterface& interface);

// Returns true if a network interface should be used for NetBios discovery.
bool ShouldUseInterface(const net::NetworkInterface& interface);

// HostLocator implementation that uses NetBIOS to locate hosts.
class NetBiosHostLocator final : public HostLocator {
 public:
  using GetInterfacesFunction =
      base::RepeatingCallback<net::NetworkInterfaceList()>;
  using NetBiosClientFactory =
      base::RepeatingCallback<std::unique_ptr<NetBiosClientInterface>()>;

  NetBiosHostLocator(GetInterfacesFunction get_interfaces,
                     NetBiosClientFactory client_factory,
                     SmbProviderClient* smb_provider_client);
  NetBiosHostLocator(GetInterfacesFunction get_interfaces,
                     NetBiosClientFactory client_factory,
                     SmbProviderClient* smb_provider_client,
                     std::unique_ptr<base::OneShotTimer> timer);

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

  ~NetBiosHostLocator() override;

  // HostLocator override.
  void FindHosts(FindHostsCallback callback) override;

 private:
  // Returns a list of network interfaces on the device.
  net::NetworkInterfaceList GetNetworkInterfaceList();

  // Finds hosts on |interface| by constructing a NetBiosClient, performing a
  // NetBios Name Request for the interface.
  void FindHostsOnInterface(const net::NetworkInterface& interface);

  // Creates a NetBiosClient using the |client_factory_|.
  std::unique_ptr<NetBiosClientInterface> CreateClient() const;

  // Executes a name request transaction for |broadcast_address| using the most
  // recently added NetBiosClient in |netbios_clients_|.
  void ExecuteNameRequest(const net::IPAddress& broadcast_address);

  // Callback handler for packets received by the |netbios_clients_|.
  void PacketReceived(const std::vector<uint8_t>& packet,
                      uint16_t transaction_id,
                      const net::IPEndPoint& sender_ip);

  // Callback handler for a request to parse a packet. Adds
  // <hostname, sender_ip> entries to |results_|.
  void OnPacketParsed(const net::IPEndPoint& sender_ip,
                      const std::vector<std::string>& hostnames);

  // Called upon expiration of |timer_|. Deletes all active netbios clients. If
  // there are no |outstanding_parse_requests_|, FinishFindHosts is called which
  // returns the results to the NetworkScanner.
  void StopDiscovery();

  // Runs |callback_| with |results_|, then calls ResetHostLocator to reset the
  // state.
  void FinishFindHosts();

  // Resets the state of the HostLocator so that it can be resued.
  void ResetHostLocator();

  // Helper function to add a <hostname, sender_ip> pair to |results_|.
  void AddHostToResult(const net::IPEndPoint& sender_ip,
                       const std::string& hostname);

  // Checks whether an entry already exists for |hostname| in |results_| with a
  // different |sender_ip|.
  bool WouldOverwriteResult(const net::IPEndPoint& sender_ip,
                            const std::string& hostname) const;

  bool running_ = false;
  bool discovery_done_ = false;
  uint16_t transaction_id_ = 0;
  int32_t outstanding_parse_requests_ = 0;
  GetInterfacesFunction get_interfaces_;
  NetBiosClientFactory client_factory_;
  raw_ptr<SmbProviderClient> smb_provider_client_;
  FindHostsCallback callback_;
  HostMap results_;
  // |netbios_clients_| is a container for storing NetBios clients that are
  // currently performing a NetBios Name Request so that they do not go out of
  // scope. One NetBiosClient exists for each network interface on the device.
  std::list<std::unique_ptr<NetBiosClientInterface>> netbios_clients_;
  std::unique_ptr<base::OneShotTimer> timer_;
  base::WeakPtrFactory<NetBiosHostLocator> weak_ptr_factory_{this};
};

}  // namespace ash::smb_client

#endif  // CHROME_BROWSER_ASH_SMB_CLIENT_DISCOVERY_NETBIOS_HOST_LOCATOR_H_