]> git.ipfire.org Git - thirdparty/squid.git/blob - src/proxyp/Header.h
Log PROXY protocol v2 TLVs; fix PROXY protocol parsing bugs (#342)
[thirdparty/squid.git] / src / proxyp / Header.h
1 /*
2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 #ifndef SQUID_PROXYP_HEADER_H
10 #define SQUID_PROXYP_HEADER_H
11
12 #include "base/RefCount.h"
13 #include "ip/Address.h"
14 #include "proxyp/Elements.h"
15 #include "sbuf/SBuf.h"
16
17 namespace ProxyProtocol {
18
19 /// PROXY protocol v1 or v2 header
20 class Header: public RefCountable
21 {
22 public:
23 typedef RefCount<Header> Pointer;
24 typedef std::vector<Two::Tlv> Tlvs;
25
26 Header(const SBuf &ver, const Two::Command cmd);
27
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).
31 SBuf toMime() const;
32
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;
35
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;
41
42 /// PROXY protocol version
43 const SBuf &version() const { return version_; }
44
45 /// whether source and destination addresses are valid addresses of the original "client" connection
46 bool hasForwardedAddresses() const { return !localConnection() && hasAddresses(); }
47
48 /// marks the header as lacking address information
49 void ignoreAddresses() { ignoreAddresses_ = true; }
50
51 /// whether the header relays address information (including LOCAL connections)
52 bool hasAddresses() const { return !ignoreAddresses_; }
53
54 /// \returns "4" or "6" if both source and destination addresses are IPv4 or IPv6
55 /// \returns "mix" otherwise
56 const SBuf &addressFamily() const;
57
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
63 Tlvs tlvs;
64
65 private:
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; }
70
71 /// PROXY protocol version
72 SBuf version_;
73
74 /// for v2 headers: the command field
75 /// for v1 headers: Two::cmdProxy
76 Two::Command command_;
77
78 /// true if the header relays no address information
79 bool ignoreAddresses_;
80 };
81
82 } // namespace ProxyProtocol
83
84 #endif
85