Я пишу сниффер-класс с c ++ как следующий код.
#include "Capture.h"
#include <sys/socket.h>
#include <cstring>
#include <linux/if_ether.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <pthread.h>
Capture::Capture(int sniff_socket): sniff_socket_(sniff_socket), sniff_threadID_(0)
{
}
bool Capture::endCapture()
{
if(!sniff_threadID_)
return false;
cout << "Asking capture thread to stop\n" <<endl;
return !pthread_cancel(sniff_threadID_);
}
bool Capture::startCapture()
{
if(pthread_create(&sniff_threadID_, NULL, Capture::sniffer_thread, NULL)) {
cerr << "Unable to create capture thread"<<endl;
return false;
}
return true;
}
void *Capture::sniffer_thread(void *)
{
int MTU = 2048;
char buffer[MTU];
ssize_t msg_len;
struct iphdr *ip_header;
struct ethhdr *eth_header;
struct tcphdr *tcp_header;
void *packet;
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
while (1) {
msg_len = recvfrom(sniff_socket_, buffer, MTU, 0, NULL, NULL);
eth_header = (struct ethhdr*)buffer;
ip_header = (iphdr*)(buffer + sizeof(ethhdr));
tcp_header = (struct tcphdr*)(buffer + sizeof(struct ethhdr) + sizeof(struct iphdr));
if(msg_len != -1){
packet = malloc(msg_len);
memcpy(packet, buffer, msg_len);
pthread_testcancel();
//queue add should be here
} else {
cerr<<"Error capture thread recvfrom"<<endl;
pthread_exit(NULL);
}
}
return NULL;
}
Capture::~Capture()
{
// TODO Auto-generated destructor stub
}
Однако, когда я скомпилировал код, g ++ сказал, что
../Capture.cpp: In member function ‘bool Capture::startCapture()’:
../Capture.cpp:30: error: argument of type ‘void* (Capture::)(void*)’ does not match ‘void* (*)(void*)’
Может ли кто-нибудь помочь мне решить эту проблему?