View Javadoc

1   /*
2    * Copyright 2014 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  package io.netty.handler.codec.haproxy;
17  
18  import static io.netty.handler.codec.haproxy.HAProxyConstants.*;
19  
20  /**
21   * A protocol proxied by HAProxy which is represented by its transport protocol and address family.
22   */
23  public enum HAProxyProxiedProtocol {
24      /**
25       * The UNKNOWN represents a connection which was forwarded for an unknown protocol and an unknown address family.
26       */
27      UNKNOWN(TPAF_UNKNOWN_BYTE, AddressFamily.AF_UNSPEC, TransportProtocol.UNSPEC),
28      /**
29       * The TCP4 represents a connection which was forwarded for an IPv4 client over TCP.
30       */
31      TCP4(TPAF_TCP4_BYTE, AddressFamily.AF_IPv4, TransportProtocol.STREAM),
32      /**
33       * The TCP6 represents a connection which was forwarded for an IPv6 client over TCP.
34       */
35      TCP6(TPAF_TCP6_BYTE, AddressFamily.AF_IPv6, TransportProtocol.STREAM),
36      /**
37       * The UDP4 represents a connection which was forwarded for an IPv4 client over UDP.
38       */
39      UDP4(TPAF_UDP4_BYTE, AddressFamily.AF_IPv4, TransportProtocol.DGRAM),
40      /**
41       * The UDP6 represents a connection which was forwarded for an IPv6 client over UDP.
42       */
43      UDP6(TPAF_UDP6_BYTE, AddressFamily.AF_IPv6, TransportProtocol.DGRAM),
44      /**
45       * The UNIX_STREAM represents a connection which was forwarded for a UNIX stream socket.
46       */
47      UNIX_STREAM(TPAF_UNIX_STREAM_BYTE, AddressFamily.AF_UNIX, TransportProtocol.STREAM),
48      /**
49       * The UNIX_DGRAM represents a connection which was forwarded for a UNIX datagram socket.
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       * Creates a new instance.
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       * Returns the {@link HAProxyProxiedProtocol} represented by the specified byte.
72       *
73       * @param tpafByte transport protocol and address family byte
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       * Returns the byte value of this protocol and address family.
99       */
100     public byte byteValue() {
101         return byteValue;
102     }
103 
104     /**
105      * Returns the {@link AddressFamily} of this protocol and address family.
106      */
107     public AddressFamily addressFamily() {
108         return addressFamily;
109     }
110 
111     /**
112      * Returns the {@link TransportProtocol} of this protocol and address family.
113      */
114     public TransportProtocol transportProtocol() {
115         return transportProtocol;
116     }
117 
118     /**
119      * The address family of an HAProxy proxy protocol header.
120      */
121     public enum AddressFamily {
122         /**
123          * The UNSPECIFIED address family represents a connection which was forwarded for an unknown protocol.
124          */
125         AF_UNSPEC(AF_UNSPEC_BYTE),
126         /**
127          * The IPV4 address family represents a connection which was forwarded for an IPV4 client.
128          */
129         AF_IPv4(AF_IPV4_BYTE),
130         /**
131          * The IPV6 address family represents a connection which was forwarded for an IPV6 client.
132          */
133         AF_IPv6(AF_IPV6_BYTE),
134         /**
135          * The UNIX address family represents a connection which was forwarded for a unix socket.
136          */
137         AF_UNIX(AF_UNIX_BYTE);
138 
139         /**
140          * The highest 4 bits of the transport protocol and address family byte contain the address family
141          */
142         private static final byte FAMILY_MASK = (byte) 0xf0;
143 
144         private final byte byteValue;
145 
146         /**
147          * Creates a new instance
148          */
149         AddressFamily(byte byteValue) {
150             this.byteValue = byteValue;
151         }
152 
153         /**
154          * Returns the {@link AddressFamily} represented by the highest 4 bits of the specified byte.
155          *
156          * @param tpafByte transport protocol and address family byte
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          * Returns the byte value of this address family.
176          */
177         public byte byteValue() {
178             return byteValue;
179         }
180     }
181 
182     /**
183      * The transport protocol of an HAProxy proxy protocol header
184      */
185     public enum TransportProtocol {
186         /**
187          * The UNSPEC transport protocol represents a connection which was forwarded for an unknown protocol.
188          */
189         UNSPEC(TRANSPORT_UNSPEC_BYTE),
190         /**
191          * The STREAM transport protocol represents a connection which was forwarded for a TCP connection.
192          */
193         STREAM(TRANSPORT_STREAM_BYTE),
194         /**
195          * The DGRAM transport protocol represents a connection which was forwarded for a UDP connection.
196          */
197         DGRAM(TRANSPORT_DGRAM_BYTE);
198 
199         /**
200          * The transport protocol is specified in the lowest 4 bits of the transport protocol and address family byte
201          */
202         private static final byte TRANSPORT_MASK = 0x0f;
203 
204         private final byte transportByte;
205 
206         /**
207          * Creates a new instance.
208          */
209         TransportProtocol(byte transportByte) {
210             this.transportByte = transportByte;
211         }
212 
213         /**
214          * Returns the {@link TransportProtocol} represented by the lowest 4 bits of the specified byte.
215          *
216          * @param tpafByte transport protocol and address family byte
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          * Returns the byte value of this transport protocol.
234          */
235         public byte byteValue() {
236             return transportByte;
237         }
238     }
239 }