nix icon indicating copy to clipboard operation
nix copied to clipboard

does not support sockaddr_in for libc::accept

Open fzyzcjy opened this issue 5 years ago • 3 comments
trafficstars

Hi thanks for the lib! I find the accept does not allow us to know the struct sockaddr_in.

fzyzcjy avatar Sep 19 '20 06:09 fzyzcjy

Could you describe your problem in more detail, please?

asomers avatar Oct 03 '20 16:10 asomers

@asomers Looking at the nix doc and the linux doc, it seems that the nix's accept has one less effective argument, thus we cannot get info of sockaddr_in. Thanks!

fzyzcjy avatar Oct 04 '20 00:10 fzyzcjy

Workaround

For people reading this in the future: I worked around it by calling getpeername() on the file descriptor.

We can prevent this call to getpeername() and I'll provide a PR if there's any possibility that it would get merged.

Description of the problem:

POSIX requires that the accept() function looks like this in C:

#include <sys/socket.h>

int accept(int socket, struct sockaddr *restrict address,
       socklen_t *restrict address_len);

So, in C, we can specify the file descriptor of the socket and get the address of the peer using the last two arguments.

Using Rust and Nix, we can't get the peer address, because Nix sends NULL pointers, which means the C library won't tell us the peer address.

Suggested change of signature

// current signature
pub fn accept(sockfd: RawFd) -> Result<RawFd>

// proposed signature
pub fn accept<T: SockaddrLike>(sockfd: RawFd) -> Result<(RawFd, T)>

We would keep the API consistent with getpeername(), getsockname() and recvfrom() which also live in the socket crate.

This change is not backwards compatible, but that seems unavoidable to me.

lk16 avatar Apr 15 '23 15:04 lk16