-rw-r--r-- | libopie2/opienet/opcap.cpp | 40 | ||||
-rw-r--r-- | libopie2/opienet/opcap.h | 7 |
2 files changed, 43 insertions, 4 deletions
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 @@ -38,8 +38,9 @@ /* QT */ #include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects) #include <qsocketnotifier.h> +#include <qobjectlist.h> /*====================================================================================== * OPacket *======================================================================================*/ @@ -86,8 +87,21 @@ int OPacket::caplen() const return _hdr.caplen; } +void OPacket::updateStats( QMap<QString,int>& stats, QObjectList* l ) +{ + if (!l) return; + QObject* o = l->first(); + while ( o ) + { + stats[o->name()]++; + updateStats( stats, const_cast<QObjectList*>( o->children() ) ); + o = l->next(); + } +} + + QString OPacket::dump( int bpl ) const { static int index = 0; index++; @@ -126,14 +140,14 @@ QString OPacket::dump( int bpl ) const return str; } - int OPacket::len() const { return _hdr.len; } + /*====================================================================================== * OEthernetPacket *======================================================================================*/ @@ -771,8 +785,15 @@ void OPacketCapturer::close() } pcap_close( _pch ); _open = false; } + + qDebug( "OPacketCapturer::close() --- dumping capturing statistics..." ); + qDebug( "--------------------------------------------------" ); + for( QMap<QString,int>::Iterator it = _stats.begin(); it != _stats.end(); ++it ) + qDebug( "%s : %d", (const char*) it.key(), it.data() ); + qDebug( "--------------------------------------------------" ); + } int OPacketCapturer::dataLink() const @@ -792,9 +813,8 @@ int OPacketCapturer::fileno() const return -1; } } - OPacket* OPacketCapturer::next() { packetheaderstruct header; qDebug( "==> OPacketCapturer::next()" ); @@ -802,13 +822,18 @@ OPacket* OPacketCapturer::next() qDebug( "<== 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<QObjectList*>( p->children() ) ); + + return p; } else { return 0; @@ -838,8 +863,9 @@ 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 ) { @@ -865,11 +891,17 @@ 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<QString,int>& 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 @@ -45,11 +45,13 @@ extern "C" // work around a bpf/pcap conflict in recent headers #include <netinet/tcp.h> #include <time.h> /* QT */ +#include <qevent.h> #include <qhostaddress.h> #include <qobject.h> #include <qstring.h> +#include <qmap.h> /* OPIE */ #include <opie2/onetutils.h> #include "802_11_user.h" @@ -79,8 +81,10 @@ class OPacket : public QObject int caplen() const; int len() const; QString dump( int = 32 ) const; + void updateStats( QMap<QString,int>&, QObjectList* ); + private: const packetheaderstruct _hdr; // pcap packet header const unsigned char* _data; // pcap packet data const unsigned char* _end; // end of pcap packet data @@ -433,8 +437,10 @@ class OPacketCapturer : public QObject OPacket* next(); bool open( const QString& name ); bool isOpen() const; + const QMap<QString,int>& statistics() const; + signals: void receivedPacket( OPacket* ); protected slots: @@ -445,8 +451,9 @@ class OPacketCapturer : public QObject bool _open; // check this before doing pcap calls pcap_t* _pch; // pcap library handle QSocketNotifier* _sn; // socket notifier for main loop mutable char _errbuf[PCAP_ERRBUF_SIZE]; + QMap<QString, int> _stats; // statistics; }; #endif // OPCAP_H |