-
Notifications
You must be signed in to change notification settings - Fork 654
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
Set the length of a socket address when calling recvmsg
on Linux
#2041
Merged
bors
merged 11 commits into
nix-rust:master
from
parallelsystems:set-sockaddr-length-linux
Jul 17, 2023
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
905c23e
Set the length of a sockaddr received on Linux
JarredAllen 96fa5a8
Add test coverage for bug with unset socket address length
JarredAllen c80828e
PR suggestion: Set the length later instead of using `from_raw`
JarredAllen 72a2f56
PR suggestions: use `skip` macro
JarredAllen e5bd9ba
Fixup accidentally-introduced changes
JarredAllen ff0fb3d
Remove redundant `unsafe` block
JarredAllen 3174d85
Merge branch 'master' into set-sockaddr-length-linux
JarredAllen 012e788
More PR feedback
JarredAllen 23125cb
Merge branch 'master' into set-sockaddr-length-linux
JarredAllen 2378f12
Correctly let the kernel set the length on BSDs
JarredAllen 2d60044
Fix lint
JarredAllen 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
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
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -202,6 +202,50 @@ pub fn test_socketpair() { | |||||||
assert_eq!(&buf[..], b"hello"); | ||||||||
} | ||||||||
|
||||||||
#[test] | ||||||||
pub fn test_recvmsg_sockaddr_un() { | ||||||||
use nix::sys::socket::{ | ||||||||
self, bind, socket, AddressFamily, MsgFlags, SockFlag, SockType, | ||||||||
}; | ||||||||
|
||||||||
let tempdir = tempfile::tempdir().unwrap(); | ||||||||
let sockname = tempdir.path().join("sock"); | ||||||||
let sock = socket( | ||||||||
AddressFamily::Unix, | ||||||||
SockType::Datagram, | ||||||||
SockFlag::empty(), | ||||||||
None, | ||||||||
) | ||||||||
.expect("socket failed"); | ||||||||
let sockaddr = UnixAddr::new(&sockname).unwrap(); | ||||||||
bind(sock, &sockaddr).expect("bind failed"); | ||||||||
|
||||||||
// Send a message | ||||||||
let send_buffer = "hello".as_bytes(); | ||||||||
if let Err(e) = socket::sendmsg( | ||||||||
sock, | ||||||||
&[std::io::IoSlice::new(send_buffer)], | ||||||||
&[], | ||||||||
MsgFlags::empty(), | ||||||||
Some(&sockaddr), | ||||||||
) { | ||||||||
print!("Couldn't send ({e:?}), so skipping test"); | ||||||||
return; | ||||||||
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.
Suggested change
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. Done in 72a2f56 |
||||||||
} | ||||||||
|
||||||||
// Receive the message | ||||||||
let mut recv_buffer = [0u8; 32]; | ||||||||
let received = socket::recvmsg( | ||||||||
sock, | ||||||||
&mut [std::io::IoSliceMut::new(&mut recv_buffer)], | ||||||||
None, | ||||||||
MsgFlags::empty(), | ||||||||
) | ||||||||
.unwrap(); | ||||||||
// Check the address in the received message | ||||||||
assert_eq!(sockaddr, received.address.unwrap()); | ||||||||
} | ||||||||
|
||||||||
#[test] | ||||||||
pub fn test_std_conversions() { | ||||||||
use nix::sys::socket::*; | ||||||||
|
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.
Why use sockaddr_storage instead of S?
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.
It didn't pass tests when I used
S
, I think because()
is shorter than the actual socket address type so it ran out of room (though I didn't check for the cause, so I might be wrong about that). Though the point is moot as I put it back to how it was before for implementing your suggestion about the different approach.