From: Michael Tremer Date: Sun, 12 Jul 2026 13:29:15 +0000 (+0000) Subject: openvpn: Move the function to escape names into a common place X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3f2e80a8be64f5b5eb16e14d06a96652c9913f48;p=telemetry.git openvpn: Move the function to escape names into a common place Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 9859270..5f08d11 100644 --- a/Makefile.am +++ b/Makefile.am @@ -178,6 +178,8 @@ dist_telemetryd_SOURCES = \ src/daemon/metrics.h \ src/daemon/netlink.c \ src/daemon/netlink.h \ + src/daemon/openvpn.c \ + src/daemon/openvpn.h \ src/daemon/parse.c \ src/daemon/parse.h \ src/daemon/priv.c \ diff --git a/src/daemon/openvpn.c b/src/daemon/openvpn.c new file mode 100644 index 0000000..959c863 --- /dev/null +++ b/src/daemon/openvpn.c @@ -0,0 +1,53 @@ +/*############################################################################# +# # +# telemetryd - The IPFire Telemetry Collection Service # +# Copyright (C) 2026 IPFire Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include +#include + +#include "openvpn.h" + +int __openvpn_escape_name(char* buffer, const size_t length, const char* name) { + const size_t l = strlen(name); + + // Check if the output buffer is long enough + if (l >= length) + return -ENOBUFS; + + const char* src = name; + char* dst = buffer; + + while (*src) { + switch (*src) { + case ' ': + *dst++ = '-'; + src++; + break; + + default: + *dst++ = *src++; + break; + } + } + + // Terminate the output buffer + *dst = '\0'; + + return 0; +} diff --git a/src/daemon/openvpn.h b/src/daemon/openvpn.h new file mode 100644 index 0000000..67dcea7 --- /dev/null +++ b/src/daemon/openvpn.h @@ -0,0 +1,33 @@ +/*############################################################################# +# # +# telemetryd - The IPFire Telemetry Collection Service # +# Copyright (C) 2026 IPFire Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#ifndef TELEMETRY_OPENVPN_H +#define TELEMETRY_OPENVPN_H + +#include + +#define OPENVPN_NAME_MAX 128 + +#define openvpn_escape_name(buffer, name) \ + __openvpn_escape_name(buffer, sizeof(buffer), name) + +int __openvpn_escape_name(char* buffer, const size_t length, const char* name); + +#endif /* TELEMETRY_OPENVPN_H */ diff --git a/src/daemon/sources/openvpn.c b/src/daemon/sources/openvpn.c index 90778b1..770ae30 100644 --- a/src/daemon/sources/openvpn.c +++ b/src/daemon/sources/openvpn.c @@ -21,6 +21,7 @@ #ifdef BUILD_SOURCE_OPENVPN #include "../ctx.h" +#include "../openvpn.h" #include "../source.h" #include "../string.h" #include "openvpn.h" @@ -41,19 +42,6 @@ typedef struct openvpn_client { size_t tx_bytes; } openvpn_client; -static void openvpn_escape_name(char* name) { - for (char* p = name; *p; p++) { - switch (*p) { - case ' ': - *p = '-'; - break; - - default: - break; - } - } -} - // This function ensures that we can parse the comma-separated as well as the // tab-separated status format. static int detect_delimiter(td_ctx* ctx, char* line, openvpn_fields* fields) { @@ -191,6 +179,7 @@ static int openvpn_parse(td_ctx* ctx, td_file* file, unsigned long lineno, char* line, size_t length, void* data) { openvpn_fields* fields = data; openvpn_client client = {}; + char name[OPENVPN_NAME_MAX]; int r; // Detect the delimiter @@ -212,15 +201,15 @@ static int openvpn_parse(td_ctx* ctx, td_file* file, unsigned long lineno, return 0; // Escape the name - openvpn_escape_name(client.common_name); + r = openvpn_escape_name(name, client.common_name); + if (r < 0) + return r; // Submit the client - return td_source_submit_values(fields->source, - client.common_name, VALUES( - VALUE_UINT64("rx_bytes", &client.rx_bytes), - VALUE_UINT64("tx_bytes", &client.tx_bytes) - ) - ); + return td_source_submit_values(fields->source, name, VALUES( + VALUE_UINT64("rx_bytes", &client.rx_bytes), + VALUE_UINT64("tx_bytes", &client.tx_bytes) + )); } return 0;