aboutsummaryrefslogtreecommitdiff
path: root/example/reader_util.c
diff options
context:
space:
mode:
authorFábio Depin <fabio@seti.com.br>2025-07-02 02:41:55 -0300
committerGitHub <noreply@github.com>2025-07-02 07:41:55 +0200
commit4eff2cdb9991eed51e7e00e4dcc2f5fa1955cf17 (patch)
tree33cb5d858bfae49cae07c7e8f004bcc8460e1c7d /example/reader_util.c
parent17e1ed00c597f2493ef5ad9587e341a8aea5c6e6 (diff)
Refactor: make src_name/dst_name dynamically allocated to reduce RAM usage (#2908)
- Changed ndpi_flow_info: replaced fixed-size char arrays (always INET6_ADDRSTRLEN) for src_name and dst_name with char* pointers. - Now IPv4 flows use only INET_ADDRSTRLEN when needed, instead of always reserving IPv6 size.
Diffstat (limited to 'example/reader_util.c')
-rw-r--r--example/reader_util.c47
1 files changed, 31 insertions, 16 deletions
diff --git a/example/reader_util.c b/example/reader_util.c
index 26145b91a..edfe7bad1 100644
--- a/example/reader_util.c
+++ b/example/reader_util.c
@@ -207,8 +207,10 @@ void ndpi_payload_analyzer(struct ndpi_flow_info *flow,
#ifdef DEBUG_PAYLOAD
printf("[hashval: %u][proto: %u][vlan: %u][%s:%u <-> %s:%u][direction: %s][payload_len: %u]\n",
flow->hashval, flow->protocol, flow->vlan_id,
- flow->src_name, flow->src_port,
- flow->dst_name, flow->dst_port,
+ flow->src_name ? flow->src_name : "",
+ flow->src_port,
+ flow->dst_name ? flow->dst_name : "",
+ flow->dst_port,
src_to_dst_direction ? "s2d" : "d2s",
payload_len);
#endif
@@ -602,6 +604,8 @@ void ndpi_flow_info_free_data(struct ndpi_flow_info *flow) {
ndpi_free_bin(&flow->payload_len_bin);
#endif
+ if(flow->src_name) ndpi_free(flow->src_name);
+ if(flow->dst_name) ndpi_free(flow->dst_name);
if(flow->tcp_fingerprint) ndpi_free(flow->tcp_fingerprint);
if(flow->risk_str) ndpi_free(flow->risk_str);
if(flow->flow_payload) ndpi_free(flow->flow_payload);
@@ -913,18 +917,29 @@ static struct ndpi_flow_info *get_ndpi_flow_info(struct ndpi_workflow * workflow
ndpi_init_bin(&newflow->payload_len_bin, ndpi_bin_family8, PLEN_NUM_BINS);
#endif
- if(version == IPVERSION) {
- inet_ntop(AF_INET, &newflow->src_ip, newflow->src_name, sizeof(newflow->src_name));
- inet_ntop(AF_INET, &newflow->dst_ip, newflow->dst_name, sizeof(newflow->dst_name));
- } else {
- newflow->src_ip6 = *(struct ndpi_in6_addr *)&iph6->ip6_src;
- inet_ntop(AF_INET6, &newflow->src_ip6,
- newflow->src_name, sizeof(newflow->src_name));
- newflow->dst_ip6 = *(struct ndpi_in6_addr *)&iph6->ip6_dst;
- inet_ntop(AF_INET6, &newflow->dst_ip6,
- newflow->dst_name, sizeof(newflow->dst_name));
- /* For consistency across platforms replace :0: with :: */
- ndpi_patchIPv6Address(newflow->src_name), ndpi_patchIPv6Address(newflow->dst_name);
+ if (version == 4 || version == 6) {
+ uint16_t inet_addrlen = (version == 4) ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN;
+ newflow->src_name = ndpi_malloc(inet_addrlen);
+ newflow->dst_name = ndpi_malloc(inet_addrlen);
+
+ if(version == 4) {
+ if (newflow->src_name)
+ inet_ntop(AF_INET, &newflow->src_ip, newflow->src_name, inet_addrlen);
+ if (newflow->dst_name)
+ inet_ntop(AF_INET, &newflow->dst_ip, newflow->dst_name, inet_addrlen);
+ } else if (version == 6) {
+ newflow->src_ip6 = *(struct ndpi_in6_addr *)&iph6->ip6_src;
+ newflow->dst_ip6 = *(struct ndpi_in6_addr *)&iph6->ip6_dst;
+
+ if (newflow->src_name)
+ inet_ntop(AF_INET6, &newflow->src_ip6, newflow->src_name, inet_addrlen);
+ if (newflow->dst_name)
+ inet_ntop(AF_INET6, &newflow->dst_ip6, newflow->dst_name, inet_addrlen);
+
+ /* For consistency across platforms replace :0: with :: */
+ if (newflow->src_name) ndpi_patchIPv6Address(newflow->src_name);
+ if (newflow->dst_name) ndpi_patchIPv6Address(newflow->dst_name);
+ }
}
if((newflow->ndpi_flow = ndpi_flow_malloc(SIZEOF_FLOW_STRUCT)) == NULL) {
@@ -1117,9 +1132,9 @@ static void dump_flow_fingerprint(struct ndpi_workflow * workflow,
u_int32_t buffer_len;
ndpi_serialize_string_uint32(&serializer, "proto", flow->protocol);
- ndpi_serialize_string_string(&serializer, "cli_ip", flow->src_name);
+ ndpi_serialize_string_string(&serializer, "cli_ip", flow->src_name ? flow->src_name : "");
ndpi_serialize_string_uint32(&serializer, "cli_port", ntohs(flow->src_port));
- ndpi_serialize_string_string(&serializer, "srv_ip", flow->dst_name);
+ ndpi_serialize_string_string(&serializer, "srv_ip", flow->dst_name ? flow->dst_name : "");
ndpi_serialize_string_uint32(&serializer, "srv_port", ntohs(flow->dst_port));
ndpi_serialize_string_string(&serializer, "proto",
ndpi_protocol2name(workflow->ndpi_struct,