Header.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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#include "squid.h"
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"
16#include "StrList.h"
17
19 version_(ver),
20 command_(cmd),
21 ignoreAddresses_(false)
22{}
23
24SBuf
26{
27 SBufStream result;
28 for (const auto fieldType: EnumRange(Two::htPseudoBegin, Two::htPseudoEnd)) {
29 const auto value = getValues(fieldType);
30 if (!value.isEmpty())
31 result << PseudoFieldTypeToFieldName(fieldType) << ": " << value << "\r\n";
32 }
33 // cannot reuse Header::getValues(): need the original TLVs layout
34 for (const auto &tlv: tlvs)
35 result << tlv.type << ": " << tlv.value << "\r\n";
36 return result.buf();
37}
38
39SBuf
40ProxyProtocol::Header::getValues(const uint32_t headerType, const char sep) const
41{
42 switch (headerType) {
43
45 return version_;
46
48 return ToSBuf(command_);
49
51 if (!hasAddresses())
52 return SBuf();
53 auto logAddr = sourceAddress;
54 logAddr.applyClientMask(Config.Addrs.client_netmask);
55 char ipBuf[MAX_IPSTRLEN];
56 return SBuf(logAddr.toStr(ipBuf, sizeof(ipBuf)));
57 }
58
60 if (!hasAddresses())
61 return SBuf();
62 char ipBuf[MAX_IPSTRLEN];
63 return SBuf(destinationAddress.toStr(ipBuf, sizeof(ipBuf)));
64 }
65
67 return hasAddresses() ? ToSBuf(sourceAddress.port()) : SBuf();
68 }
69
71 return hasAddresses() ? ToSBuf(destinationAddress.port()) : SBuf();
72 }
73
74 default: {
75 SBufStream result;
76 for (const auto &m: tlvs) {
77 if (m.type == headerType) {
78 // XXX: result.tellp() always returns -1
79 if (!result.buf().isEmpty())
80 result << sep;
81 result << m.value;
82 }
83 }
84 return result.buf();
85 }
86 }
87}
88
89SBuf
90ProxyProtocol::Header::getElem(const uint32_t headerType, const char *member, const char sep) const
91{
92 const auto whole = SBufToString(getValues(headerType, sep));
93 return getListMember(whole, member, sep);
94}
95
96const SBuf &
98{
99 static const SBuf v4("4");
100 static const SBuf v6("6");
101 static const SBuf vMix("mix");
102 return
103 (sourceAddress.isIPv6() && destinationAddress.isIPv6()) ? v6 :
104 (sourceAddress.isIPv4() && destinationAddress.isIPv4()) ? v4 :
105 vMix;
106}
107
EnumRangeT< EnumType > EnumRange(EnumType begin, EnumType one_past_end)
Definition: EnumIterator.h:192
#define false
Definition: GnuRegex.c:240
class SquidConfig Config
Definition: SquidConfig.cc:12
SBuf getListMember(const String &list, const char *key, const char delimiter)
Definition: StrList.cc:144
String SBufToString(const SBuf &s)
Definition: StringConvert.h:26
SBuf getElem(const uint32_t headerType, const char *member, const char delimiter) const
Definition: Header.cc:90
SBuf toMime() const
Definition: Header.cc:25
SBuf getValues(const uint32_t headerType, const char delimiter=',') const
Definition: Header.cc:40
Header(const SBuf &ver, const Two::Command cmd)
Definition: Header.cc:18
const SBuf & addressFamily() const
Definition: Header.cc:97
SBuf buf()
bytes written so far
Definition: Stream.h:41
Definition: SBuf.h:94
bool isEmpty() const
Definition: SBuf.h:431
struct SquidConfig::@106 Addrs
Ip::Address client_netmask
Definition: SquidConfig.h:240
#define MAX_IPSTRLEN
Length of buffer that needs to be allocated to old a null-terminated IP-string.
Definition: forward.h:25
Command
PROXY protocol 'command' field value.
Definition: Elements.h:48
const SBuf & PseudoFieldTypeToFieldName(const Two::FieldType)
Definition: Elements.cc:40
SBuf ToSBuf(Args &&... args)
slowly stream-prints all arguments into a freshly allocated SBuf
Definition: Stream.h:63

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors