Replaced obsolete libpcap pcap_lookupdev with pcap_findalldevs.

Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
This commit is contained in:
Toni Uhlig
2020-08-15 12:35:49 +02:00
parent 00b2763399
commit b31fde4bbb
+28 -5
View File
@@ -9,6 +9,7 @@
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#define MAX_FLOW_ROOTS_PER_THREAD 2048 #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) { 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); free_workflow(&workflow);
return NULL; return NULL;
} }
@@ -204,9 +206,25 @@ static void free_workflow(struct nDPI_workflow ** const workflow)
*workflow = NULL; *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) 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]; char pcap_error_buffer[PCAP_ERRBUF_SIZE];
if (reader_thread_count > MAX_READER_THREADS) { 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) { 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) { 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; return 1;
} }
} else { } 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) { for (int i = 0; i < reader_thread_count; ++i) {
reader_threads[i].workflow = init_workflow(file_or_default_device); reader_threads[i].workflow = init_workflow(file_or_default_device);
if (reader_threads[i].workflow == NULL) if (reader_threads[i].workflow == NULL)
{ {
free(file_or_default_device);
return 1; return 1;
} }
} }
free(file_or_default_device);
return 0; return 0;
} }