From b6fcf395fc76561d604875707801ca592d31e354 Mon Sep 17 00:00:00 2001
From: Amin Ben Romdhane <amin.benromdhane@iopsys.eu>
Date: Thu, 31 Oct 2024 10:26:36 +0100
Subject: [PATCH] Added support for NetworkProperties Object

---
 src/Makefile     |   5 ++
 src/deviceinfo.c |   8 ++++
 src/network.c    | 118 +++++++++++++++++++++++++++++++++++++++++++++++
 src/network.h    |  17 +++++++
 4 files changed, 148 insertions(+)
 create mode 100644 src/network.c
 create mode 100644 src/network.h

diff --git a/src/Makefile b/src/Makefile
index 253e5e1..1ab5a52 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -37,6 +37,11 @@ OBJS += reboots.o
 PROG_CFLAGS += -DSYSMNGR_REBOOTS
 endif
 
+ifeq ($(SYSMNGR_NETWORK_PROPERTIES),y)
+OBJS += network.o
+PROG_CFLAGS += -DSYSMNGR_NETWORK_PROPERTIES
+endif
+
 ifeq ($(SYSMNGR_VENDOR_EXTENSIONS),y)
 PROG_CFLAGS += -DSYSMNGR_VENDOR_EXTENSIONS
 endif
diff --git a/src/deviceinfo.c b/src/deviceinfo.c
index db8ea6c..d6c0ad9 100644
--- a/src/deviceinfo.c
+++ b/src/deviceinfo.c
@@ -35,6 +35,10 @@
 #include "supported_dm.h"
 #endif
 
+#ifdef SYSMNGR_NETWORK_PROPERTIES
+#include "network.h"
+#endif
+
 /*************************************************************
 * GET & SET PARAM
 **************************************************************/
@@ -238,6 +242,10 @@ DMOBJ tDeviceInfoObj[] = {
 {"ProcessStatus", &DMREAD, NULL, NULL, NULL, NULL, NULL, NULL, tDeviceInfoProcessStatusObj, tDeviceInfoProcessStatusParams, NULL, BBFDM_BOTH},
 #endif
 
+#ifdef SYSMNGR_NETWORK_PROPERTIES
+{"NetworkProperties", &DMREAD, NULL, NULL, NULL, NULL, NULL, NULL, NULL, tDeviceInfoNetworkPropertiesParams, NULL, BBFDM_BOTH},
+#endif
+
 #ifdef SYSMNGR_SUPPORTED_DATA_MODEL
 {"SupportedDataModel", &DMREAD, NULL, NULL, NULL, browseDeviceInfoSupportedDataModelInst, NULL, NULL, NULL, tDeviceInfoSupportedDataModelParams, NULL, BBFDM_CWMP},
 #endif
diff --git a/src/network.c b/src/network.c
new file mode 100644
index 0000000..efa644b
--- /dev/null
+++ b/src/network.c
@@ -0,0 +1,118 @@
+/*
+ * Copyright (C) 2024 iopsys Software Solutions AB
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation
+ *
+ *	  Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu>
+ *
+ */
+
+#include "utils.h"
+
+#define TCP_WMEM_PATH "/proc/sys/net/ipv4/tcp_wmem"
+#define TCP_CONGESTION_CONTROL_PATH "/proc/sys/net/ipv4/tcp_congestion_control"
+
+/*************************************************************
+* COMMON FUNCTIONS
+**************************************************************/
+static int read_file_content(const char *file_path, char *buffer, size_t buffer_size)
+{
+	int fd = open(file_path, O_RDONLY);
+	if (fd == -1)
+		return -1;
+
+	ssize_t bytes_read = read(fd, buffer, buffer_size - 1);
+	if (bytes_read == -1) {
+		close(fd);
+		return -1;
+	}
+
+	buffer[bytes_read] = '\0';  // Null-terminate the buffer
+	close(fd);
+
+	return 0;
+}
+
+static int read_tcp_write_memory_limits(int *min_size, int *default_size, int *max_size)
+{
+	char buffer[128] = {0};
+
+	// Read TCP memory settings
+	if (read_file_content(TCP_WMEM_PATH, buffer, sizeof(buffer)) == -1)
+		return -1;
+
+	// Parse the memory sizes from the file contents
+	if (sscanf(buffer, "%d %d %d", min_size, default_size, max_size) != 3)
+		return -1;
+
+	return 0;
+}
+
+static int get_supported_congestion_controls(char *output, size_t output_size)
+{
+	const char *delim = " ";
+	char *token, buffer[128];
+	size_t pos = 0;
+
+	// Read available congestion control algorithms
+	if (read_file_content(TCP_CONGESTION_CONTROL_PATH, buffer, sizeof(buffer)) == -1)
+		return -1;
+
+	// Parse the supported algorithms and store them in the output buffer
+	token = strtok(buffer, delim);
+
+	while (token != NULL) {
+		if (strstr(token, "reno"))
+			pos += snprintf(&output[pos], output_size - pos, "Reno,");
+		else if (strstr(token, "cubic"))
+			pos += snprintf(&output[pos], output_size - pos, "Cubic,");
+		else if (strstr(token, "newreno"))
+			pos += snprintf(&output[pos], output_size - pos, "New Reno,");
+		else if (strstr(token, "vegas"))
+			pos += snprintf(&output[pos], output_size - pos, "Vegas,");
+		else if (strstr(token, "tahoe"))
+			pos += snprintf(&output[pos], output_size - pos, "Tahoe,");
+
+		token = strtok(NULL, delim);
+	}
+
+	// Remove the trailing comma, if any
+	if (pos > 0)
+		output[pos - 1] = '\0';
+
+	return 0;
+}
+
+/*************************************************************
+* GET & SET PARAM
+**************************************************************/
+static int get_DeviceInfoNetworkProperties_MaxTCPWindowSize(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
+{
+	int min = 0, def = 0, max = 0;
+
+	int res = read_tcp_write_memory_limits(&min, &def, &max);
+	dmasprintf(value, "%d", !res ? max : 0);
+	return 0;
+}
+
+static int get_DeviceInfoNetworkProperties_TCPImplementation(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
+{
+	char list[256] = {0};
+
+	int res = get_supported_congestion_controls(list, sizeof(list));
+	*value = dmstrdup(!res ? list : "");
+	return 0;
+}
+
+/**********************************************************************************************************************************
+*                                            OBJ & LEAF DEFINITION
+***********************************************************************************************************************************/
+/* *** Device.DeviceInfo.NetworkProperties. *** */
+DMLEAF tDeviceInfoNetworkPropertiesParams[] = {
+/* PARAM, permission, type, getvalue, setvalue, bbfdm_type */
+{"MaxTCPWindowSize", &DMREAD, DMT_UNINT, get_DeviceInfoNetworkProperties_MaxTCPWindowSize, NULL, BBFDM_BOTH},
+{"TCPImplementation", &DMREAD, DMT_STRING, get_DeviceInfoNetworkProperties_TCPImplementation, NULL, BBFDM_BOTH},
+{0}
+};
diff --git a/src/network.h b/src/network.h
new file mode 100644
index 0000000..6a8a68a
--- /dev/null
+++ b/src/network.h
@@ -0,0 +1,17 @@
+/*
+ * Copyright (C) 2024 iopsys Software Solutions AB
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation
+ *
+ *	  Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu>
+ *
+ */
+
+#ifndef __NETWORK_H
+#define __NETWORK_H
+
+extern DMLEAF tDeviceInfoNetworkPropertiesParams[];
+
+#endif //__NETWORK_H
-- 
GitLab