-rw-r--r-- | libopie2/opienet/opcap.cpp | 21 | ||||
-rw-r--r-- | libopie2/opienet/opcap.h | 20 |
2 files changed, 33 insertions, 8 deletions
diff --git a/libopie2/opienet/opcap.cpp b/libopie2/opienet/opcap.cpp index 4081d4f..c5df041 100644 --- a/libopie2/opienet/opcap.cpp +++ b/libopie2/opienet/opcap.cpp @@ -54,13 +54,13 @@ namespace Net { * OPacket *======================================================================================*/ OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent ) :QObject( parent, "Generic" ), _hdr( header ), _data( data ) { - //qDebug( "OPacket::OPacket(): (Len %d, CapLen %d)" /*, ctime((const time_t*) header.ts.tv_sec)*/, header.len, header.caplen ); + qDebug( "OPacket::OPacket(): (Len %d, CapLen %d)" /*, ctime((const time_t*) header.ts.tv_sec)*/, header.len, header.caplen ); _end = (unsigned char*) data + header.len; //qDebug( "OPacket::data @ %0x, end @ %0x", data, _end ); switch ( datalink ) { @@ -84,12 +84,13 @@ OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* } } OPacket::~OPacket() { + qDebug( "OPacket::~OPacket( %s )", name() ); } timevalstruct OPacket::timeval() const { return _hdr.ts; @@ -1060,13 +1061,13 @@ QString OWaveLanControlPacket::controlType() const /*====================================================================================== * OPacketCapturer *======================================================================================*/ OPacketCapturer::OPacketCapturer( QObject* parent, const char* name ) - :QObject( parent, name ), _name( QString::null ), _open( false ), _pch( 0 ), _pcd( 0 ), _sn( 0 ) + :QObject( parent, name ), _name( QString::null ), _open( false ), _pch( 0 ), _pcd( 0 ), _sn( 0 ), _autodelete( true ) { } OPacketCapturer::~OPacketCapturer() { @@ -1075,12 +1076,24 @@ OPacketCapturer::~OPacketCapturer() odebug << "OPacketCapturer::~OPacketCapturer(): pcap still open, autoclosing." << oendl; close(); } } +void OPacketCapturer::setAutoDelete( bool b ) +{ + _autodelete = b; +} + + +bool OPacketCapturer::autoDelete() const +{ + return _autodelete; +} + + void OPacketCapturer::setBlocking( bool b ) { if ( pcap_setnonblock( _pch, 1-b, _errbuf ) != -1 ) { odebug << "OPacketCapturer::setBlocking(): blocking mode changed successfully." << oendl; } @@ -1318,14 +1331,14 @@ bool OPacketCapturer::isOpen() const void OPacketCapturer::readyToReceive() { odebug << "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" << oendl; OPacket* p = next(); emit receivedPacket( p ); - // emit is synchronous - packet has been dealt with, now it's safe to delete - delete p; + // emit is synchronous - packet has been dealt with, now it's safe to delete (if enabled) + if ( _autodelete ) delete p; } const QMap<QString,int>& OPacketCapturer::statistics() const { return _stats; diff --git a/libopie2/opienet/opcap.h b/libopie2/opienet/opcap.h index 72a78de..4ff8495 100644 --- a/libopie2/opienet/opcap.h +++ b/libopie2/opienet/opcap.h @@ -131,15 +131,14 @@ class OPacket : public QObject int caplen() const; int len() const; QString dump( int = 32 ) const; void updateStats( QMap<QString,int>&, QObjectList* ); - private: - QString dumpStructure() const; + private: QString _dumpStructure( QObjectList* ) const; private: const packetheaderstruct _hdr; // pcap packet header const unsigned char* _data; // pcap packet data const unsigned char* _end; // end of pcap packet data @@ -690,12 +689,24 @@ class OPacketCapturer : public QObject QString version() const; /** * @returns the packet statistic database. * @see QMap */ const QMap<QString,int>& statistics() const; + /** + * Enable or disable the auto-delete option. + * If auto-delete is enabled, then the packet capturer will delete a packet right + * after it has been emit'ted. This is the default, which is useful if the packet + * capturer has the only reference to the packets. If you pass the packet for adding + * into a collection or do processing after the SLOT, the auto delete must be disabled. + */ + void setAutoDelete( bool enable ); + /** + * @returns the auto-delete value. + */ + bool autoDelete() const; signals: /** * This signal is emitted, when a packet has been received. */ void receivedPacket( Opie::Net::OPacket* ); @@ -708,14 +719,15 @@ class OPacketCapturer : public QObject bool _open; // check this before doing pcap calls pcap_t* _pch; // pcap library handle pcap_dumper_t* _pcd; // pcap dumper handle QSocketNotifier* _sn; // socket notifier for main loop mutable char _errbuf[PCAP_ERRBUF_SIZE]; // holds error strings from libpcap QMap<QString, int> _stats; // statistics; - class Private; // Private Forward declaration - Private *d; // if we need to add data + bool _autodelete; // if we auto delete packets after emit + class Private; // Private Forward declaration + Private *d; // if we need to add data }; } } #endif // OPCAP_H |