rust-lang / socket2

Advanced configuration options for sockets.
https://docs.rs/socket2
Apache License 2.0
683 stars 227 forks source link

Expose address from SockAddr for unix domains #332

Open sullivan-sean opened 2 years ago

sullivan-sean commented 2 years ago

It would be nice to be able to access the path of a unix socket from SockAddr similar to vsock_address

socket2 is being considered as a backend for UDS on windows in mio/tokio (https://github.com/tokio-rs/mio/pull/1610#issuecomment-1218388105) and, if used, would need to expose the address of unix sockets from SocketAddr to mirror the implementation of SocketAddr in mio for unix

Thomasdezeeuw commented 2 years ago

It would be nice to be able to access the path of a unix socket from SockAddr similar to vsock_address

Do you mean https://docs.rs/socket2/latest/socket2/struct.SockAddr.html#method.unix? I think we simply forgot to expose it in https://github.com/rust-lang/socket2/pull/249.

socket2 is being considered as a backend for UDS on windows in mio/tokio (tokio-rs/mio#1610 (comment)) and, if used, would need to expose the address of unix sockets from SocketAddr to mirror the implementation of SocketAddr in mio for unix

It's not ;)

sullivan-sean commented 2 years ago

I meant more going the other way from SockAddr -> Path. Right now the SockAddr struct has private fields and there are no methods for Unix sockets exposing the Path which means once you have a SockAddr object (e.g. as returned from accept) you cannot get the corresponding Path back out, or tell whether a SockAddr is an unnamed or abstract Unix socket (i.e. the SockAddr here can't provide this functionality https://github.com/tokio-rs/mio/blob/master/src/sys/unix/uds/socketaddr.rs#L76-L96)

Thomasdezeeuw commented 2 years ago

We could add SocketAddr::as_pathname() -> Option<Path>.

sullivan-sean commented 2 years ago

What is the desired behavior for abstract unix sockets where there is a name associated with the SockAddr struct but this name has no connection with filesystem pathnames? (https://manpages.ubuntu.com/manpages/trusty/man7/unix.7.html)

Unnamed unix sockets would presumably return None from as_pathname, and normal sockets would return a Path as expected.

Thomasdezeeuw commented 2 years ago

I think we can match the std::os::unix::net::SocketAddr type and match the is_unnamed, as_pathname and as_abstract_namespace functions.

sullivan-sean commented 2 years ago

Okay, I think I have a good understanding of the desired API then -- I can take a stab at this and open a PR?

Thomasdezeeuw commented 2 years ago

Okay, I think I have a good understanding of the desired API then -- I can take a stab at this and open a PR?

Sounds good.

aviramha commented 1 year ago

Hey, this seems inactive and we need it for https://github.com/metalbear-co/mirrord/issues/1105 . I read the comments and will follow the desired design.

Thomasdezeeuw commented 1 year ago

Hey, this seems inactive and we need it for metalbear-co/mirrord#1105 . I read the comments and will follow the desired design.

Pr would be welcome 👍

Thomasdezeeuw commented 1 year ago

This has been done in https://github.com/rust-lang/socket2/pull/403.

t4lz commented 1 year ago

This has been done in #403.

Only on unix though, so maybe there should still be an open issue for Windows.

Thomasdezeeuw commented 1 year ago

Reopening for Windows