]> git.ipfire.org Git - telemetry.git/commitdiff
openvpn: Move the function to escape names into a common place
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 12 Jul 2026 13:29:15 +0000 (13:29 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 12 Jul 2026 13:29:15 +0000 (13:29 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/daemon/openvpn.c [new file with mode: 0644]
src/daemon/openvpn.h [new file with mode: 0644]
src/daemon/sources/openvpn.c

index 9859270d26ccce33e199c90957e5e30e805c3244..5f08d117df18201c5937686256a7dd50c39e4779 100644 (file)
@@ -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 (file)
index 0000000..959c863
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <string.h>
+
+#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 (file)
index 0000000..67dcea7
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef TELEMETRY_OPENVPN_H
+#define TELEMETRY_OPENVPN_H
+
+#include <stddef.h>
+
+#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 */
index 90778b1df8181e8297263712e06de3fe6fc72e09..770ae30acf9d86f4eb39f186b4f1f993d880a9d6 100644 (file)
@@ -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;