Short version. Is there a Linux API that allows to read from a file descriptor (similar to read()) without actually removing the data from the OS buffer? Some way to split the read() into the equivalent of a front() (read without removing) and pop() (remove).
Scenario. I have a TUN device which I use to deliver IP datagrams via my own network stack. The problem is, especially when the application above uses UDP, the TUN device may be flooded with data by the OS, which my stack can't manage fast enough.
Goal. I'd like to: read a datagram as soon as it arrives over the TUN device, without removing it from the kernel buffer, inspect the datagram, decide if the rest of the proprietary protocol stack below can handle it, and if so, pop() the data from the kernel buffer, otherwise keep the data there.
Why. The reason it makes a difference not to pop() from the kernel buffer is that the application above can actually realize that the lower layers of the network protocols are congested (since its own write() or send() over the network interface would fail), and act accordingly. If the data is removed, as with a regular read(), the application doesn't have any clue about congestion, and keeps flooding.