Skip to content

Commit

Permalink
transport-haproxy: use the normal $SOURCEIP, $DESTIP, $DESTPORT macros
Browse files Browse the repository at this point in the history
Instead of using proxy protocol specific name value pairs, set the
addresses in the message's saddr/daddr members.

This should be a lot faster and a lot easier to use.

Signed-off-by: Balazs Scheidler <[email protected]>
  • Loading branch information
bazsi committed Nov 9, 2024
1 parent ba0325e commit 58f020f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 35 deletions.
40 changes: 23 additions & 17 deletions lib/transport/tests/test_transport_haproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,20 @@ teardown(void)
app_shutdown();
}

static void
concat_nv(const gchar *name, const gchar *value, gsize value_len, gpointer user_data)
void
_format_addresses(GString *value, LogTransportAuxData *aux)
{
GString *aux_nv_concated = user_data;
g_string_append_printf(aux_nv_concated, "%s=%s ", name, value);
gchar buf[1024];
if (aux->peer_addr)
{
g_sockaddr_format(aux->peer_addr, buf, sizeof(buf), GSA_FULL);
g_string_append_printf(value, "source=%s ", buf);
}
if (aux->local_addr)
{
g_sockaddr_format(aux->local_addr, buf, sizeof(buf), GSA_FULL);
g_string_append_printf(value, "destination=%s", buf);
}
}

TestSuite(log_transport_proxy, .init = setup, .fini = teardown);
Expand All @@ -58,7 +67,7 @@ typedef struct
{
const gchar *proxy_header;
gboolean valid;
const gchar *aux_values;
const gchar *addresses;
gint proxy_header_len;
} ProtocolHeaderTestParams;

Expand All @@ -71,15 +80,11 @@ ParameterizedTestParameters(log_transport_proxy, test_proxy_protocol_parse_heade
{ "PROXY UNKNOWN extra ignored parameters\r\n", TRUE, "" },
{
"PROXY TCP4 1.1.1.1 2.2.2.2 3333 4444\r\n", TRUE,
.aux_values = "PROXIED_SRCIP=1.1.1.1 PROXIED_DSTIP=2.2.2.2 "
"PROXIED_SRCPORT=3333 PROXIED_DSTPORT=4444 "
"PROXIED_IP_VERSION=4 "
.addresses = "source=AF_INET(1.1.1.1:3333) destination=AF_INET(2.2.2.2:4444)"
},
{
"PROXY TCP6 ::1 ::2 3333 4444\r\n", TRUE,
.aux_values = "PROXIED_SRCIP=::1 PROXIED_DSTIP=::2 "
"PROXIED_SRCPORT=3333 PROXIED_DSTPORT=4444 "
"PROXIED_IP_VERSION=6 "
.addresses = "source=AF_INET6([::1]:3333) destination=AF_INET6([::2]:4444)"
},

/* INVALID PROTO */
Expand Down Expand Up @@ -128,7 +133,7 @@ ParameterizedTestParameters(log_transport_proxy, test_proxy_protocol_parse_heade
/* proxy v2 */
{
"\r\n\r\n\0\r\nQUIT\n!\21\0\f\1\1\1\1\2\2\2\2\2025\255\234", TRUE,
.aux_values = "PROXIED_SRCIP=1.1.1.1 PROXIED_DSTIP=2.2.2.2 PROXIED_SRCPORT=33333 PROXIED_DSTPORT=44444 PROXIED_IP_VERSION=4 ",
.addresses = "source=AF_INET(1.1.1.1:33333) destination=AF_INET(2.2.2.2:44444)",
.proxy_header_len = 28
},
};
Expand Down Expand Up @@ -160,12 +165,13 @@ ParameterizedTest(ProtocolHeaderTestParams *params, log_transport_proxy, test_pr
"This should be %s: \n>>%.*s<<\n (rc=%d, errno=%d)", params->valid ? "valid" : "invalid", proxy_header_len,
params->proxy_header, (gint) rc, errno);

if (rc == 0 && params->aux_values)
if (rc == 0 && params->addresses)
{
GString *aux_nv_concated = g_string_new(NULL);
log_transport_aux_data_foreach(&aux, concat_nv, aux_nv_concated);
cr_assert_str_eq(aux_nv_concated->str, params->aux_values);
g_string_free(aux_nv_concated, TRUE);
GString *addresses = g_string_new(NULL);

_format_addresses(addresses, &aux);
cr_assert_str_eq(addresses->str, params->addresses);
g_string_free(addresses, TRUE);
}


Expand Down
56 changes: 38 additions & 18 deletions lib/transport/transport-haproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ struct _LogTransportHAProxy
int src_port;
int dst_port;
} info;
GSockAddr *src_addr;
GSockAddr *dst_addr;

/* Flag to only process proxy header once */
gboolean proxy_header_processed;
Expand Down Expand Up @@ -507,6 +509,26 @@ _fetch_into_proxy_buffer(LogTransportHAProxy *self)
}
}

static void
_save_addresses(LogTransportHAProxy *self)
{
if (self->info.unknown)
return;

if (self->info.ip_version == 4)
{
self->src_addr = g_sockaddr_inet_new(self->info.src_ip, self->info.src_port);
self->dst_addr = g_sockaddr_inet_new(self->info.dst_ip, self->info.dst_port);
}
else if (self->info.ip_version == 6)
{
self->src_addr = g_sockaddr_inet6_new(self->info.src_ip, self->info.src_port);
self->dst_addr = g_sockaddr_inet6_new(self->info.dst_ip, self->info.dst_port);
}
else
g_assert_not_reached();
}

static Status
_proccess_proxy_header(LogTransportHAProxy *self)
{
Expand All @@ -527,6 +549,7 @@ _proccess_proxy_header(LogTransportHAProxy *self)
if (parsable)
{
msg_trace("PROXY protocol header parsed successfully");
_save_addresses(self);

return STATUS_SUCCESS;
}
Expand All @@ -540,24 +563,11 @@ _proccess_proxy_header(LogTransportHAProxy *self)
static void
_augment_aux_data(LogTransportHAProxy *self, LogTransportAuxData *aux)
{
gchar buf1[8];
gchar buf2[8];
gchar buf3[8];

if (self->info.unknown)
return;

snprintf(buf1, 8, "%i", self->info.src_port);
snprintf(buf2, 8, "%i", self->info.dst_port);
snprintf(buf3, 8, "%i", self->info.ip_version);

log_transport_aux_data_add_nv_pair(aux, "PROXIED_SRCIP", self->info.src_ip);
log_transport_aux_data_add_nv_pair(aux, "PROXIED_DSTIP", self->info.dst_ip);
log_transport_aux_data_add_nv_pair(aux, "PROXIED_SRCPORT", buf1);
log_transport_aux_data_add_nv_pair(aux, "PROXIED_DSTPORT", buf2);
log_transport_aux_data_add_nv_pair(aux, "PROXIED_IP_VERSION", buf3);

return;
if (aux)
{
log_transport_aux_data_set_peer_addr_ref(aux, g_sockaddr_ref(self->src_addr));
log_transport_aux_data_set_local_addr_ref(aux, g_sockaddr_ref(self->dst_addr));
}
}

static gssize
Expand All @@ -584,13 +594,23 @@ _haproxy_read(LogTransport *s, gpointer buf, gsize buflen, LogTransportAuxData *
return log_transport_adapter_read_method(s, buf, buflen, aux);
}

static void
_haproxy_free(LogTransport *s)
{
LogTransportHAProxy *self = (LogTransportHAProxy *) s;
g_sockaddr_unref(self->src_addr);
g_sockaddr_unref(self->dst_addr);
log_transport_free_method(s);
}

LogTransport *
log_transport_haproxy_new(LogTransportIndex base, LogTransportIndex switch_to)
{
LogTransportHAProxy *self = g_new0(LogTransportHAProxy, 1);

log_transport_adapter_init_instance(&self->super, "haproxy", base);
self->super.super.read = _haproxy_read;
self->super.super.free_fn = _haproxy_free;
self->switch_to = switch_to;

return &self->super.super;
Expand Down

0 comments on commit 58f020f

Please sign in to comment.