为什么我在使用 libnfqueue 进行的 recv() 调用中得到了额外的位?

发布于 2024-11-28 12:21:44 字数 1508 浏览 1 评论 0原文

我在使用以下代码时遇到问题。我毫无问题地收到了数据,但是该数据加载了无关的位!例如,以下代码获取从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东风软 2024-12-05 12:21:44

看来缓冲区包含值超过 128 的 char,并且发生有符号/无符号转换错误。

我已经计算了你的字节值 - 它是 164。尝试将你的字节转换为无符号字符,然后再将它们传递给 printf:

printf("%02x\n", *(unsigned char*)(buf+i));

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:

printf("%02x\n", *(unsigned char*)(buf+i));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文