Header.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 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 
24 SBuf
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 
39 SBuf
40 ProxyProtocol::Header::getValues(const uint32_t headerType, const char sep) const
41 {
42  switch (headerType) {
43 
45  return version_;
46 
48  return ToSBuf(command_);
49 
50  case Two::htPseudoSrcAddr: {
51  if (!hasAddresses())
52  return SBuf();
53  auto logAddr = sourceAddress;
55  char ipBuf[MAX_IPSTRLEN];
56  return SBuf(logAddr.toStr(ipBuf, sizeof(ipBuf)));
57  }
58 
59  case Two::htPseudoDstAddr: {
60  if (!hasAddresses())
61  return SBuf();
62  char ipBuf[MAX_IPSTRLEN];
63  return SBuf(destinationAddress.toStr(ipBuf, sizeof(ipBuf)));
64  }
65 
66  case Two::htPseudoSrcPort: {
67  return hasAddresses() ? ToSBuf(sourceAddress.port()) : SBuf();
68  }
69 
70  case Two::htPseudoDstPort: {
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 
89 SBuf
90 ProxyProtocol::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 
96 const SBuf &
98 {
99  static const SBuf v4("4");
100  static const SBuf v6("6");
101  static const SBuf vMix("mix");
102  return
105  vMix;
106 }
107 
unsigned short port() const
Definition: Address.cc:778
Ip::Address destinationAddress
intended destination address of the client connection
Definition: Header.h:63
Definition: SBuf.h:86
SBuf getValues(const uint32_t headerType, const char delimiter=',') const
Definition: Header.cc:40
SBuf getListMember(const String &list, const char *key, const char delimiter)
Definition: StrList.cc:135
SBuf ToSBuf(Args &&... args)
slowly stream-prints all arguments into a freshly allocated SBuf
Definition: Stream.h:124
Two::Command command_
Definition: Header.h:78
const SBuf & PseudoFieldTypeToFieldName(const Two::FieldType)
Definition: Elements.cc:40
bool isEmpty() const
Definition: SBuf.h:420
void applyClientMask(const Address &mask)
Definition: Address.cc:105
SBuf getElem(const uint32_t headerType, const char *member, const char delimiter) const
Definition: Header.cc:90
bool isIPv4() const
Definition: Address.cc:158
const SBuf & addressFamily() const
Definition: Header.cc:97
char * toStr(char *buf, const unsigned int blen, int force=AF_UNSPEC) const
Definition: Address.cc:802
String SBufToString(const SBuf &s)
Definition: StringConvert.h:26
bool hasAddresses() const
whether the header relays address information (including LOCAL connections)
Definition: Header.h:54
Ip::Address client_netmask
Definition: SquidConfig.h:242
bool isIPv6() const
Definition: Address.cc:164
class StoreMeta tlv
Definition: StoreMeta.h:15
SBuf version_
PROXY protocol version.
Definition: Header.h:74
struct SquidConfig::@107 Addrs
Header(const SBuf &ver, const Two::Command cmd)
Definition: Header.cc:18
SBuf toMime() const
Definition: Header.cc:25
EnumRangeT< EnumType > EnumRange(EnumType begin, EnumType one_past_end)
Definition: EnumIterator.h:186
#define MAX_IPSTRLEN
Length of buffer that needs to be allocated to old a null-terminated IP-string.
Definition: forward.h:23
Ip::Address sourceAddress
source address of the client connection
Definition: Header.h:61
SBuf buf()
Retrieve a copy of the current stream status.
Definition: Stream.h:105
Command
PROXY protocol &#39;command&#39; field value.
Definition: Elements.h:48
class SquidConfig Config
Definition: SquidConfig.cc:12
#define false
Definition: GnuRegex.c:233
Tlvs tlvs
empty in v1 headers and when ignored in v2 headers
Definition: Header.h:65

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors