nix icon indicating copy to clipboard operation
nix copied to clipboard

convert sockaddr_storage to std::net::SocketAddr

Open behzadnouri opened this issue 3 years ago • 3 comments
trafficstars

Is there any way to convert either of

libc::sockaddr_storage
nix::sys::socket::sockaddr_storage

to std::net::ScoketAddr?

I have looked into nix::sys::socket::SockaddrStorage but can't find what I am looking for.

behzadnouri avatar Jun 30 '22 16:06 behzadnouri

No, not directly. You have to pull it apart into pieces and then create a new std::net::SocketAddr using the standard constructors.

asomers avatar Jun 30 '22 16:06 asomers

No, not directly. You have to pull it apart into pieces and then create a new std::net::SocketAddr using the standard constructors.

How? Can you please point me to any api which converts sockaddr_storage to nix::sys::socket::SockaddrStorage or any other nix type which I can work with?

Thank you,

behzadnouri avatar Jun 30 '22 16:06 behzadnouri

If you're starting from a libc::sockaddr_storage, then use Nix's SockaddrStorage::from_raw. https://docs.rs/nix/latest/nix/sys/socket/trait.SockaddrLike.html#tymethod.from_raw .

asomers avatar Jun 30 '22 16:06 asomers

I'm going to close this, assuming that I've answered your question. But feel free to reopen if not.

asomers avatar Nov 19 '22 21:11 asomers