1 // Copyright (c) 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef QUICHE_QUIC_CORE_QUIC_LEGACY_VERSION_ENCAPSULATOR_H_
6 #define QUICHE_QUIC_CORE_QUIC_LEGACY_VERSION_ENCAPSULATOR_H_
7 
8 #include "absl/strings/string_view.h"
9 #include "net/third_party/quiche/src/quic/core/quic_packet_creator.h"
10 #include "net/third_party/quiche/src/quic/core/quic_packets.h"
11 #include "net/third_party/quiche/src/quic/core/quic_types.h"
12 #include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
13 
14 namespace quic {
15 
16 // QuicLegacyVersionEncapsulator is responsible for encapsulation of packets
17 // using Legacy Version Encapsulation.
18 
19 class QUIC_EXPORT_PRIVATE QuicLegacyVersionEncapsulator
20     : public QuicPacketCreator::DelegateInterface {
21  public:
22   // Encapsulates |inner_packet| into a new encapsulated packet that uses a
23   // CHLO of version LegacyVersionForEncapsulation() with server name |sni|
24   // exposed and using |server_connection_id|. The packet will be padded up to
25   // |outer_max_packet_length| bytes if necessary. On failure, returns 0. On
26   // success, returns the length of the outer encapsulated packet, and copies
27   // the contents of the encapsulated packet to |out|. |out| must point to a
28   // valid memory buffer capable of holding kMaxOutgoingPacketSize bytes.
29   static QuicPacketLength Encapsulate(
30       absl::string_view sni,
31       absl::string_view inner_packet,
32       const QuicConnectionId& server_connection_id,
33       QuicTime creation_time,
34       QuicByteCount outer_max_packet_length,
35       char* out);
36 
37   // Returns the number of bytes of minimum overhead caused by Legacy Version
38   // Encapsulation, based on the length of the provided server name |sni|.
39   // The overhead may be higher due to extra padding added.
40   static QuicByteCount GetMinimumOverhead(absl::string_view sni);
41 
42   // Overrides for QuicPacketCreator::DelegateInterface.
43   QuicPacketBuffer GetPacketBuffer() override;
44   void OnSerializedPacket(SerializedPacket serialized_packet) override;
45   void OnUnrecoverableError(QuicErrorCode error,
46                             const std::string& error_details) override;
47   bool ShouldGeneratePacket(HasRetransmittableData retransmittable,
48                             IsHandshake handshake) override;
49   const QuicFrames MaybeBundleAckOpportunistically() override;
50   SerializedPacketFate GetSerializedPacketFate(
51       bool is_mtu_discovery,
52       EncryptionLevel encryption_level) override;
53 
54   ~QuicLegacyVersionEncapsulator() override;
55 
56  private:
57   explicit QuicLegacyVersionEncapsulator(QuicPacketBuffer packet_buffer);
58 
59   // Disallow copy, move and assignment.
60   QuicLegacyVersionEncapsulator(const QuicLegacyVersionEncapsulator&) = delete;
61   QuicLegacyVersionEncapsulator(QuicLegacyVersionEncapsulator&&) = delete;
62   QuicLegacyVersionEncapsulator& operator=(
63       const QuicLegacyVersionEncapsulator&) = delete;
64   QuicLegacyVersionEncapsulator& operator=(QuicLegacyVersionEncapsulator&&) =
65       delete;
66 
67   QuicPacketBuffer packet_buffer_;
68   QuicPacketLength encrypted_length_ = 0;
69   bool unrecoverable_failure_encountered_ = false;
70 };
71 
72 }  // namespace quic
73 
74 #endif  // QUICHE_QUIC_CORE_QUIC_LEGACY_VERSION_ENCAPSULATOR_H_
75