为什么我在使用 libnfqueue 进行的 recv() 调用中得到了额外的位?
我在使用以下代码时遇到问题。我毫无问题地收到了数据,但是该数据加载了无关的位!例如,以下代码获取从 nfqueue 定向到它的所有流量,并打印出每个字节,后跟换行符。
起初,数据正是我所期望的,但后来有些行看起来有 4 个字节!
int main(int argc, char** argv) {
int fd;
ssize_t rv;
char buf[4096];
struct nfq_handle* h;
struct nfq_q_handle* qh;
h = nfq_open();
if (!h) {
fprintf(stderr, "error during nfq_open()\n");
exit(1);
}
if (nfq_unbind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nfq_unbind_pf()\n");
exit(1);
}
if (nfq_bind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nfq_bind_pf()\n");
exit(1);
}
printf("Binding to queue 0...\n");
qh = nfq_create_queue(h, 0, &cb, NULL);
if (!qh) {
fprintf(stderr, "error during nfq_create_queue()\n");
exit(1);
}
printf("Copying packets...\n");
if (nfq_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) {
fprintf(stderr, "error during nfq_set_mode()\n");
exit(1);
}
fd = nfq_fd(h);
memset(buf, 0, 4096);
while ((rv = recv(fd, buf, sizeof(buf), 0)) && rv >= 0) {
for (int i = 0; i < rv; i++) printf("%02x\n", *(buf+i));
printf("\n\n");
nfq_handle_packet(h, buf, rv);
}
nfq_destroy_queue(qh);
nfq_close(h);
}
输出:
...
50
10
39
08
48
ffffffa4
00
00
...
我在互联网上或错误跟踪器中找不到与此问题类似的任何内容。我可以从哪里开始诊断?为什么我的数据中有这些额外的位?我该如何修复它?
我与 tcpdump 的输出进行了比较,它们也没有出现在那里。
I'm having problems with the following code. I receive my data with no problems, however that data is loaded with extraneous bits! For example, the following code grabs all traffic directed to it from nfqueue, and prints out each byte followed by a newline.
At first the data is exactly what I'd expect, but then there are lines that have seeming 4 bytes on them!
int main(int argc, char** argv) {
int fd;
ssize_t rv;
char buf[4096];
struct nfq_handle* h;
struct nfq_q_handle* qh;
h = nfq_open();
if (!h) {
fprintf(stderr, "error during nfq_open()\n");
exit(1);
}
if (nfq_unbind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nfq_unbind_pf()\n");
exit(1);
}
if (nfq_bind_pf(h, AF_INET) < 0) {
fprintf(stderr, "error during nfq_bind_pf()\n");
exit(1);
}
printf("Binding to queue 0...\n");
qh = nfq_create_queue(h, 0, &cb, NULL);
if (!qh) {
fprintf(stderr, "error during nfq_create_queue()\n");
exit(1);
}
printf("Copying packets...\n");
if (nfq_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) {
fprintf(stderr, "error during nfq_set_mode()\n");
exit(1);
}
fd = nfq_fd(h);
memset(buf, 0, 4096);
while ((rv = recv(fd, buf, sizeof(buf), 0)) && rv >= 0) {
for (int i = 0; i < rv; i++) printf("%02x\n", *(buf+i));
printf("\n\n");
nfq_handle_packet(h, buf, rv);
}
nfq_destroy_queue(qh);
nfq_close(h);
}
Output:
...
50
10
39
08
48
ffffffa4
00
00
...
I couldn't find anything on the internet, or in bug trackers similar to this problem. Where can I begin to diagnose it? Why are there all these extra bits in my data? How can I fix it?
I did a comparison with the output from tcpdump, and they aren't appearing there either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来缓冲区包含值超过 128 的 char,并且发生有符号/无符号转换错误。
我已经计算了你的字节值 - 它是 164。尝试将你的字节转换为无符号字符,然后再将它们传递给 printf:
It seems that buffer contains char with value exceeding 128 and the signed/unsigned conversion error takes place.
I've calculated the value of your byte - it is 164. Try to convert your bytes into unsigned chars before passing them to printf: