File: tls_prober.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 (127 lines) | stat: -rw-r--r-- 5,555 bytes parent folder | download | duplicates (5)
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
// Copyright 2020 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_NET_NETWORK_DIAGNOSTICS_TLS_PROBER_H_
#define CHROME_BROWSER_ASH_NET_NETWORK_DIAGNOSTICS_TLS_PROBER_H_

#include <optional>

#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/completion_once_callback.h"
#include "net/base/host_port_pair.h"
#include "services/network/public/cpp/network_context_getter.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/public/mojom/tcp_socket.mojom.h"
#include "services/network/public/mojom/tls_socket.mojom.h"

namespace network {
class SimpleHostResolver;
}  // namespace network

namespace ash::network_diagnostics {

// Uses either a TCP or TLS socket to determine whether a socket connection to a
// host can be established. No read or write functionality is exposed by this
// socket. Used by network diagnostics routines.
class TlsProber {
 public:
  // Lists the ways a prober may end. The callback passed into the prober's
  // constructor is invoked while exiting.
  enum ProbeExitEnum {
    kDnsFailure,
    kTcpConnectionFailure,
    kTlsUpgradeFailure,
    kMojoDisconnectFailure,
    kSuccess,
  };
  using OnConnectCompleteOnUIThreadCallback =
      base::OnceCallback<void(int result,
                              const std::optional<net::IPEndPoint>& peer_addr)>;
  using TlsProbeCompleteCallback =
      base::OnceCallback<void(int result, ProbeExitEnum probe_exit_enum)>;

  // Establishes a TCP connection to |host_port_pair|. If |negotiate_tls| is
  // true, the underlying TCP socket upgrades to include TLS support. Note that
  // the constructor will not invoke |callback|, which is passed into TlsProber
  // during construction. This ensures the TlsProber instance is constructed
  // before |callback| is invoked.  The TlsProber must be created on the UI
  // thread and will invoke |callback| on the UI thread.
  // |network_context_getter| will be invoked on the UI thread.
  TlsProber(network::NetworkContextGetter network_context_getter,
            net::HostPortPair host_port_pair,
            bool negotiate_tls,
            TlsProbeCompleteCallback callback);
  TlsProber(const TlsProber&) = delete;
  TlsProber& operator=(const TlsProber&) = delete;
  virtual ~TlsProber();

  // Processes the results of the DNS resolution done by |host_resolver_|.
  void OnHostResolutionComplete(
      int result,
      const net::ResolveErrorInfo&,
      const std::optional<net::AddressList>& resolved_addresses,
      const std::optional<net::HostResolverEndpointResults>&);

 protected:
  // Test-only constructor.
  TlsProber();

 private:
  // On success, upgrades a TCPConnectedSocket to a TLSClientSocket. On failure,
  // invokes the callback passed into the TlsProber instance with a failure
  // response.
  // Note that |receive_stream| and |send_stream|, created on the TCP
  // connection, are only present to fit the callback signature of
  // network::mojom::NetworkContext::CreateTCPConnectedSocketCallback. As error
  // handling has not been set up, the streams should not be used and fall out
  // of scope when this method completes.
  void OnConnectComplete(int result,
                         const std::optional<net::IPEndPoint>& local_addr,
                         const std::optional<net::IPEndPoint>& peer_addr,
                         mojo::ScopedDataPipeConsumerHandle receive_stream,
                         mojo::ScopedDataPipeProducerHandle send_stream);

  // Processes the results of the TLS upgrade.
  // Note that |receive_stream| and |send_stream| are only present to fit the
  // callback signature of
  // network::mojom::NetworkContext::CreateTCPConnectedSocketCallback. As error
  // handling has not been set up, the streams should not be used.
  void OnTlsUpgrade(int result,
                    mojo::ScopedDataPipeConsumerHandle receive_stream,
                    mojo::ScopedDataPipeProducerHandle send_stream,
                    const std::optional<net::SSLInfo>& ssl_info);

  // Handles disconnects on the TCP connected and TLS client remotes.
  void OnDisconnect();

  // Signals the end of the probe. Manages the clean up and returns a response
  // to the caller.
  void OnDone(int result, ProbeExitEnum probe_exit_enum);

  // Gets the active profile-specific network context.
  const network::NetworkContextGetter network_context_getter_;
  // Contains the hostname and port.
  const net::HostPortPair host_port_pair_;
  // Indicates whether TLS support must be added to the underlying socket.
  const bool negotiate_tls_;
  // Host resolver used for DNS lookup.
  std::unique_ptr<network::SimpleHostResolver> host_resolver_;
  // Holds socket if socket was connected via TCP.
  mojo::Remote<network::mojom::TCPConnectedSocket> tcp_connected_socket_remote_;
  // Holds socket if socket was upgraded to TLS.
  mojo::Remote<network::mojom::TLSClientSocket> tls_client_socket_remote_;
  // Stores the callback invoked once probe is complete or interrupted.
  TlsProbeCompleteCallback callback_;
  // WeakPtr is used when posting tasks to |task_runner_| which might outlive
  // |this|.
  base::WeakPtrFactory<TlsProber> weak_factory_{this};
};

}  // namespace ash::network_diagnostics

#endif  // CHROME_BROWSER_ASH_NET_NETWORK_DIAGNOSTICS_TLS_PROBER_H_