如何在套接字编程中获取接收到的数据包的tcp标头? [英] how to get the tcp header of a received packet in socket programming?

查看:99
本文介绍了如何在套接字编程中获取接收到的数据包的tcp标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取每个接收到的TCP数据包的TCP标头(用于特定程序,即端口) 似乎不可能使用recv()或recvfrom()通过常规套接字获取TCP标头.

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.

所以,我想使用原始套接字 使用原始套接字,我可以使用recvfrom()接收发往主机的所有IP数据包 如果主机收到很多数据包, 我需要检查每个数据包的端口号,以便查看是否需要它. 那么这可能是非常耗时和CPU消耗的时间?

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(); 
}

有没有什么好的方法来获取接收到的tcp数据包的tcp标头? 我正在使用linux c,因此该解决方案应基于linux c.谢谢!

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!

推荐答案

您要使用 libpcap API,而不是套接字API. libpcap可让您直接读取数据包.

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

可用于简化数据包解析的另一个库是libnet.

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

这篇关于如何在套接字编程中获取接收到的数据包的tcp标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆