Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions include/iocore/net/ProxyProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@

#pragma once

#include "tscore/ink_memory.h"
#include <filesystem>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What needs this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk why my editor things filesystem is needed in this file... I'll clean that up

#include <tscore/ink_inet.h>
#include <swoc/TextView.h>
#include <unordered_map>
#include <cstdlib>
#include <optional>
#include <utility>

enum class ProxyProtocolVersion {
UNDEFINED,
Expand Down Expand Up @@ -64,7 +67,23 @@ class ProxyProtocol
: version(pp_ver), ip_family(family), src_addr(src), dst_addr(dst)
{
}
~ProxyProtocol() { ats_free(additional_data); }
ProxyProtocol(const ProxyProtocol &other)
: version(other.version), ip_family(other.ip_family), src_addr(other.src_addr), dst_addr(other.dst_addr)
{
if (!other.additional_data.empty()) {
set_additional_data(other.additional_data);
}
}
ProxyProtocol(ProxyProtocol &&other)
: version(other.version), ip_family(other.ip_family), src_addr(other.src_addr), dst_addr(other.dst_addr)
{
if (!other.additional_data.empty()) {
set_additional_data(other.additional_data);
}
other.additional_data.clear();
other.tlv.clear();
}
~ProxyProtocol() = default;
int set_additional_data(std::string_view data);
void set_ipv4_addrs(in_addr_t src_addr, uint16_t src_port, in_addr_t dst_addr, uint16_t dst_port);
void set_ipv6_addrs(const in6_addr &src_addr, uint16_t src_port, const in6_addr &dst_addr, uint16_t dst_port);
Expand All @@ -78,8 +97,46 @@ class ProxyProtocol
IpEndpoint dst_addr = {};
std::unordered_map<uint8_t, std::string_view> tlv;

ProxyProtocol &
operator=(const ProxyProtocol &other)
{
if (&other == this) {
return *this;
}
version = other.version;
ip_family = other.ip_family;
src_addr = other.src_addr;
dst_addr = other.dst_addr;
if (!other.additional_data.empty()) {
set_additional_data(other.additional_data);
} else {
additional_data.clear();
tlv.clear();
}
return *this;
}

ProxyProtocol &
operator=(ProxyProtocol &&other)
{
version = other.version;
ip_family = other.ip_family;
src_addr = other.src_addr;
dst_addr = other.dst_addr;

additional_data.clear();
tlv.clear();

if (!other.additional_data.empty()) {
set_additional_data(other.additional_data);
}
other.additional_data.clear();
other.tlv.clear();
return *this;
}

private:
char *additional_data = nullptr;
std::string additional_data;
};

const size_t PPv1_CONNECTION_HEADER_LEN_MAX = 108;
Expand Down
4 changes: 4 additions & 0 deletions include/proxy/http/HttpTransact.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <cstddef>

#include "iocore/net/ProxyProtocol.h"
#include "tsutil/DbgCtl.h"
#include "tscore/ink_assert.h"
#include "tscore/ink_platform.h"
Expand Down Expand Up @@ -883,6 +884,9 @@ class HttpTransact
delete[] ranges;
ranges = nullptr;
range_setup = RangeSetup_t::NONE;

// This avoids a potential leak since sometimes this class is not destructed (ClassAllocated via HttpSM)
pp_info.~ProxyProtocol();
return;
}

Expand Down
9 changes: 2 additions & 7 deletions src/iocore/net/ProxyProtocol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -552,14 +552,9 @@ ProxyProtocol::set_additional_data(std::string_view data)
{
uint16_t len = data.length();
Dbg(dbg_ctl_proxyprotocol_v2, "Parsing %d byte additional data", len);
additional_data = static_cast<char *>(ats_malloc(len));
if (additional_data == nullptr) {
Dbg(dbg_ctl_proxyprotocol_v2, "Memory allocation failed");
return -1;
}
data.copy(additional_data, len);
additional_data.assign(data);

const char *p = additional_data;
const char *p = additional_data.data();
const char *end = p + len;
while (p != end) {
if (end - p < 3) {
Expand Down
Loading