1 /** @file
2 
3   PROXY Protocol
4 
5   See:  https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
6 
7   @section license License
8 
9   Licensed to the Apache Software Foundation (ASF) under one
10   or more contributor license agreements.  See the NOTICE file
11   distributed with this work for additional information
12   regarding copyright ownership.  The ASF licenses this file
13   to you under the Apache License, Version 2.0 (the
14   "License"); you may not use this file except in compliance
15   with the License.  You may obtain a copy of the License at
16 
17       http://www.apache.org/licenses/LICENSE-2.0
18 
19   Unless required by applicable law or agreed to in writing, software
20   distributed under the License is distributed on an "AS IS" BASIS,
21   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   See the License for the specific language governing permissions and
23   limitations under the License.
24  */
25 
26 #pragma once
27 
28 #include <tscore/ink_inet.h>
29 #include <tscpp/util/TextView.h>
30 
31 enum class ProxyProtocolVersion {
32   UNDEFINED,
33   V1,
34   V2,
35 };
36 
37 enum class ProxyProtocolData {
38   UNDEFINED,
39   SRC,
40   DST,
41 };
42 
43 struct ProxyProtocol {
44   ProxyProtocolVersion version = ProxyProtocolVersion::UNDEFINED;
45   uint16_t ip_family           = AF_UNSPEC;
46   IpEndpoint src_addr          = {};
47   IpEndpoint dst_addr          = {};
48 };
49 
50 const size_t PPv1_CONNECTION_HEADER_LEN_MAX = 108;
51 const size_t PPv2_CONNECTION_HEADER_LEN     = 16;
52 
53 extern size_t proxy_protocol_parse(ProxyProtocol *pp_info, ts::TextView tv);
54 extern size_t proxy_protocol_build(uint8_t *buf, size_t max_buf_len, const ProxyProtocol &pp_info,
55                                    ProxyProtocolVersion force_version = ProxyProtocolVersion::UNDEFINED);
56 extern ProxyProtocolVersion proxy_protocol_version_cast(int i);
57