Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
91 changes: 11 additions & 80 deletions iocore/net/I_NetVConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
#pragma once

#include "ProxyProtocol.h"

#include <string_view>
#include <optional>

Expand Down Expand Up @@ -814,116 +816,45 @@ class NetVConnection : public VConnection, public PluginUserArgs<TS_USER_ARGS_VC
NetVConnection(const NetVConnection &) = delete;
NetVConnection &operator=(const NetVConnection &) = delete;

enum class ProxyProtocolVersion {
UNDEFINED,
V1,
V2,
};

enum class ProxyProtocolData {
UNDEFINED,
SRC,
DST,
};

int
set_proxy_protocol_addr(const ProxyProtocolData src_or_dst, ts::TextView &ip_addr_str)
{
int ret = -1;

if (src_or_dst == ProxyProtocolData::SRC) {
ret = ats_ip_pton(ip_addr_str, &pp_info.src_addr);
} else {
ret = ats_ip_pton(ip_addr_str, &pp_info.dst_addr);
}
return ret;
}

int
set_proxy_protocol_src_addr(ts::TextView src)
{
return set_proxy_protocol_addr(ProxyProtocolData::SRC, src);
}

int
set_proxy_protocol_dst_addr(ts::TextView src)
{
return set_proxy_protocol_addr(ProxyProtocolData::DST, src);
}

int
set_proxy_protocol_port(const ProxyProtocolData src_or_dst, in_port_t port)
{
if (src_or_dst == ProxyProtocolData::SRC) {
pp_info.src_addr.port() = htons(port);
} else {
pp_info.dst_addr.port() = htons(port);
}
return port;
}

int
set_proxy_protocol_src_port(in_port_t port)
{
return set_proxy_protocol_port(ProxyProtocolData::SRC, port);
}

int
set_proxy_protocol_dst_port(in_port_t port)
{
return set_proxy_protocol_port(ProxyProtocolData::DST, port);
}

void
set_proxy_protocol_version(const ProxyProtocolVersion ver)
{
pp_info.proxy_protocol_version = ver;
}

ProxyProtocolVersion
get_proxy_protocol_version()
get_proxy_protocol_version() const
{
return pp_info.proxy_protocol_version;
return pp_info.version;
}

sockaddr const *get_proxy_protocol_addr(const ProxyProtocolData) const;

sockaddr const *
get_proxy_protocol_src_addr()
get_proxy_protocol_src_addr() const
{
return get_proxy_protocol_addr(ProxyProtocolData::SRC);
}

uint16_t
get_proxy_protocol_src_port()
get_proxy_protocol_src_port() const
{
return ats_ip_port_host_order(this->get_proxy_protocol_addr(ProxyProtocolData::SRC));
}

sockaddr const *
get_proxy_protocol_dst_addr()
get_proxy_protocol_dst_addr() const
{
return get_proxy_protocol_addr(ProxyProtocolData::DST);
}

uint16_t
get_proxy_protocol_dst_port()
get_proxy_protocol_dst_port() const
{
return ats_ip_port_host_order(this->get_proxy_protocol_addr(ProxyProtocolData::DST));
};

struct ProxyProtocol {
ProxyProtocolVersion proxy_protocol_version = ProxyProtocolVersion::UNDEFINED;
uint16_t ip_family;
IpEndpoint src_addr;
IpEndpoint dst_addr;
};

ProxyProtocol pp_info;
bool has_proxy_protocol(IOBufferReader *);
bool has_proxy_protocol(char *, int64_t *);

protected:
IpEndpoint local_addr;
IpEndpoint remote_addr;
ProxyProtocol pp_info;

bool got_local_addr = false;
bool got_remote_addr = false;
Expand Down
57 changes: 57 additions & 0 deletions iocore/net/NetVConnection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#include "P_Net.h"
#include "ts/apidefs.h"

////
// NetVConnection
//
Action *
NetVConnection::send_OOB(Continuation *, char *, int)
{
Expand All @@ -45,6 +48,60 @@ NetVConnection::cancel_OOB()
return;
}

/**
PROXY Protocol check with IOBufferReader

If the buffer has PROXY Protocol, it will be consumed by this function.
*/
bool
NetVConnection::has_proxy_protocol(IOBufferReader *reader)
{
char buf[PPv1_CONNECTION_HEADER_LEN_MAX + 1];
ts::TextView tv;
tv.assign(buf, reader->memcpy(buf, sizeof(buf), 0));

size_t len = proxy_protocol_parse(&this->pp_info, tv);

if (len > 0) {
reader->consume(len);
return true;
}

return false;
}

/**
PROXY Protocol check with buffer

If the buffer has PROXY Protocol, it will be consumed by this function.
*/
bool
NetVConnection::has_proxy_protocol(char *buffer, int64_t *bytes_r)
{
ts::TextView tv;
tv.assign(buffer, *bytes_r);

size_t len = proxy_protocol_parse(&this->pp_info, tv);

if (len <= 0) {
*bytes_r = -EAGAIN;
return false;
}

*bytes_r -= len;
if (*bytes_r <= 0) {
*bytes_r = -EAGAIN;
} else {
Debug("ssl", "Moving %" PRId64 " characters remaining in the buffer from %p to %p", *bytes_r, buffer + len, buffer);
memmove(buffer, buffer + len, *bytes_r);
}

return true;
}

////
// NetVCOptions
//
std::string_view
NetVCOptions::get_proto_string() const
{
Expand Down
3 changes: 2 additions & 1 deletion iocore/net/P_NetVConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
*/

#include "I_NetVConnection.h"
#include "ProxyProtocol.h"

inline sockaddr const *
NetVConnection::get_remote_addr()
{
if (!got_remote_addr) {
if (pp_info.proxy_protocol_version != ProxyProtocolVersion::UNDEFINED) {
if (pp_info.version != ProxyProtocolVersion::UNDEFINED) {
set_remote_addr(get_proxy_protocol_src_addr());
} else {
set_remote_addr();
Expand Down
Loading