Pre-RFC: std::net expansion/refinement

I’m kinda busy right now, so I haven’t had a chance to read through everything - I’ll give a quick overview of a few things, and if there’s anything more you wanna know ping me and I’ll see if I can answer.

In terms of what socket()can handle for layer < 7 networking (data link/network/transport):

  • On Linux, you can do data link using AF_PACKET. This is not standard or portable - every other OS does this in a different and incompatible manner (ranging from custom system calls, to reading from a special /dev device), see also pnet::datalink::*.
  • In terms of network (say, implementing an IP stack) and transport layer (implementing TCP/UDP/DCCP/whatever), this is cross-platform, in a very kinda-sorta-not-really roundabout kind of manner. Each OS has its own little quirks and foibles. For example, OS X and FreeBSD will kindly change the endianness of certain fields of your packets (undocumented of course), and when working at the transport layer, the IP header may or not be included when receiving packets depending on a large variety of circumstances, and there’s something similar when sending. And, of course, this isn’t consistent per-OS.

I would highly recommend against having anything related to low-level networking in the standard library. I’ve been working on the stuff on and off for about two years with Rust, and it’s still a long way from being perfect*. It’s also a fairly nuanced/specific area, which is probably better in its own library (<insert shameless plug for people wanting to help with libpnet here>).

In terms of a standard socket() API, I’d need to think about it some more. If there’s an idea for an API I’d be happy to look over it and see how well any low level stuff could integrate with it. I can’t think of a nice way to do it off the top of my head, but that doesn’t mean it can’t be done. What I imagine is that it will be hard to have any level of sensible type safety without essentially integrating large chunks of libpnet into the standard library.

Hope this helps, I’m happy to answer any questions you may have.

* Where perfect here means something you’d be happy marking as #[stable] in libstd.

1 Like