diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2020-08-15 12:35:49 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2020-08-15 12:35:49 +0200 |
commit | b31fde4bbbfe58f7ca2ca14fa6be2895ab115824 (patch) | |
tree | 1a04647f6cc4e619f905d5a4f5fb42541cd015fe | |
parent | 00b27633999ce2a439101ff1e00261cfc8e072ae (diff) |
Replaced obsolete libpcap pcap_lookupdev with pcap_findalldevs.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | example/ndpiSimpleIntegration.c | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/example/ndpiSimpleIntegration.c b/example/ndpiSimpleIntegration.c index d5bd117d8..0b022efaf 100644 --- a/example/ndpiSimpleIntegration.c +++ b/example/ndpiSimpleIntegration.c @@ -9,6 +9,7 @@ #include <signal.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> #define MAX_FLOW_ROOTS_PER_THREAD 2048 @@ -133,7 +134,8 @@ static struct nDPI_workflow * init_workflow(char const * const file_or_device) } if (workflow->pcap_handle == NULL) { - fprintf(stderr, "pcap_open_live / pcap_open_offline_with_tstamp_precision: %s\n", pcap_error_buffer); + fprintf(stderr, "pcap_open_live / pcap_open_offline_with_tstamp_precision: %.*s\n", + (int) PCAP_ERRBUF_SIZE, pcap_error_buffer); free_workflow(&workflow); return NULL; } @@ -204,9 +206,25 @@ static void free_workflow(struct nDPI_workflow ** const workflow) *workflow = NULL; } +static char * get_default_pcapdev(char *errbuf) +{ + char * ifname; + pcap_if_t * all_devices = NULL; + + if (pcap_findalldevs(&all_devices, errbuf) != 0) + { + return NULL; + } + + ifname = strdup(all_devices[0].name); + pcap_freealldevs(all_devices); + + return ifname; +} + static int setup_reader_threads(char const * const file_or_device) { - char const * file_or_default_device; + char * file_or_default_device; char pcap_error_buffer[PCAP_ERRBUF_SIZE]; if (reader_thread_count > MAX_READER_THREADS) { @@ -214,23 +232,28 @@ static int setup_reader_threads(char const * const file_or_device) } if (file_or_device == NULL) { - file_or_default_device = pcap_lookupdev(pcap_error_buffer); + file_or_default_device = get_default_pcapdev(pcap_error_buffer); if (file_or_default_device == NULL) { - fprintf(stderr, "pcap_lookupdev: %s\n", pcap_error_buffer); + fprintf(stderr, "pcap_findalldevs: %.*s\n", (int) PCAP_ERRBUF_SIZE, pcap_error_buffer); return 1; } } else { - file_or_default_device = file_or_device; + file_or_default_device = strdup(file_or_device); + if (file_or_default_device == NULL) { + return 1; + } } for (int i = 0; i < reader_thread_count; ++i) { reader_threads[i].workflow = init_workflow(file_or_default_device); if (reader_threads[i].workflow == NULL) { + free(file_or_default_device); return 1; } } + free(file_or_default_device); return 0; } |