传递给方法后结构体值发生变化
我正在使用 libpcap 编写数据包解析器,并在尝试从 pcap 标头读取数据包长度时遇到奇怪的行为。
下面的第一个 printf
语句打印出正确的数据包长度,而第二个语句则打印出一个数字,例如 362791。
struct pcap_pkthdr header;
pcap_t *handle;
const u_char *packet;
...
while((packet = pcap_next(handle, &header)) != NULL) {
printf("[%d]\n", header.len);
process_packet(&header, packet);
}
...
}
void process_packet(struct pcap_pkthdr *header, const u_char *packet) {
printf("[%d] %d bytes\n", header->ts, header->len);
}
struct pcap_pkthdr
的定义是:
struct pcap_pkthdr {
struct timeval ts; /* time stamp */
bpf_u_int32 caplen; /* length of portion present */
bpf_u_int32 len; /* length this packet (off wire) */
};
的原型>pcap_next
是 u_char *pcap_next(pcap_t *p, struct pcap_pkthdr *h)
这有什么原因吗发生?
I am writing a packet parser using libpcap and encounter strange behaviour when trying to read the packet length form the pcap header.
The first printf
statement below prints out the correct packet length, whereas the second one prints out a number such as 362791.
struct pcap_pkthdr header;
pcap_t *handle;
const u_char *packet;
...
while((packet = pcap_next(handle, &header)) != NULL) {
printf("[%d]\n", header.len);
process_packet(&header, packet);
}
...
}
void process_packet(struct pcap_pkthdr *header, const u_char *packet) {
printf("[%d] %d bytes\n", header->ts, header->len);
}
The definition of struct pcap_pkthdr
is:
struct pcap_pkthdr {
struct timeval ts; /* time stamp */
bpf_u_int32 caplen; /* length of portion present */
bpf_u_int32 len; /* length this packet (off wire) */
};
The prototype for pcap_next
is u_char *pcap_next(pcap_t *p, struct pcap_pkthdr *h)
Is there any reason that this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为根本问题在于代码
printf("[%d] %d bytes\n", header->ts, header->len);
中的
ts< /code> 类型为 struct timeval
的数据段不仅仅是一个简单的整数数据,或者至少 sizeof(timeval) 与 sizeof(int) 不一样。这将导致 printf 在尝试获取 var 参数列表中的第二个参数时使用错误的地址偏移量。所以我认为
printf("[%d] %d bytes\n", header->len, header->ts ); 应该正确工作,但你不能简单地交换它们。
I think the root problem is that in the code
printf("[%d] %d bytes\n", header->ts, header->len);
the
ts
data section whose type isstruct timeval
is not just a simple integer data, or at least the sizeof(timeval) is not the same as sizeof(int). This will cause the printf uses the wrong address offset when trying to fetch the second parameter in a var arguments list.So I think
printf("[%d] %d bytes\n", header->len, header->ts );
should work correct, but you cannot simply exchange them.你的 C 代码看起来有效,我看不出语法有任何问题。所以这可能与C本身无关。
目前唯一想到的是某些东西正在后台同时运行并撞击您的结构。我对 pcap 了解不多,所以我无法帮助您了解具体细节...
但是,当我有这样的奇怪之处时,我通常会创建一个统计函数,它打印出有关结构的所有可想象的事情...并且到处调用统计函数...这就像一个迷你的内联单元测试。
我希望你将你的代码翻新成这个并尝试一下:
这可能会帮助你解决你的错误。
your C code looks valid, I can't see any problem with the syntax. So it might be something not related to C itself.
The only thing that currently comes to mind is that something is running concurrently in the background and slamming your struct. I don't know a lot about pcap, so I can't help you with the specifics...
BUT, When I have such oddities, I usually create a stats function which prints out every conceivable thing about a structure... and call the stats function everywhere... this is like a mini in-line unit test.
I'd like you to refurbish your code into this and try it out:
this might help you corner your bug.
...您在比较苹果和橙子吗?
... are you comparing apples to oranges?