summaryrefslogtreecommitdiff
path: root/libopie2
authormickeyl <mickeyl>2004-05-02 19:17:27 (UTC)
committer mickeyl <mickeyl>2004-05-02 19:17:27 (UTC)
commit8602449caa5a055bd5f033e3792d0a59a0b41bfa (patch) (unidiff)
tree17f60677ff09e89eb270f1e6cc837687b0eab871 /libopie2
parent3e8fab5fddb3a38d93eaca1a8d69d8f668c7cd99 (diff)
downloadopie-8602449caa5a055bd5f033e3792d0a59a0b41bfa.zip
opie-8602449caa5a055bd5f033e3792d0a59a0b41bfa.tar.gz
opie-8602449caa5a055bd5f033e3792d0a59a0b41bfa.tar.bz2
dump takes less space now
NOTE: I think dump really doesn't belong here
Diffstat (limited to 'libopie2') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opienet/opcap.cpp6
1 files changed, 2 insertions, 4 deletions
diff --git a/libopie2/opienet/opcap.cpp b/libopie2/opienet/opcap.cpp
index 8ec4f47..8184f21 100644
--- a/libopie2/opienet/opcap.cpp
+++ b/libopie2/opienet/opcap.cpp
@@ -1,1369 +1,1367 @@
1/* 1/*
2                 This file is part of the Opie Project 2                 This file is part of the Opie Project
3              Copyright (C) 2003-2004 by Michael 'Mickey' Lauer <mickey@Vanille.de> 3              Copyright (C) 2003-2004 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#include "udp_ports.h" 31#include "udp_ports.h"
32#include "opcap.h" 32#include "opcap.h"
33 33
34/* OPIE */ 34/* OPIE */
35#include <opie2/odebug.h> 35#include <opie2/odebug.h>
36using namespace Opie::Core; 36using namespace Opie::Core;
37 37
38/* QT */ 38/* QT */
39#include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects) 39#include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects)
40#include <qsocketnotifier.h> 40#include <qsocketnotifier.h>
41#include <qobjectlist.h> 41#include <qobjectlist.h>
42 42
43/* STD */ 43/* STD */
44#include <sys/time.h> 44#include <sys/time.h>
45#include <sys/types.h> 45#include <sys/types.h>
46#include <assert.h> 46#include <assert.h>
47#include <unistd.h> 47#include <unistd.h>
48#include <string.h> 48#include <string.h>
49 49
50namespace Opie { 50namespace Opie {
51namespace Net { 51namespace Net {
52 52
53/*====================================================================================== 53/*======================================================================================
54 * OPacket 54 * OPacket
55 *======================================================================================*/ 55 *======================================================================================*/
56 56
57OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent ) 57OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent )
58 :QObject( parent, "Generic" ), _hdr( header ), _data( 0 ) 58 :QObject( parent, "Generic" ), _hdr( header ), _data( 0 )
59{ 59{
60 60
61 _data = new unsigned char[ header.len ]; 61 _data = new unsigned char[ header.len ];
62 assert( _data ); 62 assert( _data );
63 memcpy( const_cast<unsigned char*>(_data), data, header.len ); 63 memcpy( const_cast<unsigned char*>(_data), data, header.len );
64 // We have to copy the data structure here, because the 'data' pointer handed by libpcap 64 // We have to copy the data structure here, because the 'data' pointer handed by libpcap
65 // points to an internal region which is reused by lipcap. 65 // points to an internal region which is reused by lipcap.
66 odebug << "OPacket: Length = " << header.len << ", Caplen = " << header.caplen << oendl; 66 odebug << "OPacket: Length = " << header.len << ", Caplen = " << header.caplen << oendl;
67 _end = (unsigned char*) _data + header.len; 67 _end = (unsigned char*) _data + header.len;
68 68
69 switch ( datalink ) 69 switch ( datalink )
70 { 70 {
71 case DLT_EN10MB: 71 case DLT_EN10MB:
72 odebug << "OPacket::OPacket(): Received Packet. Datalink = ETHERNET" << oendl; 72 odebug << "OPacket::OPacket(): Received Packet. Datalink = ETHERNET" << oendl;
73 new OEthernetPacket( _end, (const struct ether_header*) _data, this ); 73 new OEthernetPacket( _end, (const struct ether_header*) _data, this );
74 break; 74 break;
75 75
76 case DLT_IEEE802_11: 76 case DLT_IEEE802_11:
77 odebug << "OPacket::OPacket(): Received Packet. Datalink = IEEE802.11" << oendl; 77 odebug << "OPacket::OPacket(): Received Packet. Datalink = IEEE802.11" << oendl;
78 new OWaveLanPacket( _end, (const struct ieee_802_11_header*) _data, this ); 78 new OWaveLanPacket( _end, (const struct ieee_802_11_header*) _data, this );
79 break; 79 break;
80 80
81 case DLT_PRISM_HEADER: 81 case DLT_PRISM_HEADER:
82 odebug << "OPacket::OPacket(): Received Packet. Datalink = PRISM_HEADER" << oendl; 82 odebug << "OPacket::OPacket(): Received Packet. Datalink = PRISM_HEADER" << oendl;
83 new OPrismHeaderPacket( _end, (const struct prism_hdr*) (unsigned char*) _data, this ); 83 new OPrismHeaderPacket( _end, (const struct prism_hdr*) (unsigned char*) _data, this );
84 break; 84 break;
85 85
86 default: 86 default:
87 owarn << "OPacket::OPacket(): Received Packet over unsupported datalink, type " << datalink << "!" << oendl; 87 owarn << "OPacket::OPacket(): Received Packet over unsupported datalink, type " << datalink << "!" << oendl;
88 } 88 }
89} 89}
90 90
91 91
92OPacket::~OPacket() 92OPacket::~OPacket()
93{ 93{
94 odebug << "~OPacket( " << name() << " )" << oendl; 94 odebug << "~OPacket( " << name() << " )" << oendl;
95 delete _data; 95 delete _data;
96} 96}
97 97
98 98
99timevalstruct OPacket::timeval() const 99timevalstruct OPacket::timeval() const
100{ 100{
101 return _hdr.ts; 101 return _hdr.ts;
102} 102}
103 103
104 104
105int OPacket::caplen() const 105int OPacket::caplen() const
106{ 106{
107 return _hdr.caplen; 107 return _hdr.caplen;
108} 108}
109 109
110 110
111void OPacket::updateStats( QMap<QString,int>& stats, QObjectList* l ) 111void OPacket::updateStats( QMap<QString,int>& stats, QObjectList* l )
112{ 112{
113 if (!l) return; 113 if (!l) return;
114 QObject* o = l->first(); 114 QObject* o = l->first();
115 while ( o ) 115 while ( o )
116 { 116 {
117 stats[o->name()]++; 117 stats[o->name()]++;
118 updateStats( stats, const_cast<QObjectList*>( o->children() ) ); 118 updateStats( stats, const_cast<QObjectList*>( o->children() ) );
119 o = l->next(); 119 o = l->next();
120 } 120 }
121} 121}
122 122
123 123
124QString OPacket::dumpStructure() const 124QString OPacket::dumpStructure() const
125{ 125{
126 return "[ |" + _dumpStructure( const_cast<QObjectList*>( this->children() ) ) + " ]"; 126 return "[ |" + _dumpStructure( const_cast<QObjectList*>( this->children() ) ) + " ]";
127} 127}
128 128
129 129
130QString OPacket::_dumpStructure( QObjectList* l ) const 130QString OPacket::_dumpStructure( QObjectList* l ) const
131{ 131{
132 if (!l) return QString::null; 132 if (!l) return QString::null;
133 QObject* o = l->first(); 133 QObject* o = l->first();
134 QString str(" "); 134 QString str(" ");
135 135
136 while ( o ) 136 while ( o )
137 { 137 {
138 str.append( o->name() ); 138 str.append( o->name() );
139 str.append( " |" ); 139 str.append( " |" );
140 str += _dumpStructure( const_cast<QObjectList*>( o->children() ) ); 140 str += _dumpStructure( const_cast<QObjectList*>( o->children() ) );
141 o = l->next(); 141 o = l->next();
142 } 142 }
143 return str; 143 return str;
144} 144}
145 145
146QString OPacket::dump( int bpl ) const 146QString OPacket::dump( int bpl ) const
147{ 147{
148 static int index = 0; 148 static int index = 0;
149 index++; 149 index++;
150 int len = _hdr.caplen; 150 int len = _hdr.caplen;
151 QString str; 151 QString str( "000:" );
152 str.sprintf( "\n<----- Packet #%04d Len = 0x%X (%d) ----->\n\n", index, len, len );
153 str.append( "0000: " );
154 QString tmp; 152 QString tmp;
155 QString bytes; 153 QString bytes;
156 QString chars; 154 QString chars;
157 155
158 for ( int i = 0; i < len; ++i ) 156 for ( int i = 0; i < len; ++i )
159 { 157 {
160 tmp.sprintf( "%02X ", _data[i] ); bytes.append( tmp ); 158 tmp.sprintf( "%02X ", _data[i] ); bytes.append( tmp );
161 if ( (_data[i] > 31) && (_data[i]<128) ) chars.append( _data[i] ); 159 if ( (_data[i] > 31) && (_data[i]<128) ) chars.append( _data[i] );
162 else chars.append( '.' ); 160 else chars.append( '.' );
163 161
164 if ( !((i+1) % bpl) ) 162 if ( !((i+1) % bpl) )
165 { 163 {
166 str.append( bytes ); 164 str.append( bytes );
167 str.append( ' ' ); 165 str.append( ' ' );
168 str.append( chars ); 166 str.append( chars );
169 str.append( '\n' ); 167 str.append( '\n' );
170 tmp.sprintf( "%04X: ", i+1 ); str.append( tmp ); 168 tmp.sprintf( "%03X:", i+1 ); str.append( tmp );
171 bytes = ""; 169 bytes = "";
172 chars = ""; 170 chars = "";
173 } 171 }
174 172
175 } 173 }
176 if ( (len % bpl) ) 174 if ( (len % bpl) )
177 { 175 {
178 str.append( bytes.leftJustify( 1 + 3*bpl ) ); 176 str.append( bytes.leftJustify( 1 + 3*bpl ) );
179 str.append( chars ); 177 str.append( chars );
180 } 178 }
181 str.append( '\n' ); 179 str.append( '\n' );
182 return str; 180 return str;
183} 181}
184 182
185 183
186int OPacket::len() const 184int OPacket::len() const
187{ 185{
188 return _hdr.len; 186 return _hdr.len;
189} 187}
190 188
191 189
192QTextStream& operator<<( QTextStream& s, const OPacket& p ) 190QTextStream& operator<<( QTextStream& s, const OPacket& p )
193{ 191{
194 s << p.dumpStructure(); 192 s << p.dumpStructure();
195} 193}
196 194
197 195
198/*====================================================================================== 196/*======================================================================================
199 * OEthernetPacket 197 * OEthernetPacket
200 *======================================================================================*/ 198 *======================================================================================*/
201 199
202OEthernetPacket::OEthernetPacket( const unsigned char* end, const struct ether_header* data, QObject* parent ) 200OEthernetPacket::OEthernetPacket( const unsigned char* end, const struct ether_header* data, QObject* parent )
203 :QObject( parent, "Ethernet" ), _ether( data ) 201 :QObject( parent, "Ethernet" ), _ether( data )
204{ 202{
205 203
206 odebug << "Source = " << sourceAddress().toString() << oendl; 204 odebug << "Source = " << sourceAddress().toString() << oendl;
207 odebug << "Destination = " << destinationAddress().toString() << oendl; 205 odebug << "Destination = " << destinationAddress().toString() << oendl;
208 206
209 if ( sourceAddress() == OMacAddress::broadcast ) 207 if ( sourceAddress() == OMacAddress::broadcast )
210 odebug << "Source is broadcast address" << oendl; 208 odebug << "Source is broadcast address" << oendl;
211 if ( destinationAddress() == OMacAddress::broadcast ) 209 if ( destinationAddress() == OMacAddress::broadcast )
212 odebug << "Destination is broadcast address" << oendl; 210 odebug << "Destination is broadcast address" << oendl;
213 211
214 switch ( type() ) 212 switch ( type() )
215 { 213 {
216 case ETHERTYPE_IP: new OIPPacket( end, (const struct iphdr*) (data+1), this ); break; 214 case ETHERTYPE_IP: new OIPPacket( end, (const struct iphdr*) (data+1), this ); break;
217 case ETHERTYPE_ARP: new OARPPacket( end, (const struct myarphdr*) (data+1), this ); break; 215 case ETHERTYPE_ARP: new OARPPacket( end, (const struct myarphdr*) (data+1), this ); break;
218 case ETHERTYPE_REVARP: { odebug << "OPacket::OPacket(): Received Ethernet Packet : Type = RARP" << oendl; break; } 216 case ETHERTYPE_REVARP: { odebug << "OPacket::OPacket(): Received Ethernet Packet : Type = RARP" << oendl; break; }
219 default: odebug << "OPacket::OPacket(): Received Ethernet Packet : Type = UNKNOWN" << oendl; 217 default: odebug << "OPacket::OPacket(): Received Ethernet Packet : Type = UNKNOWN" << oendl;
220 } 218 }
221 219
222} 220}
223 221
224 222
225OEthernetPacket::~OEthernetPacket() 223OEthernetPacket::~OEthernetPacket()
226{ 224{
227} 225}
228 226
229 227
230OMacAddress OEthernetPacket::sourceAddress() const 228OMacAddress OEthernetPacket::sourceAddress() const
231{ 229{
232 return OMacAddress( _ether->ether_shost ); 230 return OMacAddress( _ether->ether_shost );
233} 231}
234 232
235 233
236OMacAddress OEthernetPacket::destinationAddress() const 234OMacAddress OEthernetPacket::destinationAddress() const
237{ 235{
238 return OMacAddress( _ether->ether_dhost ); 236 return OMacAddress( _ether->ether_dhost );
239} 237}
240 238
241int OEthernetPacket::type() const 239int OEthernetPacket::type() const
242{ 240{
243 return ntohs( _ether->ether_type ); 241 return ntohs( _ether->ether_type );
244} 242}
245 243
246 244
247/*====================================================================================== 245/*======================================================================================
248 * OIPPacket 246 * OIPPacket
249 *======================================================================================*/ 247 *======================================================================================*/
250 248
251 249
252OIPPacket::OIPPacket( const unsigned char* end, const struct iphdr* data, QObject* parent ) 250OIPPacket::OIPPacket( const unsigned char* end, const struct iphdr* data, QObject* parent )
253 :QObject( parent, "IP" ), _iphdr( data ) 251 :QObject( parent, "IP" ), _iphdr( data )
254{ 252{
255 odebug << "OIPPacket::OIPPacket(): decoding IP header..." << oendl; 253 odebug << "OIPPacket::OIPPacket(): decoding IP header..." << oendl;
256 254
257 odebug << "FromAddress = " << fromIPAddress().toString() << oendl; 255 odebug << "FromAddress = " << fromIPAddress().toString() << oendl;
258 odebug << " toAddress = " << toIPAddress().toString() << oendl; 256 odebug << " toAddress = " << toIPAddress().toString() << oendl;
259 257
260 switch ( protocol() ) 258 switch ( protocol() )
261 { 259 {
262 case IPPROTO_UDP: new OUDPPacket( end, (const struct udphdr*) (data+1), this ); break; 260 case IPPROTO_UDP: new OUDPPacket( end, (const struct udphdr*) (data+1), this ); break;
263 case IPPROTO_TCP: new OTCPPacket( end, (const struct tcphdr*) (data+1), this ); break; 261 case IPPROTO_TCP: new OTCPPacket( end, (const struct tcphdr*) (data+1), this ); break;
264 default: odebug << "OIPPacket::OIPPacket(): unknown IP protocol, type = " << protocol() << oendl; 262 default: odebug << "OIPPacket::OIPPacket(): unknown IP protocol, type = " << protocol() << oendl;
265 } 263 }
266 264
267} 265}
268 266
269OIPPacket::~OIPPacket() 267OIPPacket::~OIPPacket()
270{ 268{
271} 269}
272 270
273 271
274QHostAddress OIPPacket::fromIPAddress() const 272QHostAddress OIPPacket::fromIPAddress() const
275{ 273{
276 return EXTRACT_32BITS( &_iphdr->saddr ); 274 return EXTRACT_32BITS( &_iphdr->saddr );
277} 275}
278 276
279 277
280QHostAddress OIPPacket::toIPAddress() const 278QHostAddress OIPPacket::toIPAddress() const
281{ 279{
282 return EXTRACT_32BITS( &_iphdr->saddr ); 280 return EXTRACT_32BITS( &_iphdr->saddr );
283} 281}
284 282
285 283
286int OIPPacket::tos() const 284int OIPPacket::tos() const
287{ 285{
288 return _iphdr->tos; 286 return _iphdr->tos;
289} 287}
290 288
291 289
292int OIPPacket::len() const 290int OIPPacket::len() const
293{ 291{
294 return EXTRACT_16BITS( &_iphdr->tot_len ); 292 return EXTRACT_16BITS( &_iphdr->tot_len );
295} 293}
296 294
297 295
298int OIPPacket::id() const 296int OIPPacket::id() const
299{ 297{
300 return EXTRACT_16BITS( &_iphdr->id ); 298 return EXTRACT_16BITS( &_iphdr->id );
301} 299}
302 300
303 301
304int OIPPacket::offset() const 302int OIPPacket::offset() const
305{ 303{
306 return EXTRACT_16BITS( &_iphdr->frag_off ); 304 return EXTRACT_16BITS( &_iphdr->frag_off );
307} 305}
308 306
309 307
310int OIPPacket::ttl() const 308int OIPPacket::ttl() const
311{ 309{
312 return _iphdr->ttl; 310 return _iphdr->ttl;
313} 311}
314 312
315 313
316int OIPPacket::protocol() const 314int OIPPacket::protocol() const
317{ 315{
318 return _iphdr->protocol; 316 return _iphdr->protocol;
319} 317}
320 318
321 319
322int OIPPacket::checksum() const 320int OIPPacket::checksum() const
323{ 321{
324 return EXTRACT_16BITS( &_iphdr->check ); 322 return EXTRACT_16BITS( &_iphdr->check );
325} 323}
326 324
327/*====================================================================================== 325/*======================================================================================
328 * OARPPacket 326 * OARPPacket
329 *======================================================================================*/ 327 *======================================================================================*/
330 328
331 329
332OARPPacket::OARPPacket( const unsigned char* end, const struct myarphdr* data, QObject* parent ) 330OARPPacket::OARPPacket( const unsigned char* end, const struct myarphdr* data, QObject* parent )
333 :QObject( parent, "ARP" ), _arphdr( data ) 331 :QObject( parent, "ARP" ), _arphdr( data )
334{ 332{
335 odebug << "OARPPacket::OARPPacket(): decoding ARP header..." << oendl; 333 odebug << "OARPPacket::OARPPacket(): decoding ARP header..." << oendl;
336 odebug << "ARP type seems to be " << EXTRACT_16BITS( &_arphdr->ar_op ) << " = " << type() << oendl; 334 odebug << "ARP type seems to be " << EXTRACT_16BITS( &_arphdr->ar_op ) << " = " << type() << oendl;
337 odebug << "Sender: MAC " << senderMacAddress().toString() << " = IP " << senderIPV4Address().toString() << oendl; 335 odebug << "Sender: MAC " << senderMacAddress().toString() << " = IP " << senderIPV4Address().toString() << oendl;
338 odebug << "Target: MAC " << targetMacAddress().toString() << " = IP " << targetIPV4Address().toString() << oendl; 336 odebug << "Target: MAC " << targetMacAddress().toString() << " = IP " << targetIPV4Address().toString() << oendl;
339} 337}
340 338
341 339
342OARPPacket::~OARPPacket() 340OARPPacket::~OARPPacket()
343{ 341{
344} 342}
345 343
346 344
347QString OARPPacket::type() const 345QString OARPPacket::type() const
348{ 346{
349 switch ( EXTRACT_16BITS( &_arphdr->ar_op ) ) 347 switch ( EXTRACT_16BITS( &_arphdr->ar_op ) )
350 { 348 {
351 case 1: return "REQUEST"; 349 case 1: return "REQUEST";
352 case 2: return "REPLY"; 350 case 2: return "REPLY";
353 case 3: return "RREQUEST"; 351 case 3: return "RREQUEST";
354 case 4: return "RREPLY"; 352 case 4: return "RREPLY";
355 case 8: return "InREQUEST"; 353 case 8: return "InREQUEST";
356 case 9: return "InREPLY"; 354 case 9: return "InREPLY";
357 case 10: return "NAK"; 355 case 10: return "NAK";
358 default: owarn << "OARPPacket::type(): invalid ARP type!" << oendl; return "<unknown>"; 356 default: owarn << "OARPPacket::type(): invalid ARP type!" << oendl; return "<unknown>";
359 } 357 }
360} 358}
361 359
362 360
363QHostAddress OARPPacket::senderIPV4Address() const 361QHostAddress OARPPacket::senderIPV4Address() const
364{ 362{
365 return EXTRACT_32BITS( &_arphdr->ar_sip ); 363 return EXTRACT_32BITS( &_arphdr->ar_sip );
366} 364}
367 365
368 366
369QHostAddress OARPPacket::targetIPV4Address() const 367QHostAddress OARPPacket::targetIPV4Address() const
370{ 368{
371 return EXTRACT_32BITS( &_arphdr->ar_tip ); 369 return EXTRACT_32BITS( &_arphdr->ar_tip );
372} 370}
373 371
374 372
375OMacAddress OARPPacket::senderMacAddress() const 373OMacAddress OARPPacket::senderMacAddress() const
376{ 374{
377 return OMacAddress( _arphdr->ar_sha ); 375 return OMacAddress( _arphdr->ar_sha );
378} 376}
379 377
380 378
381OMacAddress OARPPacket::targetMacAddress() const 379OMacAddress OARPPacket::targetMacAddress() const
382{ 380{
383 return OMacAddress( _arphdr->ar_tha ); 381 return OMacAddress( _arphdr->ar_tha );
384} 382}
385 383
386 384
387/*====================================================================================== 385/*======================================================================================
388 * OUDPPacket 386 * OUDPPacket
389 *======================================================================================*/ 387 *======================================================================================*/
390 388
391 389
392OUDPPacket::OUDPPacket( const unsigned char* end, const struct udphdr* data, QObject* parent ) 390OUDPPacket::OUDPPacket( const unsigned char* end, const struct udphdr* data, QObject* parent )
393 :QObject( parent, "UDP" ), _udphdr( data ) 391 :QObject( parent, "UDP" ), _udphdr( data )
394 392
395{ 393{
396 odebug << "OUDPPacket::OUDPPacket(): decoding UDP header..." << oendl; 394 odebug << "OUDPPacket::OUDPPacket(): decoding UDP header..." << oendl;
397 odebug << "fromPort = " << fromPort() << oendl; 395 odebug << "fromPort = " << fromPort() << oendl;
398 odebug << " toPort = " << toPort() << oendl; 396 odebug << " toPort = " << toPort() << oendl;
399 397
400 // TODO: Make this a case or a hash if we know more udp protocols 398 // TODO: Make this a case or a hash if we know more udp protocols
401 399
402 if ( fromPort() == UDP_PORT_BOOTPS || fromPort() == UDP_PORT_BOOTPC || 400 if ( fromPort() == UDP_PORT_BOOTPS || fromPort() == UDP_PORT_BOOTPC ||
403 toPort() == UDP_PORT_BOOTPS || toPort() == UDP_PORT_BOOTPC ) 401 toPort() == UDP_PORT_BOOTPS || toPort() == UDP_PORT_BOOTPC )
404 { 402 {
405 odebug << "seems to be part of a DHCP conversation => creating DHCP packet." << oendl; 403 odebug << "seems to be part of a DHCP conversation => creating DHCP packet." << oendl;
406 new ODHCPPacket( end, (const struct dhcp_packet*) (data+1), this ); 404 new ODHCPPacket( end, (const struct dhcp_packet*) (data+1), this );
407 } 405 }
408} 406}
409 407
410 408
411OUDPPacket::~OUDPPacket() 409OUDPPacket::~OUDPPacket()
412{ 410{
413} 411}
414 412
415 413
416int OUDPPacket::fromPort() const 414int OUDPPacket::fromPort() const
417{ 415{
418 return EXTRACT_16BITS( &_udphdr->source ); 416 return EXTRACT_16BITS( &_udphdr->source );
419} 417}
420 418
421 419
422int OUDPPacket::toPort() const 420int OUDPPacket::toPort() const
423{ 421{
424 return EXTRACT_16BITS( &_udphdr->dest ); 422 return EXTRACT_16BITS( &_udphdr->dest );
425} 423}
426 424
427 425
428int OUDPPacket::length() const 426int OUDPPacket::length() const
429{ 427{
430 return EXTRACT_16BITS( &_udphdr->len ); 428 return EXTRACT_16BITS( &_udphdr->len );
431} 429}
432 430
433 431
434int OUDPPacket::checksum() const 432int OUDPPacket::checksum() const
435{ 433{
436 return EXTRACT_16BITS( &_udphdr->check ); 434 return EXTRACT_16BITS( &_udphdr->check );
437} 435}
438 436
439 437
440/*====================================================================================== 438/*======================================================================================
441 * ODHCPPacket 439 * ODHCPPacket
442 *======================================================================================*/ 440 *======================================================================================*/
443 441
444 442
445ODHCPPacket::ODHCPPacket( const unsigned char* end, const struct dhcp_packet* data, QObject* parent ) 443ODHCPPacket::ODHCPPacket( const unsigned char* end, const struct dhcp_packet* data, QObject* parent )
446 :QObject( parent, "DHCP" ), _dhcphdr( data ) 444 :QObject( parent, "DHCP" ), _dhcphdr( data )
447 445
448{ 446{
449 odebug << "ODHCPPacket::ODHCPPacket(): decoding DHCP information..." << oendl; 447 odebug << "ODHCPPacket::ODHCPPacket(): decoding DHCP information..." << oendl;
450 odebug << "DHCP opcode seems to be " << _dhcphdr->op << ": " << ( isRequest() ? "REQUEST" : "REPLY" ) << oendl; 448 odebug << "DHCP opcode seems to be " << _dhcphdr->op << ": " << ( isRequest() ? "REQUEST" : "REPLY" ) << oendl;
451 odebug << "clientAddress = " << clientAddress().toString() << oendl; 449 odebug << "clientAddress = " << clientAddress().toString() << oendl;
452 odebug << " yourAddress = " << yourAddress().toString() << oendl; 450 odebug << " yourAddress = " << yourAddress().toString() << oendl;
453 odebug << "serverAddress = " << serverAddress().toString() << oendl; 451 odebug << "serverAddress = " << serverAddress().toString() << oendl;
454 odebug << " relayAddress = " << relayAddress().toString() << oendl; 452 odebug << " relayAddress = " << relayAddress().toString() << oendl;
455 odebug << "parsing DHCP options..." << oendl; 453 odebug << "parsing DHCP options..." << oendl;
456 454
457 _type = 0; 455 _type = 0;
458 456
459 const unsigned char* option = &_dhcphdr->options[4]; 457 const unsigned char* option = &_dhcphdr->options[4];
460 char tag = -1; 458 char tag = -1;
461 char len = -1; 459 char len = -1;
462 460
463 while ( ( tag = *option++ ) != -1 /* end of option field */ ) 461 while ( ( tag = *option++ ) != -1 /* end of option field */ )
464 { 462 {
465 len = *option++; 463 len = *option++;
466 odebug << "recognized DHCP option #" << tag << ", length " << len << oendl; 464 odebug << "recognized DHCP option #" << tag << ", length " << len << oendl;
467 465
468 if ( tag == DHO_DHCP_MESSAGE_TYPE ) 466 if ( tag == DHO_DHCP_MESSAGE_TYPE )
469 _type = *option; 467 _type = *option;
470 468
471 option += len; 469 option += len;
472 if ( option >= end ) 470 if ( option >= end )
473 { 471 {
474 owarn << "DHCP parsing ERROR: sanity check says the packet is at its end!" << oendl; 472 owarn << "DHCP parsing ERROR: sanity check says the packet is at its end!" << oendl;
475 break; 473 break;
476 } 474 }
477 } 475 }
478 476
479 odebug << "DHCP type seems to be << " << type() << oendl; 477 odebug << "DHCP type seems to be << " << type() << oendl;
480} 478}
481 479
482 480
483ODHCPPacket::~ODHCPPacket() 481ODHCPPacket::~ODHCPPacket()
484{ 482{
485} 483}
486 484
487 485
488bool ODHCPPacket::isRequest() const 486bool ODHCPPacket::isRequest() const
489{ 487{
490 return ( _dhcphdr->op == 01 ); 488 return ( _dhcphdr->op == 01 );
491} 489}
492 490
493 491
494bool ODHCPPacket::isReply() const 492bool ODHCPPacket::isReply() const
495{ 493{
496 return ( _dhcphdr->op == 02 ); 494 return ( _dhcphdr->op == 02 );
497} 495}
498 496
499 497
500QString ODHCPPacket::type() const 498QString ODHCPPacket::type() const
501{ 499{
502 switch ( _type ) 500 switch ( _type )
503 { 501 {
504 case 1: return "DISCOVER"; 502 case 1: return "DISCOVER";
505 case 2: return "OFFER"; 503 case 2: return "OFFER";
506 case 3: return "REQUEST"; 504 case 3: return "REQUEST";
507 case 4: return "DECLINE"; 505 case 4: return "DECLINE";
508 case 5: return "ACK"; 506 case 5: return "ACK";
509 case 6: return "NAK"; 507 case 6: return "NAK";
510 case 7: return "RELEASE"; 508 case 7: return "RELEASE";
511 case 8: return "INFORM"; 509 case 8: return "INFORM";
512 default: owarn << "ODHCPPacket::type(): invalid DHCP type " << _dhcphdr->op << oendl; return "<unknown>"; 510 default: owarn << "ODHCPPacket::type(): invalid DHCP type " << _dhcphdr->op << oendl; return "<unknown>";
513 } 511 }
514} 512}
515 513
516 514
517QHostAddress ODHCPPacket::clientAddress() const 515QHostAddress ODHCPPacket::clientAddress() const
518{ 516{
519 return EXTRACT_32BITS( &_dhcphdr->ciaddr ); 517 return EXTRACT_32BITS( &_dhcphdr->ciaddr );
520} 518}
521 519
522 520
523QHostAddress ODHCPPacket::yourAddress() const 521QHostAddress ODHCPPacket::yourAddress() const
524{ 522{
525 return EXTRACT_32BITS( &_dhcphdr->yiaddr ); 523 return EXTRACT_32BITS( &_dhcphdr->yiaddr );
526} 524}
527 525
528 526
529QHostAddress ODHCPPacket::serverAddress() const 527QHostAddress ODHCPPacket::serverAddress() const
530{ 528{
531 return EXTRACT_32BITS( &_dhcphdr->siaddr ); 529 return EXTRACT_32BITS( &_dhcphdr->siaddr );
532} 530}
533 531
534 532
535QHostAddress ODHCPPacket::relayAddress() const 533QHostAddress ODHCPPacket::relayAddress() const
536{ 534{
537 return EXTRACT_32BITS( &_dhcphdr->giaddr ); 535 return EXTRACT_32BITS( &_dhcphdr->giaddr );
538} 536}
539 537
540 538
541OMacAddress ODHCPPacket::clientMacAddress() const 539OMacAddress ODHCPPacket::clientMacAddress() const
542{ 540{
543 return OMacAddress( _dhcphdr->chaddr ); 541 return OMacAddress( _dhcphdr->chaddr );
544} 542}
545 543
546 544
547/*====================================================================================== 545/*======================================================================================
548 * OTCPPacket 546 * OTCPPacket
549 *======================================================================================*/ 547 *======================================================================================*/
550 548
551 549
552OTCPPacket::OTCPPacket( const unsigned char* end, const struct tcphdr* data, QObject* parent ) 550OTCPPacket::OTCPPacket( const unsigned char* end, const struct tcphdr* data, QObject* parent )
553 :QObject( parent, "TCP" ), _tcphdr( data ) 551 :QObject( parent, "TCP" ), _tcphdr( data )
554 552
555{ 553{
556 odebug << "OTCPPacket::OTCPPacket(): decoding TCP header..." << oendl; 554 odebug << "OTCPPacket::OTCPPacket(): decoding TCP header..." << oendl;
557} 555}
558 556
559 557
560OTCPPacket::~OTCPPacket() 558OTCPPacket::~OTCPPacket()
561{ 559{
562} 560}
563 561
564 562
565int OTCPPacket::fromPort() const 563int OTCPPacket::fromPort() const
566{ 564{
567 return EXTRACT_16BITS( &_tcphdr->source ); 565 return EXTRACT_16BITS( &_tcphdr->source );
568} 566}
569 567
570 568
571int OTCPPacket::toPort() const 569int OTCPPacket::toPort() const
572{ 570{
573 return EXTRACT_16BITS( &_tcphdr->dest ); 571 return EXTRACT_16BITS( &_tcphdr->dest );
574} 572}
575 573
576 574
577int OTCPPacket::seq() const 575int OTCPPacket::seq() const
578{ 576{
579 return EXTRACT_16BITS( &_tcphdr->seq ); 577 return EXTRACT_16BITS( &_tcphdr->seq );
580} 578}
581 579
582 580
583int OTCPPacket::ack() const 581int OTCPPacket::ack() const
584{ 582{
585 return EXTRACT_16BITS( &_tcphdr->ack_seq ); 583 return EXTRACT_16BITS( &_tcphdr->ack_seq );
586} 584}
587 585
588 586
589int OTCPPacket::window() const 587int OTCPPacket::window() const
590{ 588{
591 return EXTRACT_16BITS( &_tcphdr->window ); 589 return EXTRACT_16BITS( &_tcphdr->window );
592} 590}
593 591
594 592
595int OTCPPacket::checksum() const 593int OTCPPacket::checksum() const
596{ 594{
597 return EXTRACT_16BITS( &_tcphdr->check ); 595 return EXTRACT_16BITS( &_tcphdr->check );
598} 596}
599 597
600/*====================================================================================== 598/*======================================================================================
601 * OPrismHeaderPacket 599 * OPrismHeaderPacket
602 *======================================================================================*/ 600 *======================================================================================*/
603 601
604 602
605OPrismHeaderPacket::OPrismHeaderPacket( const unsigned char* end, const struct prism_hdr* data, QObject* parent ) 603OPrismHeaderPacket::OPrismHeaderPacket( const unsigned char* end, const struct prism_hdr* data, QObject* parent )
606 :QObject( parent, "Prism" ), _header( data ) 604 :QObject( parent, "Prism" ), _header( data )
607 605
608{ 606{
609 odebug << "OPrismHeaderPacket::OPrismHeaderPacket(): decoding PRISM header..." << oendl; 607 odebug << "OPrismHeaderPacket::OPrismHeaderPacket(): decoding PRISM header..." << oendl;
610 608
611 odebug << "Signal Strength = " << data->signal.data << oendl; 609 odebug << "Signal Strength = " << data->signal.data << oendl;
612 610
613 new OWaveLanPacket( end, (const struct ieee_802_11_header*) (data+1), this ); 611 new OWaveLanPacket( end, (const struct ieee_802_11_header*) (data+1), this );
614} 612}
615 613
616OPrismHeaderPacket::~OPrismHeaderPacket() 614OPrismHeaderPacket::~OPrismHeaderPacket()
617{ 615{
618} 616}
619 617
620 618
621unsigned int OPrismHeaderPacket::signalStrength() const 619unsigned int OPrismHeaderPacket::signalStrength() const
622{ 620{
623 return _header->signal.data; 621 return _header->signal.data;
624} 622}
625 623
626/*====================================================================================== 624/*======================================================================================
627 * OWaveLanPacket 625 * OWaveLanPacket
628 *======================================================================================*/ 626 *======================================================================================*/
629 627
630 628
631OWaveLanPacket::OWaveLanPacket( const unsigned char* end, const struct ieee_802_11_header* data, QObject* parent ) 629OWaveLanPacket::OWaveLanPacket( const unsigned char* end, const struct ieee_802_11_header* data, QObject* parent )
632 :QObject( parent, "802.11" ), _wlanhdr( data ) 630 :QObject( parent, "802.11" ), _wlanhdr( data )
633 631
634{ 632{
635 odebug << "OWaveLanPacket::OWaveLanPacket(): decoding IEEE 802.11 header..." << oendl; 633 odebug << "OWaveLanPacket::OWaveLanPacket(): decoding IEEE 802.11 header..." << oendl;
636 odebug << "type = " << type() << oendl; 634 odebug << "type = " << type() << oendl;
637 odebug << "subType = " << subType() << oendl; 635 odebug << "subType = " << subType() << oendl;
638 odebug << "duration = " << duration() << oendl; 636 odebug << "duration = " << duration() << oendl;
639 odebug << "powermanagement = " << usesPowerManagement() << oendl; 637 odebug << "powermanagement = " << usesPowerManagement() << oendl;
640 odebug << "payload is encrypted = " << ( usesWep() ? "yes" : "no" ) << oendl; 638 odebug << "payload is encrypted = " << ( usesWep() ? "yes" : "no" ) << oendl;
641 odebug << "MAC1 = " << macAddress1().toString() << oendl; 639 odebug << "MAC1 = " << macAddress1().toString() << oendl;
642 odebug << "MAC2 = " << macAddress2().toString() << oendl; 640 odebug << "MAC2 = " << macAddress2().toString() << oendl;
643 odebug << "MAC3 = " << macAddress3().toString() << oendl; 641 odebug << "MAC3 = " << macAddress3().toString() << oendl;
644 odebug << "MAC4 = " << macAddress4().toString() << oendl; 642 odebug << "MAC4 = " << macAddress4().toString() << oendl;
645 643
646 switch ( type() ) 644 switch ( type() )
647 { 645 {
648 case T_MGMT: new OWaveLanManagementPacket( end, (const struct ieee_802_11_mgmt_header*) data, this ); break; 646 case T_MGMT: new OWaveLanManagementPacket( end, (const struct ieee_802_11_mgmt_header*) data, this ); break;
649 case T_DATA: new OWaveLanDataPacket( end, (const struct ieee_802_11_data_header*) data, this ); break; 647 case T_DATA: new OWaveLanDataPacket( end, (const struct ieee_802_11_data_header*) data, this ); break;
650 case T_CTRL: new OWaveLanControlPacket( end, (const struct ieee_802_11_control_header*) data, this ); break; 648 case T_CTRL: new OWaveLanControlPacket( end, (const struct ieee_802_11_control_header*) data, this ); break;
651 default: odebug << "OWaveLanPacket::OWaveLanPacket(): Warning: Unknown major type = " << type() << oendl; 649 default: odebug << "OWaveLanPacket::OWaveLanPacket(): Warning: Unknown major type = " << type() << oendl;
652 } 650 }
653} 651}
654 652
655OWaveLanPacket::~OWaveLanPacket() 653OWaveLanPacket::~OWaveLanPacket()
656{ 654{
657} 655}
658 656
659 657
660int OWaveLanPacket::duration() const 658int OWaveLanPacket::duration() const
661{ 659{
662 return _wlanhdr->duration; 660 return _wlanhdr->duration;
663} 661}
664 662
665 663
666OMacAddress OWaveLanPacket::macAddress1() const 664OMacAddress OWaveLanPacket::macAddress1() const
667{ 665{
668 return OMacAddress( _wlanhdr->mac1 ); 666 return OMacAddress( _wlanhdr->mac1 );
669} 667}
670 668
671 669
672OMacAddress OWaveLanPacket::macAddress2() const 670OMacAddress OWaveLanPacket::macAddress2() const
673{ 671{
674 return OMacAddress( _wlanhdr->mac2 ); 672 return OMacAddress( _wlanhdr->mac2 );
675} 673}
676 674
677 675
678OMacAddress OWaveLanPacket::macAddress3() const 676OMacAddress OWaveLanPacket::macAddress3() const
679{ 677{
680 return OMacAddress( _wlanhdr->mac3 ); 678 return OMacAddress( _wlanhdr->mac3 );
681} 679}
682 680
683 681
684OMacAddress OWaveLanPacket::macAddress4() const 682OMacAddress OWaveLanPacket::macAddress4() const
685{ 683{
686 return OMacAddress( _wlanhdr->mac4 ); 684 return OMacAddress( _wlanhdr->mac4 );
687} 685}
688 686
689 687
690int OWaveLanPacket::subType() const 688int OWaveLanPacket::subType() const
691{ 689{
692 return FC_SUBTYPE( EXTRACT_LE_16BITS( &_wlanhdr->frame_control ) ); 690 return FC_SUBTYPE( EXTRACT_LE_16BITS( &_wlanhdr->frame_control ) );
693} 691}
694 692
695 693
696int OWaveLanPacket::type() const 694int OWaveLanPacket::type() const
697{ 695{
698 return FC_TYPE( EXTRACT_LE_16BITS( &_wlanhdr->frame_control ) ); 696 return FC_TYPE( EXTRACT_LE_16BITS( &_wlanhdr->frame_control ) );
699} 697}
700 698
701 699
702int OWaveLanPacket::version() const 700int OWaveLanPacket::version() const
703{ 701{
704 return FC_VERSION( EXTRACT_LE_16BITS( &_wlanhdr->frame_control ) ); 702 return FC_VERSION( EXTRACT_LE_16BITS( &_wlanhdr->frame_control ) );
705} 703}
706 704
707 705
708bool OWaveLanPacket::fromDS() const 706bool OWaveLanPacket::fromDS() const
709{ 707{
710 return FC_FROM_DS( EXTRACT_LE_16BITS( &_wlanhdr->frame_control ) ); 708 return FC_FROM_DS( EXTRACT_LE_16BITS( &_wlanhdr->frame_control ) );
711} 709}
712 710
713 711
714bool OWaveLanPacket::toDS() const 712bool OWaveLanPacket::toDS() const
715{ 713{
716 return FC_TO_DS( EXTRACT_LE_16BITS( &_wlanhdr->frame_control ) ); 714 return FC_TO_DS( EXTRACT_LE_16BITS( &_wlanhdr->frame_control ) );
717} 715}
718 716
719 717
720bool OWaveLanPacket::usesPowerManagement() const 718bool OWaveLanPacket::usesPowerManagement() const
721{ 719{
722 return FC_POWER_MGMT( EXTRACT_LE_16BITS( &_wlanhdr->frame_control ) ); 720 return FC_POWER_MGMT( EXTRACT_LE_16BITS( &_wlanhdr->frame_control ) );
723} 721}
724 722
725 723
726bool OWaveLanPacket::usesWep() const 724bool OWaveLanPacket::usesWep() const
727{ 725{
728 return FC_WEP( EXTRACT_LE_16BITS( &_wlanhdr->frame_control ) ); 726 return FC_WEP( EXTRACT_LE_16BITS( &_wlanhdr->frame_control ) );
729} 727}
730 728
731 729
732/*====================================================================================== 730/*======================================================================================
733 * OWaveLanManagementPacket 731 * OWaveLanManagementPacket
734 *======================================================================================*/ 732 *======================================================================================*/
735 733
736OWaveLanManagementPacket::OWaveLanManagementPacket( const unsigned char* end, const struct ieee_802_11_mgmt_header* data, OWaveLanPacket* parent ) 734OWaveLanManagementPacket::OWaveLanManagementPacket( const unsigned char* end, const struct ieee_802_11_mgmt_header* data, OWaveLanPacket* parent )
737 :QObject( parent, "802.11 Management" ), _header( data ), 735 :QObject( parent, "802.11 Management" ), _header( data ),
738 _body( (const struct ieee_802_11_mgmt_body*) (data+1) ) 736 _body( (const struct ieee_802_11_mgmt_body*) (data+1) )
739{ 737{
740 odebug << "OWaveLanManagementPacket::OWaveLanManagementPacket(): decoding frame..." << oendl; 738 odebug << "OWaveLanManagementPacket::OWaveLanManagementPacket(): decoding frame..." << oendl;
741 odebug << "Detected subtype is " << managementType() << oendl; 739 odebug << "Detected subtype is " << managementType() << oendl;
742 740
743 // Grab tagged values. 741 // Grab tagged values.
744 // Beacons contain a 12 byte long fixed parameters set before the tagged parameters come, 742 // Beacons contain a 12 byte long fixed parameters set before the tagged parameters come,
745 // Other management frames don't - which is why we have to inspect the subtype here. 743 // Other management frames don't - which is why we have to inspect the subtype here.
746 744
747 const unsigned char* ptr = managementType() == "Beacon" ? (const unsigned char*) (_body+1) : (const unsigned char*) (_header+1); 745 const unsigned char* ptr = managementType() == "Beacon" ? (const unsigned char*) (_body+1) : (const unsigned char*) (_header+1);
748 746
749 while (ptr < end) 747 while (ptr < end)
750 { 748 {
751 switch ( *ptr ) 749 switch ( *ptr )
752 { 750 {
753 case E_SSID: new OWaveLanManagementSSID( end, (struct ssid_t*) ptr, this ); break; 751 case E_SSID: new OWaveLanManagementSSID( end, (struct ssid_t*) ptr, this ); break;
754 case E_FH: new OWaveLanManagementFH( end, (struct fh_t*) ptr, this ); break; 752 case E_FH: new OWaveLanManagementFH( end, (struct fh_t*) ptr, this ); break;
755 case E_DS: new OWaveLanManagementDS( end, (struct ds_t*) ptr, this ); break; 753 case E_DS: new OWaveLanManagementDS( end, (struct ds_t*) ptr, this ); break;
756 case E_RATES: new OWaveLanManagementRates( end, (struct rates_t*) ptr, this ); break; 754 case E_RATES: new OWaveLanManagementRates( end, (struct rates_t*) ptr, this ); break;
757 case E_CF: new OWaveLanManagementCF( end, (struct cf_t*) ptr, this ); break; 755 case E_CF: new OWaveLanManagementCF( end, (struct cf_t*) ptr, this ); break;
758 case E_TIM: new OWaveLanManagementTim( end, (struct tim_t*) ptr, this ); break; 756 case E_TIM: new OWaveLanManagementTim( end, (struct tim_t*) ptr, this ); break;
759 case E_IBSS: new OWaveLanManagementIBSS( end, (struct ibss_t*) ptr, this ); break; 757 case E_IBSS: new OWaveLanManagementIBSS( end, (struct ibss_t*) ptr, this ); break;
760 case E_CHALLENGE: new OWaveLanManagementChallenge( end, (struct challenge_t*) ptr, this ); break; 758 case E_CHALLENGE: new OWaveLanManagementChallenge( end, (struct challenge_t*) ptr, this ); break;
761 } 759 }
762 ptr+= ( ( struct ssid_t* ) ptr )->length; // skip length of tagged value 760 ptr+= ( ( struct ssid_t* ) ptr )->length; // skip length of tagged value
763 ptr+= 2; // skip tag ID and length 761 ptr+= 2; // skip tag ID and length
764 } 762 }
765} 763}
766 764
767 765
768OWaveLanManagementPacket::~OWaveLanManagementPacket() 766OWaveLanManagementPacket::~OWaveLanManagementPacket()
769{ 767{
770} 768}
771 769
772 770
773QString OWaveLanManagementPacket::managementType() const 771QString OWaveLanManagementPacket::managementType() const
774{ 772{
775 switch ( FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) ) 773 switch ( FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) )
776 { 774 {
777 case ST_ASSOC_REQUEST: return "AssociationRequest"; break; 775 case ST_ASSOC_REQUEST: return "AssociationRequest"; break;
778 case ST_ASSOC_RESPONSE: return "AssociationResponse"; break; 776 case ST_ASSOC_RESPONSE: return "AssociationResponse"; break;
779 case ST_REASSOC_REQUEST: return "ReassociationRequest"; break; 777 case ST_REASSOC_REQUEST: return "ReassociationRequest"; break;
780 case ST_REASSOC_RESPONSE: return "ReassociationResponse"; break; 778 case ST_REASSOC_RESPONSE: return "ReassociationResponse"; break;
781 case ST_PROBE_REQUEST: return "ProbeRequest"; break; 779 case ST_PROBE_REQUEST: return "ProbeRequest"; break;
782 case ST_PROBE_RESPONSE: return "ProbeResponse"; break; 780 case ST_PROBE_RESPONSE: return "ProbeResponse"; break;
783 case ST_BEACON: return "Beacon"; break; 781 case ST_BEACON: return "Beacon"; break;
784 case ST_ATIM: return "Atim"; break; 782 case ST_ATIM: return "Atim"; break;
785 case ST_DISASSOC: return "Disassociation"; break; 783 case ST_DISASSOC: return "Disassociation"; break;
786 case ST_AUTH: return "Authentication"; break; 784 case ST_AUTH: return "Authentication"; break;
787 case ST_DEAUTH: return "Deathentication"; break; 785 case ST_DEAUTH: return "Deathentication"; break;
788 default: owarn << "OWaveLanManagementPacket::managementType(): unhandled subtype " << FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) << oendl; return "Unknown"; 786 default: owarn << "OWaveLanManagementPacket::managementType(): unhandled subtype " << FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) << oendl; return "Unknown";
789 } 787 }
790} 788}
791 789
792 790
793int OWaveLanManagementPacket::beaconInterval() const 791int OWaveLanManagementPacket::beaconInterval() const
794{ 792{
795 return EXTRACT_LE_16BITS( &_body->beacon_interval ); 793 return EXTRACT_LE_16BITS( &_body->beacon_interval );
796} 794}
797 795
798 796
799int OWaveLanManagementPacket::capabilities() const 797int OWaveLanManagementPacket::capabilities() const
800{ 798{
801 return EXTRACT_LE_16BITS( &_body->capability_info ); 799 return EXTRACT_LE_16BITS( &_body->capability_info );
802} 800}
803 801
804 802
805bool OWaveLanManagementPacket::canESS() const 803bool OWaveLanManagementPacket::canESS() const
806{ 804{
807 return CAPABILITY_ESS( EXTRACT_LE_16BITS( &_body->capability_info ) ); 805 return CAPABILITY_ESS( EXTRACT_LE_16BITS( &_body->capability_info ) );
808} 806}
809 807
810 808
811bool OWaveLanManagementPacket::canIBSS() const 809bool OWaveLanManagementPacket::canIBSS() const
812{ 810{
813 return CAPABILITY_IBSS( EXTRACT_LE_16BITS( &_body->capability_info ) ); 811 return CAPABILITY_IBSS( EXTRACT_LE_16BITS( &_body->capability_info ) );
814} 812}
815 813
816 814
817bool OWaveLanManagementPacket::canCFP() const 815bool OWaveLanManagementPacket::canCFP() const
818{ 816{
819 return CAPABILITY_CFP( EXTRACT_LE_16BITS( &_body->capability_info ) ); 817 return CAPABILITY_CFP( EXTRACT_LE_16BITS( &_body->capability_info ) );
820} 818}
821 819
822 820
823bool OWaveLanManagementPacket::canCFP_REQ() const 821bool OWaveLanManagementPacket::canCFP_REQ() const
824{ 822{
825 return CAPABILITY_CFP_REQ( EXTRACT_LE_16BITS( &_body->capability_info ) ); 823 return CAPABILITY_CFP_REQ( EXTRACT_LE_16BITS( &_body->capability_info ) );
826} 824}
827 825
828 826
829bool OWaveLanManagementPacket::canPrivacy() const 827bool OWaveLanManagementPacket::canPrivacy() const
830{ 828{
831 return CAPABILITY_PRIVACY( EXTRACT_LE_16BITS( &_body->capability_info ) ); 829 return CAPABILITY_PRIVACY( EXTRACT_LE_16BITS( &_body->capability_info ) );
832} 830}
833 831
834 832
835/*====================================================================================== 833/*======================================================================================
836 * OWaveLanManagementSSID 834 * OWaveLanManagementSSID
837 *======================================================================================*/ 835 *======================================================================================*/
838 836
839OWaveLanManagementSSID::OWaveLanManagementSSID( const unsigned char* end, const struct ssid_t* data, QObject* parent ) 837OWaveLanManagementSSID::OWaveLanManagementSSID( const unsigned char* end, const struct ssid_t* data, QObject* parent )
840 :QObject( parent, "802.11 SSID" ), _data( data ) 838 :QObject( parent, "802.11 SSID" ), _data( data )
841{ 839{
842 odebug << "OWaveLanManagementSSID()" << oendl; 840 odebug << "OWaveLanManagementSSID()" << oendl;
843} 841}
844 842
845 843
846OWaveLanManagementSSID::~OWaveLanManagementSSID() 844OWaveLanManagementSSID::~OWaveLanManagementSSID()
847{ 845{
848} 846}
849 847
850 848
851QString OWaveLanManagementSSID::ID( bool decloak ) const 849QString OWaveLanManagementSSID::ID( bool decloak ) const
852{ 850{
853 int length = _data->length; 851 int length = _data->length;
854 if ( length > 32 ) length = 32; 852 if ( length > 32 ) length = 32;
855 char essid[length+1]; 853 char essid[length+1];
856 memcpy( &essid, &_data->ssid, length ); 854 memcpy( &essid, &_data->ssid, length );
857 essid[length] = 0x0; 855 essid[length] = 0x0;
858 if ( !decloak || length < 2 || essid[0] != '\0' ) return essid; 856 if ( !decloak || length < 2 || essid[0] != '\0' ) return essid;
859 odebug << "OWaveLanManagementSSID:ID(): SSID is cloaked - decloaking..." << oendl; 857 odebug << "OWaveLanManagementSSID:ID(): SSID is cloaked - decloaking..." << oendl;
860 858
861 QString decloakedID; 859 QString decloakedID;
862 for ( int i = 1; i < length; ++i ) 860 for ( int i = 1; i < length; ++i )
863 { 861 {
864 if ( essid[i] >= 32 && essid[i] <= 126 ) decloakedID.append( essid[i] ); 862 if ( essid[i] >= 32 && essid[i] <= 126 ) decloakedID.append( essid[i] );
865 else decloakedID.append( '.' ); 863 else decloakedID.append( '.' );
866 } 864 }
867 return decloakedID; 865 return decloakedID;
868} 866}
869 867
870 868
871/*====================================================================================== 869/*======================================================================================
872 * OWaveLanManagementRates 870 * OWaveLanManagementRates
873 *======================================================================================*/ 871 *======================================================================================*/
874 872
875OWaveLanManagementRates::OWaveLanManagementRates( const unsigned char* end, const struct rates_t* data, QObject* parent ) 873OWaveLanManagementRates::OWaveLanManagementRates( const unsigned char* end, const struct rates_t* data, QObject* parent )
876 :QObject( parent, "802.11 Rates" ), _data( data ) 874 :QObject( parent, "802.11 Rates" ), _data( data )
877{ 875{
878 odebug << "OWaveLanManagementRates()" << oendl; 876 odebug << "OWaveLanManagementRates()" << oendl;
879} 877}
880 878
881 879
882OWaveLanManagementRates::~OWaveLanManagementRates() 880OWaveLanManagementRates::~OWaveLanManagementRates()
883{ 881{
884} 882}
885 883
886/*====================================================================================== 884/*======================================================================================
887 * OWaveLanManagementCF 885 * OWaveLanManagementCF
888 *======================================================================================*/ 886 *======================================================================================*/
889 887
890OWaveLanManagementCF::OWaveLanManagementCF( const unsigned char* end, const struct cf_t* data, QObject* parent ) 888OWaveLanManagementCF::OWaveLanManagementCF( const unsigned char* end, const struct cf_t* data, QObject* parent )
891 :QObject( parent, "802.11 CF" ), _data( data ) 889 :QObject( parent, "802.11 CF" ), _data( data )
892{ 890{
893 odebug << "OWaveLanManagementCF()" << oendl; 891 odebug << "OWaveLanManagementCF()" << oendl;
894} 892}
895 893
896 894
897OWaveLanManagementCF::~OWaveLanManagementCF() 895OWaveLanManagementCF::~OWaveLanManagementCF()
898{ 896{
899} 897}
900 898
901/*====================================================================================== 899/*======================================================================================
902 * OWaveLanManagementFH 900 * OWaveLanManagementFH
903 *======================================================================================*/ 901 *======================================================================================*/
904 902
905OWaveLanManagementFH::OWaveLanManagementFH( const unsigned char* end, const struct fh_t* data, QObject* parent ) 903OWaveLanManagementFH::OWaveLanManagementFH( const unsigned char* end, const struct fh_t* data, QObject* parent )
906 :QObject( parent, "802.11 FH" ), _data( data ) 904 :QObject( parent, "802.11 FH" ), _data( data )
907{ 905{
908 odebug << "OWaveLanManagementFH()" << oendl; 906 odebug << "OWaveLanManagementFH()" << oendl;
909} 907}
910 908
911 909
912OWaveLanManagementFH::~OWaveLanManagementFH() 910OWaveLanManagementFH::~OWaveLanManagementFH()
913{ 911{
914} 912}
915 913
916/*====================================================================================== 914/*======================================================================================
917 * OWaveLanManagementDS 915 * OWaveLanManagementDS
918 *======================================================================================*/ 916 *======================================================================================*/
919 917
920OWaveLanManagementDS::OWaveLanManagementDS( const unsigned char* end, const struct ds_t* data, QObject* parent ) 918OWaveLanManagementDS::OWaveLanManagementDS( const unsigned char* end, const struct ds_t* data, QObject* parent )
921 :QObject( parent, "802.11 DS" ), _data( data ) 919 :QObject( parent, "802.11 DS" ), _data( data )
922{ 920{
923 odebug << "OWaveLanManagementDS()" << oendl; 921 odebug << "OWaveLanManagementDS()" << oendl;
924} 922}
925 923
926 924
927OWaveLanManagementDS::~OWaveLanManagementDS() 925OWaveLanManagementDS::~OWaveLanManagementDS()
928{ 926{
929} 927}
930 928
931 929
932int OWaveLanManagementDS::channel() const 930int OWaveLanManagementDS::channel() const
933{ 931{
934 return _data->channel; 932 return _data->channel;
935} 933}
936 934
937/*====================================================================================== 935/*======================================================================================
938 * OWaveLanManagementTim 936 * OWaveLanManagementTim
939 *======================================================================================*/ 937 *======================================================================================*/
940 938
941OWaveLanManagementTim::OWaveLanManagementTim( const unsigned char* end, const struct tim_t* data, QObject* parent ) 939OWaveLanManagementTim::OWaveLanManagementTim( const unsigned char* end, const struct tim_t* data, QObject* parent )
942 :QObject( parent, "802.11 Tim" ), _data( data ) 940 :QObject( parent, "802.11 Tim" ), _data( data )
943{ 941{
944 odebug << "OWaveLanManagementTim()" << oendl; 942 odebug << "OWaveLanManagementTim()" << oendl;
945} 943}
946 944
947 945
948OWaveLanManagementTim::~OWaveLanManagementTim() 946OWaveLanManagementTim::~OWaveLanManagementTim()
949{ 947{
950} 948}
951 949
952/*====================================================================================== 950/*======================================================================================
953 * OWaveLanManagementIBSS 951 * OWaveLanManagementIBSS
954 *======================================================================================*/ 952 *======================================================================================*/
955 953
956OWaveLanManagementIBSS::OWaveLanManagementIBSS( const unsigned char* end, const struct ibss_t* data, QObject* parent ) 954OWaveLanManagementIBSS::OWaveLanManagementIBSS( const unsigned char* end, const struct ibss_t* data, QObject* parent )
957 :QObject( parent, "802.11 IBSS" ), _data( data ) 955 :QObject( parent, "802.11 IBSS" ), _data( data )
958{ 956{
959 odebug << "OWaveLanManagementIBSS()" << oendl; 957 odebug << "OWaveLanManagementIBSS()" << oendl;
960} 958}
961 959
962 960
963OWaveLanManagementIBSS::~OWaveLanManagementIBSS() 961OWaveLanManagementIBSS::~OWaveLanManagementIBSS()
964{ 962{
965} 963}
966 964
967/*====================================================================================== 965/*======================================================================================
968 * OWaveLanManagementChallenge 966 * OWaveLanManagementChallenge
969 *======================================================================================*/ 967 *======================================================================================*/
970 968
971OWaveLanManagementChallenge::OWaveLanManagementChallenge( const unsigned char* end, const struct challenge_t* data, QObject* parent ) 969OWaveLanManagementChallenge::OWaveLanManagementChallenge( const unsigned char* end, const struct challenge_t* data, QObject* parent )
972 :QObject( parent, "802.11 Challenge" ), _data( data ) 970 :QObject( parent, "802.11 Challenge" ), _data( data )
973{ 971{
974 odebug << "OWaveLanManagementChallenge()" << oendl; 972 odebug << "OWaveLanManagementChallenge()" << oendl;
975} 973}
976 974
977 975
978OWaveLanManagementChallenge::~OWaveLanManagementChallenge() 976OWaveLanManagementChallenge::~OWaveLanManagementChallenge()
979{ 977{
980} 978}
981 979
982/*====================================================================================== 980/*======================================================================================
983 * OWaveLanDataPacket 981 * OWaveLanDataPacket
984 *======================================================================================*/ 982 *======================================================================================*/
985 983
986OWaveLanDataPacket::OWaveLanDataPacket( const unsigned char* end, const struct ieee_802_11_data_header* data, OWaveLanPacket* parent ) 984OWaveLanDataPacket::OWaveLanDataPacket( const unsigned char* end, const struct ieee_802_11_data_header* data, OWaveLanPacket* parent )
987 :QObject( parent, "802.11 Data" ), _header( data ) 985 :QObject( parent, "802.11 Data" ), _header( data )
988{ 986{
989 odebug << "OWaveLanDataPacket::OWaveLanDataPacket(): decoding frame..." << oendl; 987 odebug << "OWaveLanDataPacket::OWaveLanDataPacket(): decoding frame..." << oendl;
990 988
991 const unsigned char* payload = (const unsigned char*) data + sizeof( struct ieee_802_11_data_header ); 989 const unsigned char* payload = (const unsigned char*) data + sizeof( struct ieee_802_11_data_header );
992 990
993 #warning The next line works for most cases, but can not be correct generally! 991 #warning The next line works for most cases, but can not be correct generally!
994 if (!( ( (OWaveLanPacket*) this->parent())->duration() )) payload -= 6; // compensation for missing last address 992 if (!( ( (OWaveLanPacket*) this->parent())->duration() )) payload -= 6; // compensation for missing last address
995 993
996 new OLLCPacket( end, (const struct ieee_802_11_802_2_header*) payload, this ); 994 new OLLCPacket( end, (const struct ieee_802_11_802_2_header*) payload, this );
997} 995}
998 996
999 997
1000OWaveLanDataPacket::~OWaveLanDataPacket() 998OWaveLanDataPacket::~OWaveLanDataPacket()
1001{ 999{
1002} 1000}
1003 1001
1004 1002
1005/*====================================================================================== 1003/*======================================================================================
1006 * OLLCPacket 1004 * OLLCPacket
1007 *======================================================================================*/ 1005 *======================================================================================*/
1008 1006
1009OLLCPacket::OLLCPacket( const unsigned char* end, const struct ieee_802_11_802_2_header* data, QObject* parent ) 1007OLLCPacket::OLLCPacket( const unsigned char* end, const struct ieee_802_11_802_2_header* data, QObject* parent )
1010 :QObject( parent, "802.11 LLC" ), _header( data ) 1008 :QObject( parent, "802.11 LLC" ), _header( data )
1011{ 1009{
1012 odebug << "OLLCPacket::OLLCPacket(): decoding frame..." << oendl; 1010 odebug << "OLLCPacket::OLLCPacket(): decoding frame..." << oendl;
1013 1011
1014 if ( !(_header->oui[0] || _header->oui[1] || _header->oui[2]) ) 1012 if ( !(_header->oui[0] || _header->oui[1] || _header->oui[2]) )
1015 { 1013 {
1016 owarn << "OLLCPacket::OLLCPacket(): contains an encapsulated Ethernet frame (type = " << EXTRACT_16BITS( &_header->type ) << ")" << oendl; 1014 owarn << "OLLCPacket::OLLCPacket(): contains an encapsulated Ethernet frame (type = " << EXTRACT_16BITS( &_header->type ) << ")" << oendl;
1017 1015
1018 switch ( EXTRACT_16BITS( &_header->type ) ) // defined in linux/if_ether.h 1016 switch ( EXTRACT_16BITS( &_header->type ) ) // defined in linux/if_ether.h
1019 { 1017 {
1020 case ETH_P_IP: new OIPPacket( end, (const struct iphdr*) (data+1), this ); break; 1018 case ETH_P_IP: new OIPPacket( end, (const struct iphdr*) (data+1), this ); break;
1021 case ETH_P_ARP: new OARPPacket( end, (const struct myarphdr*) (data+1), this ); break; 1019 case ETH_P_ARP: new OARPPacket( end, (const struct myarphdr*) (data+1), this ); break;
1022 default: owarn << "OLLCPacket::OLLCPacket(): Unknown Encapsulation type = " << EXTRACT_16BITS( &_header->type ) << oendl; 1020 default: owarn << "OLLCPacket::OLLCPacket(): Unknown Encapsulation type = " << EXTRACT_16BITS( &_header->type ) << oendl;
1023 } 1021 }
1024 } 1022 }
1025} 1023}
1026 1024
1027 1025
1028OLLCPacket::~OLLCPacket() 1026OLLCPacket::~OLLCPacket()
1029{ 1027{
1030} 1028}
1031 1029
1032 1030
1033/*====================================================================================== 1031/*======================================================================================
1034 * OWaveLanControlPacket 1032 * OWaveLanControlPacket
1035 *======================================================================================*/ 1033 *======================================================================================*/
1036 1034
1037OWaveLanControlPacket::OWaveLanControlPacket( const unsigned char* end, const struct ieee_802_11_control_header* data, OWaveLanPacket* parent ) 1035OWaveLanControlPacket::OWaveLanControlPacket( const unsigned char* end, const struct ieee_802_11_control_header* data, OWaveLanPacket* parent )
1038 :QObject( parent, "802.11 Control" ), _header( data ) 1036 :QObject( parent, "802.11 Control" ), _header( data )
1039{ 1037{
1040 odebug << "OWaveLanControlPacket::OWaveLanDataControl(): decoding frame..." << oendl; 1038 odebug << "OWaveLanControlPacket::OWaveLanDataControl(): decoding frame..." << oendl;
1041 odebug << "Detected subtype is " << controlType() << oendl; 1039 odebug << "Detected subtype is " << controlType() << oendl;
1042} 1040}
1043 1041
1044 1042
1045OWaveLanControlPacket::~OWaveLanControlPacket() 1043OWaveLanControlPacket::~OWaveLanControlPacket()
1046{ 1044{
1047} 1045}
1048 1046
1049 1047
1050QString OWaveLanControlPacket::controlType() const 1048QString OWaveLanControlPacket::controlType() const
1051{ 1049{
1052 switch ( FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) ) 1050 switch ( FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) )
1053 { 1051 {
1054 case CTRL_PS_POLL: return "PowerSavePoll"; break; 1052 case CTRL_PS_POLL: return "PowerSavePoll"; break;
1055 case CTRL_RTS: return "RequestToSend"; break; 1053 case CTRL_RTS: return "RequestToSend"; break;
1056 case CTRL_CTS: return "ClearToSend"; break; 1054 case CTRL_CTS: return "ClearToSend"; break;
1057 case CTRL_ACK: return "Acknowledge"; break; 1055 case CTRL_ACK: return "Acknowledge"; break;
1058 case CTRL_CF_END: return "ContentionFreeEnd"; break; 1056 case CTRL_CF_END: return "ContentionFreeEnd"; break;
1059 case CTRL_END_ACK: return "AcknowledgeEnd"; break; 1057 case CTRL_END_ACK: return "AcknowledgeEnd"; break;
1060 default: 1058 default:
1061 owarn << "OWaveLanControlPacket::managementType(): unhandled subtype " << FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) << oendl; 1059 owarn << "OWaveLanControlPacket::managementType(): unhandled subtype " << FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) << oendl;
1062 return "Unknown"; 1060 return "Unknown";
1063 } 1061 }
1064} 1062}
1065 1063
1066 1064
1067/*====================================================================================== 1065/*======================================================================================
1068 * OPacketCapturer 1066 * OPacketCapturer
1069 *======================================================================================*/ 1067 *======================================================================================*/
1070 1068
1071OPacketCapturer::OPacketCapturer( QObject* parent, const char* name ) 1069OPacketCapturer::OPacketCapturer( QObject* parent, const char* name )
1072 :QObject( parent, name ), _name( QString::null ), _open( false ), _pch( 0 ), _pcd( 0 ), _sn( 0 ), _autodelete( true ) 1070 :QObject( parent, name ), _name( QString::null ), _open( false ), _pch( 0 ), _pcd( 0 ), _sn( 0 ), _autodelete( true )
1073{ 1071{
1074} 1072}
1075 1073
1076 1074
1077OPacketCapturer::~OPacketCapturer() 1075OPacketCapturer::~OPacketCapturer()
1078{ 1076{
1079 if ( _open ) 1077 if ( _open )
1080 { 1078 {
1081 odebug << "OPacketCapturer::~OPacketCapturer(): pcap still open, autoclosing." << oendl; 1079 odebug << "OPacketCapturer::~OPacketCapturer(): pcap still open, autoclosing." << oendl;
1082 close(); 1080 close();
1083 } 1081 }
1084} 1082}
1085 1083
1086 1084
1087void OPacketCapturer::setAutoDelete( bool b ) 1085void OPacketCapturer::setAutoDelete( bool b )
1088{ 1086{
1089 _autodelete = b; 1087 _autodelete = b;
1090} 1088}
1091 1089
1092 1090
1093bool OPacketCapturer::autoDelete() const 1091bool OPacketCapturer::autoDelete() const
1094{ 1092{
1095 return _autodelete; 1093 return _autodelete;
1096} 1094}
1097 1095
1098 1096
1099void OPacketCapturer::setBlocking( bool b ) 1097void OPacketCapturer::setBlocking( bool b )
1100{ 1098{
1101 if ( pcap_setnonblock( _pch, 1-b, _errbuf ) != -1 ) 1099 if ( pcap_setnonblock( _pch, 1-b, _errbuf ) != -1 )
1102 { 1100 {
1103 odebug << "OPacketCapturer::setBlocking(): blocking mode changed successfully." << oendl; 1101 odebug << "OPacketCapturer::setBlocking(): blocking mode changed successfully." << oendl;
1104 } 1102 }
1105 else 1103 else
1106 { 1104 {
1107 odebug << "OPacketCapturer::setBlocking(): can't change blocking mode: " << _errbuf << oendl; 1105 odebug << "OPacketCapturer::setBlocking(): can't change blocking mode: " << _errbuf << oendl;
1108 } 1106 }
1109} 1107}
1110 1108
1111 1109
1112bool OPacketCapturer::blocking() const 1110bool OPacketCapturer::blocking() const
1113{ 1111{
1114 int b = pcap_getnonblock( _pch, _errbuf ); 1112 int b = pcap_getnonblock( _pch, _errbuf );
1115 if ( b == -1 ) 1113 if ( b == -1 )
1116 { 1114 {
1117 odebug << "OPacketCapturer::blocking(): can't get blocking mode: " << _errbuf << oendl; 1115 odebug << "OPacketCapturer::blocking(): can't get blocking mode: " << _errbuf << oendl;
1118 return -1; 1116 return -1;
1119 } 1117 }
1120 return !b; 1118 return !b;
1121} 1119}
1122 1120
1123 1121
1124void OPacketCapturer::closeDumpFile() 1122void OPacketCapturer::closeDumpFile()
1125{ 1123{
1126 if ( _pcd ) 1124 if ( _pcd )
1127 { 1125 {
1128 pcap_dump_close( _pcd ); 1126 pcap_dump_close( _pcd );
1129 _pcd = 0; 1127 _pcd = 0;
1130 } 1128 }
1131 pcap_close( _pch ); 1129 pcap_close( _pch );
1132} 1130}
1133 1131
1134 1132
1135void OPacketCapturer::close() 1133void OPacketCapturer::close()
1136{ 1134{
1137 if ( _open ) 1135 if ( _open )
1138 { 1136 {
1139 if ( _sn ) 1137 if ( _sn )
1140 { 1138 {
1141 _sn->disconnect( SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); 1139 _sn->disconnect( SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) );
1142 delete _sn; 1140 delete _sn;
1143 } 1141 }
1144 closeDumpFile(); 1142 closeDumpFile();
1145 _open = false; 1143 _open = false;
1146 } 1144 }
1147 1145
1148 odebug << "OPacketCapturer::close() --- dumping capturing statistics..." << oendl; 1146 odebug << "OPacketCapturer::close() --- dumping capturing statistics..." << oendl;
1149 odebug << "--------------------------------------------------" << oendl; 1147 odebug << "--------------------------------------------------" << oendl;
1150 for( QMap<QString,int>::Iterator it = _stats.begin(); it != _stats.end(); ++it ) 1148 for( QMap<QString,int>::Iterator it = _stats.begin(); it != _stats.end(); ++it )
1151 odebug << it.key() << " = " << it.data() << oendl; 1149 odebug << it.key() << " = " << it.data() << oendl;
1152 odebug << "--------------------------------------------------" << oendl; 1150 odebug << "--------------------------------------------------" << oendl;
1153 1151
1154} 1152}
1155 1153
1156 1154
1157int OPacketCapturer::dataLink() const 1155int OPacketCapturer::dataLink() const
1158{ 1156{
1159 return pcap_datalink( _pch ); 1157 return pcap_datalink( _pch );
1160} 1158}
1161 1159
1162 1160
1163void OPacketCapturer::dump( OPacket* p ) 1161void OPacketCapturer::dump( OPacket* p )
1164{ 1162{
1165 if ( !_pcd ) 1163 if ( !_pcd )
1166 { 1164 {
1167 owarn << "OPacketCapturer::dump() - cannot dump without open capture file!" << oendl; 1165 owarn << "OPacketCapturer::dump() - cannot dump without open capture file!" << oendl;
1168 return; 1166 return;
1169 } 1167 }
1170 pcap_dump( (u_char*) _pcd, &p->_hdr, p->_data ); 1168 pcap_dump( (u_char*) _pcd, &p->_hdr, p->_data );
1171} 1169}
1172 1170
1173 1171
1174int OPacketCapturer::fileno() const 1172int OPacketCapturer::fileno() const
1175{ 1173{
1176 if ( _open ) 1174 if ( _open )
1177 { 1175 {
1178 return pcap_fileno( _pch ); 1176 return pcap_fileno( _pch );
1179 } 1177 }
1180 else 1178 else
1181 { 1179 {
1182 return -1; 1180 return -1;
1183 } 1181 }
1184} 1182}
1185 1183
1186 1184
1187OPacket* OPacketCapturer::next( int time ) 1185OPacket* OPacketCapturer::next( int time )
1188{ 1186{
1189 fd_set fds; 1187 fd_set fds;
1190 struct timeval tv; 1188 struct timeval tv;
1191 FD_ZERO( &fds ); 1189 FD_ZERO( &fds );
1192 FD_SET( pcap_fileno( _pch ), &fds ); 1190 FD_SET( pcap_fileno( _pch ), &fds );
1193 tv.tv_sec = time / 1000; 1191 tv.tv_sec = time / 1000;
1194 tv.tv_usec = time % 1000; 1192 tv.tv_usec = time % 1000;
1195 int retval = select( pcap_fileno( _pch )+1, &fds, NULL, NULL, &tv); 1193 int retval = select( pcap_fileno( _pch )+1, &fds, NULL, NULL, &tv);
1196 if ( retval > 0 ) // clear to read! 1194 if ( retval > 0 ) // clear to read!
1197 return next(); 1195 return next();
1198 else 1196 else
1199 return 0; 1197 return 0;
1200} 1198}
1201 1199
1202 1200
1203OPacket* OPacketCapturer::next() 1201OPacket* OPacketCapturer::next()
1204{ 1202{
1205 packetheaderstruct header; 1203 packetheaderstruct header;
1206 odebug << "==> OPacketCapturer::next()" << oendl; 1204 odebug << "==> OPacketCapturer::next()" << oendl;
1207 const unsigned char* pdata = pcap_next( _pch, &header ); 1205 const unsigned char* pdata = pcap_next( _pch, &header );
1208 odebug << "<== OPacketCapturer::next()" << oendl; 1206 odebug << "<== OPacketCapturer::next()" << oendl;
1209 1207
1210 if ( pdata && header.len ) 1208 if ( pdata && header.len )
1211 { 1209 {
1212 OPacket* p = new OPacket( dataLink(), header, pdata, 0 ); 1210 OPacket* p = new OPacket( dataLink(), header, pdata, 0 );
1213 // packets shouldn't be inserted in the QObject child-parent hierarchy, 1211 // packets shouldn't be inserted in the QObject child-parent hierarchy,
1214 // because due to memory constraints they will be deleted as soon 1212 // because due to memory constraints they will be deleted as soon
1215 // as possible - that is right after they have been processed 1213 // as possible - that is right after they have been processed
1216 // by emit() [ see below ] 1214 // by emit() [ see below ]
1217 //TODO: make gathering statistics optional, because it takes time 1215 //TODO: make gathering statistics optional, because it takes time
1218 p->updateStats( _stats, const_cast<QObjectList*>( p->children() ) ); 1216 p->updateStats( _stats, const_cast<QObjectList*>( p->children() ) );
1219 odebug << "OPacket::dumpStructure: " << p->dumpStructure() << oendl; 1217 odebug << "OPacket::dumpStructure: " << p->dumpStructure() << oendl;
1220 return p; 1218 return p;
1221 } 1219 }
1222 else 1220 else
1223 { 1221 {
1224 owarn << "OPacketCapturer::next() - no packet received!" << oendl; 1222 owarn << "OPacketCapturer::next() - no packet received!" << oendl;
1225 return 0; 1223 return 0;
1226 } 1224 }
1227} 1225}
1228 1226
1229 1227
1230bool OPacketCapturer::open( const QString& name ) 1228bool OPacketCapturer::open( const QString& name )
1231{ 1229{
1232 if ( _open ) 1230 if ( _open )
1233 { 1231 {
1234 if ( name == _name ) // ignore opening an already openend device 1232 if ( name == _name ) // ignore opening an already openend device
1235 { 1233 {
1236 return true; 1234 return true;
1237 } 1235 }
1238 else // close the last opened device 1236 else // close the last opened device
1239 { 1237 {
1240 close(); 1238 close();
1241 } 1239 }
1242 } 1240 }
1243 1241
1244 _name = name; 1242 _name = name;
1245 1243
1246 // open libpcap 1244 // open libpcap
1247 pcap_t* handle = pcap_open_live( const_cast<char*>( (const char*) name ), 1024, 0, 0, &_errbuf[0] ); 1245 pcap_t* handle = pcap_open_live( const_cast<char*>( (const char*) name ), 1024, 0, 0, &_errbuf[0] );
1248 1246
1249 if ( !handle ) 1247 if ( !handle )
1250 { 1248 {
1251 owarn << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl; 1249 owarn << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl;
1252 return false; 1250 return false;
1253 } 1251 }
1254 1252
1255 odebug << "OPacketCapturer::open(): libpcap [" << name << "] opened successfully." << oendl; 1253 odebug << "OPacketCapturer::open(): libpcap [" << name << "] opened successfully." << oendl;
1256 _pch = handle; 1254 _pch = handle;
1257 _open = true; 1255 _open = true;
1258 _stats.clear(); 1256 _stats.clear();
1259 1257
1260 // in case we have an application object, create a socket notifier 1258 // in case we have an application object, create a socket notifier
1261 if ( qApp ) //TODO: I don't like this here... 1259 if ( qApp ) //TODO: I don't like this here...
1262 { 1260 {
1263 _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); 1261 _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read );
1264 connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); 1262 connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) );
1265 } 1263 }
1266 1264
1267 return true; 1265 return true;
1268} 1266}
1269 1267
1270 1268
1271bool OPacketCapturer::openDumpFile( const QString& filename ) 1269bool OPacketCapturer::openDumpFile( const QString& filename )
1272{ 1270{
1273 pcap_dumper_t* dump = pcap_dump_open( _pch, const_cast<char*>( (const char*) filename ) ); 1271 pcap_dumper_t* dump = pcap_dump_open( _pch, const_cast<char*>( (const char*) filename ) );
1274 if ( !dump ) 1272 if ( !dump )
1275 { 1273 {
1276 owarn << "OPacketCapturer::open(): can't open dump with '" << filename << "': " << _errbuf << oendl; 1274 owarn << "OPacketCapturer::open(): can't open dump with '" << filename << "': " << _errbuf << oendl;
1277 return false; 1275 return false;
1278 } 1276 }
1279 odebug << "OPacketCapturer::open(): dump [" << filename << "] opened successfully." << oendl; 1277 odebug << "OPacketCapturer::open(): dump [" << filename << "] opened successfully." << oendl;
1280 _pcd = dump; 1278 _pcd = dump;
1281 1279
1282 return true; 1280 return true;
1283} 1281}
1284 1282
1285 1283
1286bool OPacketCapturer::openCaptureFile( const QString& name ) 1284bool OPacketCapturer::openCaptureFile( const QString& name )
1287{ 1285{
1288 if ( _open ) 1286 if ( _open )
1289 { 1287 {
1290 close(); 1288 close();
1291 if ( name == _name ) // ignore opening an already openend device 1289 if ( name == _name ) // ignore opening an already openend device
1292 { 1290 {
1293 return true; 1291 return true;
1294 } 1292 }
1295 else // close the last opened device 1293 else // close the last opened device
1296 { 1294 {
1297 close(); 1295 close();
1298 } 1296 }
1299 } 1297 }
1300 1298
1301 _name = name; 1299 _name = name;
1302 1300
1303 pcap_t* handle = pcap_open_offline( const_cast<char*>( (const char*) name ), &_errbuf[0] ); 1301 pcap_t* handle = pcap_open_offline( const_cast<char*>( (const char*) name ), &_errbuf[0] );
1304 1302
1305 if ( handle ) 1303 if ( handle )
1306 { 1304 {
1307 odebug << "OPacketCapturer::open(): libpcap opened successfully." << oendl; 1305 odebug << "OPacketCapturer::open(): libpcap opened successfully." << oendl;
1308 _pch = handle; 1306 _pch = handle;
1309 _open = true; 1307 _open = true;
1310 1308
1311 // in case we have an application object, create a socket notifier 1309 // in case we have an application object, create a socket notifier
1312 if ( qApp ) 1310 if ( qApp )
1313 { 1311 {
1314 _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); 1312 _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read );
1315 connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); 1313 connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) );
1316 } 1314 }
1317 1315
1318 return true; 1316 return true;
1319 } 1317 }
1320 else 1318 else
1321 { 1319 {
1322 odebug << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl; 1320 odebug << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl;
1323 return false; 1321 return false;
1324 } 1322 }
1325 1323
1326} 1324}
1327 1325
1328 1326
1329bool OPacketCapturer::isOpen() const 1327bool OPacketCapturer::isOpen() const
1330{ 1328{
1331 return _open; 1329 return _open;
1332} 1330}
1333 1331
1334 1332
1335void OPacketCapturer::readyToReceive() 1333void OPacketCapturer::readyToReceive()
1336{ 1334{
1337 odebug << "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" << oendl; 1335 odebug << "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" << oendl;
1338 OPacket* p = next(); 1336 OPacket* p = next();
1339 emit receivedPacket( p ); 1337 emit receivedPacket( p );
1340 // emit is synchronous - packet has been dealt with, now it's safe to delete (if enabled) 1338 // emit is synchronous - packet has been dealt with, now it's safe to delete (if enabled)
1341 if ( _autodelete ) delete p; 1339 if ( _autodelete ) delete p;
1342} 1340}
1343 1341
1344 1342
1345const QMap<QString,int>& OPacketCapturer::statistics() const 1343const QMap<QString,int>& OPacketCapturer::statistics() const
1346{ 1344{
1347 return _stats; 1345 return _stats;
1348} 1346}
1349 1347
1350 1348
1351int OPacketCapturer::snapShot() const 1349int OPacketCapturer::snapShot() const
1352{ 1350{
1353 return pcap_snapshot( _pch ); 1351 return pcap_snapshot( _pch );
1354} 1352}
1355 1353
1356 1354
1357bool OPacketCapturer::swapped() const 1355bool OPacketCapturer::swapped() const
1358{ 1356{
1359 return pcap_is_swapped( _pch ); 1357 return pcap_is_swapped( _pch );
1360} 1358}
1361 1359
1362 1360
1363QString OPacketCapturer::version() const 1361QString OPacketCapturer::version() const
1364{ 1362{
1365 return QString().sprintf( "%d.%d", pcap_major_version( _pch ), pcap_minor_version( _pch ) ); 1363 return QString().sprintf( "%d.%d", pcap_major_version( _pch ), pcap_minor_version( _pch ) );
1366} 1364}
1367 1365
1368} 1366}
1369} 1367}