Re: recvfrom with MSG_PEEK

Theodore Y. Ts'o (tytso@MIT.EDU)
Tue, 20 May 1997 20:36:40 -0400


From: alan@lxorguk.ukuu.org.uk (Alan Cox)
Date: Tue, 20 May 1997 23:35:36 +0100 (BST)

> recvfrom(sock, (char *)NULL, 0, MSG_PEEK, (struct sockaddr *)&sin, &size)
> should return the address from which the data was sent in sin. This does
> not happen on Linux but on *BSD. Where as calling with "(char *)buf, 1"

You didnt receive any data, you read none so it didnt come from
anywhere. I'd argue Linux did exactly the right thing. For a
connected socket then yes I guess it could return where the data must
have come from, but that isnt clear for some protocol types.

Since you're doing a MSG_PEEK, recvfrom is supposed to return
information about the next packet in the receive queue, *without*
*removing* *it*. That is, the next time you call recvfrom, you're
supposed to get the same packet.

Hence, if you pass in a NULL buffer with MSG_PEEK, it would make sense
to return the address of the incoming packet, again without removing
it. So the next time you call recvfrom, you get the same packet.

It makes sense to me.....

- Ted