2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 #ifndef SQUID_PROXYP_HEADER_H
10 #define SQUID_PROXYP_HEADER_H
12 #include "base/RefCount.h"
13 #include "ip/Address.h"
14 #include "proxyp/Elements.h"
15 #include "sbuf/SBuf.h"
17 namespace ProxyProtocol
{
19 /// PROXY protocol v1 or v2 header
20 class Header
: public RefCountable
23 typedef RefCount
<Header
> Pointer
;
24 typedef std::vector
<Two::Tlv
> Tlvs
;
26 Header(const SBuf
&ver
, const Two::Command cmd
);
28 /// HTTP header-like string representation of the header.
29 /// The returned string has one line per pseudo header and
30 /// one line per TLV (if any).
33 /// \returns a delimiter-separated list of values of TLVs of the given type
34 SBuf
getValues(const uint32_t headerType
, const char delimiter
= ',') const;
36 /// Searches for the first key=value pair occurrence within the
37 /// value for the provided TLV type. Assumes that the TLV value
38 /// is a delimiter-separated list.
39 /// \returns the value of the found pair or the empty string.
40 SBuf
getElem(const uint32_t headerType
, const char *member
, const char delimiter
) const;
42 /// PROXY protocol version
43 const SBuf
&version() const { return version_
; }
45 /// whether source and destination addresses are valid addresses of the original "client" connection
46 bool hasForwardedAddresses() const { return !localConnection() && hasAddresses(); }
48 /// marks the header as lacking address information
49 void ignoreAddresses() { ignoreAddresses_
= true; }
51 /// whether the header relays address information (including LOCAL connections)
52 bool hasAddresses() const { return !ignoreAddresses_
; }
54 /// \returns "4" or "6" if both source and destination addresses are IPv4 or IPv6
55 /// \returns "mix" otherwise
56 const SBuf
&addressFamily() const;
58 /// source address of the client connection
59 Ip::Address sourceAddress
;
60 /// intended destination address of the client connection
61 Ip::Address destinationAddress
;
62 /// empty in v1 headers and when ignored in v2 headers
66 /// Whether the connection over PROXY protocol is 'cmdLocal'.
67 /// Such connections are established without being relayed.
68 /// Received addresses and TLVs are discarded in this mode.
69 bool localConnection() const { return command_
== Two::cmdLocal
; }
71 /// PROXY protocol version
74 /// for v2 headers: the command field
75 /// for v1 headers: Two::cmdProxy
76 Two::Command command_
;
78 /// true if the header relays no address information
79 bool ignoreAddresses_
;
82 } // namespace ProxyProtocol