libc lwip recv MSG_PEEK doesn't behave as specified

Christian Helmuth christian.helmuth at
Tue Jun 19 19:17:01 CEST 2018

Hello Johannes,

I've to admit that we never considered about supporting this (rather
exotic) feature.

On Tue, Jun 19, 2018 at 03:52:16PM +0200, Johannes Kliemann wrote:
> Yet this doesn't seem to be the case (at least with lwip). I implemented
> a test [1] that creates a connection from the test http client and
> subsequently calls recv(sock, buf, 1, MSG_PEEK) which always should
> yield 'G' into the buffer (the first character of the GET request). Yet
> the output looks as follows:
> > [init -> test-http_clnt] got IP address
> > [init -> test-http_clnt] Create new socket ...
> > [init -> test-http_clnt] Connect to server ...
> > [init -> test-http_clnt] Send request...
> > [init -> test-libc_msg_peek] peek: G
> > [init -> test-libc_msg_peek] peek: E
> > [init -> test-libc_msg_peek] peek: T
> > [init -> test-libc_msg_peek] peek:  
> > [init -> test-libc_msg_peek] peek: /
> (this is just an extract but you get the point)
> The problem is that this doesn't only account to the recv call but also
> any further read call will miss the first "peeked" bytes which
> completely defeats the purpose of MSG_PEEK.
> According to the lwip_legacy port MSG_PEEK should be supported since
> 2007 and I'm not sure why it should stay broken such a long time.
> > 2007-06-30 Frédéric Bernon
> > * sockets.h, sockets.c: Implement MSG_PEEK flag for recv/recvfrom functions.
> As far as I can see I have used it correctly so I'm not sure why it
> isn't working. You can view the code at [1] and run the test with
> run/libc_msg_peek.

The reason for this behavior can be found in the libc_lwip library,
which (as my statement above hints) does not translate the 'flags'
parameter, which seems to be incompatibly defined in lwip and libc:

  grep -rn define.*MSG_PEEK $(./tool/ports/current lwip_legacy)/include $(./tool/ports/current libc)/include
  .../contrib/lwip_legacy-7f5610426ed8c4c64903136122d9ba58836cdc89/include/lwip_legacy/lwip/sockets.h:157:#define MSG_PEEK       0x01    /* Peeks at an incoming message */
  .../contrib/libc-5a5de5baab6e3ce23f24012a7604abf070fa990c/include/libc/sys/socket.h:450:#define	MSG_PEEK	0x2		/* peek at incoming message */

The current migration to VFS-based socket operations does not include
any support for recv flags too. Is this feature essential for your use

Christian Helmuth
Genode Labs · · /ˈdʒiː.nəʊd/

Genode Labs GmbH · Amtsgericht Dresden · HRB 28424 · Sitz Dresden
Geschäftsführer: Dr.-Ing. Norman Feske, Christian Helmuth

More information about the users mailing list