diff --git a/Pcap++/CMakeLists.txt b/Pcap++/CMakeLists.txt index 7eb772b142..653a9e2b43 100644 --- a/Pcap++/CMakeLists.txt +++ b/Pcap++/CMakeLists.txt @@ -34,6 +34,7 @@ set( header/PcapFilter.h header/PcapLiveDevice.h header/PcapLiveDeviceList.h + header/PcapUtils.h header/RawSocketDevice.h ) diff --git a/Pcap++/header/PcapDevice.h b/Pcap++/header/PcapDevice.h index 08176196ef..a750adf58d 100644 --- a/Pcap++/header/PcapDevice.h +++ b/Pcap++/header/PcapDevice.h @@ -1,6 +1,9 @@ #pragma once +#include + #include "Device.h" +#include "PcapUtils.h" // forward declaration for the pcap descriptor defined in pcap.h struct pcap; @@ -31,10 +34,10 @@ namespace pcpp explicit PcapHandle(pcap_t* pcapDescriptor) noexcept; PcapHandle(const PcapHandle&) = delete; - PcapHandle(PcapHandle&& other) noexcept; + PcapHandle(PcapHandle&& other) noexcept = default; PcapHandle& operator=(const PcapHandle&) = delete; - PcapHandle& operator=(PcapHandle&& other) noexcept; + PcapHandle& operator=(PcapHandle&& other) noexcept = default; PcapHandle& operator=(std::nullptr_t) noexcept; ~PcapHandle(); @@ -48,7 +51,7 @@ namespace pcpp /// @return The underlying pcap descriptor. pcap_t* get() const noexcept { - return m_PcapDescriptor; + return m_PcapDescriptor.get(); } /// @brief Releases ownership of the handle and returns the pcap descriptor. @@ -81,7 +84,7 @@ namespace pcpp } private: - pcap_t* m_PcapDescriptor = nullptr; + std::unique_ptr m_PcapDescriptor; }; } // namespace internal diff --git a/Pcap++/src/PcapDevice.cpp b/Pcap++/src/PcapDevice.cpp index c72585dd3a..f8c0948602 100644 --- a/Pcap++/src/PcapDevice.cpp +++ b/Pcap++/src/PcapDevice.cpp @@ -11,21 +11,6 @@ namespace pcpp PcapHandle::PcapHandle(pcap_t* pcapDescriptor) noexcept : m_PcapDescriptor(pcapDescriptor) {} - PcapHandle::PcapHandle(PcapHandle&& other) noexcept : m_PcapDescriptor(other.m_PcapDescriptor) - { - other.m_PcapDescriptor = nullptr; - } - - PcapHandle& PcapHandle::operator=(PcapHandle&& other) noexcept - { - if (this != &other) - { - reset(other.m_PcapDescriptor); - other.m_PcapDescriptor = nullptr; - } - return *this; - } - PcapHandle& PcapHandle::operator=(std::nullptr_t) noexcept { reset(); @@ -39,19 +24,12 @@ namespace pcpp pcap_t* PcapHandle::release() noexcept { - auto result = m_PcapDescriptor; - m_PcapDescriptor = nullptr; - return result; + return m_PcapDescriptor.release(); } void PcapHandle::reset(pcap_t* pcapDescriptor) noexcept { - pcap_t* oldDescriptor = m_PcapDescriptor; - m_PcapDescriptor = pcapDescriptor; - if (oldDescriptor != nullptr) - { - pcap_close(oldDescriptor); - } + m_PcapDescriptor.reset(pcapDescriptor); } char const* PcapHandle::getLastError() const noexcept @@ -62,7 +40,7 @@ namespace pcpp return noHandleError; } - return pcap_geterr(m_PcapDescriptor); + return pcap_geterr(m_PcapDescriptor.get()); } } // namespace internal