-rw-r--r-- | libopie2/opienet/opcap.cpp | 40 |
1 files changed, 36 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 | |||
@@ -18,142 +18,156 @@ | |||
18 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | 18 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; |
19 | : .. .:, . . . without even the implied warranty of | 19 | : .. .:, . . . without even the implied warranty of |
20 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | 20 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A |
21 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | 21 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU |
22 | ..}^=.= = ; Library General Public License for more | 22 | ..}^=.= = ; Library General Public License for more |
23 | ++= -. .` .: details. | 23 | ++= -. .` .: details. |
24 | : = ...= . :.=- | 24 | : = ...= . :.=- |
25 | -. .:....=;==+<; You should have received a copy of the GNU | 25 | -. .:....=;==+<; You should have received a copy of the GNU |
26 | -_. . . )=. = Library General Public License along with | 26 | -_. . . )=. = Library General Public License along with |
27 | -- :-=` this library; see the file COPYING.LIB. | 27 | -- :-=` this library; see the file COPYING.LIB. |
28 | If not, write to the Free Software Foundation, | 28 | If not, write to the Free Software Foundation, |
29 | Inc., 59 Temple Place - Suite 330, | 29 | Inc., 59 Temple Place - Suite 330, |
30 | Boston, MA 02111-1307, USA. | 30 | Boston, MA 02111-1307, USA. |
31 | 31 | ||
32 | */ | 32 | */ |
33 | 33 | ||
34 | /* OPIE */ | 34 | /* OPIE */ |
35 | 35 | ||
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 ) |
48 | :QObject( parent, "Generic" ), _hdr( header ), _data( data ) | 49 | :QObject( parent, "Generic" ), _hdr( header ), _data( data ) |
49 | { | 50 | { |
50 | //qDebug( "OPacket::OPacket(): (Len %d, CapLen %d)" /*, ctime((const time_t*) header.ts.tv_sec)*/, header.len, header.caplen ); | 51 | //qDebug( "OPacket::OPacket(): (Len %d, CapLen %d)" /*, ctime((const time_t*) header.ts.tv_sec)*/, header.len, header.caplen ); |
51 | 52 | ||
52 | _end = (unsigned char*) data + header.len; | 53 | _end = (unsigned char*) data + header.len; |
53 | //qDebug( "OPacket::data @ %0x, end @ %0x", data, _end ); | 54 | //qDebug( "OPacket::data @ %0x, end @ %0x", data, _end ); |
54 | 55 | ||
55 | switch ( datalink ) | 56 | switch ( datalink ) |
56 | { | 57 | { |
57 | case DLT_EN10MB: | 58 | case DLT_EN10MB: |
58 | qDebug( "OPacket::OPacket(): Received Packet. Datalink = ETHERNET" ); | 59 | qDebug( "OPacket::OPacket(): Received Packet. Datalink = ETHERNET" ); |
59 | new OEthernetPacket( _end, (const struct ether_header*) data, this ); | 60 | new OEthernetPacket( _end, (const struct ether_header*) data, this ); |
60 | break; | 61 | break; |
61 | 62 | ||
62 | case DLT_IEEE802_11: | 63 | case DLT_IEEE802_11: |
63 | qDebug( "OPacket::OPacket(): Received Packet. Datalink = IEEE802.11" ); | 64 | qDebug( "OPacket::OPacket(): Received Packet. Datalink = IEEE802.11" ); |
64 | new OWaveLanPacket( _end, (const struct ieee_802_11_header*) data, this ); | 65 | new OWaveLanPacket( _end, (const struct ieee_802_11_header*) data, this ); |
65 | break; | 66 | break; |
66 | 67 | ||
67 | default: | 68 | default: |
68 | qWarning( "OPacket::OPacket(): Received Packet over unsupported datalink '%s'!", datalink ); | 69 | qWarning( "OPacket::OPacket(): Received Packet over unsupported datalink '%s'!", datalink ); |
69 | } | 70 | } |
70 | } | 71 | } |
71 | 72 | ||
72 | 73 | ||
73 | OPacket::~OPacket() | 74 | OPacket::~OPacket() |
74 | { | 75 | { |
75 | } | 76 | } |
76 | 77 | ||
77 | 78 | ||
78 | timevalstruct OPacket::timeval() const | 79 | timevalstruct OPacket::timeval() const |
79 | { | 80 | { |
80 | return _hdr.ts; | 81 | return _hdr.ts; |
81 | } | 82 | } |
82 | 83 | ||
83 | 84 | ||
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; |
96 | str.sprintf( "\n<----- Packet #%04d Len = 0x%X (%d) ----->\n\n", index, len, len ); | 110 | str.sprintf( "\n<----- Packet #%04d Len = 0x%X (%d) ----->\n\n", index, len, len ); |
97 | str.append( "0000: " ); | 111 | str.append( "0000: " ); |
98 | QString tmp; | 112 | QString tmp; |
99 | QString bytes; | 113 | QString bytes; |
100 | QString chars; | 114 | QString chars; |
101 | 115 | ||
102 | for ( int i = 0; i < len; ++i ) | 116 | for ( int i = 0; i < len; ++i ) |
103 | { | 117 | { |
104 | tmp.sprintf( "%02X ", _data[i] ); bytes.append( tmp ); | 118 | tmp.sprintf( "%02X ", _data[i] ); bytes.append( tmp ); |
105 | if ( (_data[i] > 31) && (_data[i]<128) ) chars.append( _data[i] ); | 119 | if ( (_data[i] > 31) && (_data[i]<128) ) chars.append( _data[i] ); |
106 | else chars.append( '.' ); | 120 | else chars.append( '.' ); |
107 | 121 | ||
108 | if ( !((i+1) % bpl) ) | 122 | if ( !((i+1) % bpl) ) |
109 | { | 123 | { |
110 | str.append( bytes ); | 124 | str.append( bytes ); |
111 | str.append( ' ' ); | 125 | str.append( ' ' ); |
112 | str.append( chars ); | 126 | str.append( chars ); |
113 | str.append( '\n' ); | 127 | str.append( '\n' ); |
114 | tmp.sprintf( "%04X: ", i+1 ); str.append( tmp ); | 128 | tmp.sprintf( "%04X: ", i+1 ); str.append( tmp ); |
115 | bytes = ""; | 129 | bytes = ""; |
116 | chars = ""; | 130 | chars = ""; |
117 | } | 131 | } |
118 | 132 | ||
119 | } | 133 | } |
120 | if ( (len % bpl) ) | 134 | if ( (len % bpl) ) |
121 | { | 135 | { |
122 | str.append( bytes.leftJustify( 1 + 3*bpl ) ); | 136 | str.append( bytes.leftJustify( 1 + 3*bpl ) ); |
123 | str.append( chars ); | 137 | str.append( chars ); |
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 ) |
142 | 156 | ||
143 | { | 157 | { |
144 | 158 | ||
145 | qDebug( "Source = %s", (const char*) sourceAddress().toString() ); | 159 | qDebug( "Source = %s", (const char*) sourceAddress().toString() ); |
146 | qDebug( "Destination = %s", (const char*) destinationAddress().toString() ); | 160 | qDebug( "Destination = %s", (const char*) destinationAddress().toString() ); |
147 | 161 | ||
148 | if ( sourceAddress() == OMacAddress::broadcast ) | 162 | if ( sourceAddress() == OMacAddress::broadcast ) |
149 | qDebug( "Source is broadcast address" ); | 163 | qDebug( "Source is broadcast address" ); |
150 | if ( destinationAddress() == OMacAddress::broadcast ) | 164 | if ( destinationAddress() == OMacAddress::broadcast ) |
151 | qDebug( "Destination is broadcast address" ); | 165 | qDebug( "Destination is broadcast address" ); |
152 | 166 | ||
153 | switch ( type() ) | 167 | switch ( type() ) |
154 | { | 168 | { |
155 | case ETHERTYPE_IP: new OIPPacket( end, (const struct iphdr*) (data+1), this ); break; | 169 | case ETHERTYPE_IP: new OIPPacket( end, (const struct iphdr*) (data+1), this ); break; |
156 | case ETHERTYPE_ARP: { qDebug( "OPacket::OPacket(): Received Ethernet Packet : Type = ARP" ); break; } | 170 | case ETHERTYPE_ARP: { qDebug( "OPacket::OPacket(): Received Ethernet Packet : Type = ARP" ); break; } |
157 | case ETHERTYPE_REVARP: { qDebug( "OPacket::OPacket(): Received Ethernet Packet : Type = RARP" ); break; } | 171 | case ETHERTYPE_REVARP: { qDebug( "OPacket::OPacket(): Received Ethernet Packet : Type = RARP" ); break; } |
158 | default: qDebug( "OPacket::OPacket(): Received Ethernet Packet : Type = UNKNOWN" ); | 172 | default: qDebug( "OPacket::OPacket(): Received Ethernet Packet : Type = UNKNOWN" ); |
159 | } | 173 | } |
@@ -751,125 +765,143 @@ void OPacketCapturer::setBlocking( bool b ) | |||
751 | bool OPacketCapturer::blocking() const | 765 | bool OPacketCapturer::blocking() const |
752 | { | 766 | { |
753 | int b = pcap_getnonblock( _pch, _errbuf ); | 767 | int b = pcap_getnonblock( _pch, _errbuf ); |
754 | if ( b == -1 ) | 768 | if ( b == -1 ) |
755 | { | 769 | { |
756 | qDebug( "OPacketCapturer::blocking(): can't get blocking mode: %s", _errbuf ); | 770 | qDebug( "OPacketCapturer::blocking(): can't get blocking mode: %s", _errbuf ); |
757 | return -1; | 771 | return -1; |
758 | } | 772 | } |
759 | return !b; | 773 | return !b; |
760 | } | 774 | } |
761 | 775 | ||
762 | 776 | ||
763 | void OPacketCapturer::close() | 777 | void OPacketCapturer::close() |
764 | { | 778 | { |
765 | if ( _open ) | 779 | if ( _open ) |
766 | { | 780 | { |
767 | if ( _sn ) | 781 | if ( _sn ) |
768 | { | 782 | { |
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 ); |
781 | } | 802 | } |
782 | 803 | ||
783 | 804 | ||
784 | int OPacketCapturer::fileno() const | 805 | int OPacketCapturer::fileno() const |
785 | { | 806 | { |
786 | if ( _open ) | 807 | if ( _open ) |
787 | { | 808 | { |
788 | return pcap_fileno( _pch ); | 809 | return pcap_fileno( _pch ); |
789 | } | 810 | } |
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 | } |
817 | 842 | ||
818 | 843 | ||
819 | bool OPacketCapturer::open( const QString& name ) | 844 | bool OPacketCapturer::open( const QString& name ) |
820 | { | 845 | { |
821 | if ( _open ) | 846 | if ( _open ) |
822 | { | 847 | { |
823 | if ( name == _name ) // ignore opening an already openend device | 848 | if ( name == _name ) // ignore opening an already openend device |
824 | { | 849 | { |
825 | return true; | 850 | return true; |
826 | } | 851 | } |
827 | else // close the last opened device | 852 | else // close the last opened device |
828 | { | 853 | { |
829 | close(); | 854 | close(); |
830 | } | 855 | } |
831 | } | 856 | } |
832 | 857 | ||
833 | _name = name; | 858 | _name = name; |
834 | 859 | ||
835 | pcap_t* handle = pcap_open_live( const_cast<char*>( (const char*) name ), 1024, 0, 0, &_errbuf[0] ); | 860 | pcap_t* handle = pcap_open_live( const_cast<char*>( (const char*) name ), 1024, 0, 0, &_errbuf[0] ); |
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() ) ); |
848 | } | 874 | } |
849 | 875 | ||
850 | return true; | 876 | return true; |
851 | } | 877 | } |
852 | else | 878 | else |
853 | { | 879 | { |
854 | qDebug( "OPacketCapturer::open(): can't open libpcap: %s", _errbuf ); | 880 | qDebug( "OPacketCapturer::open(): can't open libpcap: %s", _errbuf ); |
855 | return false; | 881 | return false; |
856 | } | 882 | } |
857 | 883 | ||
858 | } | 884 | } |
859 | 885 | ||
860 | 886 | ||
861 | bool OPacketCapturer::isOpen() const | 887 | bool OPacketCapturer::isOpen() const |
862 | { | 888 | { |
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 | |||