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 | |||
@@ -1,185 +1,186 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of the Opie Project | 2 | This file is part of the Opie Project |
3 | Copyright (C) 2003 by Michael 'Mickey' Lauer <mickey@Vanille.de> | 3 | Copyright (C) 2003 by Michael 'Mickey' Lauer <mickey@Vanille.de> |
4 | =. | 4 | =. |
5 | .=l. | 5 | .=l. |
6 | .>+-= | 6 | .>+-= |
7 | _;:, .> :=|. This program is free software; you can | 7 | _;:, .> :=|. This program is free software; you can |
8 | .> <`_, > . <= redistribute it and/or modify it under | 8 | .> <`_, > . <= redistribute it and/or modify it under |
9 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | 9 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public |
10 | .="- .-=="i, .._ License as published by the Free Software | 10 | .="- .-=="i, .._ License as published by the Free Software |
11 | - . .-<_> .<> Foundation; either version 2 of the License, | 11 | - . .-<_> .<> Foundation; either version 2 of the License, |
12 | ._= =} : or (at your option) any later version. | 12 | ._= =} : or (at your option) any later version. |
13 | .%`+i> _;_. | 13 | .%`+i> _;_. |
14 | .i_,=:_. -<s. This program is distributed in the hope that | 14 | .i_,=:_. -<s. This program is distributed in the hope that |
15 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | 15 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; |
16 | : .. .:, . . . without even the implied warranty of | 16 | : .. .:, . . . without even the implied warranty of |
17 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | 17 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A |
18 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | 18 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU |
19 | ..}^=.= = ; Library General Public License for more | 19 | ..}^=.= = ; Library General Public License for more |
20 | ++= -. .` .: details. | 20 | ++= -. .` .: details. |
21 | : = ...= . :.=- | 21 | : = ...= . :.=- |
22 | -. .:....=;==+<; You should have received a copy of the GNU | 22 | -. .:....=;==+<; You should have received a copy of the GNU |
23 | -_. . . )=. = Library General Public License along with | 23 | -_. . . )=. = Library General Public License along with |
24 | -- :-=` this library; see the file COPYING.LIB. | 24 | -- :-=` this library; see the file COPYING.LIB. |
25 | If not, write to the Free Software Foundation, | 25 | If not, write to the Free Software Foundation, |
26 | Inc., 59 Temple Place - Suite 330, | 26 | Inc., 59 Temple Place - Suite 330, |
27 | Boston, MA 02111-1307, USA. | 27 | Boston, MA 02111-1307, USA. |
28 | 28 | ||
29 | */ | 29 | */ |
30 | 30 | ||
31 | /* OPIE */ | 31 | /* OPIE */ |
32 | #include <opie2/opcap.h> | 32 | #include <opie2/opcap.h> |
33 | #include <opie2/odebug.h> | 33 | #include <opie2/odebug.h> |
34 | 34 | ||
35 | /* QT */ | 35 | /* QT */ |
36 | #include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects) | 36 | #include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects) |
37 | #include <qsocketnotifier.h> | 37 | #include <qsocketnotifier.h> |
38 | #include <qobjectlist.h> | 38 | #include <qobjectlist.h> |
39 | 39 | ||
40 | /* SYSTEM */ | 40 | /* SYSTEM */ |
41 | #include <sys/time.h> | 41 | #include <sys/time.h> |
42 | #include <sys/types.h> | 42 | #include <sys/types.h> |
43 | #include <unistd.h> | 43 | #include <unistd.h> |
44 | 44 | ||
45 | /* LOCAL */ | 45 | /* LOCAL */ |
46 | #include "udp_ports.h" | 46 | #include "udp_ports.h" |
47 | 47 | ||
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: |
73 | odebug << "OPacket::OPacket(): Received Packet. Datalink = IEEE802.11" << oendl; | 73 | odebug << "OPacket::OPacket(): Received Packet. Datalink = IEEE802.11" << oendl; |
74 | new OWaveLanPacket( _end, (const struct ieee_802_11_header*) data, this ); | 74 | new OWaveLanPacket( _end, (const struct ieee_802_11_header*) data, this ); |
75 | break; | 75 | break; |
76 | 76 | ||
77 | case DLT_PRISM_HEADER: | 77 | case DLT_PRISM_HEADER: |
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; |
102 | } | 103 | } |
103 | 104 | ||
104 | 105 | ||
105 | void OPacket::updateStats( QMap<QString,int>& stats, QObjectList* l ) | 106 | void OPacket::updateStats( QMap<QString,int>& stats, QObjectList* l ) |
106 | { | 107 | { |
107 | if (!l) return; | 108 | if (!l) return; |
108 | QObject* o = l->first(); | 109 | QObject* o = l->first(); |
109 | while ( o ) | 110 | while ( o ) |
110 | { | 111 | { |
111 | stats[o->name()]++; | 112 | stats[o->name()]++; |
112 | updateStats( stats, const_cast<QObjectList*>( o->children() ) ); | 113 | updateStats( stats, const_cast<QObjectList*>( o->children() ) ); |
113 | o = l->next(); | 114 | o = l->next(); |
114 | } | 115 | } |
115 | } | 116 | } |
116 | 117 | ||
117 | 118 | ||
118 | QString OPacket::dumpStructure() const | 119 | QString OPacket::dumpStructure() const |
119 | { | 120 | { |
120 | return "[ |" + _dumpStructure( const_cast<QObjectList*>( this->children() ) ) + " ]"; | 121 | return "[ |" + _dumpStructure( const_cast<QObjectList*>( this->children() ) ) + " ]"; |
121 | } | 122 | } |
122 | 123 | ||
123 | 124 | ||
124 | QString OPacket::_dumpStructure( QObjectList* l ) const | 125 | QString OPacket::_dumpStructure( QObjectList* l ) const |
125 | { | 126 | { |
126 | if (!l) return QString::null; | 127 | if (!l) return QString::null; |
127 | QObject* o = l->first(); | 128 | QObject* o = l->first(); |
128 | QString str(" "); | 129 | QString str(" "); |
129 | 130 | ||
130 | while ( o ) | 131 | while ( o ) |
131 | { | 132 | { |
132 | str.append( o->name() ); | 133 | str.append( o->name() ); |
133 | str.append( " |" ); | 134 | str.append( " |" ); |
134 | str += _dumpStructure( const_cast<QObjectList*>( o->children() ) ); | 135 | str += _dumpStructure( const_cast<QObjectList*>( o->children() ) ); |
135 | o = l->next(); | 136 | o = l->next(); |
136 | } | 137 | } |
137 | return str; | 138 | return str; |
138 | } | 139 | } |
139 | 140 | ||
140 | QString OPacket::dump( int bpl ) const | 141 | QString OPacket::dump( int bpl ) const |
141 | { | 142 | { |
142 | static int index = 0; | 143 | static int index = 0; |
143 | index++; | 144 | index++; |
144 | int len = _hdr.caplen; | 145 | int len = _hdr.caplen; |
145 | QString str; | 146 | QString str; |
146 | str.sprintf( "\n<----- Packet #%04d Len = 0x%X (%d) ----->\n\n", index, len, len ); | 147 | str.sprintf( "\n<----- Packet #%04d Len = 0x%X (%d) ----->\n\n", index, len, len ); |
147 | str.append( "0000: " ); | 148 | str.append( "0000: " ); |
148 | QString tmp; | 149 | QString tmp; |
149 | QString bytes; | 150 | QString bytes; |
150 | QString chars; | 151 | QString chars; |
151 | 152 | ||
152 | for ( int i = 0; i < len; ++i ) | 153 | for ( int i = 0; i < len; ++i ) |
153 | { | 154 | { |
154 | tmp.sprintf( "%02X ", _data[i] ); bytes.append( tmp ); | 155 | tmp.sprintf( "%02X ", _data[i] ); bytes.append( tmp ); |
155 | if ( (_data[i] > 31) && (_data[i]<128) ) chars.append( _data[i] ); | 156 | if ( (_data[i] > 31) && (_data[i]<128) ) chars.append( _data[i] ); |
156 | else chars.append( '.' ); | 157 | else chars.append( '.' ); |
157 | 158 | ||
158 | if ( !((i+1) % bpl) ) | 159 | if ( !((i+1) % bpl) ) |
159 | { | 160 | { |
160 | str.append( bytes ); | 161 | str.append( bytes ); |
161 | str.append( ' ' ); | 162 | str.append( ' ' ); |
162 | str.append( chars ); | 163 | str.append( chars ); |
163 | str.append( '\n' ); | 164 | str.append( '\n' ); |
164 | tmp.sprintf( "%04X: ", i+1 ); str.append( tmp ); | 165 | tmp.sprintf( "%04X: ", i+1 ); str.append( tmp ); |
165 | bytes = ""; | 166 | bytes = ""; |
166 | chars = ""; | 167 | chars = ""; |
167 | } | 168 | } |
168 | 169 | ||
169 | } | 170 | } |
170 | if ( (len % bpl) ) | 171 | if ( (len % bpl) ) |
171 | { | 172 | { |
172 | str.append( bytes.leftJustify( 1 + 3*bpl ) ); | 173 | str.append( bytes.leftJustify( 1 + 3*bpl ) ); |
173 | str.append( chars ); | 174 | str.append( chars ); |
174 | } | 175 | } |
175 | str.append( '\n' ); | 176 | str.append( '\n' ); |
176 | return str; | 177 | return str; |
177 | } | 178 | } |
178 | 179 | ||
179 | 180 | ||
180 | int OPacket::len() const | 181 | int OPacket::len() const |
181 | { | 182 | { |
182 | return _hdr.len; | 183 | return _hdr.len; |
183 | } | 184 | } |
184 | 185 | ||
185 | 186 | ||
@@ -970,207 +971,219 @@ OWaveLanManagementChallenge::OWaveLanManagementChallenge( const unsigned char* e | |||
970 | 971 | ||
971 | 972 | ||
972 | OWaveLanManagementChallenge::~OWaveLanManagementChallenge() | 973 | OWaveLanManagementChallenge::~OWaveLanManagementChallenge() |
973 | { | 974 | { |
974 | } | 975 | } |
975 | 976 | ||
976 | /*====================================================================================== | 977 | /*====================================================================================== |
977 | * OWaveLanDataPacket | 978 | * OWaveLanDataPacket |
978 | *======================================================================================*/ | 979 | *======================================================================================*/ |
979 | 980 | ||
980 | OWaveLanDataPacket::OWaveLanDataPacket( const unsigned char* end, const struct ieee_802_11_data_header* data, OWaveLanPacket* parent ) | 981 | OWaveLanDataPacket::OWaveLanDataPacket( const unsigned char* end, const struct ieee_802_11_data_header* data, OWaveLanPacket* parent ) |
981 | :QObject( parent, "802.11 Data" ), _header( data ) | 982 | :QObject( parent, "802.11 Data" ), _header( data ) |
982 | { | 983 | { |
983 | odebug << "OWaveLanDataPacket::OWaveLanDataPacket(): decoding frame..." << oendl; | 984 | odebug << "OWaveLanDataPacket::OWaveLanDataPacket(): decoding frame..." << oendl; |
984 | 985 | ||
985 | const unsigned char* payload = (const unsigned char*) data + sizeof( struct ieee_802_11_data_header ); | 986 | const unsigned char* payload = (const unsigned char*) data + sizeof( struct ieee_802_11_data_header ); |
986 | 987 | ||
987 | #warning The next line works for most cases, but can not be correct generally! | 988 | #warning The next line works for most cases, but can not be correct generally! |
988 | if (!( ( (OWaveLanPacket*) this->parent())->duration() )) payload -= 6; // compensation for missing last address | 989 | if (!( ( (OWaveLanPacket*) this->parent())->duration() )) payload -= 6; // compensation for missing last address |
989 | 990 | ||
990 | new OLLCPacket( end, (const struct ieee_802_11_802_2_header*) payload, this ); | 991 | new OLLCPacket( end, (const struct ieee_802_11_802_2_header*) payload, this ); |
991 | } | 992 | } |
992 | 993 | ||
993 | 994 | ||
994 | OWaveLanDataPacket::~OWaveLanDataPacket() | 995 | OWaveLanDataPacket::~OWaveLanDataPacket() |
995 | { | 996 | { |
996 | } | 997 | } |
997 | 998 | ||
998 | 999 | ||
999 | /*====================================================================================== | 1000 | /*====================================================================================== |
1000 | * OLLCPacket | 1001 | * OLLCPacket |
1001 | *======================================================================================*/ | 1002 | *======================================================================================*/ |
1002 | 1003 | ||
1003 | OLLCPacket::OLLCPacket( const unsigned char* end, const struct ieee_802_11_802_2_header* data, QObject* parent ) | 1004 | OLLCPacket::OLLCPacket( const unsigned char* end, const struct ieee_802_11_802_2_header* data, QObject* parent ) |
1004 | :QObject( parent, "802.11 LLC" ), _header( data ) | 1005 | :QObject( parent, "802.11 LLC" ), _header( data ) |
1005 | { | 1006 | { |
1006 | odebug << "OLLCPacket::OLLCPacket(): decoding frame..." << oendl; | 1007 | odebug << "OLLCPacket::OLLCPacket(): decoding frame..." << oendl; |
1007 | 1008 | ||
1008 | if ( !(_header->oui[0] || _header->oui[1] || _header->oui[2]) ) | 1009 | if ( !(_header->oui[0] || _header->oui[1] || _header->oui[2]) ) |
1009 | { | 1010 | { |
1010 | owarn << "OLLCPacket::OLLCPacket(): contains an encapsulated Ethernet frame (type = " << EXTRACT_16BITS( &_header->type ) << ")" << oendl; | 1011 | owarn << "OLLCPacket::OLLCPacket(): contains an encapsulated Ethernet frame (type = " << EXTRACT_16BITS( &_header->type ) << ")" << oendl; |
1011 | 1012 | ||
1012 | switch ( EXTRACT_16BITS( &_header->type ) ) // defined in linux/if_ether.h | 1013 | switch ( EXTRACT_16BITS( &_header->type ) ) // defined in linux/if_ether.h |
1013 | { | 1014 | { |
1014 | case ETH_P_IP: new OIPPacket( end, (const struct iphdr*) (data+1), this ); break; | 1015 | case ETH_P_IP: new OIPPacket( end, (const struct iphdr*) (data+1), this ); break; |
1015 | case ETH_P_ARP: new OARPPacket( end, (const struct myarphdr*) (data+1), this ); break; | 1016 | case ETH_P_ARP: new OARPPacket( end, (const struct myarphdr*) (data+1), this ); break; |
1016 | default: owarn << "OLLCPacket::OLLCPacket(): Unknown Encapsulation type = " << EXTRACT_16BITS( &_header->type ) << oendl; | 1017 | default: owarn << "OLLCPacket::OLLCPacket(): Unknown Encapsulation type = " << EXTRACT_16BITS( &_header->type ) << oendl; |
1017 | } | 1018 | } |
1018 | } | 1019 | } |
1019 | } | 1020 | } |
1020 | 1021 | ||
1021 | 1022 | ||
1022 | OLLCPacket::~OLLCPacket() | 1023 | OLLCPacket::~OLLCPacket() |
1023 | { | 1024 | { |
1024 | } | 1025 | } |
1025 | 1026 | ||
1026 | 1027 | ||
1027 | /*====================================================================================== | 1028 | /*====================================================================================== |
1028 | * OWaveLanControlPacket | 1029 | * OWaveLanControlPacket |
1029 | *======================================================================================*/ | 1030 | *======================================================================================*/ |
1030 | 1031 | ||
1031 | OWaveLanControlPacket::OWaveLanControlPacket( const unsigned char* end, const struct ieee_802_11_control_header* data, OWaveLanPacket* parent ) | 1032 | OWaveLanControlPacket::OWaveLanControlPacket( const unsigned char* end, const struct ieee_802_11_control_header* data, OWaveLanPacket* parent ) |
1032 | :QObject( parent, "802.11 Control" ), _header( data ) | 1033 | :QObject( parent, "802.11 Control" ), _header( data ) |
1033 | { | 1034 | { |
1034 | odebug << "OWaveLanControlPacket::OWaveLanDataControl(): decoding frame..." << oendl; | 1035 | odebug << "OWaveLanControlPacket::OWaveLanDataControl(): decoding frame..." << oendl; |
1035 | odebug << "Detected subtype is " << controlType() << oendl; | 1036 | odebug << "Detected subtype is " << controlType() << oendl; |
1036 | } | 1037 | } |
1037 | 1038 | ||
1038 | 1039 | ||
1039 | OWaveLanControlPacket::~OWaveLanControlPacket() | 1040 | OWaveLanControlPacket::~OWaveLanControlPacket() |
1040 | { | 1041 | { |
1041 | } | 1042 | } |
1042 | 1043 | ||
1043 | 1044 | ||
1044 | QString OWaveLanControlPacket::controlType() const | 1045 | QString OWaveLanControlPacket::controlType() const |
1045 | { | 1046 | { |
1046 | switch ( FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) ) | 1047 | switch ( FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) ) |
1047 | { | 1048 | { |
1048 | case CTRL_PS_POLL: return "PowerSavePoll"; break; | 1049 | case CTRL_PS_POLL: return "PowerSavePoll"; break; |
1049 | case CTRL_RTS: return "RequestToSend"; break; | 1050 | case CTRL_RTS: return "RequestToSend"; break; |
1050 | case CTRL_CTS: return "ClearToSend"; break; | 1051 | case CTRL_CTS: return "ClearToSend"; break; |
1051 | case CTRL_ACK: return "Acknowledge"; break; | 1052 | case CTRL_ACK: return "Acknowledge"; break; |
1052 | case CTRL_CF_END: return "ContentionFreeEnd"; break; | 1053 | case CTRL_CF_END: return "ContentionFreeEnd"; break; |
1053 | case CTRL_END_ACK: return "AcknowledgeEnd"; break; | 1054 | case CTRL_END_ACK: return "AcknowledgeEnd"; break; |
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 | ||
1093 | 1106 | ||
1094 | bool OPacketCapturer::blocking() const | 1107 | bool OPacketCapturer::blocking() const |
1095 | { | 1108 | { |
1096 | int b = pcap_getnonblock( _pch, _errbuf ); | 1109 | int b = pcap_getnonblock( _pch, _errbuf ); |
1097 | if ( b == -1 ) | 1110 | if ( b == -1 ) |
1098 | { | 1111 | { |
1099 | odebug << "OPacketCapturer::blocking(): can't get blocking mode: " << _errbuf << oendl; | 1112 | odebug << "OPacketCapturer::blocking(): can't get blocking mode: " << _errbuf << oendl; |
1100 | return -1; | 1113 | return -1; |
1101 | } | 1114 | } |
1102 | return !b; | 1115 | return !b; |
1103 | } | 1116 | } |
1104 | 1117 | ||
1105 | 1118 | ||
1106 | void OPacketCapturer::closeDumpFile() | 1119 | void OPacketCapturer::closeDumpFile() |
1107 | { | 1120 | { |
1108 | if ( _pcd ) | 1121 | if ( _pcd ) |
1109 | { | 1122 | { |
1110 | pcap_dump_close( _pcd ); | 1123 | pcap_dump_close( _pcd ); |
1111 | _pcd = 0; | 1124 | _pcd = 0; |
1112 | } | 1125 | } |
1113 | pcap_close( _pch ); | 1126 | pcap_close( _pch ); |
1114 | } | 1127 | } |
1115 | 1128 | ||
1116 | 1129 | ||
1117 | void OPacketCapturer::close() | 1130 | void OPacketCapturer::close() |
1118 | { | 1131 | { |
1119 | if ( _open ) | 1132 | if ( _open ) |
1120 | { | 1133 | { |
1121 | if ( _sn ) | 1134 | if ( _sn ) |
1122 | { | 1135 | { |
1123 | _sn->disconnect( SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); | 1136 | _sn->disconnect( SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); |
1124 | delete _sn; | 1137 | delete _sn; |
1125 | } | 1138 | } |
1126 | closeDumpFile(); | 1139 | closeDumpFile(); |
1127 | _open = false; | 1140 | _open = false; |
1128 | } | 1141 | } |
1129 | 1142 | ||
1130 | odebug << "OPacketCapturer::close() --- dumping capturing statistics..." << oendl; | 1143 | odebug << "OPacketCapturer::close() --- dumping capturing statistics..." << oendl; |
1131 | odebug << "--------------------------------------------------" << oendl; | 1144 | odebug << "--------------------------------------------------" << oendl; |
1132 | for( QMap<QString,int>::Iterator it = _stats.begin(); it != _stats.end(); ++it ) | 1145 | for( QMap<QString,int>::Iterator it = _stats.begin(); it != _stats.end(); ++it ) |
1133 | odebug << it.key() << " = " << it.data() << oendl; | 1146 | odebug << it.key() << " = " << it.data() << oendl; |
1134 | odebug << "--------------------------------------------------" << oendl; | 1147 | odebug << "--------------------------------------------------" << oendl; |
1135 | 1148 | ||
1136 | } | 1149 | } |
1137 | 1150 | ||
1138 | 1151 | ||
1139 | int OPacketCapturer::dataLink() const | 1152 | int OPacketCapturer::dataLink() const |
1140 | { | 1153 | { |
1141 | return pcap_datalink( _pch ); | 1154 | return pcap_datalink( _pch ); |
1142 | } | 1155 | } |
1143 | 1156 | ||
1144 | 1157 | ||
1145 | void OPacketCapturer::dump( OPacket* p ) | 1158 | void OPacketCapturer::dump( OPacket* p ) |
1146 | { | 1159 | { |
1147 | if ( !_pcd ) | 1160 | if ( !_pcd ) |
1148 | { | 1161 | { |
1149 | owarn << "OPacketCapturer::dump() - cannot dump without open capture file!" << oendl; | 1162 | owarn << "OPacketCapturer::dump() - cannot dump without open capture file!" << oendl; |
1150 | return; | 1163 | return; |
1151 | } | 1164 | } |
1152 | pcap_dump( (u_char*) _pcd, &p->_hdr, p->_data ); | 1165 | pcap_dump( (u_char*) _pcd, &p->_hdr, p->_data ); |
1153 | } | 1166 | } |
1154 | 1167 | ||
1155 | 1168 | ||
1156 | int OPacketCapturer::fileno() const | 1169 | int OPacketCapturer::fileno() const |
1157 | { | 1170 | { |
1158 | if ( _open ) | 1171 | if ( _open ) |
1159 | { | 1172 | { |
1160 | return pcap_fileno( _pch ); | 1173 | return pcap_fileno( _pch ); |
1161 | } | 1174 | } |
1162 | else | 1175 | else |
1163 | { | 1176 | { |
1164 | return -1; | 1177 | return -1; |
1165 | } | 1178 | } |
1166 | } | 1179 | } |
1167 | 1180 | ||
1168 | 1181 | ||
1169 | OPacket* OPacketCapturer::next( int time ) | 1182 | OPacket* OPacketCapturer::next( int time ) |
1170 | { | 1183 | { |
1171 | fd_set fds; | 1184 | fd_set fds; |
1172 | struct timeval tv; | 1185 | struct timeval tv; |
1173 | FD_ZERO( &fds ); | 1186 | FD_ZERO( &fds ); |
1174 | FD_SET( pcap_fileno( _pch ), &fds ); | 1187 | FD_SET( pcap_fileno( _pch ), &fds ); |
1175 | tv.tv_sec = time / 1000; | 1188 | tv.tv_sec = time / 1000; |
1176 | tv.tv_usec = time % 1000; | 1189 | tv.tv_usec = time % 1000; |
@@ -1228,126 +1241,126 @@ bool OPacketCapturer::open( const QString& name ) | |||
1228 | // open libpcap | 1241 | // open libpcap |
1229 | pcap_t* handle = pcap_open_live( const_cast<char*>( (const char*) name ), 1024, 0, 0, &_errbuf[0] ); | 1242 | pcap_t* handle = pcap_open_live( const_cast<char*>( (const char*) name ), 1024, 0, 0, &_errbuf[0] ); |
1230 | 1243 | ||
1231 | if ( !handle ) | 1244 | if ( !handle ) |
1232 | { | 1245 | { |
1233 | owarn << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl; | 1246 | owarn << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl; |
1234 | return false; | 1247 | return false; |
1235 | } | 1248 | } |
1236 | 1249 | ||
1237 | odebug << "OPacketCapturer::open(): libpcap [" << name << "] opened successfully." << oendl; | 1250 | odebug << "OPacketCapturer::open(): libpcap [" << name << "] opened successfully." << oendl; |
1238 | _pch = handle; | 1251 | _pch = handle; |
1239 | _open = true; | 1252 | _open = true; |
1240 | _stats.clear(); | 1253 | _stats.clear(); |
1241 | 1254 | ||
1242 | // in case we have an application object, create a socket notifier | 1255 | // in case we have an application object, create a socket notifier |
1243 | if ( qApp ) //TODO: I don't like this here... | 1256 | if ( qApp ) //TODO: I don't like this here... |
1244 | { | 1257 | { |
1245 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); | 1258 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); |
1246 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); | 1259 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); |
1247 | } | 1260 | } |
1248 | 1261 | ||
1249 | return true; | 1262 | return true; |
1250 | } | 1263 | } |
1251 | 1264 | ||
1252 | 1265 | ||
1253 | bool OPacketCapturer::openDumpFile( const QString& filename ) | 1266 | bool OPacketCapturer::openDumpFile( const QString& filename ) |
1254 | { | 1267 | { |
1255 | pcap_dumper_t* dump = pcap_dump_open( _pch, const_cast<char*>( (const char*) filename ) ); | 1268 | pcap_dumper_t* dump = pcap_dump_open( _pch, const_cast<char*>( (const char*) filename ) ); |
1256 | if ( !dump ) | 1269 | if ( !dump ) |
1257 | { | 1270 | { |
1258 | owarn << "OPacketCapturer::open(): can't open dump with '" << filename << "': " << _errbuf << oendl; | 1271 | owarn << "OPacketCapturer::open(): can't open dump with '" << filename << "': " << _errbuf << oendl; |
1259 | return false; | 1272 | return false; |
1260 | } | 1273 | } |
1261 | odebug << "OPacketCapturer::open(): dump [" << filename << "] opened successfully." << oendl; | 1274 | odebug << "OPacketCapturer::open(): dump [" << filename << "] opened successfully." << oendl; |
1262 | _pcd = dump; | 1275 | _pcd = dump; |
1263 | 1276 | ||
1264 | return true; | 1277 | return true; |
1265 | } | 1278 | } |
1266 | 1279 | ||
1267 | 1280 | ||
1268 | bool OPacketCapturer::open( const QFile& file ) | 1281 | bool OPacketCapturer::open( const QFile& file ) |
1269 | { | 1282 | { |
1270 | QString name = file.name(); | 1283 | QString name = file.name(); |
1271 | 1284 | ||
1272 | if ( _open ) | 1285 | if ( _open ) |
1273 | { | 1286 | { |
1274 | close(); | 1287 | close(); |
1275 | if ( name == _name ) // ignore opening an already openend device | 1288 | if ( name == _name ) // ignore opening an already openend device |
1276 | { | 1289 | { |
1277 | return true; | 1290 | return true; |
1278 | } | 1291 | } |
1279 | else // close the last opened device | 1292 | else // close the last opened device |
1280 | { | 1293 | { |
1281 | close(); | 1294 | close(); |
1282 | } | 1295 | } |
1283 | } | 1296 | } |
1284 | 1297 | ||
1285 | _name = name; | 1298 | _name = name; |
1286 | 1299 | ||
1287 | pcap_t* handle = pcap_open_offline( const_cast<char*>( (const char*) name ), &_errbuf[0] ); | 1300 | pcap_t* handle = pcap_open_offline( const_cast<char*>( (const char*) name ), &_errbuf[0] ); |
1288 | 1301 | ||
1289 | if ( handle ) | 1302 | if ( handle ) |
1290 | { | 1303 | { |
1291 | odebug << "OPacketCapturer::open(): libpcap opened successfully." << oendl; | 1304 | odebug << "OPacketCapturer::open(): libpcap opened successfully." << oendl; |
1292 | _pch = handle; | 1305 | _pch = handle; |
1293 | _open = true; | 1306 | _open = true; |
1294 | 1307 | ||
1295 | // in case we have an application object, create a socket notifier | 1308 | // in case we have an application object, create a socket notifier |
1296 | if ( qApp ) | 1309 | if ( qApp ) |
1297 | { | 1310 | { |
1298 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); | 1311 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); |
1299 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); | 1312 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); |
1300 | } | 1313 | } |
1301 | 1314 | ||
1302 | return true; | 1315 | return true; |
1303 | } | 1316 | } |
1304 | else | 1317 | else |
1305 | { | 1318 | { |
1306 | odebug << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl; | 1319 | odebug << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl; |
1307 | return false; | 1320 | return false; |
1308 | } | 1321 | } |
1309 | 1322 | ||
1310 | } | 1323 | } |
1311 | 1324 | ||
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 ); |
1338 | } | 1351 | } |
1339 | 1352 | ||
1340 | 1353 | ||
1341 | bool OPacketCapturer::swapped() const | 1354 | bool OPacketCapturer::swapped() const |
1342 | { | 1355 | { |
1343 | return pcap_is_swapped( _pch ); | 1356 | return pcap_is_swapped( _pch ); |
1344 | } | 1357 | } |
1345 | 1358 | ||
1346 | 1359 | ||
1347 | QString OPacketCapturer::version() const | 1360 | QString OPacketCapturer::version() const |
1348 | { | 1361 | { |
1349 | return QString().sprintf( "%d.%d", pcap_major_version( _pch ), pcap_minor_version( _pch ) ); | 1362 | return QString().sprintf( "%d.%d", pcap_major_version( _pch ), pcap_minor_version( _pch ) ); |
1350 | } | 1363 | } |
1351 | 1364 | ||
1352 | } | 1365 | } |
1353 | } | 1366 | } |
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 | |||
@@ -41,195 +41,194 @@ | |||
41 | #include <qobject.h> | 41 | #include <qobject.h> |
42 | #include <qstring.h> | 42 | #include <qstring.h> |
43 | #include <qtextstream.h> | 43 | #include <qtextstream.h> |
44 | #include <qmap.h> | 44 | #include <qmap.h> |
45 | 45 | ||
46 | /* STD */ | 46 | /* STD */ |
47 | extern "C" // work around a bpf/pcap conflict in recent headers | 47 | extern "C" // work around a bpf/pcap conflict in recent headers |
48 | { | 48 | { |
49 | #include <pcap.h> | 49 | #include <pcap.h> |
50 | } | 50 | } |
51 | #include <netinet/ether.h> | 51 | #include <netinet/ether.h> |
52 | #include <netinet/ip.h> | 52 | #include <netinet/ip.h> |
53 | #include <netinet/udp.h> | 53 | #include <netinet/udp.h> |
54 | #include <netinet/tcp.h> | 54 | #include <netinet/tcp.h> |
55 | #include <time.h> | 55 | #include <time.h> |
56 | 56 | ||
57 | /* Custom Network Includes (must go here, don't reorder!) */ | 57 | /* Custom Network Includes (must go here, don't reorder!) */ |
58 | #include "802_11_user.h" | 58 | #include "802_11_user.h" |
59 | #include "dhcp.h" | 59 | #include "dhcp.h" |
60 | 60 | ||
61 | 61 | ||
62 | /* TYPEDEFS */ | 62 | /* TYPEDEFS */ |
63 | typedef struct timeval timevalstruct; | 63 | typedef struct timeval timevalstruct; |
64 | typedef struct pcap_pkthdr packetheaderstruct; | 64 | typedef struct pcap_pkthdr packetheaderstruct; |
65 | 65 | ||
66 | /* FORWARDS */ | 66 | /* FORWARDS */ |
67 | class QSocketNotifier; | 67 | class QSocketNotifier; |
68 | namespace Opie { | 68 | namespace Opie { |
69 | namespace Net { | 69 | namespace Net { |
70 | class OPacketCapturer; | 70 | class OPacketCapturer; |
71 | 71 | ||
72 | /*====================================================================================== | 72 | /*====================================================================================== |
73 | * OPacket - A frame on the wire | 73 | * OPacket - A frame on the wire |
74 | *======================================================================================*/ | 74 | *======================================================================================*/ |
75 | 75 | ||
76 | /** @brief A class representing a data frame on the wire. | 76 | /** @brief A class representing a data frame on the wire. |
77 | * | 77 | * |
78 | * The whole family of the packet classes are used when capturing frames from a network. | 78 | * The whole family of the packet classes are used when capturing frames from a network. |
79 | * Most standard network protocols in use share a common architecture, which mostly is | 79 | * Most standard network protocols in use share a common architecture, which mostly is |
80 | * a packet header and then the packet payload. In layered architectures, each lower layer | 80 | * a packet header and then the packet payload. In layered architectures, each lower layer |
81 | * encapsulates data from its upper layer - that is it | 81 | * encapsulates data from its upper layer - that is it |
82 | * treats the data from its upper layer as payload and prepends an own header to the packet, | 82 | * treats the data from its upper layer as payload and prepends an own header to the packet, |
83 | * which - again - is treated as the payload for the layer below. The figure below is an | 83 | * which - again - is treated as the payload for the layer below. The figure below is an |
84 | * example for how such a data frame is composed out of packets, e.g. when sending a mail. | 84 | * example for how such a data frame is composed out of packets, e.g. when sending a mail. |
85 | * | 85 | * |
86 | * <pre> | 86 | * <pre> |
87 | * | User Data | == Mail Data | 87 | * | User Data | == Mail Data |
88 | * | SMTP Header | User Data | == SMTP | 88 | * | SMTP Header | User Data | == SMTP |
89 | * | TCP Header | SMTP Header | User Data | == TCP | 89 | * | TCP Header | SMTP Header | User Data | == TCP |
90 | * | IP Header | TCP Header | SMTP Header | User Data | == IP | 90 | * | IP Header | TCP Header | SMTP Header | User Data | == IP |
91 | * | MAC Header | IP Header | TCP Header | SMTP Header | User Data | == MAC | 91 | * | MAC Header | IP Header | TCP Header | SMTP Header | User Data | == MAC |
92 | * | 92 | * |
93 | * </pre> | 93 | * </pre> |
94 | * | 94 | * |
95 | * The example is trimmed for simplicity, because the MAC (Medium Access Control) layer | 95 | * The example is trimmed for simplicity, because the MAC (Medium Access Control) layer |
96 | * also contains a few more levels of encapsulation. | 96 | * also contains a few more levels of encapsulation. |
97 | * Since the type of the payload is more or less independent from the encapsulating protocol, | 97 | * Since the type of the payload is more or less independent from the encapsulating protocol, |
98 | * the header must be inspected before attempting to decode the payload. Hence, the | 98 | * the header must be inspected before attempting to decode the payload. Hence, the |
99 | * encapsulation level varies and can't be deduced without actually looking into the packets. | 99 | * encapsulation level varies and can't be deduced without actually looking into the packets. |
100 | * | 100 | * |
101 | * For actually working with captured frames, it's useful to identify the packets via names and | 101 | * For actually working with captured frames, it's useful to identify the packets via names and |
102 | * insert them into a parent/child - relationship based on the encapsulation. This is why | 102 | * insert them into a parent/child - relationship based on the encapsulation. This is why |
103 | * all packet classes derive from QObject. The amount of overhead caused by the QObject is | 103 | * all packet classes derive from QObject. The amount of overhead caused by the QObject is |
104 | * not a problem in this case, because we're talking about a theoratical maximum of about | 104 | * not a problem in this case, because we're talking about a theoratical maximum of about |
105 | * 10 packets per captured frame. We need to stuff them into a searchable list anyway and the | 105 | * 10 packets per captured frame. We need to stuff them into a searchable list anyway and the |
106 | * QObject also cares about destroying the sub-, (child-) packets. | 106 | * QObject also cares about destroying the sub-, (child-) packets. |
107 | * | 107 | * |
108 | * This enables us to perform a simple look for packets of a certain type: | 108 | * This enables us to perform a simple look for packets of a certain type: |
109 | * @code | 109 | * @code |
110 | * OPacketCapturer* pcap = new OPacketCapturer(); | 110 | * OPacketCapturer* pcap = new OPacketCapturer(); |
111 | * pcap->open( "eth0" ); | 111 | * pcap->open( "eth0" ); |
112 | * OPacket* p = pcap->next(); | 112 | * OPacket* p = pcap->next(); |
113 | * OIPPacket* ip = (OIPPacket*) p->child( "IP" ); // returns 0, if no such child exists | 113 | * OIPPacket* ip = (OIPPacket*) p->child( "IP" ); // returns 0, if no such child exists |
114 | * odebug << "got ip packet from " << ip->fromIPAddress().toString() << " to " << ip->toIPAddress().toString() << oendl; | 114 | * odebug << "got ip packet from " << ip->fromIPAddress().toString() << " to " << ip->toIPAddress().toString() << oendl; |
115 | * | 115 | * |
116 | */ | 116 | */ |
117 | 117 | ||
118 | class OPacket : public QObject | 118 | class OPacket : public QObject |
119 | { | 119 | { |
120 | Q_OBJECT | 120 | Q_OBJECT |
121 | 121 | ||
122 | friend class OPacketCapturer; | 122 | friend class OPacketCapturer; |
123 | friend QTextStream& operator<<( QTextStream& s, const OPacket& p ); | 123 | friend QTextStream& operator<<( QTextStream& s, const OPacket& p ); |
124 | 124 | ||
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 ); |
152 | 151 | ||
153 | /*====================================================================================== | 152 | /*====================================================================================== |
154 | * OEthernetPacket - DLT_EN10MB frame | 153 | * OEthernetPacket - DLT_EN10MB frame |
155 | *======================================================================================*/ | 154 | *======================================================================================*/ |
156 | 155 | ||
157 | class OEthernetPacket : public QObject | 156 | class OEthernetPacket : public QObject |
158 | { | 157 | { |
159 | Q_OBJECT | 158 | Q_OBJECT |
160 | 159 | ||
161 | public: | 160 | public: |
162 | OEthernetPacket( const unsigned char*, const struct ether_header*, QObject* parent = 0 ); | 161 | OEthernetPacket( const unsigned char*, const struct ether_header*, QObject* parent = 0 ); |
163 | virtual ~OEthernetPacket(); | 162 | virtual ~OEthernetPacket(); |
164 | 163 | ||
165 | OMacAddress sourceAddress() const; | 164 | OMacAddress sourceAddress() const; |
166 | OMacAddress destinationAddress() const; | 165 | OMacAddress destinationAddress() const; |
167 | int type() const; | 166 | int type() const; |
168 | 167 | ||
169 | private: | 168 | private: |
170 | const struct ether_header* _ether; | 169 | const struct ether_header* _ether; |
171 | private: | 170 | private: |
172 | class Private; | 171 | class Private; |
173 | Private *d; | 172 | Private *d; |
174 | }; | 173 | }; |
175 | 174 | ||
176 | /*====================================================================================== | 175 | /*====================================================================================== |
177 | * OPrismHeaderPacket - DLT_PRISM_HEADER frame | 176 | * OPrismHeaderPacket - DLT_PRISM_HEADER frame |
178 | *======================================================================================*/ | 177 | *======================================================================================*/ |
179 | 178 | ||
180 | class OPrismHeaderPacket : public QObject | 179 | class OPrismHeaderPacket : public QObject |
181 | { | 180 | { |
182 | Q_OBJECT | 181 | Q_OBJECT |
183 | 182 | ||
184 | public: | 183 | public: |
185 | OPrismHeaderPacket( const unsigned char*, const struct prism_hdr*, QObject* parent = 0 ); | 184 | OPrismHeaderPacket( const unsigned char*, const struct prism_hdr*, QObject* parent = 0 ); |
186 | virtual ~OPrismHeaderPacket(); | 185 | virtual ~OPrismHeaderPacket(); |
187 | 186 | ||
188 | unsigned int signalStrength() const; | 187 | unsigned int signalStrength() const; |
189 | 188 | ||
190 | private: | 189 | private: |
191 | const struct prism_hdr* _header; | 190 | const struct prism_hdr* _header; |
192 | class Private; | 191 | class Private; |
193 | Private *d; | 192 | Private *d; |
194 | }; | 193 | }; |
195 | 194 | ||
196 | /*====================================================================================== | 195 | /*====================================================================================== |
197 | * OWaveLanPacket - DLT_IEEE802_11 frame | 196 | * OWaveLanPacket - DLT_IEEE802_11 frame |
198 | *======================================================================================*/ | 197 | *======================================================================================*/ |
199 | 198 | ||
200 | class OWaveLanPacket : public QObject | 199 | class OWaveLanPacket : public QObject |
201 | { | 200 | { |
202 | Q_OBJECT | 201 | Q_OBJECT |
203 | 202 | ||
204 | public: | 203 | public: |
205 | OWaveLanPacket( const unsigned char*, const struct ieee_802_11_header*, QObject* parent = 0 ); | 204 | OWaveLanPacket( const unsigned char*, const struct ieee_802_11_header*, QObject* parent = 0 ); |
206 | virtual ~OWaveLanPacket(); | 205 | virtual ~OWaveLanPacket(); |
207 | 206 | ||
208 | int duration() const; | 207 | int duration() const; |
209 | bool fromDS() const; | 208 | bool fromDS() const; |
210 | bool toDS() const; | 209 | bool toDS() const; |
211 | virtual OMacAddress macAddress1() const; | 210 | virtual OMacAddress macAddress1() const; |
212 | virtual OMacAddress macAddress2() const; | 211 | virtual OMacAddress macAddress2() const; |
213 | virtual OMacAddress macAddress3() const; | 212 | virtual OMacAddress macAddress3() const; |
214 | virtual OMacAddress macAddress4() const; | 213 | virtual OMacAddress macAddress4() const; |
215 | bool usesPowerManagement() const; | 214 | bool usesPowerManagement() const; |
216 | int type() const; | 215 | int type() const; |
217 | int subType() const; | 216 | int subType() const; |
218 | int version() const; | 217 | int version() const; |
219 | bool usesWep() const; | 218 | bool usesWep() const; |
220 | 219 | ||
221 | private: | 220 | private: |
222 | const struct ieee_802_11_header* _wlanhdr; | 221 | const struct ieee_802_11_header* _wlanhdr; |
223 | class Private; | 222 | class Private; |
224 | Private *d; | 223 | Private *d; |
225 | }; | 224 | }; |
226 | 225 | ||
227 | 226 | ||
228 | /*====================================================================================== | 227 | /*====================================================================================== |
229 | * OWaveLanManagementPacket - type: management (T_MGMT) | 228 | * OWaveLanManagementPacket - type: management (T_MGMT) |
230 | *======================================================================================*/ | 229 | *======================================================================================*/ |
231 | 230 | ||
232 | class OWaveLanManagementPacket : public QObject | 231 | class OWaveLanManagementPacket : public QObject |
233 | { | 232 | { |
234 | Q_OBJECT | 233 | Q_OBJECT |
235 | 234 | ||
@@ -600,122 +599,135 @@ class OTCPPacket : public QObject | |||
600 | 599 | ||
601 | /** | 600 | /** |
602 | * @brief A class based wrapper for network packet capturing. | 601 | * @brief A class based wrapper for network packet capturing. |
603 | * | 602 | * |
604 | * This class is the base of a high-level interface to the well known packet capturing | 603 | * This class is the base of a high-level interface to the well known packet capturing |
605 | * library libpcap. | 604 | * library libpcap. |
606 | * @see http://tcpdump.org | 605 | * @see http://tcpdump.org |
607 | */ | 606 | */ |
608 | class OPacketCapturer : public QObject | 607 | class OPacketCapturer : public QObject |
609 | { | 608 | { |
610 | Q_OBJECT | 609 | Q_OBJECT |
611 | 610 | ||
612 | public: | 611 | public: |
613 | /** | 612 | /** |
614 | * Constructor. | 613 | * Constructor. |
615 | */ | 614 | */ |
616 | OPacketCapturer( QObject* parent = 0, const char* name = 0 ); | 615 | OPacketCapturer( QObject* parent = 0, const char* name = 0 ); |
617 | /** | 616 | /** |
618 | * Destructor. | 617 | * Destructor. |
619 | */ | 618 | */ |
620 | ~OPacketCapturer(); | 619 | ~OPacketCapturer(); |
621 | /** | 620 | /** |
622 | * Set the packet capturer to use blocking or non-blocking IO. This can be useful when | 621 | * Set the packet capturer to use blocking or non-blocking IO. This can be useful when |
623 | * not using the socket notifier, e.g. without an application object. | 622 | * not using the socket notifier, e.g. without an application object. |
624 | */ | 623 | */ |
625 | void setBlocking( bool ); | 624 | void setBlocking( bool ); |
626 | /** | 625 | /** |
627 | * @returns true if the packet capturer uses blocking IO calls. | 626 | * @returns true if the packet capturer uses blocking IO calls. |
628 | */ | 627 | */ |
629 | bool blocking() const; | 628 | bool blocking() const; |
630 | /** | 629 | /** |
631 | * Close the packet capturer. This is automatically done in the destructor. | 630 | * Close the packet capturer. This is automatically done in the destructor. |
632 | */ | 631 | */ |
633 | void close(); | 632 | void close(); |
634 | /** | 633 | /** |
635 | * Close the output capture file. | 634 | * Close the output capture file. |
636 | */ | 635 | */ |
637 | void closeDumpFile(); | 636 | void closeDumpFile(); |
638 | /** | 637 | /** |
639 | * @returns the data link type. | 638 | * @returns the data link type. |
640 | * @see <pcap.h> for possible values. | 639 | * @see <pcap.h> for possible values. |
641 | */ | 640 | */ |
642 | int dataLink() const; | 641 | int dataLink() const; |
643 | /** | 642 | /** |
644 | * Dump a packet to the output capture file. | 643 | * Dump a packet to the output capture file. |
645 | */ | 644 | */ |
646 | void dump( OPacket* ); | 645 | void dump( OPacket* ); |
647 | /** | 646 | /** |
648 | * @returns the file descriptor of the packet capturer. This is only useful, if | 647 | * @returns the file descriptor of the packet capturer. This is only useful, if |
649 | * not using the socket notifier, e.g. without an application object. | 648 | * not using the socket notifier, e.g. without an application object. |
650 | */ | 649 | */ |
651 | int fileno() const; | 650 | int fileno() const; |
652 | /** | 651 | /** |
653 | * @returns the next @ref OPacket from the packet capturer. | 652 | * @returns the next @ref OPacket from the packet capturer. |
654 | * @note If blocking mode is true then this call might block. | 653 | * @note If blocking mode is true then this call might block. |
655 | */ | 654 | */ |
656 | OPacket* next(); | 655 | OPacket* next(); |
657 | /** | 656 | /** |
658 | * @returns the next @ref OPacket from the packet capturer, if | 657 | * @returns the next @ref OPacket from the packet capturer, if |
659 | * one arrives within @a time milliseconds. | 658 | * one arrives within @a time milliseconds. |
660 | */ | 659 | */ |
661 | OPacket* next( int time ); | 660 | OPacket* next( int time ); |
662 | /** | 661 | /** |
663 | * Open the packet capturer to capture packets in live-mode from @a interface. | 662 | * Open the packet capturer to capture packets in live-mode from @a interface. |
664 | */ | 663 | */ |
665 | bool open( const QString& interface ); | 664 | bool open( const QString& interface ); |
666 | /** | 665 | /** |
667 | * Open the packet capturer to capture packets in offline-mode from @a file. | 666 | * Open the packet capturer to capture packets in offline-mode from @a file. |
668 | */ | 667 | */ |
669 | bool open( const QFile& file ); | 668 | bool open( const QFile& file ); |
670 | /** | 669 | /** |
671 | * Open a prerecorded tcpdump compatible capture file for use with @ref dump() | 670 | * Open a prerecorded tcpdump compatible capture file for use with @ref dump() |
672 | */ | 671 | */ |
673 | bool openDumpFile( const QString& filename ); | 672 | bool openDumpFile( const QString& filename ); |
674 | /** | 673 | /** |
675 | * @returns true if the packet capturer is open | 674 | * @returns true if the packet capturer is open |
676 | */ | 675 | */ |
677 | bool isOpen() const; | 676 | bool isOpen() const; |
678 | /** | 677 | /** |
679 | * @returns the snapshot length of this packet capturer | 678 | * @returns the snapshot length of this packet capturer |
680 | */ | 679 | */ |
681 | int snapShot() const; | 680 | int snapShot() const; |
682 | /** | 681 | /** |
683 | * @returns true if the input capture file has a different byte-order | 682 | * @returns true if the input capture file has a different byte-order |
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 | ||