-
Notifications
You must be signed in to change notification settings - Fork 677
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
return false; | ||
} | ||
|
||
|
@@ -505,12 +505,15 @@ namespace pcpp | |
} | ||
|
||
// bind raw socket to interface | ||
struct ifreq ifr; | ||
memset(&ifr, 0, sizeof(ifr)); | ||
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifaceName.c_str()); | ||
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (void*)&ifr, sizeof(ifr)) == -1) | ||
struct sockaddr_ll saddr; | ||
memset(&saddr, 0, sizeof(saddr)); | ||
saddr.sll_family = AF_PACKET; | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Do we need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in c43e110 |
||
{ | ||
PCPP_LOG_ERROR("Cannot bind raw socket to interface '" << ifaceName << "'"); | ||
PCPP_LOG_ERROR("Cannot bind raw socket to interface '" << ifaceName << "': " << strerror(errno)); | ||
egecetin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
::close(fd); | ||
return false; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 safeThere was a problem hiding this comment.
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...There was a problem hiding this comment.
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
There was a problem hiding this comment.
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