diff --git a/Pcap++/header/PcapLiveDevice.h b/Pcap++/header/PcapLiveDevice.h index 561e025ce9..33b2aa6867 100644 --- a/Pcap++/header/PcapLiveDevice.h +++ b/Pcap++/header/PcapLiveDevice.h @@ -186,10 +186,8 @@ namespace pcpp PCPP_OUT }; - /** - * Set which source provides timestamps associated to each captured packet - * (you can read more here: ) - */ + /// Set which source provides timestamps associated to each captured packet + /// (you can read more here: ) enum class TimestampProvider { /** host-provided, unknown characteristics, default */ @@ -206,10 +204,8 @@ namespace pcpp HostHighPrecUnsynced }; - /** - * Set the precision of timestamps associated to each captured packet - * (you can read more here: ) - */ + /// Set the precision of timestamps associated to each captured packet + /// (you can read more here: ) enum class TimestampPrecision { /** use timestamps with microsecond precision, default */ @@ -268,16 +264,12 @@ namespace pcpp /// In Unix-like system, use poll() for blocking mode. bool usePoll; - /** - * Set which timestamp provider is used. - * Depending on the capture device and the software on the host, different types of time stamp can be used - */ + /// Set which timestamp provider is used. + /// Depending on the capture device and the software on the host, different types of time stamp can be used TimestampProvider timestampProvider; - /** - * Set which timestamp precision is used. - * Depending on the capture device and the software on the host, different precision can be used - */ + /// Set which timestamp precision is used. + /// Depending on the capture device and the software on the host, different precision can be used TimestampPrecision timestampPrecision; /** diff --git a/Pcap++/src/PcapLiveDevice.cpp b/Pcap++/src/PcapLiveDevice.cpp index a877f1686a..7be267e87b 100644 --- a/Pcap++/src/PcapLiveDevice.cpp +++ b/Pcap++/src/PcapLiveDevice.cpp @@ -135,36 +135,40 @@ namespace pcpp static bool isTimestampProviderSupportedByDevice(pcap_t* pcap, const PcapLiveDevice::TimestampProvider timestampProvider) { - int tstampType = timestampProviderMap(timestampProvider); - int* supportedTstampTypes = nullptr; + const auto tstampType = timestampProviderMap(timestampProvider); + + // Use unique_ptr with a custom deleter directly + std::unique_ptr supportedTstampTypes(nullptr, [](int* ptr) { + if (ptr != nullptr) + { + pcap_free_tstamp_types(ptr); + } + }); + const int numSupportedTstampTypes = pcap_list_tstamp_types(pcap, &supportedTstampTypes); - bool isSupported = false; if (numSupportedTstampTypes < 0) { std::cerr << "Error retrieving timestamp types: " << pcap_geterr(pcap) << " - default Host will be used" << std::endl; - isSupported = false; + return false; } - else if (numSupportedTstampTypes == 1) + + if (numSupportedTstampTypes == 1) { - // If 1 is returned, then the only available typestamp is TimestampProvider::Host; + // If 1 is returned, then the only available timestamp is TimestampProvider::Host return timestampProvider == PcapLiveDevice::TimestampProvider::Host; } - else + + for (int i = 0; i < numSupportedTstampTypes; ++i) { - for (int i = 0; i < numSupportedTstampTypes; ++i) + if (supportedTstampTypes[i] == tstampType) { - if (supportedTstampTypes[i] == tstampType) - { - isSupported = true; - break; - } + return true; } } - pcap_free_tstamp_types(supportedTstampTypes); - return isSupported; + return false; } static void setTimestampProvider(pcap_t* pcap, const PcapLiveDevice::TimestampProvider timestampProvider)