-rw-r--r-- | libopie2/opienet/opcap.cpp | 95 |
1 files changed, 69 insertions, 26 deletions
diff --git a/libopie2/opienet/opcap.cpp b/libopie2/opienet/opcap.cpp index 40aac2c..5c464cf 100644 --- a/libopie2/opienet/opcap.cpp +++ b/libopie2/opienet/opcap.cpp | |||
@@ -35,56 +35,55 @@ | |||
35 | 35 | ||
36 | #include <opie2/opcap.h> | 36 | #include <opie2/opcap.h> |
37 | 37 | ||
38 | /* QT */ | 38 | /* QT */ |
39 | 39 | ||
40 | #include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects) | 40 | #include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects) |
41 | #include <qsocketnotifier.h> | 41 | #include <qsocketnotifier.h> |
42 | 42 | ||
43 | /*====================================================================================== | 43 | /*====================================================================================== |
44 | * OPacket | 44 | * OPacket |
45 | *======================================================================================*/ | 45 | *======================================================================================*/ |
46 | 46 | ||
47 | OPacket::OPacket( packetheaderstruct header, const unsigned char* data, QObject* parent ) | 47 | OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent ) |
48 | :QObject( parent, "Generic" ), _hdr( header ), _data( data ) | 48 | :QObject( parent, "Generic" ), _hdr( header ), _data( data ) |
49 | { | 49 | { |
50 | qDebug( "OPacket::OPacket(): (Len %d, CapLen %d)" /*, ctime((const time_t*) header.ts.tv_sec)*/, header.len, header.caplen ); | 50 | //qDebug( "OPacket::OPacket(): (Len %d, CapLen %d)" /*, ctime((const time_t*) header.ts.tv_sec)*/, header.len, header.caplen ); |
51 | 51 | ||
52 | _end = (unsigned char*) data + header.len; | 52 | _end = (unsigned char*) data + header.len; |
53 | qDebug( "OPacket::data @ %0x, end @ %0x", data, _end ); | 53 | //qDebug( "OPacket::data @ %0x, end @ %0x", data, _end ); |
54 | 54 | ||
55 | if ( packetCapturer()->dataLink() == DLT_EN10MB ) | 55 | switch ( datalink ) |
56 | { | 56 | { |
57 | case DLT_EN10MB: | ||
57 | qDebug( "OPacket::OPacket(): Received Packet. Datalink = ETHERNET" ); | 58 | qDebug( "OPacket::OPacket(): Received Packet. Datalink = ETHERNET" ); |
58 | new OEthernetPacket( _end, (const struct ether_header*) data, this ); | 59 | new OEthernetPacket( _end, (const struct ether_header*) data, this ); |
59 | } | 60 | break; |
60 | else | 61 | |
61 | { | 62 | case DLT_IEEE802_11: |
62 | qDebug( "OPacket::OPacket(): Received Packet. Datalink = IEEE802.11" ); | 63 | qDebug( "OPacket::OPacket(): Received Packet. Datalink = IEEE802.11" ); |
63 | new OWaveLanPacket( _end, (const struct ieee_802_11_header*) data, this ); | 64 | new OWaveLanPacket( _end, (const struct ieee_802_11_header*) data, this ); |
65 | break; | ||
66 | |||
67 | default: | ||
68 | qWarning( "OPacket::OPacket(): Received Packet over unsupported datalink '%s'!", datalink ); | ||
64 | } | 69 | } |
65 | } | 70 | } |
66 | 71 | ||
67 | 72 | ||
68 | OPacket::~OPacket() | 73 | OPacket::~OPacket() |
69 | { | 74 | { |
70 | } | 75 | } |
71 | 76 | ||
72 | 77 | ||
73 | OPacketCapturer* OPacket::packetCapturer() const | ||
74 | { | ||
75 | return parent()->inherits( "OPacketCapturer" ) ? static_cast<OPacketCapturer*>( parent() ) : 0; | ||
76 | } | ||
77 | |||
78 | |||
79 | timevalstruct OPacket::timeval() const | 78 | timevalstruct OPacket::timeval() const |
80 | { | 79 | { |
81 | return _hdr.ts; | 80 | return _hdr.ts; |
82 | } | 81 | } |
83 | 82 | ||
84 | 83 | ||
85 | int OPacket::caplen() const | 84 | int OPacket::caplen() const |
86 | { | 85 | { |
87 | return _hdr.caplen; | 86 | return _hdr.caplen; |
88 | } | 87 | } |
89 | 88 | ||
90 | 89 | ||
@@ -307,36 +306,36 @@ OTCPPacket::~OTCPPacket() | |||
307 | *======================================================================================*/ | 306 | *======================================================================================*/ |
308 | 307 | ||
309 | 308 | ||
310 | OWaveLanPacket::OWaveLanPacket( const unsigned char* end, const struct ieee_802_11_header* data, QObject* parent ) | 309 | OWaveLanPacket::OWaveLanPacket( const unsigned char* end, const struct ieee_802_11_header* data, QObject* parent ) |
311 | :QObject( parent, "802.11" ), _wlanhdr( data ) | 310 | :QObject( parent, "802.11" ), _wlanhdr( data ) |
312 | 311 | ||
313 | { | 312 | { |
314 | qDebug( "OWaveLanPacket::OWaveLanPacket(): decoding IEEE 802.11 header..." ); | 313 | qDebug( "OWaveLanPacket::OWaveLanPacket(): decoding IEEE 802.11 header..." ); |
315 | qDebug( "type: %0X", type() ); | 314 | qDebug( "type: %0X", type() ); |
316 | qDebug( "subType: %0X", subType() ); | 315 | qDebug( "subType: %0X", subType() ); |
317 | qDebug( "duration: %d", duration() ); | 316 | qDebug( "duration: %d", duration() ); |
318 | qDebug( "powermanagement: %d", usesPowerManagement() ); | 317 | qDebug( "powermanagement: %d", usesPowerManagement() ); |
319 | qDebug( "wep: %d", usesWep() ); | 318 | qDebug( "payload is encrypted: %s", usesWep() ? "yes" : "no" ); |
320 | qDebug( "MAC1: %s", (const char*) macAddress1().toString() ); | 319 | qDebug( "MAC1: %s", (const char*) macAddress1().toString() ); |
321 | qDebug( "MAC2: %s", (const char*) macAddress2().toString() ); | 320 | qDebug( "MAC2: %s", (const char*) macAddress2().toString() ); |
322 | qDebug( "MAC3: %s", (const char*) macAddress3().toString() ); | 321 | qDebug( "MAC3: %s", (const char*) macAddress3().toString() ); |
323 | qDebug( "MAC4: %s", (const char*) macAddress4().toString() ); | 322 | qDebug( "MAC4: %s", (const char*) macAddress4().toString() ); |
324 | 323 | ||
325 | switch ( type() ) | 324 | switch ( type() ) |
326 | { | 325 | { |
327 | case T_MGMT: new OWaveLanManagementPacket( end, (const struct ieee_802_11_mgmt_header*) data, this ); break; | 326 | case T_MGMT: new OWaveLanManagementPacket( end, (const struct ieee_802_11_mgmt_header*) data, this ); break; |
328 | case T_DATA: new OWaveLanDataPacket( end, (const struct ieee_802_11_data_header*) data, this ); break; | 327 | case T_DATA: new OWaveLanDataPacket( end, (const struct ieee_802_11_data_header*) data, this ); break; |
329 | //case T_CTRL: new OWaveLanControlPacket( end, (const struct ieee_802_11_ctrl_header*) data, this ); break; | 328 | case T_CTRL: new OWaveLanControlPacket( end, (const struct ieee_802_11_control_header*) data, this ); break; |
330 | default: qDebug( "OWaveLanPacket::OWaveLanPacket(): Warning: Unknown type!" ); | 329 | default: qDebug( "OWaveLanPacket::OWaveLanPacket(): Warning: Unknown major type '%d'!", type() ); |
331 | } | 330 | } |
332 | } | 331 | } |
333 | 332 | ||
334 | OWaveLanPacket::~OWaveLanPacket() | 333 | OWaveLanPacket::~OWaveLanPacket() |
335 | { | 334 | { |
336 | } | 335 | } |
337 | 336 | ||
338 | 337 | ||
339 | int OWaveLanPacket::duration() const | 338 | int OWaveLanPacket::duration() const |
340 | { | 339 | { |
341 | return _wlanhdr->duration; | 340 | return _wlanhdr->duration; |
342 | } | 341 | } |
@@ -408,32 +407,25 @@ bool OWaveLanPacket::usesWep() const | |||
408 | } | 407 | } |
409 | 408 | ||
410 | 409 | ||
411 | /*====================================================================================== | 410 | /*====================================================================================== |
412 | * OWaveLanManagementPacket | 411 | * OWaveLanManagementPacket |
413 | *======================================================================================*/ | 412 | *======================================================================================*/ |
414 | 413 | ||
415 | OWaveLanManagementPacket::OWaveLanManagementPacket( const unsigned char* end, const struct ieee_802_11_mgmt_header* data, OWaveLanPacket* parent ) | 414 | OWaveLanManagementPacket::OWaveLanManagementPacket( const unsigned char* end, const struct ieee_802_11_mgmt_header* data, OWaveLanPacket* parent ) |
416 | :QObject( parent, "802.11 Management" ), _header( data ), | 415 | :QObject( parent, "802.11 Management" ), _header( data ), |
417 | _body( (const struct ieee_802_11_mgmt_body*) (data+1) ) | 416 | _body( (const struct ieee_802_11_mgmt_body*) (data+1) ) |
418 | { | 417 | { |
419 | qDebug( "OWaveLanManagementPacket::OWaveLanManagementPacket(): decoding frame..." ); | 418 | qDebug( "OWaveLanManagementPacket::OWaveLanManagementPacket(): decoding frame..." ); |
420 | 419 | qDebug( "Detected subtype is '%s'", (const char*) managementType() ); | |
421 | switch ( ((OWaveLanPacket*) this->parent() )->subType() ) | ||
422 | { | ||
423 | case ST_BEACON: | ||
424 | { | ||
425 | // nice, received a beacon... | ||
426 | } | ||
427 | } | ||
428 | 420 | ||
429 | // grab tagged values | 421 | // grab tagged values |
430 | const unsigned char* ptr = (const unsigned char*) (_body+1); | 422 | const unsigned char* ptr = (const unsigned char*) (_body+1); |
431 | while (ptr < end) | 423 | while (ptr < end) |
432 | { | 424 | { |
433 | switch ( *ptr ) | 425 | switch ( *ptr ) |
434 | { | 426 | { |
435 | case E_SSID: new OWaveLanManagementSSID( end, (struct ssid_t*) ptr, this ); break; | 427 | case E_SSID: new OWaveLanManagementSSID( end, (struct ssid_t*) ptr, this ); break; |
436 | case E_FH: new OWaveLanManagementFH( end, (struct fh_t*) ptr, this ); break; | 428 | case E_FH: new OWaveLanManagementFH( end, (struct fh_t*) ptr, this ); break; |
437 | case E_DS: new OWaveLanManagementDS( end, (struct ds_t*) ptr, this ); break; | 429 | case E_DS: new OWaveLanManagementDS( end, (struct ds_t*) ptr, this ); break; |
438 | case E_RATES: new OWaveLanManagementRates( end, (struct rates_t*) ptr, this ); break; | 430 | case E_RATES: new OWaveLanManagementRates( end, (struct rates_t*) ptr, this ); break; |
439 | case E_CF: new OWaveLanManagementCF( end, (struct cf_t*) ptr, this ); break; | 431 | case E_CF: new OWaveLanManagementCF( end, (struct cf_t*) ptr, this ); break; |
@@ -443,24 +435,46 @@ OWaveLanManagementPacket::OWaveLanManagementPacket( const unsigned char* end, co | |||
443 | } | 435 | } |
444 | ptr+= ( ( struct ssid_t* ) ptr )->length; // skip length of tagged value | 436 | ptr+= ( ( struct ssid_t* ) ptr )->length; // skip length of tagged value |
445 | ptr+= 2; // skip tag ID and length | 437 | ptr+= 2; // skip tag ID and length |
446 | } | 438 | } |
447 | } | 439 | } |
448 | 440 | ||
449 | 441 | ||
450 | OWaveLanManagementPacket::~OWaveLanManagementPacket() | 442 | OWaveLanManagementPacket::~OWaveLanManagementPacket() |
451 | { | 443 | { |
452 | } | 444 | } |
453 | 445 | ||
454 | 446 | ||
447 | QString OWaveLanManagementPacket::managementType() const | ||
448 | { | ||
449 | switch ( FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) ) | ||
450 | { | ||
451 | case ST_ASSOC_REQUEST: return "AssociationRequest"; break; | ||
452 | case ST_ASSOC_RESPONSE: return "AssociationResponse"; break; | ||
453 | case ST_REASSOC_REQUEST: return "ReassociationRequest"; break; | ||
454 | case ST_REASSOC_RESPONSE: return "ReassociationResponse"; break; | ||
455 | case ST_PROBE_REQUEST: return "ProbeRequest"; break; | ||
456 | case ST_PROBE_RESPONSE: return "ProbeResponse"; break; | ||
457 | case ST_BEACON: return "Beacon"; break; | ||
458 | case ST_ATIM: return "Atim"; break; | ||
459 | case ST_DISASSOC: return "Disassociation"; break; | ||
460 | case ST_AUTH: return "Authentication"; break; | ||
461 | case ST_DEAUTH: return "Deathentication"; break; | ||
462 | default: | ||
463 | qWarning( "OWaveLanManagementPacket::managementType(): unhandled subtype %d", FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) ); | ||
464 | return "Unknown"; | ||
465 | } | ||
466 | } | ||
467 | |||
468 | |||
455 | int OWaveLanManagementPacket::beaconInterval() const | 469 | int OWaveLanManagementPacket::beaconInterval() const |
456 | { | 470 | { |
457 | return EXTRACT_LE_16BITS( &_body->beacon_interval ); | 471 | return EXTRACT_LE_16BITS( &_body->beacon_interval ); |
458 | } | 472 | } |
459 | 473 | ||
460 | 474 | ||
461 | int OWaveLanManagementPacket::capabilities() const | 475 | int OWaveLanManagementPacket::capabilities() const |
462 | { | 476 | { |
463 | return EXTRACT_LE_16BITS( &_body->capability_info ); | 477 | return EXTRACT_LE_16BITS( &_body->capability_info ); |
464 | } | 478 | } |
465 | 479 | ||
466 | 480 | ||
@@ -673,24 +687,42 @@ OLLCPacket::OLLCPacket( const unsigned char* end, const struct ieee_802_11_802_2 | |||
673 | case ETH_P_IP: new OIPPacket( end, (const struct iphdr*) (data+1), this ); break; | 687 | case ETH_P_IP: new OIPPacket( end, (const struct iphdr*) (data+1), this ); break; |
674 | default: qDebug( "OLLCPacket::OLLCPacket(): Unknown Encapsulation Type" ); | 688 | default: qDebug( "OLLCPacket::OLLCPacket(): Unknown Encapsulation Type" ); |
675 | } | 689 | } |
676 | 690 | ||
677 | } | 691 | } |
678 | } | 692 | } |
679 | 693 | ||
680 | 694 | ||
681 | OLLCPacket::~OLLCPacket() | 695 | OLLCPacket::~OLLCPacket() |
682 | { | 696 | { |
683 | } | 697 | } |
684 | 698 | ||
699 | |||
700 | /*====================================================================================== | ||
701 | * OWaveLanControlPacket | ||
702 | *======================================================================================*/ | ||
703 | |||
704 | OWaveLanControlPacket::OWaveLanControlPacket( const unsigned char* end, const struct ieee_802_11_control_header* data, OWaveLanPacket* parent ) | ||
705 | :QObject( parent, "802.11 Data" ), _header( data ) | ||
706 | { | ||
707 | qDebug( "OWaveLanControlPacket::OWaveLanDataControl(): decoding frame..." ); | ||
708 | //TODO: Implement this | ||
709 | } | ||
710 | |||
711 | |||
712 | OWaveLanControlPacket::~OWaveLanControlPacket() | ||
713 | { | ||
714 | } | ||
715 | |||
716 | |||
685 | /*====================================================================================== | 717 | /*====================================================================================== |
686 | * OPacketCapturer | 718 | * OPacketCapturer |
687 | *======================================================================================*/ | 719 | *======================================================================================*/ |
688 | 720 | ||
689 | OPacketCapturer::OPacketCapturer( QObject* parent, const char* name ) | 721 | OPacketCapturer::OPacketCapturer( QObject* parent, const char* name ) |
690 | :QObject( parent, name ), _name( QString::null ), _open( false ), | 722 | :QObject( parent, name ), _name( QString::null ), _open( false ), |
691 | _pch( 0 ), _sn( 0 ) | 723 | _pch( 0 ), _sn( 0 ) |
692 | { | 724 | { |
693 | } | 725 | } |
694 | 726 | ||
695 | 727 | ||
696 | OPacketCapturer::~OPacketCapturer() | 728 | OPacketCapturer::~OPacketCapturer() |
@@ -761,28 +793,36 @@ int OPacketCapturer::fileno() const | |||
761 | } | 793 | } |
762 | } | 794 | } |
763 | 795 | ||
764 | 796 | ||
765 | OPacket* OPacketCapturer::next() | 797 | OPacket* OPacketCapturer::next() |
766 | { | 798 | { |
767 | packetheaderstruct header; | 799 | packetheaderstruct header; |
768 | qDebug( "==> OPacketCapturer::next()" ); | 800 | qDebug( "==> OPacketCapturer::next()" ); |
769 | const unsigned char* pdata = pcap_next( _pch, &header ); | 801 | const unsigned char* pdata = pcap_next( _pch, &header ); |
770 | qDebug( "<== OPacketCapturer::next()" ); | 802 | qDebug( "<== OPacketCapturer::next()" ); |
771 | 803 | ||
772 | if ( header.len ) | 804 | if ( header.len ) |
773 | return new OPacket( header, pdata, this ); | 805 | { |
806 | return new OPacket( dataLink(), header, pdata, 0 ); | ||
807 | // packets shouldn't be inserted in the QObject child-parent hierarchy, | ||
808 | // because due to memory constraints they will be deleted as soon | ||
809 | // as possible - that is right after they have been processed | ||
810 | // by emit() [ see below ] | ||
811 | } | ||
774 | else | 812 | else |
813 | { | ||
775 | return 0; | 814 | return 0; |
776 | } | 815 | } |
816 | } | ||
777 | 817 | ||
778 | 818 | ||
779 | bool OPacketCapturer::open( const QString& name ) | 819 | bool OPacketCapturer::open( const QString& name ) |
780 | { | 820 | { |
781 | if ( _open ) | 821 | if ( _open ) |
782 | { | 822 | { |
783 | if ( name == _name ) // ignore opening an already openend device | 823 | if ( name == _name ) // ignore opening an already openend device |
784 | { | 824 | { |
785 | return true; | 825 | return true; |
786 | } | 826 | } |
787 | else // close the last opened device | 827 | else // close the last opened device |
788 | { | 828 | { |
@@ -818,15 +858,18 @@ bool OPacketCapturer::open( const QString& name ) | |||
818 | } | 858 | } |
819 | 859 | ||
820 | 860 | ||
821 | bool OPacketCapturer::isOpen() const | 861 | bool OPacketCapturer::isOpen() const |
822 | { | 862 | { |
823 | return _open; | 863 | return _open; |
824 | } | 864 | } |
825 | 865 | ||
826 | 866 | ||
827 | void OPacketCapturer::readyToReceive() | 867 | void OPacketCapturer::readyToReceive() |
828 | { | 868 | { |
829 | qDebug( "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(...)'" ); | 869 | qDebug( "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(...)'" ); |
830 | emit receivedPacket( next() ); | 870 | OPacket* p = next(); |
871 | emit receivedPacket( p ); | ||
872 | // emit is synchronous - packet has been dealt with, now it's safe to delete | ||
873 | delete p; | ||
831 | } | 874 | } |
832 | 875 | ||