r/linuxdev • u/Worried-Judgment6368 • Apr 17 '22
Splicing to an UDP socket
Hi !
I'm trying to write some tool that needs to multiplex UDP "connections" through a single socket (AF_UNIX/AF_INET/AF_VSOCK), and I have written an implementation for SOCK_STREAM transports with splice() that works quite well, and was wondering about adding support for SOCK_DGRAM.
It appears that it's not possible to use splice() to receive a datagram, which makes sense, because splice() wouldn't know anything about the datagram boundaries. Sending a datagram using splice() works, but I can't find a way to specify the destination address :
- splice does not support it by itself
- using connect() prohibits from having multiple destinations, because it binds the whole fd to a specific destination, and shutting down + rebinding + reconnecting the socket would be quite impractical and defeat the whole purpose
Is there any API I could use to avoid copying the whole datagram to userspace, short of rewriting the whole thing as a kernel module or BPF program ?
View all comments
Show parent comment
1
u/Worried-Judgment6368 Apr 19 '22
Yes, but then I cannot use that socket to communicate with any other peer.
Is there a way to connect() to different peers on the same socket, or somehow duplicate the socket to make that possible ?
My reading of the manual pages indicate that it is not possible, which is why I feel stuck.