libpcap中的数据包向量 [英] vector of packets in libpcap
本文介绍了libpcap中的数据包向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在c/c ++中使用libpcap,在向量中插入指针时遇到问题. 这是我的代码:
I'm working with libpcap in c / c + + and I have a problem when inserting pointers in a vector. This is my code:
typedef vector <u_char *>vPack;
...
vPack vect;
...
if (pcap_dispatch (p, 0, &mycallback, (u_char *) &vect) < 0){
cout << "Error" << endl;
pcap_perror (p, prestr);
}
....
void mycallback (u_char * args, const struct pcap_pkthdr *pkthdr, const u_char * packet){
u_char *pk;
pk = (u_char *)packet;
vPack *vec = (vPack *) args;
vec[0].push_back(pk);
}
问题在于元素插入到相同的存储位置,并且向量始终包含相同的元素. 有什么建议吗?
The problem is that the elements are inserted in the same memory location, and the vector always contains the same element. Any suggestions?
PD:对不起,我的英语.
PD: sorry for my english.
推荐答案
扩展了Karrek的尝试!
extending Karrek's attempt!
struct packet_handler
{
typedef std::vector<u_char> PacketType;
typedef std::vector <PacketType> PacketBuffer;
static void handler(u_char *user, const pcap_pkthdr *h, const u_char *bytes)
{
packet_handler* ph = reinterpret_cast<packet_handler*>(user);
ph->handle(PacketType(bytes, bytes + h->caplen));
}
void handle(PacketType const& packet)
{
_packetBuffer.push_back(packet);
}
PacketBuffer _packetBuffer;
};
int main()
{
packet_handler ph;
// pass in the handler class as user data
int result = pcap_dispatch(p, 0, packet_handler::handler, reinterpret_cast<uchar*>(&ph));
}
这篇关于libpcap中的数据包向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文