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 * 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
17 package io.netty.handler.codec.haproxy;
18
19 import io.netty.buffer.ByteBuf;
20 import io.netty.buffer.Unpooled;
21 import io.netty.util.internal.StringUtil;
22
23 import java.util.Collections;
24 import java.util.List;
25
26 /**
27 * Represents a {@link HAProxyTLV} of the type {@link HAProxyTLV.Type#PP2_TYPE_SSL}.
28 * This TLV encapsulates other TLVs and has additional information like verification information and a client bitfield.
29 */
30 public final class HAProxySSLTLV extends HAProxyTLV {
31
32 private final int verify;
33 private final List<HAProxyTLV> tlvs;
34 private final byte clientBitField;
35
36 /**
37 * Creates a new HAProxySSLTLV
38 *
39 * @param verify the verification result as defined in the specification for the pp2_tlv_ssl struct (see
40 * https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt)
41 * @param clientBitField the bitfield with client information
42 * @param tlvs the encapsulated {@link HAProxyTLV}s
43 */
44 public HAProxySSLTLV(final int verify, final byte clientBitField, final List<HAProxyTLV> tlvs) {
45 this(verify, clientBitField, tlvs, Unpooled.EMPTY_BUFFER);
46 }
47
48 /**
49 * Creates a new HAProxySSLTLV
50 *
51 * @param verify the verification result as defined in the specification for the pp2_tlv_ssl struct (see
52 * https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt)
53 * @param clientBitField the bitfield with client information
54 * @param tlvs the encapsulated {@link HAProxyTLV}s
55 * @param rawContent the raw TLV content
56 */
57 HAProxySSLTLV(final int verify, final byte clientBitField, final List<HAProxyTLV> tlvs, final ByteBuf rawContent) {
58 super(Type.PP2_TYPE_SSL, (byte) 0x20, rawContent);
59
60 this.verify = verify;
61 this.tlvs = Collections.unmodifiableList(tlvs);
62 this.clientBitField = clientBitField;
63 }
64
65 /**
66 * Returns {@code true} if the bit field for PP2_CLIENT_CERT_CONN was set
67 */
68 public boolean isPP2ClientCertConn() {
69 return (clientBitField & 0x2) != 0;
70 }
71
72 /**
73 * Returns {@code true} if the bit field for PP2_CLIENT_SSL was set
74 */
75 public boolean isPP2ClientSSL() {
76 return (clientBitField & 0x1) != 0;
77 }
78
79 /**
80 * Returns {@code true} if the bit field for PP2_CLIENT_CERT_SESS was set
81 */
82 public boolean isPP2ClientCertSess() {
83 return (clientBitField & 0x4) != 0;
84 }
85
86 /**
87 * Returns the client bit field
88 */
89 public byte client() {
90 return clientBitField;
91 }
92
93 /**
94 * Returns the verification result
95 */
96 public int verify() {
97 return verify;
98 }
99
100 /**
101 * Returns an unmodifiable Set of encapsulated {@link HAProxyTLV}s.
102 */
103 public List<HAProxyTLV> encapsulatedTLVs() {
104 return tlvs;
105 }
106
107 @Override
108 int contentNumBytes() {
109 int tlvNumBytes = 0;
110 for (int i = 0; i < tlvs.size(); i++) {
111 tlvNumBytes += tlvs.get(i).totalNumBytes();
112 }
113 return 5 + tlvNumBytes; // clientBit(1) + verify(4) + tlvs
114 }
115
116 @Override
117 public String toString() {
118 return StringUtil.simpleClassName(this) +
119 "(type: " + type() +
120 ", typeByteValue: " + typeByteValue() +
121 ", client: " + client() +
122 ", verify: " + verify() +
123 ", numEncapsulatedTlvs: " + tlvs.size() + ')';
124 }
125 }