aboutsummaryrefslogtreecommitdiff
path: root/nDPId.c
diff options
context:
space:
mode:
authorToni <matzeton@googlemail.com>2023-11-06 13:25:49 +0100
committerGitHub <noreply@github.com>2023-11-06 13:25:49 +0100
commit93498fff02c5edecff102d895f5cd239cb1504a2 (patch)
tree950b8c4da0b3af990690fee16cce17421a32b3b9 /nDPId.c
parent1b679271693a17ce0b653b9ba45db77b731db42e (diff)
Apple/BSD port (#30)
* Add MacOS to Github CI builds. * Fixed libnDPI-4.8 CI build. * Fixed missing include for `struct sockaddr*`. * Reworked IPv4 address and netmask retrieval. Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'nDPId.c')
-rw-r--r--nDPId.c92
1 files changed, 38 insertions, 54 deletions
diff --git a/nDPId.c b/nDPId.c
index 9fada5e40..b1a0c8b11 100644
--- a/nDPId.c
+++ b/nDPId.c
@@ -1,3 +1,6 @@
+#if defined(__FreeBSD__) || defined(__APPLE__)
+#include <sys/types.h>
+#endif
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
@@ -17,7 +20,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
+#if !defined(__FreeBSD__) && !defined(__APPLE__)
#include <sys/signalfd.h>
+#endif
#include <sys/un.h>
#include <unistd.h>
#ifdef ENABLE_ZLIB
@@ -429,7 +434,6 @@ static struct nDPId_reader_thread reader_threads[nDPId_MAX_READER_THREADS] = {};
static struct nDPIsrvd_address collector_address;
static MT_VALUE(nDPId_main_thread_shutdown, int) = MT_INIT(0);
static MT_VALUE(global_flow_id, uint64_t) = MT_INIT(1);
-static int ip4_interface_avail = 0, ip6_interface_avail = 0;
#ifdef ENABLE_MEMORY_PROFILING
static MT_VALUE(ndpi_memory_alloc_count, uint64_t) = MT_INIT(0);
@@ -929,10 +933,17 @@ static void get_ip6_from_sockaddr(struct sockaddr_in6 const * const saddr, union
switch (saddr->sin6_family)
{
case AF_INET6:
+#if defined(__FreeBSD__) || defined(__APPLE__)
+ dest->v6.ip_u32[0] = saddr->sin6_addr.__u6_addr.__u6_addr32[0];
+ dest->v6.ip_u32[1] = saddr->sin6_addr.__u6_addr.__u6_addr32[1];
+ dest->v6.ip_u32[2] = saddr->sin6_addr.__u6_addr.__u6_addr32[2];
+ dest->v6.ip_u32[3] = saddr->sin6_addr.__u6_addr.__u6_addr32[3];
+#else
dest->v6.ip_u32[0] = saddr->sin6_addr.s6_addr32[0];
dest->v6.ip_u32[1] = saddr->sin6_addr.s6_addr32[1];
dest->v6.ip_u32[2] = saddr->sin6_addr.s6_addr32[2];
dest->v6.ip_u32[3] = saddr->sin6_addr.s6_addr32[3];
+#endif
break;
default:
return;
@@ -996,7 +1007,11 @@ static int get_ip6_address_and_netmask(char const * const ifa_name, size_t ifnam
memset(&sap.sin6_addr.s6_addr, 0xFF, plen / 8);
if (plen < 128 && (plen % 32) != 0)
{
+#if defined(__FreeBSD__) || defined(__APPLE__)
+ sap.sin6_addr.__u6_addr.__u6_addr32[plen / 32] = 0xFFFFFFFF << (32 - (plen % 32));
+#else
sap.sin6_addr.s6_addr32[plen / 32] = 0xFFFFFFFF << (32 - (plen % 32));
+#endif
}
inet_ntop(AF_INET6, &sap.sin6_addr, netmask6, sizeof(netmask6));
sap.sin6_family = AF_INET6;
@@ -1024,50 +1039,14 @@ error:
return retval;
}
-static int get_ip4_address_and_netmask(char const * const ifa_name, size_t ifnamelen)
+static void get_ip4_address_and_netmask(struct ifaddrs const * const ifaddr)
{
- int retval = 0;
- int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
- struct ifreq ifr;
-
- if (sock < 0)
- {
- retval = 1;
- goto error;
- }
- if (ifnamelen >= sizeof(ifr.ifr_name))
- {
- retval = 1;
- goto error;
- }
-
- memset(&ifr, 0, sizeof(ifr));
- memcpy(ifr.ifr_name, ifa_name, ifnamelen);
- ifr.ifr_name[ifnamelen] = '\0';
- ifr.ifr_netmask.sa_family = AF_INET;
- if (ioctl(sock, SIOCGIFNETMASK, &ifr) == -1)
- {
- retval = 1;
- goto error;
- }
- get_ip4_from_sockaddr((struct sockaddr_in *)&ifr.ifr_netmask, &nDPId_options.pcap_dev_netmask4);
-
- memset(&ifr, 0, sizeof(ifr));
- memcpy(ifr.ifr_name, ifa_name, ifnamelen);
- ifr.ifr_name[ifnamelen] = '\0';
- ifr.ifr_addr.sa_family = AF_INET;
- if (ioctl(sock, SIOCGIFADDR, &ifr) == -1)
- {
- retval = 1;
- goto error;
- }
- get_ip4_from_sockaddr((struct sockaddr_in *)&ifr.ifr_netmask, &nDPId_options.pcap_dev_ip4);
-
+ get_ip4_from_sockaddr((struct sockaddr_in *)ifaddr->ifa_netmask, &nDPId_options.pcap_dev_netmask4);
+ get_ip4_from_sockaddr((struct sockaddr_in *)ifaddr->ifa_addr, &nDPId_options.pcap_dev_ip4);
ip_netmask_to_subnet(&nDPId_options.pcap_dev_ip4,
&nDPId_options.pcap_dev_netmask4,
&nDPId_options.pcap_dev_subnet4,
L3_IP);
-
{
char addr[INET_ADDRSTRLEN];
char netm[INET_ADDRSTRLEN];
@@ -1082,15 +1061,12 @@ static int get_ip4_address_and_netmask(char const * const ifa_name, size_t ifnam
inet_ntop(AF_INET, snetm, netm, sizeof(netm)),
inet_ntop(AF_INET, ssubn, subn, sizeof(subn)));
}
-
-error:
- close(sock);
- return retval;
}
static int get_ip_netmask_from_pcap_dev(char const * const pcap_dev)
{
int retval = 0, found_dev = 0;
+ int ip4_interface_avail = 0, ip6_interface_avail = 0;
struct ifaddrs * ifaddrs = NULL;
struct ifaddrs * ifa;
@@ -1113,10 +1089,7 @@ static int get_ip_netmask_from_pcap_dev(char const * const pcap_dev)
switch (ifa->ifa_addr->sa_family)
{
case AF_INET:
- if (ip4_interface_avail == 0 && get_ip4_address_and_netmask(ifa->ifa_name, ifnamelen) != 0)
- {
- retval = 1;
- }
+ get_ip4_address_and_netmask(ifa);
ip4_interface_avail = 1;
break;
case AF_INET6:
@@ -1586,6 +1559,8 @@ static int setup_reader_threads(void)
"Could not get netmask for pcap device %s: %s",
get_cmdarg(&nDPId_options.pcap_file_or_interface),
strerror(errno));
+ } else {
+ logger_early(1, "Unexpected error while retrieving netmask for pcap device %s", get_cmdarg(&nDPId_options.pcap_file_or_interface));
}
return 1;
}
@@ -2245,8 +2220,8 @@ static int connect_to_collector(struct nDPId_reader_thread * const reader_thread
}
int sock_type = (collector_address.raw.sa_family == AF_UNIX ? SOCK_STREAM : SOCK_DGRAM);
- reader_thread->collector_sockfd = socket(collector_address.raw.sa_family, sock_type | SOCK_CLOEXEC, 0);
- if (reader_thread->collector_sockfd < 0)
+ reader_thread->collector_sockfd = socket(collector_address.raw.sa_family, sock_type, 0);
+ if (reader_thread->collector_sockfd < 0 || set_fd_cloexec(reader_thread->collector_sockfd) < 0)
{
reader_thread->collector_sock_last_errno = errno;
return 1;
@@ -3458,7 +3433,7 @@ static int process_datalink_layer(struct nDPId_reader_thread * const reader_thre
break;
case ETHERTYPE_PAE: /* 802.1X Authentication */
return 1;
- case ETH_P_ARP: /* ARP */
+ case ETHERTYPE_ARP: /* ARP */
return 1;
default:
if (is_error_event_threshold(reader_thread->workflow) == 0)
@@ -4274,6 +4249,7 @@ static void get_current_time(struct timeval * const tval)
gettimeofday(tval, NULL);
}
+#if !defined(__FreeBSD__) && !defined(__APPLE__)
static void ndpi_log_flow_walker(void const * const A, ndpi_VISIT which, int depth, void * const user_data)
{
struct nDPId_reader_thread const * const reader_thread = (struct nDPId_reader_thread *)user_data;
@@ -4358,6 +4334,7 @@ static void log_all_flows(struct nDPId_reader_thread const * const reader_thread
ndpi_twalk(workflow->ndpi_flows_active[scan_index], ndpi_log_flow_walker, (void *)reader_thread);
}
}
+#endif
static void run_pcap_loop(struct nDPId_reader_thread * const reader_thread)
{
@@ -4380,6 +4357,7 @@ static void run_pcap_loop(struct nDPId_reader_thread * const reader_thread)
}
else
{
+#if !defined(__FreeBSD__) && !defined(__APPLE__)
sigset_t thread_signal_set, old_signal_set;
sigfillset(&thread_signal_set);
if (pthread_sigmask(SIG_BLOCK, &thread_signal_set, &old_signal_set) != 0)
@@ -4392,13 +4370,14 @@ static void run_pcap_loop(struct nDPId_reader_thread * const reader_thread)
sigaddset(&thread_signal_set, SIGINT);
sigaddset(&thread_signal_set, SIGTERM);
sigaddset(&thread_signal_set, SIGUSR1);
- int signal_fd = signalfd(-1, &thread_signal_set, SFD_NONBLOCK | SFD_CLOEXEC);
- if (signal_fd < 0)
+ int signal_fd = signalfd(-1, &thread_signal_set, SFD_NONBLOCK);
+ if (signal_fd < 0 || set_fd_cloexec(signal_fd) < 0)
{
logger(1, "signalfd: %s", strerror(errno));
MT_GET_AND_ADD(reader_thread->workflow->error_or_eof, 1);
return;
}
+#endif
int pcap_fd = pcap_get_selectable_fd(reader_thread->workflow->pcap_handle);
if (pcap_fd < 0)
@@ -4433,6 +4412,7 @@ static void run_pcap_loop(struct nDPId_reader_thread * const reader_thread)
nio_free(&io);
return;
}
+#if !defined(__FreeBSD__) && !defined(__APPLE__)
errno = 0;
if (nio_add_fd(&io, signal_fd, NIO_EVENT_INPUT, NULL) != NIO_SUCCESS)
{
@@ -4443,6 +4423,7 @@ static void run_pcap_loop(struct nDPId_reader_thread * const reader_thread)
nio_free(&io);
return;
}
+#endif
int const timeout_ms = 1000; /* TODO: Configurable? */
struct timeval tval_before_epoll, tval_after_epoll;
@@ -4482,6 +4463,7 @@ static void run_pcap_loop(struct nDPId_reader_thread * const reader_thread)
int fd = nio_get_fd(&io, i);
+#if !defined(__FreeBSD__) && !defined(__APPLE__)
if (fd == signal_fd)
{
struct signalfd_siginfo fdsi;
@@ -4513,7 +4495,9 @@ static void run_pcap_loop(struct nDPId_reader_thread * const reader_thread)
logger(1, "Received signal %d (%s)", fdsi.ssi_signo, signame);
}
}
- else if (fd == pcap_fd)
+ else
+#endif
+ if (fd == pcap_fd)
{
switch (pcap_dispatch(
reader_thread->workflow->pcap_handle, -1, ndpi_process_packet, (uint8_t *)reader_thread))