diff options
author | Luca Deri <lucaderi@users.noreply.github.com> | 2020-08-16 10:03:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-16 10:03:33 +0200 |
commit | 019a64630bf293781f0ac4780b13d08bd2a20810 (patch) | |
tree | 8793c131197f4e4a8048a22a800a151cbc060a0e /example/ndpiSimpleIntegration.c | |
parent | 98a9afc40cb585107507ffce2f0b6910921d8aa1 (diff) | |
parent | b31fde4bbbfe58f7ca2ca14fa6be2895ab115824 (diff) |
Merge pull request #983 from lnslbrty/fix/libpcap-obsolete-pcap_lookupdev-usage
Replaced obsolete libpcap pcap_lookupdev with pcap_findalldevs.
Diffstat (limited to 'example/ndpiSimpleIntegration.c')
-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 bf16dbd95..eedf86ffc 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; } |