-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 | |||
@@ -36,12 +36,13 @@ | |||
36 | #include <opie2/opcap.h> | 36 | #include <opie2/opcap.h> |
37 | 37 | ||
38 | /* QT */ | 38 | /* QT */ |
39 | 39 | ||
40 | #include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects) | 40 | #include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects) |
41 | #include <qsocketnotifier.h> | 41 | #include <qsocketnotifier.h> |
42 | #include <qobjectlist.h> | ||
42 | 43 | ||
43 | /*====================================================================================== | 44 | /*====================================================================================== |
44 | * OPacket | 45 | * OPacket |
45 | *======================================================================================*/ | 46 | *======================================================================================*/ |
46 | 47 | ||
47 | OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent ) | 48 | OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent ) |
@@ -84,12 +85,25 @@ timevalstruct OPacket::timeval() const | |||
84 | int OPacket::caplen() const | 85 | int OPacket::caplen() const |
85 | { | 86 | { |
86 | return _hdr.caplen; | 87 | return _hdr.caplen; |
87 | } | 88 | } |
88 | 89 | ||
89 | 90 | ||
91 | void OPacket::updateStats( QMap<QString,int>& stats, QObjectList* l ) | ||
92 | { | ||
93 | if (!l) return; | ||
94 | QObject* o = l->first(); | ||
95 | while ( o ) | ||
96 | { | ||
97 | stats[o->name()]++; | ||
98 | updateStats( stats, const_cast<QObjectList*>( o->children() ) ); | ||
99 | o = l->next(); | ||
100 | } | ||
101 | } | ||
102 | |||
103 | |||
90 | QString OPacket::dump( int bpl ) const | 104 | QString OPacket::dump( int bpl ) const |
91 | { | 105 | { |
92 | static int index = 0; | 106 | static int index = 0; |
93 | index++; | 107 | index++; |
94 | int len = _hdr.caplen; | 108 | int len = _hdr.caplen; |
95 | QString str; | 109 | QString str; |
@@ -124,18 +138,18 @@ QString OPacket::dump( int bpl ) const | |||
124 | } | 138 | } |
125 | str.append( '\n' ); | 139 | str.append( '\n' ); |
126 | return str; | 140 | return str; |
127 | } | 141 | } |
128 | 142 | ||
129 | 143 | ||
130 | |||
131 | int OPacket::len() const | 144 | int OPacket::len() const |
132 | { | 145 | { |
133 | return _hdr.len; | 146 | return _hdr.len; |
134 | } | 147 | } |
135 | 148 | ||
149 | |||
136 | /*====================================================================================== | 150 | /*====================================================================================== |
137 | * OEthernetPacket | 151 | * OEthernetPacket |
138 | *======================================================================================*/ | 152 | *======================================================================================*/ |
139 | 153 | ||
140 | OEthernetPacket::OEthernetPacket( const unsigned char* end, const struct ether_header* data, QObject* parent ) | 154 | OEthernetPacket::OEthernetPacket( const unsigned char* end, const struct ether_header* data, QObject* parent ) |
141 | :QObject( parent, "Ethernet" ), _ether( data ) | 155 | :QObject( parent, "Ethernet" ), _ether( data ) |
@@ -769,12 +783,19 @@ void OPacketCapturer::close() | |||
769 | _sn->disconnect( SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); | 783 | _sn->disconnect( SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); |
770 | delete _sn; | 784 | delete _sn; |
771 | } | 785 | } |
772 | pcap_close( _pch ); | 786 | pcap_close( _pch ); |
773 | _open = false; | 787 | _open = false; |
774 | } | 788 | } |
789 | |||
790 | qDebug( "OPacketCapturer::close() --- dumping capturing statistics..." ); | ||
791 | qDebug( "--------------------------------------------------" ); | ||
792 | for( QMap<QString,int>::Iterator it = _stats.begin(); it != _stats.end(); ++it ) | ||
793 | qDebug( "%s : %d", (const char*) it.key(), it.data() ); | ||
794 | qDebug( "--------------------------------------------------" ); | ||
795 | |||
775 | } | 796 | } |
776 | 797 | ||
777 | 798 | ||
778 | int OPacketCapturer::dataLink() const | 799 | int OPacketCapturer::dataLink() const |
779 | { | 800 | { |
780 | return pcap_datalink( _pch ); | 801 | return pcap_datalink( _pch ); |
@@ -790,27 +811,31 @@ int OPacketCapturer::fileno() const | |||
790 | else | 811 | else |
791 | { | 812 | { |
792 | return -1; | 813 | return -1; |
793 | } | 814 | } |
794 | } | 815 | } |
795 | 816 | ||
796 | |||
797 | OPacket* OPacketCapturer::next() | 817 | OPacket* OPacketCapturer::next() |
798 | { | 818 | { |
799 | packetheaderstruct header; | 819 | packetheaderstruct header; |
800 | qDebug( "==> OPacketCapturer::next()" ); | 820 | qDebug( "==> OPacketCapturer::next()" ); |
801 | const unsigned char* pdata = pcap_next( _pch, &header ); | 821 | const unsigned char* pdata = pcap_next( _pch, &header ); |
802 | qDebug( "<== OPacketCapturer::next()" ); | 822 | qDebug( "<== OPacketCapturer::next()" ); |
803 | 823 | ||
804 | if ( header.len ) | 824 | if ( header.len ) |
805 | { | 825 | { |
806 | return new OPacket( dataLink(), header, pdata, 0 ); | 826 | OPacket* p = new OPacket( dataLink(), header, pdata, 0 ); |
807 | // packets shouldn't be inserted in the QObject child-parent hierarchy, | 827 | // packets shouldn't be inserted in the QObject child-parent hierarchy, |
808 | // because due to memory constraints they will be deleted as soon | 828 | // because due to memory constraints they will be deleted as soon |
809 | // as possible - that is right after they have been processed | 829 | // as possible - that is right after they have been processed |
810 | // by emit() [ see below ] | 830 | // by emit() [ see below ] |
831 | |||
832 | //TODO: make gathering statistics optional, because it takes time | ||
833 | p->updateStats( _stats, const_cast<QObjectList*>( p->children() ) ); | ||
834 | |||
835 | return p; | ||
811 | } | 836 | } |
812 | else | 837 | else |
813 | { | 838 | { |
814 | return 0; | 839 | return 0; |
815 | } | 840 | } |
816 | } | 841 | } |
@@ -836,12 +861,13 @@ bool OPacketCapturer::open( const QString& name ) | |||
836 | 861 | ||
837 | if ( handle ) | 862 | if ( handle ) |
838 | { | 863 | { |
839 | qDebug( "OPacketCapturer::open(): libpcap opened successfully." ); | 864 | qDebug( "OPacketCapturer::open(): libpcap opened successfully." ); |
840 | _pch = handle; | 865 | _pch = handle; |
841 | _open = true; | 866 | _open = true; |
867 | _stats.clear(); | ||
842 | 868 | ||
843 | // in case we have an application object, create a socket notifier | 869 | // in case we have an application object, create a socket notifier |
844 | if ( qApp ) | 870 | if ( qApp ) |
845 | { | 871 | { |
846 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); | 872 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); |
847 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); | 873 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); |
@@ -863,13 +889,19 @@ bool OPacketCapturer::isOpen() const | |||
863 | return _open; | 889 | return _open; |
864 | } | 890 | } |
865 | 891 | ||
866 | 892 | ||
867 | void OPacketCapturer::readyToReceive() | 893 | void OPacketCapturer::readyToReceive() |
868 | { | 894 | { |
869 | qDebug( "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(...)'" ); | 895 | qDebug( "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" ); |
870 | OPacket* p = next(); | 896 | OPacket* p = next(); |
871 | emit receivedPacket( p ); | 897 | emit receivedPacket( p ); |
872 | // emit is synchronous - packet has been dealt with, now it's safe to delete | 898 | // emit is synchronous - packet has been dealt with, now it's safe to delete |
873 | delete p; | 899 | delete p; |
874 | } | 900 | } |
875 | 901 | ||
902 | |||
903 | const QMap<QString,int>& OPacketCapturer::statistics() const | ||
904 | { | ||
905 | return _stats; | ||
906 | } | ||
907 | |||
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 | |||
@@ -43,15 +43,17 @@ extern "C" // work around a bpf/pcap conflict in recent headers | |||
43 | #include <netinet/ip.h> | 43 | #include <netinet/ip.h> |
44 | #include <netinet/udp.h> | 44 | #include <netinet/udp.h> |
45 | #include <netinet/tcp.h> | 45 | #include <netinet/tcp.h> |
46 | #include <time.h> | 46 | #include <time.h> |
47 | 47 | ||
48 | /* QT */ | 48 | /* QT */ |
49 | #include <qevent.h> | ||
49 | #include <qhostaddress.h> | 50 | #include <qhostaddress.h> |
50 | #include <qobject.h> | 51 | #include <qobject.h> |
51 | #include <qstring.h> | 52 | #include <qstring.h> |
53 | #include <qmap.h> | ||
52 | 54 | ||
53 | /* OPIE */ | 55 | /* OPIE */ |
54 | #include <opie2/onetutils.h> | 56 | #include <opie2/onetutils.h> |
55 | #include "802_11_user.h" | 57 | #include "802_11_user.h" |
56 | 58 | ||
57 | /* TYPEDEFS */ | 59 | /* TYPEDEFS */ |
@@ -77,12 +79,14 @@ class OPacket : public QObject | |||
77 | timevalstruct timeval() const; | 79 | timevalstruct timeval() const; |
78 | 80 | ||
79 | int caplen() const; | 81 | int caplen() const; |
80 | int len() const; | 82 | int len() const; |
81 | QString dump( int = 32 ) const; | 83 | QString dump( int = 32 ) const; |
82 | 84 | ||
85 | void updateStats( QMap<QString,int>&, QObjectList* ); | ||
86 | |||
83 | private: | 87 | private: |
84 | const packetheaderstruct _hdr; // pcap packet header | 88 | const packetheaderstruct _hdr; // pcap packet header |
85 | const unsigned char* _data; // pcap packet data | 89 | const unsigned char* _data; // pcap packet data |
86 | const unsigned char* _end; // end of pcap packet data | 90 | const unsigned char* _end; // end of pcap packet data |
87 | }; | 91 | }; |
88 | 92 | ||
@@ -431,22 +435,25 @@ class OPacketCapturer : public QObject | |||
431 | int dataLink() const; | 435 | int dataLink() const; |
432 | int fileno() const; | 436 | int fileno() const; |
433 | OPacket* next(); | 437 | OPacket* next(); |
434 | bool open( const QString& name ); | 438 | bool open( const QString& name ); |
435 | bool isOpen() const; | 439 | bool isOpen() const; |
436 | 440 | ||
441 | const QMap<QString,int>& statistics() const; | ||
442 | |||
437 | signals: | 443 | signals: |
438 | void receivedPacket( OPacket* ); | 444 | void receivedPacket( OPacket* ); |
439 | 445 | ||
440 | protected slots: | 446 | protected slots: |
441 | void readyToReceive(); | 447 | void readyToReceive(); |
442 | 448 | ||
443 | protected: | 449 | protected: |
444 | QString _name; // devicename | 450 | QString _name; // devicename |
445 | bool _open; // check this before doing pcap calls | 451 | bool _open; // check this before doing pcap calls |
446 | pcap_t* _pch; // pcap library handle | 452 | pcap_t* _pch; // pcap library handle |
447 | QSocketNotifier* _sn; // socket notifier for main loop | 453 | QSocketNotifier* _sn; // socket notifier for main loop |
448 | mutable char _errbuf[PCAP_ERRBUF_SIZE]; | 454 | mutable char _errbuf[PCAP_ERRBUF_SIZE]; |
455 | QMap<QString, int> _stats; // statistics; | ||
449 | }; | 456 | }; |
450 | 457 | ||
451 | #endif // OPCAP_H | 458 | #endif // OPCAP_H |
452 | 459 | ||