]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/proxyp/Header.cc
2 * Copyright (C) 1996-2021 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.
10 #include "base/EnumIterator.h"
11 #include "proxyp/Elements.h"
12 #include "proxyp/Header.h"
13 #include "sbuf/Stream.h"
14 #include "sbuf/StringConvert.h"
15 #include "SquidConfig.h"
18 ProxyProtocol::Header::Header(const SBuf
&ver
, const Two::Command cmd
):
21 ignoreAddresses_(false)
25 ProxyProtocol::Header::toMime() const
28 for (const auto fieldType
: EnumRange(Two::htPseudoBegin
, Two::htPseudoEnd
)) {
29 const auto value
= getValues(fieldType
);
31 result
<< PseudoFieldTypeToFieldName(fieldType
) << ": " << value
<< "\r\n";
33 // cannot reuse Header::getValues(): need the original TLVs layout
34 for (const auto &tlv
: tlvs
)
35 result
<< tlv
.type
<< ": " << tlv
.value
<< "\r\n";
40 ProxyProtocol::Header::getValues(const uint32_t headerType
, const char sep
) const
44 case Two::htPseudoVersion
:
47 case Two::htPseudoCommand
:
48 return ToSBuf(command_
);
50 case Two::htPseudoSrcAddr
: {
53 auto logAddr
= sourceAddress
;
54 logAddr
.applyClientMask(Config
.Addrs
.client_netmask
);
55 char ipBuf
[MAX_IPSTRLEN
];
56 return SBuf(logAddr
.toStr(ipBuf
, sizeof(ipBuf
)));
59 case Two::htPseudoDstAddr
: {
62 char ipBuf
[MAX_IPSTRLEN
];
63 return SBuf(destinationAddress
.toStr(ipBuf
, sizeof(ipBuf
)));
66 case Two::htPseudoSrcPort
: {
67 return hasAddresses() ? ToSBuf(sourceAddress
.port()) : SBuf();
70 case Two::htPseudoDstPort
: {
71 return hasAddresses() ? ToSBuf(destinationAddress
.port()) : SBuf();
76 for (const auto &m
: tlvs
) {
77 if (m
.type
== headerType
) {
78 // XXX: result.tellp() always returns -1
79 if (!result
.buf().isEmpty())
90 ProxyProtocol::Header::getElem(const uint32_t headerType
, const char *member
, const char sep
) const
92 const auto whole
= SBufToString(getValues(headerType
, sep
));
93 return getListMember(whole
, member
, sep
);
97 ProxyProtocol::Header::addressFamily() const
99 static const SBuf
v4("4");
100 static const SBuf
v6("6");
101 static const SBuf
vMix("mix");
103 (sourceAddress
.isIPv6() && destinationAddress
.isIPv6()) ? v6
:
104 (sourceAddress
.isIPv4() && destinationAddress
.isIPv4()) ? v4
: