From 10fc12a183b2bf78eaf91b475f634171e8bd12b1 Mon Sep 17 00:00:00 2001 From: Mohd Mehdi Date: Mon, 29 Jul 2024 12:23:33 +0000 Subject: [PATCH] ActivePort: merge with ip.c --- libbbfdm/dmtree/tr181/activeport.c | 182 ----------------------------- libbbfdm/dmtree/tr181/activeport.h | 20 ---- libbbfdm/dmtree/tr181/ip.c | 154 +++++++++++++++++++++++- libbbfdm/dmtree/tr181/ip.h | 1 + 4 files changed, 153 insertions(+), 204 deletions(-) delete mode 100644 libbbfdm/dmtree/tr181/activeport.c delete mode 100644 libbbfdm/dmtree/tr181/activeport.h diff --git a/libbbfdm/dmtree/tr181/activeport.c b/libbbfdm/dmtree/tr181/activeport.c deleted file mode 100644 index 3312a591..00000000 --- a/libbbfdm/dmtree/tr181/activeport.c +++ /dev/null @@ -1,182 +0,0 @@ -/* - * 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: Mohd Husaam Mehdi - */ - -#include -#include -#include -#include -#include - -#include "activeport.h" - -#define STATUS_SIZE 16 - -typedef struct { - char local_ip[INET6_ADDRSTRLEN]; - uint16_t local_port; - char remote_ip[INET6_ADDRSTRLEN]; - uint16_t remote_port; - unsigned int state; -} ActivePort; - -/************************************************************* - * UTILITY METHODS - **************************************************************/ -void format_ipv6_address(const char *hex_str_ip, char *ipv6_addr) -{ - struct in6_addr addr = {}; - - sscanf(hex_str_ip, "%08X%08X%08X%08X", - &addr.s6_addr32[0], &addr.s6_addr32[1], - &addr.s6_addr32[2], &addr.s6_addr32[3]); - - // Convert the address to the standard IPv6 format - inet_ntop(AF_INET6, &addr, ipv6_addr, INET6_ADDRSTRLEN); -} - -void parse_tcp_line(const char* line, int is_ipv6, ActivePort* port) -{ - unsigned int local_port, remote_port; - unsigned int state; - char local_ip[INET6_ADDRSTRLEN] = {0}; - char remote_ip[INET6_ADDRSTRLEN] = {0}; - - if (is_ipv6) { - char local_ip6[33] = {0}, remote_ip6[33] = {0}; - sscanf(line, "%*d: %32s:%4X %32s:%4X %2X", local_ip6, &local_port, remote_ip6, &remote_port, &state); - format_ipv6_address(local_ip6, local_ip); - format_ipv6_address(remote_ip6, remote_ip); - } else { - unsigned int local_ip_num, remote_ip_num; - sscanf(line, "%*d: %8X:%4X %8X:%4X %2X", &local_ip_num, &local_port, &remote_ip_num, &remote_port, &state); - - struct in_addr local_addr = { local_ip_num }; - struct in_addr remote_addr = { remote_ip_num }; - - inet_ntop(AF_INET, &local_addr, local_ip, INET_ADDRSTRLEN); - inet_ntop(AF_INET, &remote_addr, remote_ip, INET_ADDRSTRLEN); - } - - DM_STRNCPY(port->local_ip, local_ip, INET6_ADDRSTRLEN); - port->local_port = local_port; - DM_STRNCPY(port->remote_ip, remote_ip, INET6_ADDRSTRLEN); - port->remote_port = remote_port; - port->state = state; -} - -/************************************************************* - * ENTRY METHOD - **************************************************************/ -static void browse_ip_port(struct dmctx *dmctx, DMNODE *parent_node, bool is_ipv6, const char *proc_path, int *id, char *inst) -{ - if (proc_path == NULL || DM_STRLEN(proc_path) == 0) - return; - - FILE* fp = fopen(proc_path, "r"); - if (fp == NULL) { - return; - } - - char line[256] = {0}; - fgets(line, sizeof(line), fp); // Skip header line - - while (fgets(line, sizeof(line), fp)) { - struct dm_data curr_data = {0}; - - ActivePort port; - memset(&port, 0, sizeof(port)); - parse_tcp_line(line, is_ipv6, &port); - - // only display LISTEN or ESTABLISHED - if (port.state != 1 && port.state != 10) - continue; - - curr_data.additional_data = (void *)(&port); - inst = handle_instance_without_section(dmctx, parent_node, ++(*id)); - - if (DM_LINK_INST_OBJ(dmctx, parent_node, (void *)&curr_data, inst) == DM_STOP) - break; - } - - fclose(fp); -} - -int browseIPActivePortInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance) -{ - char *inst = NULL; - int id = 0; - - browse_ip_port(dmctx, parent_node, 0, "/proc/net/tcp", &id, inst); - browse_ip_port(dmctx, parent_node, 1, "/proc/net/tcp6", &id, inst); - - return 0; -} - -/************************************************************* - * GET & SET PARAM - **************************************************************/ -static int get_IP_ActivePort_LocalIPAddress(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) -{ - *value = dmstrdup(((ActivePort *)((struct dm_data *)data)->additional_data)->local_ip); - return 0; -} - -static int get_IP_ActivePort_LocalPort(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) -{ - dmasprintf(value, "%u", ((ActivePort *)((struct dm_data *)data)->additional_data)->local_port); - return 0; -} - -static int get_IP_ActivePort_RemoteIPAddress(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) -{ - *value = dmstrdup(((ActivePort *)((struct dm_data *)data)->additional_data)->remote_ip); - return 0; -} - -static int get_IP_ActivePort_RemotePort(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) -{ - dmasprintf(value, "%u", ((ActivePort *)((struct dm_data *)data)->additional_data)->remote_port); - return 0; -} - -static int get_IP_ActivePort_Status(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) -{ - unsigned int state = (((ActivePort *)((struct dm_data *)data)->additional_data)->state); - - switch (state) { - case 1: - *value = "ESTABLISHED"; - break; - case 10: - *value = "LISTEN"; - break; - default: - *value = ""; - break; - } - - return 0; -} - -/********************************************************************************************************************************** - * OBJ & PARAM DEFINITION - ***********************************************************************************************************************************/ - -/* *** Device.IP.ActivePort.{i}. *** */ -DMLEAF tIPActivePortParams[] = { -/* PARAM, permission, type, getvalue, setvalue, bbfdm_type */ -{"LocalIPAddress", &DMREAD, DMT_STRING, get_IP_ActivePort_LocalIPAddress, NULL, BBFDM_BOTH}, -{"LocalPort", &DMREAD, DMT_UNINT, get_IP_ActivePort_LocalPort, NULL, BBFDM_BOTH}, -{"RemoteIPAddress", &DMREAD, DMT_STRING, get_IP_ActivePort_RemoteIPAddress, NULL, BBFDM_BOTH}, -{"RemotePort", &DMREAD, DMT_UNINT, get_IP_ActivePort_RemotePort, NULL, BBFDM_BOTH}, -{"Status", &DMREAD, DMT_STRING, get_IP_ActivePort_Status, NULL, BBFDM_BOTH}, -{0} -}; - diff --git a/libbbfdm/dmtree/tr181/activeport.h b/libbbfdm/dmtree/tr181/activeport.h deleted file mode 100644 index 764cef4b..00000000 --- a/libbbfdm/dmtree/tr181/activeport.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * 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: Mohd Husaam Mehdi - */ - -#ifndef __ACTIVE_PORT_H -#define __ACTIVE_PORT_H - -#include "libbbfdm-api/dmcommon.h" - -extern DMLEAF tIPActivePortParams[]; -int browseIPActivePortInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance); - -#endif - diff --git a/libbbfdm/dmtree/tr181/ip.c b/libbbfdm/dmtree/tr181/ip.c index f9bb44db..04e5f10e 100644 --- a/libbbfdm/dmtree/tr181/ip.c +++ b/libbbfdm/dmtree/tr181/ip.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 iopsys Software Solutions AB + * Copyright (C) 2020-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 @@ -7,16 +7,102 @@ * * Author: Anis Ellouze * Author: Amin Ben Romdhane + * Author: Mohd Husaam Mehdi * */ #include "dmlayer.h" #include "ip.h" -#include "activeport.h" + +#define STATUS_SIZE 16 + +typedef struct { + char local_ip[INET6_ADDRSTRLEN]; + uint16_t local_port; + char remote_ip[INET6_ADDRSTRLEN]; + uint16_t remote_port; + unsigned int state; +} ActivePort; /************************************************************* * INIT **************************************************************/ +static void format_ipv6_address(const char *hex_str_ip, char *ipv6_addr) +{ + struct in6_addr addr = {}; + + sscanf(hex_str_ip, "%08X%08X%08X%08X", + &addr.s6_addr32[0], &addr.s6_addr32[1], + &addr.s6_addr32[2], &addr.s6_addr32[3]); + + // Convert the address to the standard IPv6 format + inet_ntop(AF_INET6, &addr, ipv6_addr, INET6_ADDRSTRLEN); +} + +static void parse_tcp_line(const char* line, int is_ipv6, ActivePort* port) +{ + unsigned int local_port, remote_port; + unsigned int state; + char local_ip[INET6_ADDRSTRLEN] = {0}; + char remote_ip[INET6_ADDRSTRLEN] = {0}; + + if (is_ipv6) { + char local_ip6[33] = {0}, remote_ip6[33] = {0}; + sscanf(line, "%*d: %32s:%4X %32s:%4X %2X", local_ip6, &local_port, remote_ip6, &remote_port, &state); + format_ipv6_address(local_ip6, local_ip); + format_ipv6_address(remote_ip6, remote_ip); + } else { + unsigned int local_ip_num, remote_ip_num; + sscanf(line, "%*d: %8X:%4X %8X:%4X %2X", &local_ip_num, &local_port, &remote_ip_num, &remote_port, &state); + + struct in_addr local_addr = { local_ip_num }; + struct in_addr remote_addr = { remote_ip_num }; + + inet_ntop(AF_INET, &local_addr, local_ip, INET_ADDRSTRLEN); + inet_ntop(AF_INET, &remote_addr, remote_ip, INET_ADDRSTRLEN); + } + + DM_STRNCPY(port->local_ip, local_ip, INET6_ADDRSTRLEN); + port->local_port = local_port; + DM_STRNCPY(port->remote_ip, remote_ip, INET6_ADDRSTRLEN); + port->remote_port = remote_port; + port->state = state; +} + +static void browse_ip_port(struct dmctx *dmctx, DMNODE *parent_node, bool is_ipv6, const char *proc_path, int *id, char *inst) +{ + if (proc_path == NULL || DM_STRLEN(proc_path) == 0) + return; + + FILE* fp = fopen(proc_path, "r"); + if (fp == NULL) { + return; + } + + char line[256] = {0}; + fgets(line, sizeof(line), fp); // Skip header line + + while (fgets(line, sizeof(line), fp)) { + struct dm_data curr_data = {0}; + + ActivePort port; + memset(&port, 0, sizeof(port)); + parse_tcp_line(line, is_ipv6, &port); + + // only display LISTEN or ESTABLISHED + if (port.state != 1 && port.state != 10) + continue; + + curr_data.additional_data = (void *)(&port); + inst = handle_instance_without_section(dmctx, parent_node, ++(*id)); + + if (DM_LINK_INST_OBJ(dmctx, parent_node, (void *)&curr_data, inst) == DM_STOP) + break; + } + + fclose(fp); +} + static int init_interface_ip_args(struct dm_data *args, struct uci_section *iface_sec, struct uci_section *dmmap_sec, json_object *json_obj) { args->config_section = iface_sec; @@ -814,6 +900,17 @@ end: return 0; } +static int browseIPActivePortInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance) +{ + char *inst = NULL; + int id = 0; + + browse_ip_port(dmctx, parent_node, 0, "/proc/net/tcp", &id, inst); + browse_ip_port(dmctx, parent_node, 1, "/proc/net/tcp6", &id, inst); + + return 0; +} + /************************************************************* * ADD & DEL OBJ **************************************************************/ @@ -2183,6 +2280,48 @@ static int get_IPInterfaceStats_MulticastPacketsReceived(char *refparam, struct return get_ip_iface_sysfs(data, "statistics/multicast", value); } +static int get_IP_ActivePort_LocalIPAddress(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = dmstrdup(((ActivePort *)((struct dm_data *)data)->additional_data)->local_ip); + return 0; +} + +static int get_IP_ActivePort_LocalPort(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmasprintf(value, "%u", ((ActivePort *)((struct dm_data *)data)->additional_data)->local_port); + return 0; +} + +static int get_IP_ActivePort_RemoteIPAddress(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = dmstrdup(((ActivePort *)((struct dm_data *)data)->additional_data)->remote_ip); + return 0; +} + +static int get_IP_ActivePort_RemotePort(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmasprintf(value, "%u", ((ActivePort *)((struct dm_data *)data)->additional_data)->remote_port); + return 0; +} + +static int get_IP_ActivePort_Status(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + unsigned int state = (((ActivePort *)((struct dm_data *)data)->additional_data)->state); + + switch (state) { + case 1: + *value = "ESTABLISHED"; + break; + case 10: + *value = "LISTEN"; + break; + default: + *value = ""; + break; + } + + return 0; +} /************************************************************* * OPERATE COMMANDS *************************************************************/ @@ -2323,3 +2462,14 @@ DMLEAF tIPInterfaceStatsParams[] = { //{"UnknownProtoPacketsReceived", &DMREAD, DMT_UNINT, get_IPInterfaceStats_UnknownProtoPacketsReceived, NULL, BBFDM_BOTH}, {0} }; + +/* *** Device.IP.ActivePort.{i}. *** */ +DMLEAF tIPActivePortParams[] = { +/* PARAM, permission, type, getvalue, setvalue, bbfdm_type */ +{"LocalIPAddress", &DMREAD, DMT_STRING, get_IP_ActivePort_LocalIPAddress, NULL, BBFDM_BOTH}, +{"LocalPort", &DMREAD, DMT_UNINT, get_IP_ActivePort_LocalPort, NULL, BBFDM_BOTH}, +{"RemoteIPAddress", &DMREAD, DMT_STRING, get_IP_ActivePort_RemoteIPAddress, NULL, BBFDM_BOTH}, +{"RemotePort", &DMREAD, DMT_UNINT, get_IP_ActivePort_RemotePort, NULL, BBFDM_BOTH}, +{"Status", &DMREAD, DMT_STRING, get_IP_ActivePort_Status, NULL, BBFDM_BOTH}, +{0} +}; diff --git a/libbbfdm/dmtree/tr181/ip.h b/libbbfdm/dmtree/tr181/ip.h index 39c0ee5c..97f7fe7f 100644 --- a/libbbfdm/dmtree/tr181/ip.h +++ b/libbbfdm/dmtree/tr181/ip.h @@ -23,6 +23,7 @@ extern DMLEAF tIPInterfaceIPv4AddressParams[]; extern DMLEAF tIPInterfaceIPv6AddressParams[]; extern DMLEAF tIPInterfaceIPv6PrefixParams[]; extern DMLEAF tIPInterfaceStatsParams[]; +extern DMLEAF tIPActivePortParams[]; #endif //__IP_H