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

Change PcapLiveDevice to store IP information as IPAddresses. #1497

Merged
merged 25 commits into from
Oct 12, 2024

Conversation

Dimi1010
Copy link
Collaborator

@Dimi1010 Dimi1010 commented Jul 12, 2024

Fixes #1439 and #1499.

This IP changes the internal structure of PcapLiveDevice to hold the IP information in IPAddress classes instead of pcap_addr structs. This fixes the issues where IP information might be randomly overwritten. Additionally the clean separation between the device information and the device proper allows cleaner clone procedure where the cached device information can be used to instantiate another Device object. The reworked procedure also fixes the clone issues with PcapRemoteDevice.

PcapLiveDevice changes

A new protected structure DeviceInformationDetails has been added to PcapLiveDevice to hold the relatively static information about the device (name, description, addresses). The previously separate device members are replaced by a single member structure that is now used to hold that information.

NB: The change removes the previously deprecated method getAddresses() and should NOT be merged before its deprecation cycle is complete

In addition to the changes a new constructor overload has been added to PcapLiveDevice and derived classes taking the abovementioned structure instead of pcap_if_t pointer, allowing device construction from cached information.

Optimizing the clone procedure

During the clone procedure, the m_InterfaceDetails structure is used to inject the current device's parameters to the clone's constructor without the need to re-fetch and search through the entire device list from the Pcap layer. The clone method is updated to a virtual method and performs the construction of a new device object directly without re-fetching the devices.

The method cloneInternal has been removed as superfluous, and derived implementations have been updated to override the clone method instead.

Fix for clone of PcapRemoteDevice.

  • Added an override of clone to PcapRemoteDevice to instantiate a new remote device from the current one's interface details.
  • Added unit tests to check if the return object of a PcapRemoteDevice::clone method returns a non-null PcapLiveDevice pointer that can be successfully downcasted to a PcapRemoteDevice pointer.

@Dimi1010 Dimi1010 requested a review from seladb as a code owner July 12, 2024 18:03
@Dimi1010 Dimi1010 linked an issue Jul 12, 2024 that may be closed by this pull request
@Dimi1010 Dimi1010 marked this pull request as draft July 12, 2024 18:08
@Dimi1010 Dimi1010 linked an issue Aug 12, 2024 that may be closed by this pull request
2 tasks
@Dimi1010 Dimi1010 changed the title Fix PcapRemoteDevice clone method. Change PcapLiveDevice to store IP information as IPAddresses. Aug 12, 2024
Copy link

codecov bot commented Aug 12, 2024

Codecov Report

Attention: Patch coverage is 57.72358% with 52 lines in your changes missing coverage. Please review.

Project coverage is 83.23%. Comparing base (40ba4c5) to head (8e412c0).
Report is 1 commits behind head on dev.

Files with missing lines Patch % Lines
Pcap++/src/PcapLiveDevice.cpp 75.40% 12 Missing and 3 partials ⚠️
Pcap++/src/PcapRemoteDevice.cpp 0.00% 12 Missing ⚠️
Pcap++/src/PcapRemoteDeviceList.cpp 0.00% 12 Missing ⚠️
Pcap++/src/PcapLiveDeviceList.cpp 50.00% 6 Missing ⚠️
Pcap++/header/PcapRemoteDevice.h 0.00% 3 Missing ⚠️
Pcap++/src/WinPcapLiveDevice.cpp 70.00% 3 Missing ⚠️
Pcap++/header/PcapLiveDevice.h 88.88% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1497      +/-   ##
==========================================
+ Coverage   80.05%   83.23%   +3.17%     
==========================================
  Files         276      276              
  Lines       42074    46883    +4809     
  Branches     9339     9407      +68     
==========================================
+ Hits        33684    39021    +5337     
- Misses       6664     7008     +344     
+ Partials     1726      854     -872     
Flag Coverage Δ
fedora40 74.86% <52.38%> (+0.13%) ⬆️
macos-12 81.19% <62.50%> (+0.06%) ⬆️
macos-13 80.62% <63.01%> (+0.06%) ⬆️
macos-14 80.54% <63.01%> (+0.07%) ⬆️
mingw32 70.84% <35.78%> (+0.20%) ⬆️
mingw64 70.80% <35.78%> (+0.20%) ⬆️
npcap 85.20% <52.47%> (?)
rhel94 74.68% <50.00%> (+0.10%) ⬆️
ubuntu2004 58.01% <47.45%> (+0.09%) ⬆️
ubuntu2004-zstd 58.11% <47.45%> (+0.06%) ⬆️
ubuntu2204 74.61% <48.38%> (+0.08%) ⬆️
ubuntu2204-icpx 58.88% <44.59%> (+0.17%) ⬆️
ubuntu2404 74.88% <50.81%> (+0.12%) ⬆️
unittest 83.23% <57.72%> (+3.17%) ⬆️
windows-2019 85.24% <52.47%> (?)
windows-2022 85.26% <53.60%> (?)
winpcap 85.22% <52.47%> (?)
xdp 49.73% <40.32%> (+0.14%) ⬆️

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.

# Conflicts:
#	Pcap++/header/PcapDevice.h
#	Pcap++/header/WinPcapLiveDevice.h
#	Pcap++/src/PcapLiveDevice.cpp
#	Pcap++/src/PcapRemoteDevice.cpp
#	Pcap++/src/PcapRemoteDeviceList.cpp
@Dimi1010 Dimi1010 added the breaking change Pull request contains a breaking change to the public API. label Oct 4, 2024
@Dimi1010 Dimi1010 marked this pull request as ready for review October 4, 2024 09:54
Copy link
Owner

@seladb seladb left a comment

Choose a reason for hiding this comment

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

Please see a few minor comments, otherwise LGTM

Comment on lines +83 to +96
/// @struct DeviceInterfaceDetails
/// @brief A struct that contains all details of a network interface.
struct DeviceInterfaceDetails
{
explicit DeviceInterfaceDetails(pcap_if_t* pInterface);
/// @brief Name of the device.
std::string name;
/// @brief Description of the device.
std::string description;
/// @brief IP addresses associated with the device.
std::vector<IPAddress> addresses;
/// @brief Flag to indicate if the device is a loopback device.
bool isLoopback;
};
Copy link
Owner

Choose a reason for hiding this comment

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

very nit: no need for doxygen documentation for private/protected structs

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, its not strictly required, but I think its fine to stay, right?

Copy link
Owner

Choose a reason for hiding this comment

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

Sure we can leave it 🙂

@@ -91,8 +91,13 @@ namespace pcpp
std::shared_ptr<PcapRemoteAuthentication> m_RemoteAuthentication;

// c'tor is private, as only PcapRemoteDeviceList should create instances of it, and it'll create only one for
// every remote interface
Copy link
Owner

Choose a reason for hiding this comment

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

Why remove this line? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

huh, good catch. did not mean that to be removed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed d9bef35

@Dimi1010 Dimi1010 merged commit 662f027 into seladb:dev Oct 12, 2024
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Pull request contains a breaking change to the public API. bug refactoring
Projects
None yet
2 participants