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