Skip to content

Commit

Permalink
Merge branch 'dev' into clang-format-for-examples
Browse files Browse the repository at this point in the history
  • Loading branch information
seladb authored Jul 29, 2024
2 parents b757967 + d21c05c commit 63bab8d
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 8 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ jobs:
- name: Test Tutorials
run: cd build_examples/tutorials_bin && ./Tutorial-HelloWorld

- name: Create Cobertura Report
run: |
${{ matrix.python }} -m pip install gcovr
gcovr -v -r . ${{ matrix.additional-gcov-flags }} $GCOVR_FLAGS -o coverage.xml
# - name: Create Cobertura Report
# run: |
# ${{ matrix.python }} -m pip install gcovr
# gcovr -v -r . ${{ matrix.additional-gcov-flags }} $GCOVR_FLAGS -o coverage.xml

# - name: Upload Coverage Results
# uses: codecov/codecov-action@ab904c41d6ece82784817410c45d8b8c02684457 # v3.1.6
Expand Down
22 changes: 20 additions & 2 deletions Common++/header/PointerVector.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <cstddef>
#include <stdio.h>
#include <stdint.h>
#include <stdexcept>
Expand Down Expand Up @@ -121,19 +122,36 @@ namespace pcpp
m_Vector.clear();
}

/**
* Adding a nullptr to the vector is not allowed.
*/
void pushBack(std::nullptr_t element, bool freeElementOnError = true) = delete;

/**
* Add a new (pointer to an) element to the vector
* @param[in] element A pointer to an element to assume ownership of.
* @param[in] freeElementOnError If set to true, the element is freed if an exception is thrown during the push.
* @throws std::invalid_argument The provided pointer is a nullptr.
*/
void pushBack(T* element)
void pushBack(T* element, bool freeElementOnError = true)
{
if (element == nullptr)
{
throw std::invalid_argument("Element is nullptr");
}

m_Vector.push_back(element);
try
{
m_Vector.push_back(element);
}
catch (const std::exception&)
{
if (freeElementOnError)
{
delete element;
}
throw;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Packet++/header/Asn1Codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ namespace pcpp
{
auto encodedRecord = (*recordIter)->encode();
auto copyRecord = Asn1Record::decode(encodedRecord.data(), encodedRecord.size(), false);
m_SubRecords.pushBack(copyRecord.release());
m_SubRecords.pushBack(std::move(copyRecord));
recordValueLength += encodedRecord.size();
}

Expand Down
2 changes: 1 addition & 1 deletion Packet++/src/LdapLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ namespace pcpp {
Asn1OctetStringRecord typeRecord(attribute.type);
Asn1SetRecord valuesRecord(valuesSubRecords);

attributesSubRecords.pushBack(new Asn1SequenceRecord({&typeRecord, &valuesRecord}));
attributesSubRecords.pushBack(new Asn1SequenceRecord({ &typeRecord, &valuesRecord }));
}

Asn1OctetStringRecord objectNameRecord(objectName);
Expand Down
7 changes: 7 additions & 0 deletions Pcap++/header/PcapLiveDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,16 @@ namespace pcpp

/**
* @return A vector containing all addresses defined for this interface, each in pcap_addr_t struct
* @deprecated This method is deprecated and will be removed in future versions. Please use getIPAddresses() instead.
*/
PCPP_DEPRECATED("This method is deprecated and will be removed in future versions. Please use getIPAddresses() instead.")
const std::vector<pcap_addr_t>& getAddresses() const { return m_Addresses; }

/**
* @return A vector containing all IP addresses defined for this interface.
*/
std::vector<IPAddress> getIPAddresses() const;

/**
* @return The MAC address for this interface
*/
Expand Down
23 changes: 23 additions & 0 deletions Pcap++/src/PcapLiveDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,29 @@ void PcapLiveDevice::setDefaultGateway()
#endif
}

std::vector<IPAddress> PcapLiveDevice::getIPAddresses() const
{
std::vector<IPAddress> results;
for (const auto& address : m_Addresses)
{
in_addr* ipv4Addr = internal::try_sockaddr2in_addr(address.addr);
if (ipv4Addr != nullptr)
{
results.push_back(IPv4Address(ipv4Addr->s_addr));
continue;
}

in6_addr* ipv6Addr = internal::try_sockaddr2in6_addr(address.addr);
if (ipv6Addr != nullptr)
{
results.push_back(IPv6Address(ipv6Addr->s6_addr));
continue;
}
}

return results;
}

IPv4Address PcapLiveDevice::getIPv4Address() const
{
for(const auto& addrIter : m_Addresses)
Expand Down
9 changes: 9 additions & 0 deletions Tests/Pcap++Test/Tests/LiveDeviceTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "../Common/TestUtils.h"
#include "../Common/PcapFileNamesDef.h"
#include <array>
#include <algorithm>
#include <cstdio>
#if defined(_WIN32)
# include "PcapRemoteDevice.h"
Expand Down Expand Up @@ -296,6 +297,14 @@ PTF_TEST_CASE(TestPcapLiveDevice)
PTF_ASSERT_NOT_NULL(liveDev);
PTF_ASSERT_GREATER_THAN(liveDev->getMtu(), 0);
PTF_ASSERT_TRUE(liveDev->open());

PTF_ASSERT_EQUAL(liveDev->getIPv4Address(), ipToSearch);
{
// Should probably be refactored as PTF_ASSERT_CONTAINS or similar.
auto const ipAddresses = liveDev->getIPAddresses();
PTF_ASSERT_TRUE(std::any_of(ipAddresses.begin(), ipAddresses.end(), [ipToSearch](pcpp::IPAddress const& addr) { return addr == ipToSearch; }));
}

DeviceTeardown devTeardown(liveDev);
int packetCount = 0;
int numOfTimeStatsWereInvoked = 0;
Expand Down

0 comments on commit 63bab8d

Please sign in to comment.