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

Use bind() instead of setsockopt() for binding raw sockets on Linux #1646

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

seladb
Copy link
Owner

@seladb seladb commented Nov 27, 2024

Fixes #1640 (implementing the solution proposed in the issue)

Copy link

codecov bot commented Nov 27, 2024

Codecov Report

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

Project coverage is 83.16%. Comparing base (871cbad) to head (c43e110).

Files with missing lines Patch % Lines
Pcap++/src/RawSocketDevice.cpp 62.50% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##              dev    #1646   +/-   ##
=======================================
  Coverage   83.16%   83.16%           
=======================================
  Files         277      277           
  Lines       48189    48191    +2     
  Branches     9731     9754   +23     
=======================================
+ Hits        40074    40079    +5     
+ Misses       7224     7222    -2     
+ Partials      891      890    -1     
Flag Coverage Δ
fedora40 75.20% <57.14%> (+<0.01%) ⬆️
macos-13 80.66% <ø> (ø)
macos-14 80.66% <ø> (ø)
macos-15 80.63% <ø> (ø)
mingw32 70.91% <ø> (ø)
mingw64 70.88% <ø> (ø)
npcap 85.32% <ø> (ø)
rhel94 75.03% <57.14%> (+<0.01%) ⬆️
ubuntu2004 58.61% <50.00%> (+<0.01%) ⬆️
ubuntu2004-zstd 58.73% <50.00%> (-0.02%) ⬇️
ubuntu2204 74.97% <50.00%> (+0.03%) ⬆️
ubuntu2204-icpx 61.44% <50.00%> (+0.03%) ⬆️
ubuntu2404 75.22% <50.00%> (+<0.01%) ⬆️
unittest 83.16% <62.50%> (+<0.01%) ⬆️
windows-2019 85.34% <ø> (ø)
windows-2022 85.37% <ø> (ø)
winpcap 85.33% <ø> (ø)
xdp 50.53% <0.00%> (-0.01%) ⬇️

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.

@seladb seladb marked this pull request as ready for review November 27, 2024 08:24
saddr.sll_protocol = htons(ETH_P_ALL);
saddr.sll_ifindex = if_nametoindex(ifaceName.c_str());

if (bind(fd, reinterpret_cast<struct sockaddr*>(&saddr), sizeof(saddr)) < 0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Do we need the struct keyword here and on L508?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Fixed in c43e110

@@ -456,7 +456,7 @@ namespace pcpp
int fd = socket(AF_PACKET, SOCK_RAW, htobe16(ETH_P_ALL));
if (fd < 0)
{
PCPP_LOG_ERROR("Failed to create raw socket. Error code was " << errno);
PCPP_LOG_ERROR("Failed to create raw socket. Error code was " << strerror(errno));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not checked the full code so maybe not important but strerror is not thread safe

Copy link
Owner Author

Choose a reason for hiding this comment

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

I guess errno isn't thread safe either, so we should handle it differently if we want to be fully thread safe. In this case it might not be required beacuse it's the creation and binding of the socket...

Copy link
Collaborator

@egecetin egecetin Dec 2, 2024

Choose a reason for hiding this comment

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

Actually errno is thread safe since it is thread local but there are other strerror functions in library so better to fix it all later

Copy link
Owner Author

Choose a reason for hiding this comment

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

yes, I think you're right. I'll open a separate PR for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants