我有一个纳秒的libpcap(nanosec.pcap)文件,并且可以使用Wireshark显示纳秒时间戳(例如2.123456789).现在我想用C语言打开纳秒级libpcap文件,源代码如下.当我尝试使用pcap_open_offine()打开nanosec.pcap时,它将返回“未知文件格式”错误.此外,通过将nanosec.pcap标头处的幻数更改为普通pcap(0x1A2B3C4D),我从终端(Ubuntu)获得了分段错误.这里的任何专家都可以建议如何使用libpcap显示时间戳的纳秒部分?提前致谢!
以下是代码:
#include
#include
#include
#include
#include
#include
#include
#include
struct UDP_hdr {
u_short uh_sport; /* source port */
u_short uh_dport; /* destination port */
u_short uh_ulen; /* datagram length */
u_short uh_sum; /* datagram checksum */
};
/* Some helper functions, which we define at the end of this file. */
/* Returns a string representation of a timestamp. */
const char *timestamp_string(struct timeval ts);
/* Report a problem with dumping the packet with the given timestamp.

本文介绍了如何使用C语言和libpcap库解析包含纳秒时间戳的pcap文件。作者遇到的问题是在尝试用pcap_open_offline()打开文件时收到"未知文件格式"错误,通过修改pcap文件头部魔术字为标准pcap导致分段错误。代码示例展示了如何读取并打印UDP包的源端口、目标端口和长度,以及处理包过短或非UDP包的情况。
最低0.47元/天 解锁文章
1753

被折叠的 条评论
为什么被折叠?



