0
votes

I understand that a TCP "packet" contains several headers (Ethernet, IP, TCP, ...). When calling read() on a SOCK_STREAM socket, what header information is included? Does the kernel strip off some of the headers before exposing the data the application-space?

Scenario: I want to read TCP packets on a socket in my application. To get a full packet, I need to know the length of the packet. To know that, I need to know how to interpret the data read by the SOCK_STREAM socket, and to do that I need to know if the data I read includes the TCP header or only the information after the header.

2
No header information is included. You just get the data.Barmar
Yeah - at application level, with a TCP SOCK_STREAM socket, there are no packets. read/recv, if sucessful and the connection is not closed, returns some number of streamed bytes above zero and not greater than the passed buffer size. There is no guaranteed 1-1 correspondence with any packets or segments sent or received on any interfaces by any routes below.Martin James

2 Answers

1
votes

When you call read(), you just get the data, none of the header information.

You can get information about the socket by using functions like getsockaddr() and ioctl(). But this won't return information about individual packets.

If you need packet-level details, you need to use something like libpcap.

0
votes

Using read() will not provide you with the header information you needed, but will only get you the data