I'm using Visual Studio 2017 and PcapPlusPlus.I want my app to print a certain header(for example user-agent) of http packets and then drop packages that have a certain value(but I didn't reach the dropping part yet).When I compile my code it works fine but when I make a request to some site it prints for example "user-agent whatever" and then throws an exception at me: read access violation. **this* was 0x58. I'm a total newbie to c++ and visual studio so I have no idea what does that exception mean.How do I fix this?Here's my code:
#include "stdlib.h" #include "PcapLiveDeviceList.h" #include "PlatformSpecificUtils.h" #include "Packet.h" #include "TcpLayer.h" #include "HttpLayer.h" struct PacketStats { void consumePacket(pcpp::Packet& packet) { pcpp::HttpRequestLayer* httpRequestLayer = packet.getLayerOfType<pcpp::HttpRequestLayer>(); if (packet.isPacketOfType(pcpp::HTTP)) printf("HTTP,%s\n",httpRequestLayer->getFieldByName(PCPP_HTTP_USER_AGENT_FIELD)->getFieldValue().c_str()); } }; static void onPacketArrives(pcpp::RawPacket* packet, pcpp::PcapLiveDevice* dev, void* cookie) { PacketStats* stats = (PacketStats*)cookie; pcpp::Packet parsedPacket(packet); stats->consumePacket(parsedPacket); } int main(int argc, char* argv[]) { std::string interfaceIPAddr = "192.168.0.249"; pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(interfaceIPAddr.c_str()); if (dev == NULL) { printf("Cannot find interface with IPv4 address of '%s'\n", interfaceIPAddr.c_str()); exit(1); } if (!dev->open()) { printf("Cannot open device\n"); exit(1); } PacketStats stats; pcpp::PortFilter portFilter(80, pcpp::SRC_OR_DST); pcpp::ProtoFilter protocolFilter(pcpp::TCP); pcpp::AndFilter andFilter; andFilter.addFilter(&portFilter); andFilter.addFilter(&protocolFilter); dev->setFilter(andFilter); printf("\nStarting packet capture with a filter in place...\n"); dev->startCapture(onPacketArrives, &stats); PCAP_SLEEP(10); dev->stopCapture(); dev->close(); }