libpcap 数据包大小
我正在 linux (centos) 上使用 C 语言的 libpcap,并且正在关注 this我想简单地以 ascii 格式打印出整个数据包,并且我设法通过在“my_callback”函数中将其转换为 u_char* 来使其工作。但我不知道如何获取数据的长度。 strstr 不起作用。标头有一个 len 成员,您可以访问该成员来获取大小,但我找不到与正在传递的 * 数据包类似的任何内容。任何帮助表示赞赏。
I'm working with libpcap in C on linux (centos) and I'm following this guide I want to simply print out the entire packet in ascii and i'v managed to get it working by casting it a u_char* in the "my_callback" function. But I can't figure out how to get the length of the data. strstr didn't work. the header has a len member that you can access to get the size but I can't find anything similar for the *packet being passed. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在回调中,pkthdr 变量的 caplen 成员(请参阅 struct pcap_pkthdr)包含捕获的数据包的大小。
例如,假设捕获了一个数据包。帧的总长度为1024字节。但是,捕获驱动程序仅捕获帧的前 128 个字节并使其可供回调使用。
在这种情况下,您应该期望 pkthdr->caplen 为 128,而 header->len 为 1024。
In your callback the caplen member of the pkthdr variable (see struct pcap_pkthdr) contains the size of the captured packet.
For example assume a packet is captured. The total length of the frame is 1024 bytes. However the capture driver only captured the first 128 bytes of the frame and made it available to your callback.
In this case you should expect pkthdr->caplen to be 128 and header->len to be 1024.
总数据包大小位于 ip 标头的“总长度”字段中 (http://en. wikipedia.org/wiki/IPv4_header#Total_Length)。
如何使用 libpcap 获取该值可以在此示例中找到:http://www.tcpdump.org/sniffex.c
您只需从以下位置获取引用“总长度”(名为 ip_len)的字段值:这个变量:
The total packet size is at the "total length" field at the ip header (http://en.wikipedia.org/wiki/IPv4_header#Total_Length).
How to get that value with libpcap can be found at this example: http://www.tcpdump.org/sniffex.c
You just have to get the value of the field that references to the "total length" (named ip_len) from this variable: