Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getter to fetch all IP addresses of a live device as IPAddress objects. #1500

Merged
merged 7 commits into from
Jul 29, 2024

Conversation

Dimi1010
Copy link
Collaborator

Stage one of #1499

  • Deprecated getAddresses() in PcapLiveDevice.h, advising to use getIPAddresses() instead for better functionality.
  • Implemented getIPAddresses() in PcapLiveDevice.cpp, which returns a vector of IPAddress objects for both IPv4 and IPv6 addresses.
  • Updated LiveDeviceTests.cpp to include tests for the new getIPAddresses() method, ensuring it correctly identifies and returns IP addresses.

Dimi1010 added 2 commits July 17, 2024 19:08
- Deprecated `getAddresses()` in `PcapLiveDevice.h`, advising to use `getIPAddresses()` instead for better functionality.
- Implemented `getIPAddresses()` in `PcapLiveDevice.cpp`, which returns a vector of `IPAddress` objects for both IPv4 and IPv6 addresses.
- Updated `LiveDeviceTests.cpp` to include tests for the new `getIPAddresses()` method, ensuring it correctly identifies and returns IP addresses.
@Dimi1010 Dimi1010 changed the title Refactor/live device ip addresses Add getter to fetch all IP addresses of a live device as a IPAddress objects. Jul 17, 2024
Copy link

codecov bot commented Jul 17, 2024

Codecov Report

Attention: Patch coverage is 88.46154% with 3 lines in your changes missing coverage. Please review.

Project coverage is 82.13%. Comparing base (1a436d9) to head (07264a2).

Files Patch % Lines
Tests/Pcap++Test/Tests/LiveDeviceTests.cpp 71.42% 0 Missing and 2 partials ⚠️
Pcap++/src/PcapLiveDevice.cpp 94.73% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1500      +/-   ##
==========================================
+ Coverage   82.09%   82.13%   +0.04%     
==========================================
  Files         273      273              
  Lines       43542    43568      +26     
  Branches     9509     9408     -101     
==========================================
+ Hits        35747    35786      +39     
+ Misses       7157     6952     -205     
- Partials      638      830     +192     
Flag Coverage Δ
fedora39 74.20% <53.33%> (+<0.01%) ⬆️
macos-12 80.33% <88.88%> (+0.02%) ⬆️
macos-13 79.71% <90.47%> (+0.03%) ⬆️
macos-14 79.63% <90.47%> (+0.05%) ⬆️
mingw32 70.71% <75.00%> (+0.02%) ⬆️
mingw64 70.72% <75.00%> (+0.02%) ⬆️
npcap 83.96% <93.75%> (+0.01%) ⬆️
rhel94 73.92% <53.33%> (-0.02%) ⬇️
ubuntu2004 74.20% <53.33%> (+0.03%) ⬆️
ubuntu2004-zstd 74.35% <53.33%> (+0.03%) ⬆️
ubuntu2204 73.88% <53.33%> (+0.01%) ⬆️
ubuntu2204-icpx 55.86% <42.85%> (+<0.01%) ⬆️
unittest 82.13% <88.46%> (+0.04%) ⬆️
windows-2019 84.00% <93.75%> (+0.04%) ⬆️
windows-2022 84.01% <93.75%> (+0.04%) ⬆️
winpcap 83.98% <93.75%> (+0.05%) ⬆️
xdp 48.74% <0.00%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Dimi1010 Dimi1010 linked an issue Jul 17, 2024 that may be closed by this pull request
2 tasks
@Dimi1010 Dimi1010 changed the title Add getter to fetch all IP addresses of a live device as a IPAddress objects. Add getter to fetch all IP addresses of a live device as IPAddress objects. Jul 18, 2024
@Dimi1010 Dimi1010 marked this pull request as ready for review July 21, 2024 16:58
@Dimi1010 Dimi1010 requested a review from seladb as a code owner July 21, 2024 16:58
in_addr* ipv4Addr = internal::try_sockaddr2in_addr(address.addr);
if (ipv4Addr != nullptr)
{
results.push_back(IPv4Address(ipv4Addr->s_addr));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be emplace_back, but it's also fine now.

Copy link
Collaborator Author

@Dimi1010 Dimi1010 Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in this?

Suggested change
results.push_back(IPv4Address(ipv4Addr->s_addr));
results.emplace_back(IPv4Address(ipv4Addr->s_addr));

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think push_back(std::move(IPv4Address)) is equal to emplace_back(IPv4Address

how about this?

results.emplace_back(ipv4Addr->s_addr);

But it might be too trivial to distinguish the difference.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested it on VS. That works for the IPv4, due to the uint32_t constructor. The IPv6 gets confused tho coz both v4 and v6 have a constructor from pointer.

Copy link
Collaborator Author

@Dimi1010 Dimi1010 Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it might be too trivial to distinguish the difference.

Eh, it can technically save a single move constructor if the compiler does not optimize it away anyway. Personally I think its fine with the push_back for now.

if (ipv4Addr != nullptr)
{
results.push_back(IPv4Address(ipv4Addr->s_addr));
PCPP_LOG_DEBUG("IPv4 address found: " << results.back());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this log?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. Added it for consistency as both getIPv4Address and getIPv6Address output address logs.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very old code, I think we can remove the logs 🙂

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed 4100859

std::vector<IPAddress> PcapLiveDevice::getIPAddresses() const
{
std::vector<IPAddress> results;
for (const auto& address : m_Addresses)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be simpler to use sockaddr2string to convert the address to string, and the call IPAddress c'tor with this string

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, wouldn't that not achieve the same but with extra steps? We add encoding to string and then decoding from string for not much benefit. Atm, most operations are read/check operations.

Maybe it would be more concise and skip the if checks in the current function, but I don't think the current implementatios is that bad.

Copy link
Owner

@seladb seladb Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean is that you can do something like this which is shorter and more concise:

for (const auto& address : m_Addresses)
{
	std::array<char, INET6_ADDRSTRLEN> addrAsString;
	internal::sockaddr2string(address.addr, addrAsString.data(), addrAsString.size());
	try
	{
		results.push_back(IPAddress(addrAsString));
	}
	catch(...) {}
}

Copy link
Collaborator Author

@Dimi1010 Dimi1010 Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but looking at the way it will be implemented adds some overhead that is avoided by the current code.

This is the current implementation of the IPAddress string constructor.

IPAddress::IPAddress(const std::string& addrAsString)
{
	if (IPv4Address::isValidIPv4Address(addrAsString))  //  <-- string parse
	{
		m_Type = IPv4AddressType;
		m_IPv4 = IPv4Address(addrAsString);  //  <-- potential string parse
	}
	else if (IPv6Address::isValidIPv6Address(addrAsString))  //  <-- string parse
	{
		m_Type = IPv6AddressType;
		m_IPv6 = IPv6Address(addrAsString);  //  <-- potential string parse
	}
	else
	{
		throw std::invalid_argument("Not a valid IP address: " + addrAsString);
	}
}

This is between 2 and 3 parses of the string address to get the IP object in addition to the initial encode of the IP to a string. The current implementation, while slightly longer is at most 4 nullptr checks, 2 integer equality checks and a buffer copy.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, we can go with your approach 🙂

if (ipv4Addr != nullptr)
{
results.push_back(IPv4Address(ipv4Addr->s_addr));
PCPP_LOG_DEBUG("IPv4 address found: " << results.back());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very old code, I think we can remove the logs 🙂

@seladb seladb merged commit d21c05c into seladb:dev Jul 29, 2024
38 checks passed
@Dimi1010 Dimi1010 deleted the refactor/live-device-IPAddresses branch July 29, 2024 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants