pcap_loop的pcap_handler的第三个参数是什么意思?

Redshadows 2012-08-10 05:11:26
用户手册里面写着,可是本人英语不太好,最后一句看不懂


typedef void (*pcap_handler)(u_char *user, const struct pcap_pkthdr *h,
const u_char *bytes);

int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user);

callback specifies a pcap_handler routine to be called with three argu-
ments: a u_char pointer which is passed in the user argument to
pcap_loop() or pcap_dispatch(), a const struct pcap_pkthdr pointer
pointing to the packet time stamp and lengths, and a const u_char
pointer to the first caplen (as given in the struct pcap_pkthdr a
pointer to which is passed to the callback routine) bytes of data from
the packet.


a const u_char pointer to the first caplen (as given in the struct pcap_pkthdr a pointer to which is passed to the callback routine) bytes of data from the packet.
请问一下是什么意思?

就是pcap_handler的const u_char *bytes是什么干什么用的?我google了也找不到。。。

万分感谢。
...全文
446 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2012-08-11
  • 打赏
  • 举报
回复
建议多读官方文档:

ts
a struct timeval containing the time when the packet was captured
caplen
a bpf_u_int32 giving the number of bytes of the packet that are available from the capture
len
a bpf_u_int32 giving the length of the packet, in bytes (which might be more than the number of bytes available from the capture, if the length of the packet is larger than the maximum number of bytes to capture)

这是pcap_pkthdr的成员,原理很简单:

回调被调用时,一定是一个完整的包被捕获了,pcap就是这么强大,所有类型它都能识别并完成的捕获。

caplen是capture length的意思,是被捕获的长度。 和len的区别就是pcap支持限制捕获包的长度,超长部分将会被截断,这个len就是包原本的长度,caplen就是被截断的长度。

你不特殊设置,包是不会被截断的,多长都会被容纳,所以caplen和len是相同的,都是指bytes的字节数。

bytes就是从链路层开始的一个完整的包,无论是什么协议,这需要你自己去解析bytes了,就是先解链路层,偏移后再解IP/ARP等等层,慢慢到TCP/UDP等。 在未限制包长情况下,bytes一定是完整的一个包,如果限制了那么注意不要超过bytes的caplen长度,否则会非法操作内存。

Redshadows 2012-08-11
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

建议多读官方文档:

ts
a struct timeval containing the time when the packet was captured
caplen
a bpf_u_int32 giving the number of bytes of the packet that are available from the capture
len
a bpf_u_in……
[/Quote]
刚才自己解决了,和你说的一样,非常感谢。

看过man,不理解那句话,所以才来这里问~~
Redshadows 2012-08-10
  • 打赏
  • 举报
回复
typedef void (*pcap_handler)(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes);

我看到一段别人定义的callback的代码

它把从bytes + 12处开始的长为h->len的数据保存起来,这又是什么意思呢?

分不多,希望各位不吝赐教,感激不尽。
Redshadows 2012-08-10
  • 打赏
  • 举报
回复
a const u_char pointer to the first caplen (as given in the struct pcap_pkthdr a pointer to which is passed to the callback routine) bytes of data from the packet.

caplen不是一个值吗?表示接收到的数据包的实际长度?怎么又有个指针指向第一个caplen了???

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧