Question

I want to get the TCP header of each received TCP packet (destined for a specific program,namely, a port) it seems not possible to use recv() or recvfrom() to get the TCP header using regular socket.

So, I want to use raw socket with raw socket, I can use recvfrom() to receive all IP packets destined to the host if the host receives a lot packets, I need to check the port number of each packet so see whether it is the needed. then it could be very time and CPU comsuming?

while()
{
recvfrom();
check_port_number(); 
}

are there any good ways to get the tcp header of a received tcp packet? I'm working with linux c, so THE solution should be linux c-based. thanks!

Was it helpful?

Solution

You want to use the libpcap API, not the socket API. libpcap lets you read packets right off the wire.

Another library you can use to ease parsing the packets is libnet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top