【问题标题】:How does nfq_get_payload structure its return data?nfq_get_payload 如何构造它的返回数据?
【发布时间】:2013-04-03 08:44:38
【问题描述】:

首先,我试图从 Netfilter 队列有效负载的有效负载中获取源地址和目标端口(使用 nfq_get_payload 函数检索有效负载)。以下问题问同样的问题并得到正确答案:

How to extract source and destination port number from packet in queue of iptables

不幸的是,没有解释为什么将 20 和 22 添加到地址会使您在正确的位置阅读信息。我认为这是因为数据的结构(显然),但如果有一个定义的结构,它是什么?

文档没有明确解释数据的格式,只是“此函数检索的数据类型将取决于使用 nfq_set_mode() 函数设置的模式”,但是 set_mode 的文档没有提及有关数据类型的任何信息,并且来源不会立即显示任何内容。

我觉得这一定是我遗漏或不理解的常见网络编程结构的核心内容。

备注:nfq_get_payload 函数:http://www.netfilter.org/projects/libnetfilter_queue/doxygen/group__Parsing.html#gaf79628558c94630e25dbfcbde09f2933

【问题讨论】:

    标签: c++ linux network-programming iptables netfilter


    【解决方案1】:

    我设法解决了这个问题,我将把它留在这里让其他人找到。

    有效负载以 iphdr 结构开始。 iphdr struct 有一个协议字段,例如 tcp,如果是 tcp,那么 iphdr struct 之后的数据就是 tcphdr struct,如果是 udp,那么还有另一个 struct hdr 用于那个,以此类推 icmp 等等。

    要访问端口,假设 q_data 是指向 nfq_data 结构的指针:

    unsigned char *data;
    nfq_get_payload(q_data, (unsigned char**)&data);
    struct iphdr * ip_info = (struct iphdr *)data;
    if(ip_info->protocol == IPPROTO_TCP) {
        struct tcphdr * tcp_info = (struct tcphdr*)(data + sizeof(*ip_info));
        unsigned short dest_port = ntohs(tcp_info->dest);
    } else if(ip_info->protocol == IPPROTO_UDP) {
        //etc etc
    }
    

    【讨论】:

    • 不应该是:struct iphdr * ip_info = (struct iphdr *) (data + sizeof(struct ethhdr)); 吗?你为什么不跳过struct ethhdr 以进入`struct iphdr'?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 2011-06-17
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多