1 /*
2 * Copyright 2016 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 * http://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
17 package io.netty.handler.codec.haproxy;
18
19 import io.netty.buffer.ByteBuf;
20 import io.netty.buffer.DefaultByteBufHolder;
21
22 import static io.netty.util.internal.ObjectUtil.*;
23
24 /**
25 * A Type-Length Value (TLV vector) that can be added to the PROXY protocol
26 * to include additional information like SSL information.
27 *
28 * @see HAProxySSLTLV
29 */
30 public class HAProxyTLV extends DefaultByteBufHolder {
31
32 private final Type type;
33 private final byte typeByteValue;
34
35 /**
36 * The registered types a TLV can have regarding the PROXY protocol 1.5 spec
37 */
38 public enum Type {
39 PP2_TYPE_ALPN,
40 PP2_TYPE_AUTHORITY,
41 PP2_TYPE_SSL,
42 PP2_TYPE_SSL_VERSION,
43 PP2_TYPE_SSL_CN,
44 PP2_TYPE_NETNS,
45 /**
46 * A TLV type that is not officially defined in the spec. May be used for nonstandard TLVs
47 */
48 OTHER;
49
50 /**
51 * Returns the the {@link Type} for a specific byte value as defined in the PROXY protocol 1.5 spec
52 * <p>
53 * If the byte value is not an official one, it will return {@link Type#OTHER}.
54 *
55 * @param byteValue the byte for a type
56 *
57 * @return the {@link Type} of a TLV
58 */
59 public static Type typeForByteValue(final byte byteValue) {
60 switch (byteValue) {
61 case 0x01:
62 return PP2_TYPE_ALPN;
63 case 0x02:
64 return PP2_TYPE_AUTHORITY;
65 case 0x20:
66 return PP2_TYPE_SSL;
67 case 0x21:
68 return PP2_TYPE_SSL_VERSION;
69 case 0x22:
70 return PP2_TYPE_SSL_CN;
71 case 0x30:
72 return PP2_TYPE_NETNS;
73 default:
74 return OTHER;
75 }
76 }
77 }
78
79 /**
80 * Creates a new HAProxyTLV
81 *
82 * @param type the {@link Type} of the TLV
83 * @param typeByteValue the byteValue of the TLV. This is especially important if non-standard TLVs are used
84 * @param content the raw content of the TLV
85 */
86 HAProxyTLV(final Type type, final byte typeByteValue, final ByteBuf content) {
87 super(content);
88 checkNotNull(type, "type");
89
90 this.type = type;
91 this.typeByteValue = typeByteValue;
92 }
93
94 /**
95 * Returns the {@link Type} of this TLV
96 */
97 public Type type() {
98 return type;
99 }
100
101 /**
102 * Returns the type of the TLV as byte
103 */
104 public byte typeByteValue() {
105 return typeByteValue;
106 }
107
108 @Override
109 public HAProxyTLV copy() {
110 return replace(content().copy());
111 }
112
113 @Override
114 public HAProxyTLV duplicate() {
115 return replace(content().duplicate());
116 }
117
118 private HAProxyTLV replace(ByteBuf content) {
119 return new HAProxyTLV(type, typeByteValue, content);
120 }
121
122 @Override
123 public HAProxyTLV retain() {
124 super.retain();
125 return this;
126 }
127
128 @Override
129 public HAProxyTLV retain(int increment) {
130 super.retain(increment);
131 return this;
132 }
133 }