Netlink socket support in SwiftNIO

We have a need to listen for interface up/down events in Linux and the normal way to accomplish this is using a netlink socket (See: netlink(7) - Linux manual page). My question is: does it seem appropriate to build netlink socket support into SwiftNIO?

As an experiment I attempted to create a netlink socket and use DatagramBootstrap.withBoundSocket to bootstrap this setup. However I ran into "unknown sockaddr family AF_NETLINK" errors. Pushing this even further I added netlink support to SocketAddress as a simple proof of concept and was able to properly receive and decode netlink messages. The commit for that proof of concept is here: PoC: Add netlink socket address support · Austinpayne/swift-nio@b043722 · GitHub

I realize that normally netlink is received over a raw socket and that my proof of concept breaks the public API so I do not think it is the proper way. I am looking for guidance from the NIO maintainers on how this might be implemented. My initial thoughts are that 1) we would implement a RawBootstrap class in NIO core that has less strict requirements for the socket address but would support netlink sockets (via sockaddr_nl) and 2) actual netlink message parsing code would be implemented outside of NIO core, probably in a third-party package.

Thanks for your input!

cc: @lukasa

Thanks for raising this!

Yes, I think it's appropriate to build netlink support into SwiftNIO. It's unfortunate that you've run into trouble with SocketAddress (though not entirely surprising, as we've hit this corner case ourselves a few times).

In the short term I think that we can consider netlink a special case and provide a direct NetlinkBootstrap. This could actually build directly on top of the DatagramChannel inside NIO, and simply add some special cases to it that allow it to tolerate being used in a "netlink mode" (avoiding attempting to populate local and remote address, for example). This would reduce the amount of code you needed to write.

I agree that the actual netlink parsing code should be done outside of NIO core.

Great, thanks for the tips Cory! I will take a look at this and see if I can get a PR opened soon.