xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/frames/quic_connection_close_frame.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2016 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 #include "quiche/quic/core/frames/quic_connection_close_frame.h"
6 
7 #include <memory>
8 
9 #include "quiche/quic/core/quic_constants.h"
10 #include "quiche/quic/core/quic_error_codes.h"
11 #include "quiche/quic/core/quic_types.h"
12 
13 namespace quic {
14 
QuicConnectionCloseFrame(QuicTransportVersion transport_version,QuicErrorCode error_code,QuicIetfTransportErrorCodes ietf_error,std::string error_phrase,uint64_t frame_type)15 QuicConnectionCloseFrame::QuicConnectionCloseFrame(
16     QuicTransportVersion transport_version, QuicErrorCode error_code,
17     QuicIetfTransportErrorCodes ietf_error, std::string error_phrase,
18     uint64_t frame_type)
19     : quic_error_code(error_code), error_details(error_phrase) {
20   if (!VersionHasIetfQuicFrames(transport_version)) {
21     close_type = GOOGLE_QUIC_CONNECTION_CLOSE;
22     wire_error_code = error_code;
23     transport_close_frame_type = 0;
24     return;
25   }
26   QuicErrorCodeToIetfMapping mapping =
27       QuicErrorCodeToTransportErrorCode(error_code);
28   if (ietf_error != NO_IETF_QUIC_ERROR) {
29     wire_error_code = ietf_error;
30   } else {
31     wire_error_code = mapping.error_code;
32   }
33   if (mapping.is_transport_close) {
34     // Maps to a transport close
35     close_type = IETF_QUIC_TRANSPORT_CONNECTION_CLOSE;
36     transport_close_frame_type = frame_type;
37     return;
38   }
39   // Maps to an application close.
40   close_type = IETF_QUIC_APPLICATION_CONNECTION_CLOSE;
41   transport_close_frame_type = 0;
42 }
43 
operator <<(std::ostream & os,const QuicConnectionCloseFrame & connection_close_frame)44 std::ostream& operator<<(
45     std::ostream& os, const QuicConnectionCloseFrame& connection_close_frame) {
46   os << "{ Close type: " << connection_close_frame.close_type;
47   switch (connection_close_frame.close_type) {
48     case IETF_QUIC_TRANSPORT_CONNECTION_CLOSE:
49       os << ", wire_error_code: "
50          << static_cast<QuicIetfTransportErrorCodes>(
51                 connection_close_frame.wire_error_code);
52       break;
53     case IETF_QUIC_APPLICATION_CONNECTION_CLOSE:
54       os << ", wire_error_code: " << connection_close_frame.wire_error_code;
55       break;
56     case GOOGLE_QUIC_CONNECTION_CLOSE:
57       // Do not log, value same as |quic_error_code|.
58       break;
59   }
60   os << ", quic_error_code: "
61      << QuicErrorCodeToString(connection_close_frame.quic_error_code)
62      << ", error_details: '" << connection_close_frame.error_details << "'";
63   if (connection_close_frame.close_type ==
64       IETF_QUIC_TRANSPORT_CONNECTION_CLOSE) {
65     os << ", frame_type: "
66        << static_cast<QuicIetfFrameType>(
67               connection_close_frame.transport_close_frame_type);
68   }
69   os << "}\n";
70   return os;
71 }
72 
73 }  // namespace quic
74