From eff8a8fae84c32a102fea0607cdd903a43f65501 Mon Sep 17 00:00:00 2001 From: Florian Reimold <11774314+FlorianReimold@users.noreply.github.com> Date: Thu, 22 Feb 2024 14:21:21 +0100 Subject: [PATCH] Solved many TODOs --- udpcap/include/udpcap/udpcap_socket.h | 5 --- udpcap/src/udpcap_socket.cpp | 2 - udpcap/src/udpcap_socket_private.cpp | 59 +-------------------------- udpcap/src/udpcap_socket_private.h | 3 -- 4 files changed, 2 insertions(+), 67 deletions(-) diff --git a/udpcap/include/udpcap/udpcap_socket.h b/udpcap/include/udpcap/udpcap_socket.h index fc070fa..4e21adc 100644 --- a/udpcap/include/udpcap/udpcap_socket.h +++ b/udpcap/include/udpcap/udpcap_socket.h @@ -131,11 +131,6 @@ namespace Udpcap */ UDPCAP_EXPORT bool setReceiveBufferSize(int receive_buffer_size); - /** - * @brief Returns whether there are datagrams ready to be read - */ - UDPCAP_EXPORT bool hasPendingDatagrams() const; - /** * @brief Blocks for the given time until a packet arives and copies it to the given memory * diff --git a/udpcap/src/udpcap_socket.cpp b/udpcap/src/udpcap_socket.cpp index c7a48d1..dcd2dde 100644 --- a/udpcap/src/udpcap_socket.cpp +++ b/udpcap/src/udpcap_socket.cpp @@ -47,8 +47,6 @@ namespace Udpcap bool UdpcapSocket::setReceiveBufferSize (int receive_buffer_size) { return udpcap_socket_private_->setReceiveBufferSize(receive_buffer_size); } - bool UdpcapSocket::hasPendingDatagrams () const { return udpcap_socket_private_->hasPendingDatagrams(); } - 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); } diff --git a/udpcap/src/udpcap_socket_private.cpp b/udpcap/src/udpcap_socket_private.cpp index c939bd4..31310df 100644 --- a/udpcap/src/udpcap_socket_private.cpp +++ b/udpcap/src/udpcap_socket_private.cpp @@ -69,11 +69,6 @@ namespace Udpcap { close(); } - //UdpcapSocketPrivate::~UdpcapSocketPrivate() - //{ - // // @todo: reinvestigate why it crashes on close. (Maybe check if i have implemented copy / move constructors properly) - // //close(); - //} bool UdpcapSocketPrivate::isValid() const { @@ -231,56 +226,6 @@ namespace Udpcap return true; } - bool UdpcapSocketPrivate::hasPendingDatagrams() const - { - if (!is_valid_) - { - // Invalid socket, cannot bind => fail! - LOG_DEBUG("Has Pending Datagrams error: Socket is invalid"); - return false; - } - - if (!bound_state_) - { - // Not bound => fail! - LOG_DEBUG("Has Pending Datagrams error: Socket is not bound"); - return false; - } - - // Lock the lists of open pcap devices in read-mode. We may use the handles, but not modify the lists themselfes. - const std::shared_lock pcap_devices_lists_lock(pcap_devices_lists_mutex_); - - { - const std::lock_guard pcap_callback_lock(pcap_devices_callback_mutex_); - if (pcap_devices_closed_) - { - // No open devices => fail! - LOG_DEBUG("Has Pending Datagrams error: Socket has been closed."); - return false; - } - } - - if (pcap_win32_handles_.empty()) - { - // No open devices => fail! - LOG_DEBUG("Has Pending Datagrams error: No open devices"); - return false; - } - - // Wait 0 ms for data - DWORD num_handles = static_cast(pcap_win32_handles_.size()); - if (num_handles > MAXIMUM_WAIT_OBJECTS) - { - LOG_DEBUG("WARNING: Too many open Adapters. " + std::to_string(num_handles) + " adapters are open, only " + std::to_string(MAXIMUM_WAIT_OBJECTS) + " are supported."); - num_handles = MAXIMUM_WAIT_OBJECTS; - } - - const DWORD wait_result = WaitForMultipleObjects(num_handles, pcap_win32_handles_.data(), static_cast(false), 0); - - // Check if any HADNLE was in signaled state - return((wait_result >= WAIT_OBJECT_0) && wait_result <= (WAIT_OBJECT_0 + num_handles - 1)); - } - size_t UdpcapSocketPrivate::receiveDatagram(char* data , size_t max_len , long long timeout_ms @@ -319,7 +264,7 @@ namespace Udpcap // Check for data on pcap devices until we are either out of time or have // received a datagaram. A datagram may consist of multiple packaets in // case of IP Fragmentation. - while (true) // TODO: respect the timeout parameter + while (true) { bool received_any_data = false; @@ -724,7 +669,7 @@ namespace Udpcap if (receive_buffer_size_ > 0) { - pcap_set_buffer_size(pcap_handle, receive_buffer_size_); // TODO: the buffer size should probably not be zero by default. Currently (2024-01-31) it is. + pcap_set_buffer_size(pcap_handle, receive_buffer_size_); } const int errorcode = pcap_activate(pcap_handle); // TODO : If pcap_activate() fails, the pcap_t * is not closed and freed; it should be closed using pcap_close(3PCAP). diff --git a/udpcap/src/udpcap_socket_private.h b/udpcap/src/udpcap_socket_private.h index 31d8373..58a917d 100644 --- a/udpcap/src/udpcap_socket_private.h +++ b/udpcap/src/udpcap_socket_private.h @@ -118,9 +118,6 @@ namespace Udpcap bool setReceiveBufferSize(int buffer_size); - // TODO: Re-implement or remove. This is currently (2024-02-06) implemented faulty. - bool hasPendingDatagrams() const; - size_t receiveDatagram(char* data , size_t max_len , long long timeout_ms