diff --git a/Pcap++/header/PcapLiveDevice.h b/Pcap++/header/PcapLiveDevice.h index e4b88ba2f0..868af6a3a7 100644 --- a/Pcap++/header/PcapLiveDevice.h +++ b/Pcap++/header/PcapLiveDevice.h @@ -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& getAddresses() const { return m_Addresses; } + /** + * @return A vector containing all IP addresses defined for this interface. + */ + std::vector getIPAddresses() const; + /** * @return The MAC address for this interface */ diff --git a/Pcap++/src/PcapLiveDevice.cpp b/Pcap++/src/PcapLiveDevice.cpp index a2e1a79c8b..aeee7bb093 100644 --- a/Pcap++/src/PcapLiveDevice.cpp +++ b/Pcap++/src/PcapLiveDevice.cpp @@ -1168,6 +1168,29 @@ void PcapLiveDevice::setDefaultGateway() #endif } +std::vector PcapLiveDevice::getIPAddresses() const +{ + std::vector 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) diff --git a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp index 6cabae8912..b93c799899 100644 --- a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp +++ b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp @@ -11,6 +11,7 @@ #include "../Common/TestUtils.h" #include "../Common/PcapFileNamesDef.h" #include +#include #include #if defined(_WIN32) # include "PcapRemoteDevice.h" @@ -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;