Remix.run Logo
dgl 6 hours ago

The BSD socket API has 3 parameters when creating a socket with socket(), the family (e.g. inet) the kind (datagram in this case) and the protocol (often 0, but IPPROTO_ICMP in this case).

Because when the protocol is 0 it means a UDP socket Rust has called its API for creating any(?) datagram sockets UdpSocket, partly resulting in this confusion.

The kernel patch introducing the API also explains it was partly based on the UDP code, due to obviously sharing a lot of properties with it. https://lwn.net/Articles/420800/

erk__ 6 hours ago | parent | next [-]

The std api can only create UdpSockets, the trick here is that you use Socket2 which allows more kinds of sockets and then you tell UdpSocket that some raw file descriptor is a upd socket through a unsafe api with no checks and I guess it works because they use the same api on posix.

Edit: It is possible in safe rust as well, see child comment.

The macro used by socket2: https://docs.rs/socket2/0.6.1/src/socket2/lib.rs.html#108

The FromRawFd trait: https://doc.rust-lang.org/stable/std/os/fd/trait.FromRawFd.h...

the8472 4 hours ago | parent [-]

From/Into conversion via OwnedFd is the safe API, RawFd is the older and lower-level one.

erk__ 4 hours ago | parent [-]

Ahh I guess that means that its possible in safe rust to cast a file descriptor to a different type. I was just looking at how socket2 did it and forgot to have a proper look.

stingraycharles 6 hours ago | parent | prev | next [-]

So UdpSocket should really be called DatagramSocket, UDP being the protocol that operates on these datagrams?

Surprising that they got such a fundamental thing wrong.

krater23 5 hours ago | parent [-]

That happens when someones learning project ("I rewrite a library in the new language I want to learn") ends up in productive code.

IshKebab 5 hours ago | parent [-]

This is in the standard library; it's not a learning project. And it also isn't even incorrect - see erk__'s comment.

Rust is an excellent language and fully capable of production use.

Sharlin 3 hours ago | parent [-]

It's not, it's the `socket2` library. The standard sockets don't allow (ab)using actual `UdpSocket`s as a different kind of datagram socket.

IshKebab an hour ago | parent [-]

We're talking about UdpSocket: https://doc.rust-lang.org/stable/std/net/struct.UdpSocket.ht...

Sharlin 43 minutes ago | parent [-]

Yes, I know, but the point is that the standard UdpSocket is correctly named as it doesn’t represent any other datagram socket. Uh, we’re pribably in agreement here actually.

IshKebab 18 minutes ago | parent [-]

Yeah exactly! :-D

stavros 6 hours ago | parent | prev [-]

Thanks, that's quite the misnomer then.