c语言解析pcap,使用libpcap读取纳秒pcap文件

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

我有一个纳秒的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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值