1 /*
2 * Copyright 2023 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty.handler.codec.quic;
17
18 import java.util.Arrays;
19
20 /**
21 * Event that is generated if the remote peer sends a
22 * <a href="https://www.rfc-editor.org/rfc/rfc9000#name-connection_close-frames">CLOSE_CONNECTION frame</a>.
23 * This allows to inspect the various details of the cause of the close.
24 */
25 public final class QuicConnectionCloseEvent implements QuicEvent {
26
27 final boolean applicationClose;
28 final int error;
29 final byte[] reason;
30
31 QuicConnectionCloseEvent(boolean applicationClose, int error, byte[] reason) {
32 this.applicationClose = applicationClose;
33 this.error = error;
34 this.reason = reason;
35 }
36
37 /**
38 * Return {@code true} if this was an application close, {@code false} otherwise.
39 *
40 * @return if this is an application close.
41 */
42 public boolean isApplicationClose() {
43 return applicationClose;
44 }
45
46 /**
47 * Return the error that was provided for the close.
48 *
49 * @return the error.
50 */
51 public int error() {
52 return error;
53 }
54
55 /**
56 * Returns {@code true} if a <a href="https://www.rfc-editor.org/rfc/rfc9001#section-4.8">TLS error</a>
57 * is contained.
58 * @return {@code true} if this is an {@code TLS error}, {@code false} otherwise.
59 */
60 public boolean isTlsError() {
61 return !applicationClose && error >= 0x0100;
62 }
63
64 /**
65 * Returns the reason for the close, which may be empty if no reason was given as part of the close.
66 *
67 * @return the reason.
68 */
69 public byte[] reason() {
70 return reason.clone();
71 }
72
73 @Override
74 public String toString() {
75 return "QuicConnectionCloseEvent{" +
76 "applicationClose=" + applicationClose +
77 ", error=" + error +
78 ", reason=" + Arrays.toString(reason) +
79 '}';
80 }
81
82 /**
83 * Extract the contained {@code TLS error} from the {@code QUIC error}. If the given {@code QUIC error} does not
84 * contain a {@code TLS error} it will return {@code -1}.
85 *
86 * @param error the {@code QUIC error}
87 * @return the {@code TLS error} or {@code -1} if there was no {@code TLS error} contained.
88 */
89 public static int extractTlsError(int error) {
90 int tlsError = error - 0x0100;
91 if (tlsError < 0) {
92 return -1;
93 }
94 return tlsError;
95 }
96 }