Skip to content

Add accept_from returning remote address #2609

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2609.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `accept_from` function returning the remote-address.
35 changes: 35 additions & 0 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2364,6 +2364,41 @@ pub fn accept4(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> {
Errno::result(res)
}

/// Accept a connection on a socket, returning the remote address.
///
/// [Further reading](https://man7.org/linux/man-pages/man2/accept.2.html)
#[cfg(any(
all(
target_os = "android",
any(
target_arch = "aarch64",
target_arch = "x86",
target_arch = "x86_64"
)
),
freebsdlike,
netbsdlike,
target_os = "emscripten",
target_os = "fuchsia",
solarish,
target_os = "linux",
))]
pub fn accept_from<S: SockaddrLike>(sockfd: RawFd, flags: SockFlag) -> Result<(RawFd, Option<S>)> {
let mut storage = std::mem::MaybeUninit::<libc::sockaddr_storage>::uninit();
let mut socklen = std::mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;
let res = unsafe {
libc::accept4(sockfd, storage.as_mut_ptr().cast(), &mut socklen as *mut _, flags.bits())
};

let sock = Errno::result(res)?;
let addr = unsafe {
let storage = storage.assume_init();
S::from_raw((&storage as *const libc::sockaddr_storage).cast(), Some(socklen))
};

Ok((sock, addr))
}

/// Initiate a connection on a socket
///
/// [Further reading](https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html)
Expand Down
53 changes: 53 additions & 0 deletions test/sys/test_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3161,3 +3161,56 @@ fn can_open_routing_socket() {
socket(AddressFamily::Route, SockType::Raw, SockFlag::empty(), None)
.expect("Failed to open routing socket");
}

#[cfg(any(
all(
target_os = "android",
any(
target_arch = "aarch64",
target_arch = "x86",
target_arch = "x86_64"
)
),
freebsdlike,
netbsdlike,
target_os = "emscripten",
target_os = "fuchsia",
solarish,
target_os = "linux",
))]
#[test]
fn test_accept_from() {
use nix::sys::socket::{accept_from, bind, connect, listen, socket};
use nix::sys::socket::{Backlog, SockFlag, SockType, SockaddrIn};
use std::net::Ipv4Addr;

let sock_addr = SockaddrIn::from_str("127.0.0.1:6780").unwrap();
let listener = socket(
AddressFamily::Inet,
SockType::Stream,
SockFlag::empty(),
None,
)
.expect("listener socket failed");
bind(listener.as_raw_fd(), &sock_addr).expect("bind failed");
listen(&listener, Backlog::MAXCONN).expect("listen failed");

let connector = socket(
AddressFamily::Inet,
SockType::Stream,
SockFlag::empty(),
None,
)
.expect("connector socket failed");

let send_thread =
std::thread::spawn(move || connect(connector.as_raw_fd(), &sock_addr));
let (_peer, address) =
accept_from::<SockaddrIn>(listener.as_raw_fd(), SockFlag::empty())
.unwrap();
let address = address.expect("no address");

assert_eq!(address.ip(), Ipv4Addr::LOCALHOST);

send_thread.join().unwrap().unwrap();
}
Loading