1

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(); } 
2
  • 1
    What does the debugger tell you? Commented Jul 7, 2018 at 19:01
  • I'd like to recommend looking at ClangFormat ... Commented Jul 7, 2018 at 19:01

1 Answer 1

1

I haven't worked with C++ in this capacity, but I have a few ideas. First, you're getting a "read access violation". I'd be thinking reading invalid memory. Second, it's telling you that the 'this' pointer is 0x58. That's a very low number, close to 0x0. It's likely that you are getting back a null pointer, and using it somewhere else.

Could it be that httpRequestLayer, or something else within your call chain in consumePacket is coming back null?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.