author | mickeyl <mickeyl> | 2004-03-28 16:52:43 (UTC) |
---|---|---|
committer | mickeyl <mickeyl> | 2004-03-28 16:52:43 (UTC) |
commit | e2368b92a101437f7ed289d5ae62f7e7e2466344 (patch) (unidiff) | |
tree | 1e7775ee681d970c9cd785b9afb6946143c212e1 | |
parent | 6c102bd456d760fbe237fd49ce46f212614f228a (diff) | |
download | opie-e2368b92a101437f7ed289d5ae62f7e7e2466344.zip opie-e2368b92a101437f7ed289d5ae62f7e7e2466344.tar.gz opie-e2368b92a101437f7ed289d5ae62f7e7e2466344.tar.bz2 |
OPacketCapturer API addition: setAutoDelete() and autoDelete()
-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 | |||
@@ -48,25 +48,25 @@ | |||
48 | using namespace Opie::Core; | 48 | using namespace Opie::Core; |
49 | 49 | ||
50 | namespace Opie { | 50 | namespace Opie { |
51 | namespace Net { | 51 | namespace Net { |
52 | 52 | ||
53 | /*====================================================================================== | 53 | /*====================================================================================== |
54 | * OPacket | 54 | * OPacket |
55 | *======================================================================================*/ | 55 | *======================================================================================*/ |
56 | 56 | ||
57 | OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent ) | 57 | OPacket::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; |
69 | new OEthernetPacket( _end, (const struct ether_header*) data, this ); | 69 | new OEthernetPacket( _end, (const struct ether_header*) data, this ); |
70 | break; | 70 | break; |
71 | 71 | ||
72 | case DLT_IEEE802_11: | 72 | case DLT_IEEE802_11: |
@@ -78,24 +78,25 @@ OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* | |||
78 | odebug << "OPacket::OPacket(): Received Packet. Datalink = PRISM_HEADER" << oendl; | 78 | odebug << "OPacket::OPacket(): Received Packet. Datalink = PRISM_HEADER" << oendl; |
79 | new OPrismHeaderPacket( _end, (const struct prism_hdr*) (unsigned char*) data, this ); | 79 | new OPrismHeaderPacket( _end, (const struct prism_hdr*) (unsigned char*) data, this ); |
80 | break; | 80 | break; |
81 | 81 | ||
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 | ||
88 | OPacket::~OPacket() | 88 | OPacket::~OPacket() |
89 | { | 89 | { |
90 | qDebug( "OPacket::~OPacket( %s )", name() ); | ||
90 | } | 91 | } |
91 | 92 | ||
92 | 93 | ||
93 | timevalstruct OPacket::timeval() const | 94 | timevalstruct OPacket::timeval() const |
94 | { | 95 | { |
95 | return _hdr.ts; | 96 | return _hdr.ts; |
96 | } | 97 | } |
97 | 98 | ||
98 | 99 | ||
99 | int OPacket::caplen() const | 100 | int OPacket::caplen() const |
100 | { | 101 | { |
101 | return _hdr.caplen; | 102 | return _hdr.caplen; |
@@ -1054,39 +1055,51 @@ QString OWaveLanControlPacket::controlType() const | |||
1054 | default: | 1055 | default: |
1055 | owarn << "OWaveLanControlPacket::managementType(): unhandled subtype " << FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) << oendl; | 1056 | owarn << "OWaveLanControlPacket::managementType(): unhandled subtype " << FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) << oendl; |
1056 | return "Unknown"; | 1057 | return "Unknown"; |
1057 | } | 1058 | } |
1058 | } | 1059 | } |
1059 | 1060 | ||
1060 | 1061 | ||
1061 | /*====================================================================================== | 1062 | /*====================================================================================== |
1062 | * OPacketCapturer | 1063 | * OPacketCapturer |
1063 | *======================================================================================*/ | 1064 | *======================================================================================*/ |
1064 | 1065 | ||
1065 | OPacketCapturer::OPacketCapturer( QObject* parent, const char* name ) | 1066 | OPacketCapturer::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 | ||
1071 | OPacketCapturer::~OPacketCapturer() | 1072 | OPacketCapturer::~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 | ||
1082 | void OPacketCapturer::setAutoDelete( bool b ) | ||
1083 | { | ||
1084 | _autodelete = b; | ||
1085 | } | ||
1086 | |||
1087 | |||
1088 | bool OPacketCapturer::autoDelete() const | ||
1089 | { | ||
1090 | return _autodelete; | ||
1091 | } | ||
1092 | |||
1093 | |||
1081 | void OPacketCapturer::setBlocking( bool b ) | 1094 | void 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 | { |
1089 | odebug << "OPacketCapturer::setBlocking(): can't change blocking mode: " << _errbuf << oendl; | 1102 | odebug << "OPacketCapturer::setBlocking(): can't change blocking mode: " << _errbuf << oendl; |
1090 | } | 1103 | } |
1091 | } | 1104 | } |
1092 | 1105 | ||
@@ -1312,26 +1325,26 @@ bool OPacketCapturer::open( const QFile& file ) | |||
1312 | 1325 | ||
1313 | bool OPacketCapturer::isOpen() const | 1326 | bool OPacketCapturer::isOpen() const |
1314 | { | 1327 | { |
1315 | return _open; | 1328 | return _open; |
1316 | } | 1329 | } |
1317 | 1330 | ||
1318 | 1331 | ||
1319 | void OPacketCapturer::readyToReceive() | 1332 | void 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 | ||
1329 | const QMap<QString,int>& OPacketCapturer::statistics() const | 1342 | const QMap<QString,int>& OPacketCapturer::statistics() const |
1330 | { | 1343 | { |
1331 | return _stats; | 1344 | return _stats; |
1332 | } | 1345 | } |
1333 | 1346 | ||
1334 | 1347 | ||
1335 | int OPacketCapturer::snapShot() const | 1348 | int OPacketCapturer::snapShot() const |
1336 | { | 1349 | { |
1337 | return pcap_snapshot( _pch ); | 1350 | return pcap_snapshot( _pch ); |
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 | |||
@@ -125,27 +125,26 @@ class OPacket : public QObject | |||
125 | public: | 125 | public: |
126 | OPacket( int datalink, packetheaderstruct, const unsigned char*, QObject* parent ); | 126 | OPacket( int datalink, packetheaderstruct, const unsigned char*, QObject* parent ); |
127 | virtual ~OPacket(); | 127 | virtual ~OPacket(); |
128 | 128 | ||
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; |
148 | Private *d; | 147 | Private *d; |
149 | }; | 148 | }; |
150 | 149 | ||
151 | QTextStream& operator<<( QTextStream& s, const OPacket& p ); | 150 | QTextStream& operator<<( QTextStream& s, const OPacket& p ); |
@@ -684,38 +683,51 @@ class OPacketCapturer : public QObject | |||
684 | * than the byte-order of the running system. | 683 | * than the byte-order of the running system. |
685 | */ | 684 | */ |
686 | bool swapped() const; | 685 | bool swapped() const; |
687 | /** | 686 | /** |
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: |
704 | void readyToReceive(); | 715 | void readyToReceive(); |
705 | 716 | ||
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 | ||