From 468b0559185fa2367947434df2d01d9d26453230 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R." Date: Tue, 5 Aug 2025 05:35:27 +0200 Subject: [PATCH] Quiet initilization fo decpcap when output_json. --- src/decpcap.c | 40 +++++++++++++++++++++------------------- src/decpcap.h | 4 ++-- src/decpcap_test.cpp | 2 +- src/libnethogs.cpp | 2 +- src/main.cpp | 3 ++- 5 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/decpcap.c b/src/decpcap.c index 37e2fcf..f2ab655 100644 --- a/src/decpcap.c +++ b/src/decpcap.c @@ -35,7 +35,7 @@ bool catchall = false; /* functions to set up a handle (which is basically just a pcap handle) */ -struct dp_handle *dp_fillhandle(pcap_t *phandle) { +struct dp_handle *dp_fillhandle(pcap_t *phandle, bool quiet) { struct dp_handle *retval = (struct dp_handle *)malloc(sizeof(struct dp_handle)); int i; @@ -47,37 +47,39 @@ struct dp_handle *dp_fillhandle(pcap_t *phandle) { retval->linktype = pcap_datalink(retval->pcap_handle); - switch (retval->linktype) { - case (DLT_EN10MB): - fprintf(stdout, "Ethernet link detected\n"); - break; - case (DLT_PPP): - fprintf(stdout, "PPP link detected\n"); - break; - case (DLT_LINUX_SLL): - fprintf(stdout, "Linux Cooked Socket link detected\n"); - break; - default: - fprintf(stdout, "No PPP or Ethernet link: %d\n", retval->linktype); - // TODO maybe error? or 'other' callback? - break; + if((!quiet)){ + switch (retval->linktype) { + case (DLT_EN10MB): + fprintf(stdout, "Ethernet link detected\n"); + break; + case (DLT_PPP): + fprintf(stdout, "PPP link detected\n"); + break; + case (DLT_LINUX_SLL): + fprintf(stdout, "Linux Cooked Socket link detected\n"); + break; + default: + fprintf(stdout, "No PPP or Ethernet link: %d\n", retval->linktype); + // TODO maybe error? or 'other' callback? + break; + } } return retval; } -struct dp_handle *dp_open_offline(char *fname, char *ebuf) { +struct dp_handle *dp_open_offline(char *fname, char *ebuf, bool quiet) { pcap_t *temp = pcap_open_offline(fname, ebuf); if (temp == NULL) { return NULL; } - return dp_fillhandle(temp); + return dp_fillhandle(temp, quiet); } struct dp_handle *dp_open_live(const char *device, int snaplen, int promisc, - int to_ms, char *filter, char *errbuf) { + int to_ms, char *filter, char *errbuf, bool quiet) { struct bpf_program fp; // compiled filter program bpf_u_int32 maskp; // subnet mask bpf_u_int32 netp; // interface IP @@ -107,7 +109,7 @@ struct dp_handle *dp_open_live(const char *device, int snaplen, int promisc, } } - return dp_fillhandle(temp); + return dp_fillhandle(temp, quiet); } /* function to get packet statistics, e.g. dropped packets */ diff --git a/src/decpcap.h b/src/decpcap.h index 6d2c86f..3be4db6 100644 --- a/src/decpcap.h +++ b/src/decpcap.h @@ -67,8 +67,8 @@ struct dp_handle { /* functions to set up a handle (which is basically just a pcap handle) */ struct dp_handle *dp_open_live(const char *device, int snaplen, int promisc, - int to_ms, char *filter, char *errbuf); -struct dp_handle *dp_open_offline(char *fname, char *ebuf); + int to_ms, char *filter, char *errbuf, bool quiet); +struct dp_handle *dp_open_offline(char *fname, char *ebuf, bool quiet); /* function to get packet statistics, e.g. dropped packets */ diff --git a/src/decpcap_test.cpp b/src/decpcap_test.cpp index 5e7923a..3b51269 100644 --- a/src/decpcap_test.cpp +++ b/src/decpcap_test.cpp @@ -39,7 +39,7 @@ int main(int argc, char **argv) { char *errbuf = new char[DP_ERRBUF_SIZE]; - dp_handle *newhandle = dp_open_offline(argv[1], errbuf); + dp_handle *newhandle = dp_open_offline(argv[1], errbuf, false); dp_addcb(newhandle, dp_packet_tcp, process_tcp); int ret = dp_dispatch(newhandle, -1, NULL, 0); if (ret == -1) { diff --git a/src/libnethogs.cpp b/src/libnethogs.cpp index d088090..ee87fae 100644 --- a/src/libnethogs.cpp +++ b/src/libnethogs.cpp @@ -102,7 +102,7 @@ static int nethogsmonitor_init(int devc, char **devicenames, bool all, char errbuf[PCAP_ERRBUF_SIZE]; dp_handle *newhandle = dp_open_live(current_dev->name, BUFSIZ, promiscuous, - to_ms, filter, errbuf); + to_ms, filter, errbuf, false); if (newhandle != NULL) { dp_addcb(newhandle, dp_packet_ip, process_ip); dp_addcb(newhandle, dp_packet_ip6, process_ip6); diff --git a/src/main.cpp b/src/main.cpp index 1ee9658..a09956e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -254,8 +254,9 @@ int main(int argc, char **argv) { forceExit(false, "getifaddrs failed while establishing local IP."); } + bool quiet = output_json; dp_handle *newhandle = - dp_open_live(current_dev->name, BUFSIZ, promisc, 100, filter, errbuf); + dp_open_live(current_dev->name, BUFSIZ, promisc, 100, filter, errbuf, quiet); if (newhandle != NULL) { dp_addcb(newhandle, dp_packet_ip, process_ip); dp_addcb(newhandle, dp_packet_ip6, process_ip6);