Examples/HttpAnalyzer/main.cpp
/**
 * activate HTTP analysis from live traffic
 */
activate HTTP analysis from live traffic
activate HTTP analysis from live traffic
void analyzeHttpFromLiveTraffic(pcpp::PcapLiveDevice* dev, bool printRatesPeriodically, int printRatePeriod, std::string savePacketsToFileName, uint16_t dstPort)
{
	if (!dev->open())
		EXIT_WITH_ERROR("Could not open the device");
	pcpp::PortFilter httpPortFilter(dstPort, pcpp::SRC_OR_DST);
	if (!dev->setFilter(httpPortFilter))
		EXIT_WITH_ERROR("Could not set up filter on device");
	pcpp::PcapFileWriterDevice* pcapWriter = NULL;
	if (savePacketsToFileName != "")
	{
		pcapWriter = new pcpp::PcapFileWriterDevice(savePacketsToFileName);
		if (!pcapWriter->open())
		{
			EXIT_WITH_ERROR("Could not open pcap file for writing");
		}
	}
	HttpPacketArrivedData data;
	HttpStatsCollector collector(dstPort);
	data.statsCollector = &collector;
	data.pcapWriter = pcapWriter;
	dev->startCapture(httpPacketArrive, &data);
	bool shouldStop = false;
	pcpp::ApplicationEventHandler::getInstance().onApplicationInterrupted(onApplicationInterrupted, &shouldStop);
	while(!shouldStop)
	{
		pcpp::multiPlatformSleep(printRatePeriod);
		if (printRatesPeriodically)
		{
			collector.calcRates();
			printCurrentRates(collector);
		}
	}
	dev->stopCapture();
	dev->close();
	collector.calcRates();
		std::cout << std::endl << std::endl
		<< "STATS SUMMARY" << std::endl
		<< "=============" << std::endl;
	printStatsSummary(collector);
	if (pcapWriter != NULL)
	{
		pcapWriter->close();
		delete pcapWriter;
	}
}
void analyzeHttpFromLiveTraffic ( pcpp :: PcapLiveDevice * dev , bool printRatesPeriodically , int printRatePeriod , std :: string savePacketsToFileName , uint16_t dstPort ) { if ( ! dev -> open ( ) ) EXIT_WITH_ERROR ( " " ) ; pcpp :: PortFilter httpPortFilter ( dstPort , pcpp :: SRC_OR_DST ) ; if ( ! dev -> setFilter ( httpPortFilter ) ) EXIT_WITH_ERROR ( " " ) ; pcpp :: PcapFileWriterDevice * pcapWriter = NULL ; if ( savePacketsToFileName != " " ) { pcapWriter = new pcpp :: PcapFileWriterDevice ( savePacketsToFileName ) ; if ( ! pcapWriter -> open ( ) ) { EXIT_WITH_ERROR ( " " ) ; } } HttpPacketArrivedData data ; HttpStatsCollector collector ( dstPort ) ; data . statsCollector = & collector ; data . pcapWriter = pcapWriter ; dev -> startCapture ( httpPacketArrive , & data ) ; bool shouldStop = false ; pcpp :: ApplicationEventHandler :: getInstance ( ) . onApplicationInterrupted ( onApplicationInterrupted , & shouldStop ) ; while ( ! shouldStop ) { pcpp :: multiPlatformSleep ( printRatePeriod ) ; if ( printRatesPeriodically ) { collector . calcRates ( ) ; printCurrentRates ( collector ) ; } } dev -> stopCapture ( ) ; dev -> close ( ) ; collector . calcRates ( ) ; std :: cout << std :: endl << std :: endl << " " << std :: endl << " " << std :: endl ; printStatsSummary ( collector ) ; if ( pcapWriter != NULL ) { pcapWriter -> close ( ) ; delete pcapWriter ; } }
activate HTTP analysis from live traffic
activate HTTP analysis from live traffic
// open the device
 // if needed to save the captured packets to file - open a writer device
 // start capturing packets and collecting stats
 // register the on app close event to print summary stats on app termination
 // calculate rates
 // stop capturing and close the live device
 // calculate final rates
 // print stats summary
 // close and free the writer device
