From b055856776807f0a459a86b1e1f62902d2d3a9c3 Mon Sep 17 00:00:00 2001 From: mickeyl Date: Wed, 09 Apr 2003 21:37:05 +0000 Subject: implements a statistic interface for OPacketCapturer --- diff --git a/libopie2/opienet/opcap.cpp b/libopie2/opienet/opcap.cpp index 5c464cf..6a3dc26 100644 --- a/libopie2/opienet/opcap.cpp +++ b/libopie2/opienet/opcap.cpp @@ -39,6 +39,7 @@ #include // don't use oapplication here (will decrease reusability in other projects) #include +#include /*====================================================================================== * OPacket @@ -87,6 +88,19 @@ int OPacket::caplen() const } +void OPacket::updateStats( QMap& stats, QObjectList* l ) +{ + if (!l) return; + QObject* o = l->first(); + while ( o ) + { + stats[o->name()]++; + updateStats( stats, const_cast( o->children() ) ); + o = l->next(); + } +} + + QString OPacket::dump( int bpl ) const { static int index = 0; @@ -127,12 +141,12 @@ QString OPacket::dump( int bpl ) const } - int OPacket::len() const { return _hdr.len; } + /*====================================================================================== * OEthernetPacket *======================================================================================*/ @@ -772,6 +786,13 @@ void OPacketCapturer::close() pcap_close( _pch ); _open = false; } + + qDebug( "OPacketCapturer::close() --- dumping capturing statistics..." ); + qDebug( "--------------------------------------------------" ); + for( QMap::Iterator it = _stats.begin(); it != _stats.end(); ++it ) + qDebug( "%s : %d", (const char*) it.key(), it.data() ); + qDebug( "--------------------------------------------------" ); + } @@ -793,7 +814,6 @@ int OPacketCapturer::fileno() const } } - OPacket* OPacketCapturer::next() { packetheaderstruct header; @@ -803,11 +823,16 @@ OPacket* OPacketCapturer::next() if ( header.len ) { - return new OPacket( dataLink(), header, pdata, 0 ); + OPacket* p = new OPacket( dataLink(), header, pdata, 0 ); // packets shouldn't be inserted in the QObject child-parent hierarchy, // because due to memory constraints they will be deleted as soon // as possible - that is right after they have been processed // by emit() [ see below ] + + //TODO: make gathering statistics optional, because it takes time + p->updateStats( _stats, const_cast( p->children() ) ); + + return p; } else { @@ -839,6 +864,7 @@ bool OPacketCapturer::open( const QString& name ) qDebug( "OPacketCapturer::open(): libpcap opened successfully." ); _pch = handle; _open = true; + _stats.clear(); // in case we have an application object, create a socket notifier if ( qApp ) @@ -866,10 +892,16 @@ bool OPacketCapturer::isOpen() const void OPacketCapturer::readyToReceive() { - qDebug( "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(...)'" ); + qDebug( "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" ); OPacket* p = next(); emit receivedPacket( p ); // emit is synchronous - packet has been dealt with, now it's safe to delete delete p; } + +const QMap& OPacketCapturer::statistics() const +{ + return _stats; +} + diff --git a/libopie2/opienet/opcap.h b/libopie2/opienet/opcap.h index ddef278..c9b0624 100644 --- a/libopie2/opienet/opcap.h +++ b/libopie2/opienet/opcap.h @@ -46,9 +46,11 @@ extern "C" // work around a bpf/pcap conflict in recent headers #include /* QT */ +#include #include #include #include +#include /* OPIE */ #include @@ -80,6 +82,8 @@ class OPacket : public QObject int len() const; QString dump( int = 32 ) const; + void updateStats( QMap&, QObjectList* ); + private: const packetheaderstruct _hdr; // pcap packet header const unsigned char* _data; // pcap packet data @@ -434,6 +438,8 @@ class OPacketCapturer : public QObject bool open( const QString& name ); bool isOpen() const; + const QMap& statistics() const; + signals: void receivedPacket( OPacket* ); @@ -446,6 +452,7 @@ class OPacketCapturer : public QObject pcap_t* _pch; // pcap library handle QSocketNotifier* _sn; // socket notifier for main loop mutable char _errbuf[PCAP_ERRBUF_SIZE]; + QMap _stats; // statistics; }; #endif // OPCAP_H -- cgit v0.9.0.2