/codec-haproxy/src/main/java/io/netty/handler/codec/haproxy/HAProxyProxiedProtocol.java

https://gitlab.com/taichu/netty · Java · 239 lines · 107 code · 21 blank · 111 comment · 2 complexity · 34b7b65939b4539a3bad50f9f55cb119 MD5 · raw file

  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. import static io.netty.handler.codec.haproxy.HAProxyConstants.*;
  18. /**
  19. * A protocol proxied by HAProxy which is represented by its transport protocol and address family.
  20. */
  21. public enum HAProxyProxiedProtocol {
  22. /**
  23. * The UNKNOWN represents a connection which was forwarded for an unknown protocol and an unknown address family.
  24. */
  25. UNKNOWN(TPAF_UNKNOWN_BYTE, AddressFamily.AF_UNSPEC, TransportProtocol.UNSPEC),
  26. /**
  27. * The TCP4 represents a connection which was forwarded for an IPv4 client over TCP.
  28. */
  29. TCP4(TPAF_TCP4_BYTE, AddressFamily.AF_IPv4, TransportProtocol.STREAM),
  30. /**
  31. * The TCP6 represents a connection which was forwarded for an IPv6 client over TCP.
  32. */
  33. TCP6(TPAF_TCP6_BYTE, AddressFamily.AF_IPv6, TransportProtocol.STREAM),
  34. /**
  35. * The UDP4 represents a connection which was forwarded for an IPv4 client over UDP.
  36. */
  37. UDP4(TPAF_UDP4_BYTE, AddressFamily.AF_IPv4, TransportProtocol.DGRAM),
  38. /**
  39. * The UDP6 represents a connection which was forwarded for an IPv6 client over UDP.
  40. */
  41. UDP6(TPAF_UDP6_BYTE, AddressFamily.AF_IPv6, TransportProtocol.DGRAM),
  42. /**
  43. * The UNIX_STREAM represents a connection which was forwarded for a UNIX stream socket.
  44. */
  45. UNIX_STREAM(TPAF_UNIX_STREAM_BYTE, AddressFamily.AF_UNIX, TransportProtocol.STREAM),
  46. /**
  47. * The UNIX_DGRAM represents a connection which was forwarded for a UNIX datagram socket.
  48. */
  49. UNIX_DGRAM(TPAF_UNIX_DGRAM_BYTE, AddressFamily.AF_UNIX, TransportProtocol.DGRAM);
  50. private final byte byteValue;
  51. private final AddressFamily addressFamily;
  52. private final TransportProtocol transportProtocol;
  53. /**
  54. * Creates a new instance.
  55. */
  56. HAProxyProxiedProtocol(
  57. byte byteValue,
  58. AddressFamily addressFamily,
  59. TransportProtocol transportProtocol) {
  60. this.byteValue = byteValue;
  61. this.addressFamily = addressFamily;
  62. this.transportProtocol = transportProtocol;
  63. }
  64. /**
  65. * Returns the {@link HAProxyProxiedProtocol} represented by the specified byte.
  66. *
  67. * @param tpafByte transport protocol and address family byte
  68. */
  69. public static HAProxyProxiedProtocol valueOf(byte tpafByte) {
  70. switch (tpafByte) {
  71. case TPAF_TCP4_BYTE:
  72. return TCP4;
  73. case TPAF_TCP6_BYTE:
  74. return TCP6;
  75. case TPAF_UNKNOWN_BYTE:
  76. return UNKNOWN;
  77. case TPAF_UDP4_BYTE:
  78. return UDP4;
  79. case TPAF_UDP6_BYTE:
  80. return UDP6;
  81. case TPAF_UNIX_STREAM_BYTE:
  82. return UNIX_STREAM;
  83. case TPAF_UNIX_DGRAM_BYTE:
  84. return UNIX_DGRAM;
  85. default:
  86. throw new IllegalArgumentException(
  87. "unknown transport protocol + address family: " + (tpafByte & 0xFF));
  88. }
  89. }
  90. /**
  91. * Returns the byte value of this protocol and address family.
  92. */
  93. public byte byteValue() {
  94. return byteValue;
  95. }
  96. /**
  97. * Returns the {@link AddressFamily} of this protocol and address family.
  98. */
  99. public AddressFamily addressFamily() {
  100. return addressFamily;
  101. }
  102. /**
  103. * Returns the {@link TransportProtocol} of this protocol and address family.
  104. */
  105. public TransportProtocol transportProtocol() {
  106. return transportProtocol;
  107. }
  108. /**
  109. * The address family of an HAProxy proxy protocol header.
  110. */
  111. public enum AddressFamily {
  112. /**
  113. * The UNSPECIFIED address family represents a connection which was forwarded for an unkown protocol.
  114. */
  115. AF_UNSPEC(AF_UNSPEC_BYTE),
  116. /**
  117. * The IPV4 address family represents a connection which was forwarded for an IPV4 client.
  118. */
  119. AF_IPv4(AF_IPV4_BYTE),
  120. /**
  121. * The IPV6 address family represents a connection which was forwarded for an IPV6 client.
  122. */
  123. AF_IPv6(AF_IPV6_BYTE),
  124. /**
  125. * The UNIX address family represents a connection which was forwarded for a unix socket.
  126. */
  127. AF_UNIX(AF_UNIX_BYTE);
  128. /**
  129. * The highest 4 bits of the transport protocol and address family byte contain the address family
  130. */
  131. private static final byte FAMILY_MASK = (byte) 0xf0;
  132. private final byte byteValue;
  133. /**
  134. * Creates a new instance
  135. */
  136. AddressFamily(byte byteValue) {
  137. this.byteValue = byteValue;
  138. }
  139. /**
  140. * Returns the {@link AddressFamily} represented by the highest 4 bits of the specified byte.
  141. *
  142. * @param tpafByte transport protocol and address family byte
  143. */
  144. public static AddressFamily valueOf(byte tpafByte) {
  145. int addressFamily = tpafByte & FAMILY_MASK;
  146. switch((byte) addressFamily) {
  147. case AF_IPV4_BYTE:
  148. return AF_IPv4;
  149. case AF_IPV6_BYTE:
  150. return AF_IPv6;
  151. case AF_UNSPEC_BYTE:
  152. return AF_UNSPEC;
  153. case AF_UNIX_BYTE:
  154. return AF_UNIX;
  155. default:
  156. throw new IllegalArgumentException("unknown address family: " + addressFamily);
  157. }
  158. }
  159. /**
  160. * Returns the byte value of this address family.
  161. */
  162. public byte byteValue() {
  163. return byteValue;
  164. }
  165. }
  166. /**
  167. * The transport protocol of an HAProxy proxy protocol header
  168. */
  169. public enum TransportProtocol {
  170. /**
  171. * The UNSPEC transport protocol represents a connection which was forwarded for an unkown protocol.
  172. */
  173. UNSPEC(TRANSPORT_UNSPEC_BYTE),
  174. /**
  175. * The STREAM transport protocol represents a connection which was forwarded for a TCP connection.
  176. */
  177. STREAM(TRANSPORT_STREAM_BYTE),
  178. /**
  179. * The DGRAM transport protocol represents a connection which was forwarded for a UDP connection.
  180. */
  181. DGRAM(TRANSPORT_DGRAM_BYTE);
  182. /**
  183. * The transport protocol is specified in the lowest 4 bits of the transport protocol and address family byte
  184. */
  185. private static final byte TRANSPORT_MASK = 0x0f;
  186. private final byte transportByte;
  187. /**
  188. * Creates a new instance.
  189. */
  190. TransportProtocol(byte transportByte) {
  191. this.transportByte = transportByte;
  192. }
  193. /**
  194. * Returns the {@link TransportProtocol} represented by the lowest 4 bits of the specified byte.
  195. *
  196. * @param tpafByte transport protocol and address family byte
  197. */
  198. public static TransportProtocol valueOf(byte tpafByte) {
  199. int transportProtocol = tpafByte & TRANSPORT_MASK;
  200. switch ((byte) transportProtocol) {
  201. case TRANSPORT_STREAM_BYTE:
  202. return STREAM;
  203. case TRANSPORT_UNSPEC_BYTE:
  204. return UNSPEC;
  205. case TRANSPORT_DGRAM_BYTE:
  206. return DGRAM;
  207. default:
  208. throw new IllegalArgumentException("unknown transport protocol: " + transportProtocol);
  209. }
  210. }
  211. /**
  212. * Returns the byte value of this transport protocol.
  213. */
  214. public byte byteValue() {
  215. return transportByte;
  216. }
  217. }
  218. }