diff --git a/.clang-tidy b/.clang-tidy index 43f8989..4696ee4 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -34,21 +34,25 @@ Checks: "-*, -cppcoreguidelines-pro-bounds-pointer-arithmetic, -cppcoreguidelines-pro-type-vararg, -cppcoreguidelines-pro-type-reinterpret-cast, + -cppcoreguidelines-use-default-member-init, misc-*, -misc-non-private-member-variables-in-classes, -misc-no-recursion, + -misc-include-cleaner, modernize-*, -modernize-pass-by-value, -modernize-use-trailing-return-type, -modernize-use-auto, + -modernize-use-default-member-init, -modernize-concat-nested-namespaces, -modernize-return-braced-init-list, -modernize-use-nodiscard, -modernize-avoid-bind, performance-*, + -performance-avoid-endl, readability-*, -readability-braces-around-statements, diff --git a/tests/udpcap_test/src/atomic_signalable.h b/tests/udpcap_test/src/atomic_signalable.h index 237a59d..b10b024 100644 --- a/tests/udpcap_test/src/atomic_signalable.h +++ b/tests/udpcap_test/src/atomic_signalable.h @@ -27,7 +27,7 @@ class atomic_signalable atomic_signalable& operator=(const T new_value) { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); value = new_value; cv.notify_all(); return *this; @@ -35,7 +35,7 @@ class atomic_signalable T operator++() { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); T newValue = ++value; cv.notify_all(); return newValue; @@ -43,7 +43,7 @@ class atomic_signalable T operator++(T) { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); T oldValue = value++; cv.notify_all(); return oldValue; @@ -51,7 +51,7 @@ class atomic_signalable T operator--() { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); T newValue = --value; cv.notify_all(); return newValue; @@ -59,7 +59,7 @@ class atomic_signalable T operator--(T) { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); T oldValue = value--; cv.notify_all(); return oldValue; @@ -67,7 +67,7 @@ class atomic_signalable T operator+=(const T& other) { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); value += other; cv.notify_all(); return value; @@ -75,7 +75,7 @@ class atomic_signalable T operator-=(const T& other) { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); value -= other; cv.notify_all(); return value; @@ -83,7 +83,7 @@ class atomic_signalable T operator*=(const T& other) { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); value *= other; cv.notify_all(); return value; @@ -91,7 +91,7 @@ class atomic_signalable T operator/=(const T& other) { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); value /= other; cv.notify_all(); return value; @@ -99,7 +99,7 @@ class atomic_signalable T operator%=(const T& other) { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); value %= other; cv.notify_all(); return value; @@ -114,13 +114,13 @@ class atomic_signalable T get() const { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); return value; } bool operator==(T other) const { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); return value == other; } @@ -133,31 +133,31 @@ class atomic_signalable bool operator!=(T other) const { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); return value != other; } bool operator<(T other) const { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); return value < other; } bool operator<=(T other) const { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); return value <= other; } bool operator>(T other) const { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); return value > other; } bool operator>=(T other) const { - std::lock_guard lock(mutex); + const std::lock_guard lock(mutex); return value >= other; } diff --git a/tests/udpcap_test/src/udpcap_test.cpp b/tests/udpcap_test/src/udpcap_test.cpp index 6f83295..2060d5c 100644 --- a/tests/udpcap_test/src/udpcap_test.cpp +++ b/tests/udpcap_test/src/udpcap_test.cpp @@ -31,7 +31,7 @@ TEST(udpcap, RAII) { { // Create a udpcap socket - Udpcap::UdpcapSocket udpcap_socket; + const Udpcap::UdpcapSocket udpcap_socket; ASSERT_TRUE(udpcap_socket.isValid()); // Delete the socket } @@ -45,7 +45,7 @@ TEST(udpcap, RAIIWithClose) ASSERT_TRUE(udpcap_socket.isValid()); // bind the socket - bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000); + const bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000); ASSERT_TRUE(success); // Close the socket @@ -60,7 +60,7 @@ TEST(udpcap, RAIIWithSomebodyWaiting) ASSERT_TRUE(udpcap_socket.isValid()); // bind the socket - bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000); + const bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000); ASSERT_TRUE(success); // Blocking receive a datagram @@ -73,7 +73,7 @@ TEST(udpcap, RAIIWithSomebodyWaiting) Udpcap::Error error = Udpcap::Error::ErrorCode::GENERIC_ERROR; // blocking receive - size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 0, error); + const size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 0, error); // Check that we didn't receive any bytes ASSERT_EQ(received_bytes, 0); @@ -107,7 +107,7 @@ TEST(udpcap, SimpleReceive) ASSERT_TRUE(udpcap_socket.isValid()); { - bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000); + const bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000); ASSERT_TRUE(success); } @@ -124,7 +124,7 @@ TEST(udpcap, SimpleReceive) received_datagram.resize(65536); // blocking receive - size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error); + const size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error); received_datagram.resize(received_bytes); // No error must have occurred @@ -172,7 +172,7 @@ TEST(udpcap, MultipleSmallPackages) // Bind the udpcap socket to all interfaces { - bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000); + const bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000); ASSERT_TRUE(success); } @@ -188,7 +188,7 @@ TEST(udpcap, MultipleSmallPackages) received_datagram.resize(65536); // blocking receive - size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), error); + const size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), error); if (error) { @@ -249,7 +249,7 @@ TEST(udpcap, SimpleReceiveWithBuffer) ASSERT_TRUE(udpcap_socket.isValid()); { - bool success = udpcap_socket.bind(Udpcap::HostAddress::LocalHost(), 14000); + const bool success = udpcap_socket.bind(Udpcap::HostAddress::LocalHost(), 14000); ASSERT_TRUE(success); } @@ -317,7 +317,7 @@ TEST(udpcap, DelayedPackageReceiveMultiplePackages) // Bind the udpcap socket to all interfaces { - bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000); + const bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000); ASSERT_TRUE(success); } @@ -335,7 +335,7 @@ TEST(udpcap, DelayedPackageReceiveMultiplePackages) std::vector received_datagram; received_datagram.resize(65536); - size_t bytes_received = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error); + const size_t bytes_received = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error); received_datagram.resize(bytes_received); if (error) @@ -396,14 +396,12 @@ TEST(udpcap, DelayedPackageReceiveMultiplePackages) // Test the timeout of the receiveDatagram function TEST(udpcap, Timeout) { - atomic_signalable received_messages(0); - // Create a udpcap socket Udpcap::UdpcapSocket udpcap_socket; ASSERT_TRUE(udpcap_socket.isValid()); { - bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000); + const bool success = udpcap_socket.bind(Udpcap::HostAddress::Any(), 14000); ASSERT_TRUE(success); } @@ -429,7 +427,7 @@ TEST(udpcap, Timeout) auto start_time = std::chrono::steady_clock::now(); // blocking receive with a 100ms timeout - size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 100, &sender_address, &sender_port, error); + const size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 100, &sender_address, &sender_port, error); // Take End time auto end_time = std::chrono::steady_clock::now(); @@ -450,7 +448,7 @@ TEST(udpcap, Timeout) auto start_time = std::chrono::steady_clock::now(); // blocking receive with a 500ms timeout - size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 500, &sender_address, &sender_port, error); + const size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 500, &sender_address, &sender_port, error); // Take End time auto end_time = std::chrono::steady_clock::now(); @@ -470,7 +468,7 @@ TEST(udpcap, Timeout) auto start_time = std::chrono::steady_clock::now(); // blocking receive with a 0ms timeout - size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 0, &sender_address, &sender_port, error); + const size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 0, &sender_address, &sender_port, error); // Take End time auto end_time = std::chrono::steady_clock::now(); @@ -488,7 +486,7 @@ TEST(udpcap, Timeout) auto start_time = std::chrono::steady_clock::now(); // blocking receive with a 0ms timeout - size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 0, &sender_address, &sender_port, error); + const size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), 0, &sender_address, &sender_port, error); // Take End time auto end_time = std::chrono::steady_clock::now(); @@ -525,7 +523,7 @@ TEST(udpcap, ReceiveNotBound) Udpcap::Error error = Udpcap::Error::ErrorCode::GENERIC_ERROR; // blocking receive - size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error); + const size_t received_bytes = udpcap_socket.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error); // Check if the received datagram is valid and contains "Hello World" ASSERT_EQ(received_bytes, 0); @@ -551,27 +549,27 @@ TEST(udpcap, MulticastReceive) // Bind the udpcap sockets to all interfaces { - bool success = udpcap_socket1.bind(Udpcap::HostAddress::Any(), 14000); + const bool success = udpcap_socket1.bind(Udpcap::HostAddress::Any(), 14000); ASSERT_TRUE(success); } { - bool success = udpcap_socket2.bind(Udpcap::HostAddress::Any(), 14000); + const bool success = udpcap_socket2.bind(Udpcap::HostAddress::Any(), 14000); ASSERT_TRUE(success); } // Join the multicast group 224.0.0.1 on both sockets { - bool success = udpcap_socket1.joinMulticastGroup(Udpcap::HostAddress("224.0.0.1")); + const bool success = udpcap_socket1.joinMulticastGroup(Udpcap::HostAddress("224.0.0.1")); ASSERT_TRUE(success); } { - bool success = udpcap_socket2.joinMulticastGroup(Udpcap::HostAddress("224.0.0.1")); + const bool success = udpcap_socket2.joinMulticastGroup(Udpcap::HostAddress("224.0.0.1")); ASSERT_TRUE(success); } // Join the multicast group 224.0.0.2 on the second socket { - bool success = udpcap_socket2.joinMulticastGroup(Udpcap::HostAddress("224.0.0.2")); + const bool success = udpcap_socket2.joinMulticastGroup(Udpcap::HostAddress("224.0.0.2")); ASSERT_TRUE(success); } @@ -598,7 +596,7 @@ TEST(udpcap, MulticastReceive) std::vector received_datagram; received_datagram.resize(65536); - size_t bytes_received = udpcap_socket1.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error); + const size_t bytes_received = udpcap_socket1.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error); received_datagram.resize(bytes_received); if (error) @@ -635,7 +633,7 @@ TEST(udpcap, MulticastReceive) std::vector received_datagram; received_datagram.resize(65536); - size_t bytes_received = udpcap_socket2.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error); + const size_t bytes_received = udpcap_socket2.receiveDatagram(received_datagram.data(), received_datagram.size(), &sender_address, &sender_port, error); received_datagram.resize(bytes_received); if (error) diff --git a/udpcap/include/udpcap/host_address.h b/udpcap/include/udpcap/host_address.h index e6ba5ce..1eb9ad6 100644 --- a/udpcap/include/udpcap/host_address.h +++ b/udpcap/include/udpcap/host_address.h @@ -20,7 +20,10 @@ #pragma once #include + +// IWYU pragma: begin_exports #include +// IWYU pragma: end_exports namespace Udpcap { @@ -57,7 +60,7 @@ namespace Udpcap UDPCAP_EXPORT HostAddress(const std::string& address); /** @brief Constructs a HostAddress from a 32bit integer in host byte order. */ - UDPCAP_EXPORT HostAddress(const uint32_t address); + UDPCAP_EXPORT HostAddress(uint32_t address); /** @brief Checks if the Host Address is valid. * Invalid HostAddresses are created when providing a wrong IPv4 string, diff --git a/udpcap/include/udpcap/npcap_helpers.h b/udpcap/include/udpcap/npcap_helpers.h index 874febb..a764a10 100644 --- a/udpcap/include/udpcap/npcap_helpers.h +++ b/udpcap/include/udpcap/npcap_helpers.h @@ -19,10 +19,12 @@ #pragma once -#include #include +#include +// IWYU pragma: begin_exports #include +// IWYU pragma: end_exports namespace Udpcap { diff --git a/udpcap/include/udpcap/udpcap_socket.h b/udpcap/include/udpcap/udpcap_socket.h index 7ab9308..fc070fa 100644 --- a/udpcap/include/udpcap/udpcap_socket.h +++ b/udpcap/include/udpcap/udpcap_socket.h @@ -19,9 +19,11 @@ #pragma once +// IWYU pragma: begin_exports +#include #include #include -#include +// IWYU pragma: end_exports #include #include @@ -155,7 +157,7 @@ namespace Udpcap */ UDPCAP_EXPORT size_t receiveDatagram(char* data , size_t max_len - , unsigned long timeout_ms + , long long timeout_ms , HostAddress* source_address , uint16_t* source_port , Udpcap::Error& error); @@ -163,7 +165,7 @@ namespace Udpcap // TODO: Copy documentation here UDPCAP_EXPORT size_t receiveDatagram(char* data , size_t max_len - , unsigned long timeout_ms + , long long timeout_ms , Udpcap::Error& error); // TODO: Copy documentation here diff --git a/udpcap/src/host_address.cpp b/udpcap/src/host_address.cpp index 7501164..91e3ce8 100644 --- a/udpcap/src/host_address.cpp +++ b/udpcap/src/host_address.cpp @@ -24,6 +24,8 @@ #include #include +#include +#include namespace Udpcap { @@ -43,7 +45,7 @@ namespace Udpcap valid_ = (inet_pton(AF_INET, address.c_str(), (void*)(&ipv4_)) == 1); } - HostAddress::HostAddress(const uint32_t address) + HostAddress::HostAddress(uint32_t address) : valid_(true) , ipv4_(address) {} diff --git a/udpcap/src/ip_reassembly.cpp b/udpcap/src/ip_reassembly.cpp index 725a5c4..0e49c08 100644 --- a/udpcap/src/ip_reassembly.cpp +++ b/udpcap/src/ip_reassembly.cpp @@ -29,7 +29,11 @@ #pragma warning( pop ) #endif // _MSC_VER +#include +#include #include +#include +#include namespace Udpcap { diff --git a/udpcap/src/ip_reassembly.h b/udpcap/src/ip_reassembly.h index 33968d6..63bbdc6 100644 --- a/udpcap/src/ip_reassembly.h +++ b/udpcap/src/ip_reassembly.h @@ -29,8 +29,8 @@ #endif // _MSC_VER #include -#include #include +#include namespace Udpcap { diff --git a/udpcap/src/npcap_helpers.cpp b/udpcap/src/npcap_helpers.cpp index 83dd8c9..5e0629c 100644 --- a/udpcap/src/npcap_helpers.cpp +++ b/udpcap/src/npcap_helpers.cpp @@ -19,20 +19,21 @@ #include "udpcap/npcap_helpers.h" -#include - -#include -#include #include -#include #include - -#include +#include #include +#include +#include +#include +#include +#include +#include +#include #define NOMINMAX #define WIN32_LEAN_AND_MEAN -#include +#include // IWYU pragma: keep #include diff --git a/udpcap/src/udpcap_socket.cpp b/udpcap/src/udpcap_socket.cpp index b852de7..c7a48d1 100644 --- a/udpcap/src/udpcap_socket.cpp +++ b/udpcap/src/udpcap_socket.cpp @@ -21,6 +21,10 @@ #include "udpcap_socket_private.h" +#include +#include +#include + namespace Udpcap { UdpcapSocket::UdpcapSocket() @@ -45,10 +49,10 @@ namespace Udpcap bool UdpcapSocket::hasPendingDatagrams () const { return udpcap_socket_private_->hasPendingDatagrams(); } - size_t UdpcapSocket::receiveDatagram(char* data, size_t max_len, unsigned long timeout_ms, HostAddress* source_address, uint16_t* source_port, Udpcap::Error& error) { return udpcap_socket_private_->receiveDatagram(data, max_len, timeout_ms, source_address, source_port, error); } - size_t UdpcapSocket::receiveDatagram(char* data, size_t max_len, unsigned long timeout_ms, Udpcap::Error& error) { return udpcap_socket_private_->receiveDatagram(data, max_len, timeout_ms, nullptr, nullptr, error); } - size_t UdpcapSocket::receiveDatagram(char* data, size_t max_len, Udpcap::Error& error) { return udpcap_socket_private_->receiveDatagram(data, max_len, -1, nullptr, nullptr, error); } - size_t UdpcapSocket::receiveDatagram(char* data, size_t max_len, HostAddress* source_address, uint16_t* source_port, Udpcap::Error& error) { return udpcap_socket_private_->receiveDatagram(data, max_len, -1, source_address, source_port, error); } + size_t UdpcapSocket::receiveDatagram(char* data, size_t max_len, long long timeout_ms, HostAddress* source_address, uint16_t* source_port, Udpcap::Error& error) { return udpcap_socket_private_->receiveDatagram(data, max_len, timeout_ms, source_address, source_port, error); } + size_t UdpcapSocket::receiveDatagram(char* data, size_t max_len, long long timeout_ms, Udpcap::Error& error) { return udpcap_socket_private_->receiveDatagram(data, max_len, timeout_ms, nullptr, nullptr, error); } + size_t UdpcapSocket::receiveDatagram(char* data, size_t max_len, Udpcap::Error& error) { return udpcap_socket_private_->receiveDatagram(data, max_len, -1, nullptr, nullptr, error); } + size_t UdpcapSocket::receiveDatagram(char* data, size_t max_len, HostAddress* source_address, uint16_t* source_port, Udpcap::Error& error) { return udpcap_socket_private_->receiveDatagram(data, max_len, -1, source_address, source_port, error); } bool UdpcapSocket::joinMulticastGroup (const HostAddress& group_address) { return udpcap_socket_private_->joinMulticastGroup(group_address); } bool UdpcapSocket::leaveMulticastGroup (const HostAddress& group_address) { return udpcap_socket_private_->leaveMulticastGroup(group_address); } diff --git a/udpcap/src/udpcap_socket_private.cpp b/udpcap/src/udpcap_socket_private.cpp index 0995f7f..c939bd4 100644 --- a/udpcap/src/udpcap_socket_private.cpp +++ b/udpcap/src/udpcap_socket_private.cpp @@ -19,7 +19,11 @@ #include "udpcap_socket_private.h" -#include "udpcap/npcap_helpers.h" +#include +#include +#include + +#include "ip_reassembly.h" #include "log_debug.h" #define WIN32_LEAN_AND_MEAN @@ -27,13 +31,23 @@ #include // User-space defines for NDIS driver communication -#include -#include #include -#include +#include +#include +#include +#include +#include +#include +#include #include +#include +#include +#include +#include +#include +#include -#include +#include // IWYU pragma: keep namespace Udpcap { @@ -93,7 +107,7 @@ namespace Udpcap // Valid address => Try to bind to address! - std::unique_lock pcap_devices_lists_lock(pcap_devices_lists_mutex_); + const std::unique_lock pcap_devices_lists_lock(pcap_devices_lists_mutex_); if (local_address.isLoopback()) { @@ -269,7 +283,7 @@ namespace Udpcap size_t UdpcapSocketPrivate::receiveDatagram(char* data , size_t max_len - , unsigned long timeout_ms + , long long timeout_ms , HostAddress* source_address , uint16_t* source_port , Udpcap::Error& error) @@ -343,7 +357,7 @@ namespace Udpcap { CallbackArgsRawPtr callback_args(data, max_len, source_address, source_port, bound_port_, pcpp::LinkLayerType::LINKTYPE_NULL); - int pcap_next_packet_errorcode = pcap_next_ex(pcap_dev.pcap_handle_, &packet_header, &packet_data); + const int pcap_next_packet_errorcode = pcap_next_ex(pcap_dev.pcap_handle_, &packet_header, &packet_data); if (pcap_next_packet_errorcode == 1) { @@ -576,7 +590,7 @@ namespace Udpcap bool UdpcapSocketPrivate::isClosed() const { - std::lock_guard pcap_callback_lock(pcap_devices_callback_mutex_); + const std::lock_guard pcap_callback_lock(pcap_devices_callback_mutex_); return pcap_devices_closed_; } diff --git a/udpcap/src/udpcap_socket_private.h b/udpcap/src/udpcap_socket_private.h index bab61ca..31d8373 100644 --- a/udpcap/src/udpcap_socket_private.h +++ b/udpcap/src/udpcap_socket_private.h @@ -22,12 +22,12 @@ #include #include -#include -#include -#include #include #include +#include +#include #include +#include #define WIN32_LEAN_AND_MEAN #define NOMINMAX @@ -123,7 +123,7 @@ namespace Udpcap size_t receiveDatagram(char* data , size_t max_len - , unsigned long timeout_ms + , long long timeout_ms , HostAddress* source_address , uint16_t* source_port , Udpcap::Error& error); @@ -144,7 +144,7 @@ namespace Udpcap static std::pair getDeviceByIp(const HostAddress& ip); static std::vector> getAllDevices(); - static std::string getMac(pcap_t* const pcap_handle); + static std::string getMac(pcap_t* pcap_handle); bool openPcapDevice_nolock(const std::string& device_name);