summaryrefslogtreecommitdiff
authormickeyl <mickeyl>2004-03-28 16:52:43 (UTC)
committer mickeyl <mickeyl>2004-03-28 16:52:43 (UTC)
commite2368b92a101437f7ed289d5ae62f7e7e2466344 (patch) (unidiff)
tree1e7775ee681d970c9cd785b9afb6946143c212e1
parent6c102bd456d760fbe237fd49ce46f212614f228a (diff)
downloadopie-e2368b92a101437f7ed289d5ae62f7e7e2466344.zip
opie-e2368b92a101437f7ed289d5ae62f7e7e2466344.tar.gz
opie-e2368b92a101437f7ed289d5ae62f7e7e2466344.tar.bz2
OPacketCapturer API addition: setAutoDelete() and autoDelete()
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opienet/opcap.cpp21
-rw-r--r--libopie2/opienet/opcap.h20
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
@@ -52,17 +52,17 @@ namespace Net {
52 52
53/*====================================================================================== 53/*======================================================================================
54 * OPacket 54 * OPacket
55 *======================================================================================*/ 55 *======================================================================================*/
56 56
57OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent ) 57OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent )
58 :QObject( parent, "Generic" ), _hdr( header ), _data( data ) 58 :QObject( parent, "Generic" ), _hdr( header ), _data( data )
59{ 59{
60 //qDebug( "OPacket::OPacket(): (Len %d, CapLen %d)" /*, ctime((const time_t*) header.ts.tv_sec)*/, header.len, header.caplen ); 60 qDebug( "OPacket::OPacket(): (Len %d, CapLen %d)" /*, ctime((const time_t*) header.ts.tv_sec)*/, header.len, header.caplen );
61 61
62 _end = (unsigned char*) data + header.len; 62 _end = (unsigned char*) data + header.len;
63 //qDebug( "OPacket::data @ %0x, end @ %0x", data, _end ); 63 //qDebug( "OPacket::data @ %0x, end @ %0x", data, _end );
64 64
65 switch ( datalink ) 65 switch ( datalink )
66 { 66 {
67 case DLT_EN10MB: 67 case DLT_EN10MB:
68 odebug << "OPacket::OPacket(): Received Packet. Datalink = ETHERNET" << oendl; 68 odebug << "OPacket::OPacket(): Received Packet. Datalink = ETHERNET" << oendl;
@@ -82,16 +82,17 @@ OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char*
82 default: 82 default:
83 owarn << "OPacket::OPacket(): Received Packet over unsupported datalink, type " << datalink << "!" << oendl; 83 owarn << "OPacket::OPacket(): Received Packet over unsupported datalink, type " << datalink << "!" << oendl;
84 } 84 }
85} 85}
86 86
87 87
88OPacket::~OPacket() 88OPacket::~OPacket()
89{ 89{
90 qDebug( "OPacket::~OPacket( %s )", name() );
90} 91}
91 92
92 93
93timevalstruct OPacket::timeval() const 94timevalstruct OPacket::timeval() const
94{ 95{
95 return _hdr.ts; 96 return _hdr.ts;
96} 97}
97 98
@@ -1058,31 +1059,43 @@ QString OWaveLanControlPacket::controlType() const
1058} 1059}
1059 1060
1060 1061
1061/*====================================================================================== 1062/*======================================================================================
1062 * OPacketCapturer 1063 * OPacketCapturer
1063 *======================================================================================*/ 1064 *======================================================================================*/
1064 1065
1065OPacketCapturer::OPacketCapturer( QObject* parent, const char* name ) 1066OPacketCapturer::OPacketCapturer( QObject* parent, const char* name )
1066 :QObject( parent, name ), _name( QString::null ), _open( false ), _pch( 0 ), _pcd( 0 ), _sn( 0 ) 1067 :QObject( parent, name ), _name( QString::null ), _open( false ), _pch( 0 ), _pcd( 0 ), _sn( 0 ), _autodelete( true )
1067{ 1068{
1068} 1069}
1069 1070
1070 1071
1071OPacketCapturer::~OPacketCapturer() 1072OPacketCapturer::~OPacketCapturer()
1072{ 1073{
1073 if ( _open ) 1074 if ( _open )
1074 { 1075 {
1075 odebug << "OPacketCapturer::~OPacketCapturer(): pcap still open, autoclosing." << oendl; 1076 odebug << "OPacketCapturer::~OPacketCapturer(): pcap still open, autoclosing." << oendl;
1076 close(); 1077 close();
1077 } 1078 }
1078} 1079}
1079 1080
1080 1081
1082void OPacketCapturer::setAutoDelete( bool b )
1083{
1084 _autodelete = b;
1085}
1086
1087
1088bool OPacketCapturer::autoDelete() const
1089{
1090 return _autodelete;
1091}
1092
1093
1081void OPacketCapturer::setBlocking( bool b ) 1094void OPacketCapturer::setBlocking( bool b )
1082{ 1095{
1083 if ( pcap_setnonblock( _pch, 1-b, _errbuf ) != -1 ) 1096 if ( pcap_setnonblock( _pch, 1-b, _errbuf ) != -1 )
1084 { 1097 {
1085 odebug << "OPacketCapturer::setBlocking(): blocking mode changed successfully." << oendl; 1098 odebug << "OPacketCapturer::setBlocking(): blocking mode changed successfully." << oendl;
1086 } 1099 }
1087 else 1100 else
1088 { 1101 {
@@ -1316,18 +1329,18 @@ bool OPacketCapturer::isOpen() const
1316} 1329}
1317 1330
1318 1331
1319void OPacketCapturer::readyToReceive() 1332void OPacketCapturer::readyToReceive()
1320{ 1333{
1321 odebug << "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" << oendl; 1334 odebug << "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" << oendl;
1322 OPacket* p = next(); 1335 OPacket* p = next();
1323 emit receivedPacket( p ); 1336 emit receivedPacket( p );
1324 // emit is synchronous - packet has been dealt with, now it's safe to delete 1337 // emit is synchronous - packet has been dealt with, now it's safe to delete (if enabled)
1325 delete p; 1338 if ( _autodelete ) delete p;
1326} 1339}
1327 1340
1328 1341
1329const QMap<QString,int>& OPacketCapturer::statistics() const 1342const QMap<QString,int>& OPacketCapturer::statistics() const
1330{ 1343{
1331 return _stats; 1344 return _stats;
1332} 1345}
1333 1346
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
@@ -129,19 +129,18 @@ class OPacket : public QObject
129 timevalstruct timeval() const; 129 timevalstruct timeval() const;
130 130
131 int caplen() const; 131 int caplen() const;
132 int len() const; 132 int len() const;
133 QString dump( int = 32 ) const; 133 QString dump( int = 32 ) const;
134 134
135 void updateStats( QMap<QString,int>&, QObjectList* ); 135 void updateStats( QMap<QString,int>&, QObjectList* );
136 136
137 private:
138
139 QString dumpStructure() const; 137 QString dumpStructure() const;
138 private:
140 QString _dumpStructure( QObjectList* ) const; 139 QString _dumpStructure( QObjectList* ) const;
141 140
142 private: 141 private:
143 const packetheaderstruct _hdr; // pcap packet header 142 const packetheaderstruct _hdr; // pcap packet header
144 const unsigned char* _data; // pcap packet data 143 const unsigned char* _data; // pcap packet data
145 const unsigned char* _end; // end of pcap packet data 144 const unsigned char* _end; // end of pcap packet data
146 private: 145 private:
147 class Private; 146 class Private;
@@ -688,16 +687,28 @@ class OPacketCapturer : public QObject
688 * @returns the libpcap version string used to write the input capture file. 687 * @returns the libpcap version string used to write the input capture file.
689 */ 688 */
690 QString version() const; 689 QString version() const;
691 /** 690 /**
692 * @returns the packet statistic database. 691 * @returns the packet statistic database.
693 * @see QMap 692 * @see QMap
694 */ 693 */
695 const QMap<QString,int>& statistics() const; 694 const QMap<QString,int>& statistics() const;
695 /**
696 * Enable or disable the auto-delete option.
697 * If auto-delete is enabled, then the packet capturer will delete a packet right
698 * after it has been emit'ted. This is the default, which is useful if the packet
699 * capturer has the only reference to the packets. If you pass the packet for adding
700 * into a collection or do processing after the SLOT, the auto delete must be disabled.
701 */
702 void setAutoDelete( bool enable );
703 /**
704 * @returns the auto-delete value.
705 */
706 bool autoDelete() const;
696 707
697 signals: 708 signals:
698 /** 709 /**
699 * This signal is emitted, when a packet has been received. 710 * This signal is emitted, when a packet has been received.
700 */ 711 */
701 void receivedPacket( Opie::Net::OPacket* ); 712 void receivedPacket( Opie::Net::OPacket* );
702 713
703 protected slots: 714 protected slots:
@@ -706,16 +717,17 @@ class OPacketCapturer : public QObject
706 protected: 717 protected:
707 QString _name; // devicename 718 QString _name; // devicename
708 bool _open; // check this before doing pcap calls 719 bool _open; // check this before doing pcap calls
709 pcap_t* _pch; // pcap library handle 720 pcap_t* _pch; // pcap library handle
710 pcap_dumper_t* _pcd; // pcap dumper handle 721 pcap_dumper_t* _pcd; // pcap dumper handle
711 QSocketNotifier* _sn; // socket notifier for main loop 722 QSocketNotifier* _sn; // socket notifier for main loop
712 mutable char _errbuf[PCAP_ERRBUF_SIZE]; // holds error strings from libpcap 723 mutable char _errbuf[PCAP_ERRBUF_SIZE]; // holds error strings from libpcap
713 QMap<QString, int> _stats; // statistics; 724 QMap<QString, int> _stats; // statistics;
714 class Private; // Private Forward declaration 725 bool _autodelete; // if we auto delete packets after emit
715 Private *d; // if we need to add data 726 class Private; // Private Forward declaration
727 Private *d; // if we need to add data
716}; 728};
717} 729}
718} 730}
719 731
720#endif // OPCAP_H 732#endif // OPCAP_H
721 733