pcap_loop中的回调方法

callback method in pcap_loop

本文关键字:回调 方法 loop pcap      更新时间:2023-10-16

我正试图将Linux中libpcab库中的pcap_loop函数与以下原型一起使用:

int pcap_loop(pcap_t *, int, pcap_handler, u_char *);

pcap_pkthdr是一个函数指针:

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

在我的程序中,我在类SniffEthernet中定义了以下方法:

void SniffEthernet::got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);

现在调用pcap_loop如下

pcap_loop(handle, num_packets, this->got_packet, NULL);

给我以下编译时错误:

SniffEthernet.cc:139:58: error: cannot convert ‘VENTOS::SniffEthernet::got_packet’ from type ‘void (VENTOS::SniffEthernet::)(u_char*, const pcap_pkthdr*, const u_char*) {aka void (VENTOS::SniffEthernet::)(unsigned char*, const pcap_pkthdr*, const unsigned char*)}’ to type ‘pcap_handler {aka void (*)(unsigned char*, const pcap_pkthdr*, const unsigned char*)}’

我在这里做错了什么?

编辑:我在这里找到了一个类似的帖子。

回调函数不能是成员函数(方法)。不要忘记,成员函数总是有隐藏的参数this

回调函数必须是命名空间级别的函数或类的静态成员。

如果您想让对象可用于CB函数,您可以使用user成员(pcap_loop()的最后一个参数,回调函数的第一个成员)和适当的类型强制转换来传递任意数据,在您的情况下,该数据应该是用于捕获的对象。

下面的代码是不完整未经测试,但可能会给您一个想法。

class SniffEther {
    private:
        pcap_t *cap_handler;
        char errbuf[PCAP_ERRBUF_SIZE];
        /* capture-related data members (properties) */
    public:
        static friend void pkt_callback(u_char *user, const pcap_pkthdr *hdr, const u_char *bytes){
            SniffEther *sniffer=reinterpret_cast<SniffEther *>(user);
            /*
                Process header and bytes.
                You can call things like sniffer->somemethod(), and also
                access sniffer->someproperty.
            */
        }
        // constructor
        SniffEther(const char *if_name){
            cap_handler=pcap_create(if_name, errbuf);
            if(!cap_handler)
                throw runtime_error(errbuf);
            /* Set the many pcap_options (see pcap(3)). */
            if(pcap_activate(cap_handler)!=0){
                string error(pcap_geterr(cap_handler));
                pcap_close(cap_handler);
                throw runtime_error(error);
            }
        }
        ~SniffEther(){
            if(cap_handler)
                pcap_close(cap_handler);
        }
        void capture_loop(int pkt_count=-1){
            if(
                pcap_loop(
                    cap_handler, pkt_count, pkt_callback,
                    reinterpret_cast<u_char *>(this)
                )==-1
            )
                throw runtime_error(pcap_geterr(cap_handler));
        }
};