summaryrefslogtreecommitdiff
path: root/chromium/net/third_party/quiche/src/quic/masque/masque_server_session.h
blob: 14b8293e9b1b8b9671b9656750bdfb987a8b1017 (plain)
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
134
135
136
137
138
139
140
141
142
143
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef QUICHE_QUIC_MASQUE_MASQUE_SERVER_SESSION_H_
#define QUICHE_QUIC_MASQUE_MASQUE_SERVER_SESSION_H_

#include "quic/core/quic_types.h"
#include "quic/core/quic_udp_socket.h"
#include "quic/masque/masque_compression_engine.h"
#include "quic/masque/masque_server_backend.h"
#include "quic/masque/masque_utils.h"
#include "quic/platform/api/quic_epoll.h"
#include "quic/platform/api/quic_export.h"
#include "quic/tools/quic_simple_server_session.h"

namespace quic {

// QUIC server session for connection to MASQUE proxy.
class QUIC_NO_EXPORT MasqueServerSession
    : public QuicSimpleServerSession,
      public MasqueServerBackend::BackendClient,
      public QuicEpollCallbackInterface {
 public:
  // Interface meant to be implemented by owner of this MasqueServerSession
  // instance.
  class QUIC_NO_EXPORT Visitor {
   public:
    virtual ~Visitor() {}
    // Register a client connection ID as being handled by this session.
    virtual void RegisterClientConnectionId(
        QuicConnectionId client_connection_id,
        MasqueServerSession* masque_server_session) = 0;

    // Unregister a client connection ID.
    virtual void UnregisterClientConnectionId(
        QuicConnectionId client_connection_id) = 0;
  };

  explicit MasqueServerSession(
      MasqueMode masque_mode,
      const QuicConfig& config,
      const ParsedQuicVersionVector& supported_versions,
      QuicConnection* connection,
      QuicSession::Visitor* visitor,
      Visitor* owner,
      QuicEpollServer* epoll_server,
      QuicCryptoServerStreamBase::Helper* helper,
      const QuicCryptoServerConfig* crypto_config,
      QuicCompressedCertsCache* compressed_certs_cache,
      MasqueServerBackend* masque_server_backend);

  // Disallow copy and assign.
  MasqueServerSession(const MasqueServerSession&) = delete;
  MasqueServerSession& operator=(const MasqueServerSession&) = delete;

  // From QuicSession.
  void OnMessageReceived(absl::string_view message) override;
  void OnMessageAcked(QuicMessageId message_id,
                      QuicTime receive_timestamp) override;
  void OnMessageLost(QuicMessageId message_id) override;
  void OnConnectionClosed(const QuicConnectionCloseFrame& frame,
                          ConnectionCloseSource source) override;
  void OnStreamClosed(QuicStreamId stream_id) override;

  // From MasqueServerBackend::BackendClient.
  std::unique_ptr<QuicBackendResponse> HandleMasqueRequest(
      const std::string& masque_path,
      const spdy::Http2HeaderBlock& request_headers,
      const std::string& request_body,
      QuicSimpleServerBackend::RequestHandler* request_handler) override;

  // From QuicEpollCallbackInterface.
  void OnRegistration(QuicEpollServer* eps,
                      QuicUdpSocketFd fd,
                      int event_mask) override;
  void OnModification(QuicUdpSocketFd fd, int event_mask) override;
  void OnEvent(QuicUdpSocketFd fd, QuicEpollEvent* event) override;
  void OnUnregistration(QuicUdpSocketFd fd, bool replaced) override;
  void OnShutdown(QuicEpollServer* eps, QuicUdpSocketFd fd) override;
  std::string Name() const override;

  // Handle packet for client, meant to be called by MasqueDispatcher.
  void HandlePacketFromServer(const ReceivedPacketInfo& packet_info);

  QuicEpollServer* epoll_server() const { return epoll_server_; }

 private:
  // State that the MasqueServerSession keeps for each CONNECT-UDP request.
  class QUIC_NO_EXPORT ConnectUdpServerState
      : public QuicSpdySession::Http3DatagramVisitor {
   public:
    // ConnectUdpServerState takes ownership of |fd|. It will unregister it
    // from |epoll_server| and close the file descriptor when destructed.
    explicit ConnectUdpServerState(
        QuicDatagramFlowId flow_id,
        QuicStreamId stream_id,
        const QuicSocketAddress& target_server_address,
        QuicUdpSocketFd fd,
        MasqueServerSession* masque_session);

    ~ConnectUdpServerState();

    // Disallow copy but allow move.
    ConnectUdpServerState(const ConnectUdpServerState&) = delete;
    ConnectUdpServerState(ConnectUdpServerState&&);
    ConnectUdpServerState& operator=(const ConnectUdpServerState&) = delete;
    ConnectUdpServerState& operator=(ConnectUdpServerState&&);

    QuicDatagramFlowId flow_id() const {
      QUICHE_DCHECK(flow_id_.has_value());
      return *flow_id_;
    }
    QuicStreamId stream_id() const { return stream_id_; }
    const QuicSocketAddress& target_server_address() const {
      return target_server_address_;
    }
    QuicUdpSocketFd fd() const { return fd_; }

    // From QuicSpdySession::Http3DatagramVisitor.
    void OnHttp3Datagram(QuicDatagramFlowId flow_id,
                         absl::string_view payload) override;

   private:
    absl::optional<QuicDatagramFlowId> flow_id_;
    QuicStreamId stream_id_;
    QuicSocketAddress target_server_address_;
    QuicUdpSocketFd fd_;             // Owned.
    MasqueServerSession* masque_session_;  // Unowned.
  };

  MasqueServerBackend* masque_server_backend_;  // Unowned.
  Visitor* owner_;                              // Unowned.
  QuicEpollServer* epoll_server_;               // Unowned.
  MasqueCompressionEngine compression_engine_;
  MasqueMode masque_mode_;
  std::list<ConnectUdpServerState> connect_udp_server_states_;
  bool masque_initialized_ = false;
};

}  // namespace quic

#endif  // QUICHE_QUIC_MASQUE_MASQUE_SERVER_SESSION_H_