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 <balazs.scheidler@axoflow.com>
  • Loading branch information
bazsi committed Nov 2, 2024
1 parent a967442 commit 7c83f22
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 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 @@ -524,6 +546,8 @@ _proccess_proxy_header(LogTransportHAProxy *self)
? evt_tag_mem("header", self->proxy_header_buff, self->proxy_header_buff_len)
: evt_tag_str("header", "<binary_data>"));

_save_addresses(self);

if (parsable)
{
msg_trace("PROXY protocol header parsed successfully");
Expand All @@ -540,24 +564,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)
{
aux->peer_addr = g_sockaddr_ref(self->src_addr);
aux->local_addr = g_sockaddr_ref(self->dst_addr);
}
}

static gssize
Expand All @@ -584,13 +595,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(LogTransportStack *stack, LogTransportIndex base, LogTransportIndex switch_to)
{
LogTransportHAProxy *self = g_new0(LogTransportHAProxy, 1);

log_transport_adapter_init_instance(&self->super, "haproxy", stack, 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 7c83f22

Please sign in to comment.