File: netbios_client.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 (113 lines) | stat: -rw-r--r-- 3,974 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
// 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_CLIENT_H_
#define CHROME_BROWSER_ASH_SMB_CLIENT_DISCOVERY_NETBIOS_CLIENT_H_

#include <memory>
#include <vector>

#include "base/memory/weak_ptr.h"
#include "chrome/browser/ash/smb_client/discovery/netbios_client_interface.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/mojom/udp_socket.mojom.h"

namespace net {
class IPEndPoint;
}  // namespace net

namespace network::mojom {
class NetworkContext;
}  // namespace network::mojom

namespace chromeos {
class FirewallHole;
}  // namespace chromeos

namespace ash::smb_client {

// NetBiosClient handles a NetBios Name Query Request.
// On construction, the Name Query Request process starts.
//
// Name Query Request Process:
// - A UDP server socket is bound on an open port.
// - A firewall hole is opened on that port.
// - A NetBios Name Query Request Packet is sent to |broadcast_address|
// - Any responses to the NetBios Name Query Request are forwarded to the
//   callback passed in the constructor.
//
// The socket remains open and receives response as long as the instance of this
// class is alive. Upon destruction, the socket and corresponding firewall hole
// are closed.
class NetBiosClient : public network::mojom::UDPSocketListener,
                      public NetBiosClientInterface {
 public:
  using NetBiosResponseCallback = base::RepeatingCallback<
      void(const std::vector<uint8_t>&, uint16_t, const net::IPEndPoint&)>;

  explicit NetBiosClient(network::mojom::NetworkContext* network_context);

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

  ~NetBiosClient() override;

  // NetBiosClientInterface override.
  void ExecuteNameRequest(const net::IPAddress& broadcast_address,
                          uint16_t transaction_id,
                          NetBiosResponseCallback callback) override;

 private:
  // Binds the socket to the wildcard address 0.0.0.0:0
  void BindSocket();

  // Opens a firewall hole for |port| so that response packets can be received.
  void OpenPort(uint16_t port);

  // Sets the socket to allow sending to the broadcast address.
  void SetBroadcast();

  // Creates and sends the NetBios Name Query Response packet.
  void SendPacket();

  // Callback handler for bind. Calls OpenPort.
  void OnBindComplete(int32_t result,
                      const std::optional<net::IPEndPoint>& local_ip);

  // Callback handler for OpenPort. Calls SetBroadcast.
  void OnOpenPortComplete(
      std::unique_ptr<chromeos::FirewallHole> firewall_hole);

  // Callback handler for SetBroadcast. Calls SendPacket.
  void OnSetBroadcastCompleted(int32_t result);

  // Callback handler for SendPacket.
  void OnSendCompleted(int32_t result);

  // network::mojom::UDPSocketListener implementation.
  void OnReceived(int32_t result,
                  const std::optional<net::IPEndPoint>& src_ip,
                  std::optional<base::span<const uint8_t>> data) override;

  // Creates a NetBios Name Query Request packet.
  // https://tools.ietf.org/html/rfc1002
  // Section 4.2.12
  std::vector<uint8_t> GenerateBroadcastPacket();

  bool executed_ = false;
  const net::IPEndPoint bind_address_;
  net::IPEndPoint broadcast_address_;
  uint16_t transaction_id_;
  NetBiosResponseCallback callback_;
  std::unique_ptr<chromeos::FirewallHole> firewall_hole_;
  mojo::Remote<network::mojom::UDPSocket> server_socket_;
  mojo::Receiver<network::mojom::UDPSocketListener> listener_receiver_{this};
  base::WeakPtrFactory<NetBiosClient> weak_ptr_factory_{this};
};

}  // namespace ash::smb_client

#endif  // CHROME_BROWSER_ASH_SMB_CLIENT_DISCOVERY_NETBIOS_CLIENT_H_