1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.haproxy;
17
18 import static io.netty.handler.codec.haproxy.HAProxyConstants.*;
19
20
21
22
23 public enum HAProxyProxiedProtocol {
24
25
26
27 UNKNOWN(TPAF_UNKNOWN_BYTE, AddressFamily.AF_UNSPEC, TransportProtocol.UNSPEC),
28
29
30
31 TCP4(TPAF_TCP4_BYTE, AddressFamily.AF_IPv4, TransportProtocol.STREAM),
32
33
34
35 TCP6(TPAF_TCP6_BYTE, AddressFamily.AF_IPv6, TransportProtocol.STREAM),
36
37
38
39 UDP4(TPAF_UDP4_BYTE, AddressFamily.AF_IPv4, TransportProtocol.DGRAM),
40
41
42
43 UDP6(TPAF_UDP6_BYTE, AddressFamily.AF_IPv6, TransportProtocol.DGRAM),
44
45
46
47 UNIX_STREAM(TPAF_UNIX_STREAM_BYTE, AddressFamily.AF_UNIX, TransportProtocol.STREAM),
48
49
50
51 UNIX_DGRAM(TPAF_UNIX_DGRAM_BYTE, AddressFamily.AF_UNIX, TransportProtocol.DGRAM);
52
53 private final byte byteValue;
54 private final AddressFamily addressFamily;
55 private final TransportProtocol transportProtocol;
56
57
58
59
60 HAProxyProxiedProtocol(
61 byte byteValue,
62 AddressFamily addressFamily,
63 TransportProtocol transportProtocol) {
64
65 this.byteValue = byteValue;
66 this.addressFamily = addressFamily;
67 this.transportProtocol = transportProtocol;
68 }
69
70
71
72
73
74
75 public static HAProxyProxiedProtocol valueOf(byte tpafByte) {
76 switch (tpafByte) {
77 case TPAF_TCP4_BYTE:
78 return TCP4;
79 case TPAF_TCP6_BYTE:
80 return TCP6;
81 case TPAF_UNKNOWN_BYTE:
82 return UNKNOWN;
83 case TPAF_UDP4_BYTE:
84 return UDP4;
85 case TPAF_UDP6_BYTE:
86 return UDP6;
87 case TPAF_UNIX_STREAM_BYTE:
88 return UNIX_STREAM;
89 case TPAF_UNIX_DGRAM_BYTE:
90 return UNIX_DGRAM;
91 default:
92 throw new IllegalArgumentException(
93 "unknown transport protocol + address family: " + (tpafByte & 0xFF));
94 }
95 }
96
97
98
99
100 public byte byteValue() {
101 return byteValue;
102 }
103
104
105
106
107 public AddressFamily addressFamily() {
108 return addressFamily;
109 }
110
111
112
113
114 public TransportProtocol transportProtocol() {
115 return transportProtocol;
116 }
117
118
119
120
121 public enum AddressFamily {
122
123
124
125 AF_UNSPEC(AF_UNSPEC_BYTE),
126
127
128
129 AF_IPv4(AF_IPV4_BYTE),
130
131
132
133 AF_IPv6(AF_IPV6_BYTE),
134
135
136
137 AF_UNIX(AF_UNIX_BYTE);
138
139
140
141
142 private static final byte FAMILY_MASK = (byte) 0xf0;
143
144 private final byte byteValue;
145
146
147
148
149 AddressFamily(byte byteValue) {
150 this.byteValue = byteValue;
151 }
152
153
154
155
156
157
158 public static AddressFamily valueOf(byte tpafByte) {
159 int addressFamily = tpafByte & FAMILY_MASK;
160 switch((byte) addressFamily) {
161 case AF_IPV4_BYTE:
162 return AF_IPv4;
163 case AF_IPV6_BYTE:
164 return AF_IPv6;
165 case AF_UNSPEC_BYTE:
166 return AF_UNSPEC;
167 case AF_UNIX_BYTE:
168 return AF_UNIX;
169 default:
170 throw new IllegalArgumentException("unknown address family: " + addressFamily);
171 }
172 }
173
174
175
176
177 public byte byteValue() {
178 return byteValue;
179 }
180 }
181
182
183
184
185 public enum TransportProtocol {
186
187
188
189 UNSPEC(TRANSPORT_UNSPEC_BYTE),
190
191
192
193 STREAM(TRANSPORT_STREAM_BYTE),
194
195
196
197 DGRAM(TRANSPORT_DGRAM_BYTE);
198
199
200
201
202 private static final byte TRANSPORT_MASK = 0x0f;
203
204 private final byte transportByte;
205
206
207
208
209 TransportProtocol(byte transportByte) {
210 this.transportByte = transportByte;
211 }
212
213
214
215
216
217
218 public static TransportProtocol valueOf(byte tpafByte) {
219 int transportProtocol = tpafByte & TRANSPORT_MASK;
220 switch ((byte) transportProtocol) {
221 case TRANSPORT_STREAM_BYTE:
222 return STREAM;
223 case TRANSPORT_UNSPEC_BYTE:
224 return UNSPEC;
225 case TRANSPORT_DGRAM_BYTE:
226 return DGRAM;
227 default:
228 throw new IllegalArgumentException("unknown transport protocol: " + transportProtocol);
229 }
230 }
231
232
233
234
235 public byte byteValue() {
236 return transportByte;
237 }
238 }
239 }