11 PcapBackend::PcapBackend(
12 string dev, array<uint8_t, 6> srcMac
15 : dev(dev), srcMac(srcMac) {
18 cout <<
"WARNING: Running pcap wihout root priviledges may be a bad idea" << endl;
21 handle = pcap_open_live(dev.c_str(), bufSize, 1, 1, errbuf);
23 cout <<
"PcapBackend: pcap_open_live() failed" << endl;
27 if (pcap_setnonblock(handle, 1, errbuf) < 0) {
28 cout <<
"PcapBackend: pcap_setnonblock() failed" << endl;
32 D(cout <<
"PcapBackend: device opened" << endl;)
35 PcapBackend::~PcapBackend() {
37 for (
auto i : packetPool) {
47 for (uint32_t i = 0; i < sendCount; i++) {
51 struct ether_header *eth =
reinterpret_cast<ether_header *
>(p->
getData());
52 memset(eth->ether_dhost, 0xff, 6);
53 memcpy(eth->ether_shost, srcMac.data(), 6);
54 eth->ether_type = htons(ETHERTYPE_IP);
58 cout <<
"PcapBackend::sendBatch() pcap_inject() failed" << endl;
63 packetPool.push_back(p);
74 for (uint32_t i = 0; i < freeCount; i++) {
75 packetPool.push_back(spArray[i]);
86 struct pcap_pkthdr header;
87 const u_char *pcapPacket;
89 unsigned int count = 0;
91 while ((pcapPacket = pcap_next(handle, &header)) && count < 64) {
93 if (header.len > bufSize) {
97 memcpy(pkt->
getData(), pcapPacket, header.len);
100 D(cout <<
"PcapBackend::recvBatch() got another packet" << endl;)
110 if (packetPool.empty()) {
111 void *data = malloc(bufSize);
115 packetPool.pop_back();
void setDataLen(uint16_t l)
uint32_t getFreeCount() const
Get the number of packets currently marked as free.
Example for a packet class.
uint32_t getSendCount() const
Get the number of packets currently marked as send.
void getSendBufs(Packet **sendBufs) const
Get all the packets which are to be sent.
void getFreeBufs(Packet **freeBufs) const
Get all the packets which are to be freed.
Wrapper around MoonGen bufarrays.