summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opienet/opcap.cpp21
-rw-r--r--libopie2/opienet/opcap.h20
2 files changed, 33 insertions, 8 deletions
diff --git a/libopie2/opienet/opcap.cpp b/libopie2/opienet/opcap.cpp
index 4081d4f..c5df041 100644
--- a/libopie2/opienet/opcap.cpp
+++ b/libopie2/opienet/opcap.cpp
@@ -28,94 +28,95 @@
28 28
29*/ 29*/
30 30
31/* OPIE */ 31/* OPIE */
32#include <opie2/opcap.h> 32#include <opie2/opcap.h>
33#include <opie2/odebug.h> 33#include <opie2/odebug.h>
34 34
35/* QT */ 35/* QT */
36#include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects) 36#include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects)
37#include <qsocketnotifier.h> 37#include <qsocketnotifier.h>
38#include <qobjectlist.h> 38#include <qobjectlist.h>
39 39
40/* SYSTEM */ 40/* SYSTEM */
41#include <sys/time.h> 41#include <sys/time.h>
42#include <sys/types.h> 42#include <sys/types.h>
43#include <unistd.h> 43#include <unistd.h>
44 44
45/* LOCAL */ 45/* LOCAL */
46#include "udp_ports.h" 46#include "udp_ports.h"
47 47
48using namespace Opie::Core; 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( data )
59{ 59{
60 //qDebug( "OPacket::OPacket(): (Len %d, CapLen %d)" /*, ctime((const time_t*) header.ts.tv_sec)*/, header.len, header.caplen ); 60 qDebug( "OPacket::OPacket(): (Len %d, CapLen %d)" /*, ctime((const time_t*) header.ts.tv_sec)*/, header.len, header.caplen );
61 61
62 _end = (unsigned char*) data + header.len; 62 _end = (unsigned char*) data + header.len;
63 //qDebug( "OPacket::data @ %0x, end @ %0x", data, _end ); 63 //qDebug( "OPacket::data @ %0x, end @ %0x", data, _end );
64 64
65 switch ( datalink ) 65 switch ( datalink )
66 { 66 {
67 case DLT_EN10MB: 67 case DLT_EN10MB:
68 odebug << "OPacket::OPacket(): Received Packet. Datalink = ETHERNET" << oendl; 68 odebug << "OPacket::OPacket(): Received Packet. Datalink = ETHERNET" << oendl;
69 new OEthernetPacket( _end, (const struct ether_header*) data, this ); 69 new OEthernetPacket( _end, (const struct ether_header*) data, this );
70 break; 70 break;
71 71
72 case DLT_IEEE802_11: 72 case DLT_IEEE802_11:
73 odebug << "OPacket::OPacket(): Received Packet. Datalink = IEEE802.11" << oendl; 73 odebug << "OPacket::OPacket(): Received Packet. Datalink = IEEE802.11" << oendl;
74 new OWaveLanPacket( _end, (const struct ieee_802_11_header*) data, this ); 74 new OWaveLanPacket( _end, (const struct ieee_802_11_header*) data, this );
75 break; 75 break;
76 76
77 case DLT_PRISM_HEADER: 77 case DLT_PRISM_HEADER:
78 odebug << "OPacket::OPacket(): Received Packet. Datalink = PRISM_HEADER" << oendl; 78 odebug << "OPacket::OPacket(): Received Packet. Datalink = PRISM_HEADER" << oendl;
79 new OPrismHeaderPacket( _end, (const struct prism_hdr*) (unsigned char*) data, this ); 79 new OPrismHeaderPacket( _end, (const struct prism_hdr*) (unsigned char*) data, this );
80 break; 80 break;
81 81
82 default: 82 default:
83 owarn << "OPacket::OPacket(): Received Packet over unsupported datalink, type " << datalink << "!" << oendl; 83 owarn << "OPacket::OPacket(): Received Packet over unsupported datalink, type " << datalink << "!" << oendl;
84 } 84 }
85} 85}
86 86
87 87
88OPacket::~OPacket() 88OPacket::~OPacket()
89{ 89{
90 qDebug( "OPacket::~OPacket( %s )", name() );
90} 91}
91 92
92 93
93timevalstruct OPacket::timeval() const 94timevalstruct OPacket::timeval() const
94{ 95{
95 return _hdr.ts; 96 return _hdr.ts;
96} 97}
97 98
98 99
99int OPacket::caplen() const 100int OPacket::caplen() const
100{ 101{
101 return _hdr.caplen; 102 return _hdr.caplen;
102} 103}
103 104
104 105
105void OPacket::updateStats( QMap<QString,int>& stats, QObjectList* l ) 106void OPacket::updateStats( QMap<QString,int>& stats, QObjectList* l )
106{ 107{
107 if (!l) return; 108 if (!l) return;
108 QObject* o = l->first(); 109 QObject* o = l->first();
109 while ( o ) 110 while ( o )
110 { 111 {
111 stats[o->name()]++; 112 stats[o->name()]++;
112 updateStats( stats, const_cast<QObjectList*>( o->children() ) ); 113 updateStats( stats, const_cast<QObjectList*>( o->children() ) );
113 o = l->next(); 114 o = l->next();
114 } 115 }
115} 116}
116 117
117 118
118QString OPacket::dumpStructure() const 119QString OPacket::dumpStructure() const
119{ 120{
120 return "[ |" + _dumpStructure( const_cast<QObjectList*>( this->children() ) ) + " ]"; 121 return "[ |" + _dumpStructure( const_cast<QObjectList*>( this->children() ) ) + " ]";
121} 122}
@@ -1034,79 +1035,91 @@ OWaveLanControlPacket::OWaveLanControlPacket( const unsigned char* end, const st
1034 odebug << "OWaveLanControlPacket::OWaveLanDataControl(): decoding frame..." << oendl; 1035 odebug << "OWaveLanControlPacket::OWaveLanDataControl(): decoding frame..." << oendl;
1035 odebug << "Detected subtype is " << controlType() << oendl; 1036 odebug << "Detected subtype is " << controlType() << oendl;
1036} 1037}
1037 1038
1038 1039
1039OWaveLanControlPacket::~OWaveLanControlPacket() 1040OWaveLanControlPacket::~OWaveLanControlPacket()
1040{ 1041{
1041} 1042}
1042 1043
1043 1044
1044QString OWaveLanControlPacket::controlType() const 1045QString OWaveLanControlPacket::controlType() const
1045{ 1046{
1046 switch ( FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) ) 1047 switch ( FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) )
1047 { 1048 {
1048 case CTRL_PS_POLL: return "PowerSavePoll"; break; 1049 case CTRL_PS_POLL: return "PowerSavePoll"; break;
1049 case CTRL_RTS: return "RequestToSend"; break; 1050 case CTRL_RTS: return "RequestToSend"; break;
1050 case CTRL_CTS: return "ClearToSend"; break; 1051 case CTRL_CTS: return "ClearToSend"; break;
1051 case CTRL_ACK: return "Acknowledge"; break; 1052 case CTRL_ACK: return "Acknowledge"; break;
1052 case CTRL_CF_END: return "ContentionFreeEnd"; break; 1053 case CTRL_CF_END: return "ContentionFreeEnd"; break;
1053 case CTRL_END_ACK: return "AcknowledgeEnd"; break; 1054 case CTRL_END_ACK: return "AcknowledgeEnd"; break;
1054 default: 1055 default:
1055 owarn << "OWaveLanControlPacket::managementType(): unhandled subtype " << FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) << oendl; 1056 owarn << "OWaveLanControlPacket::managementType(): unhandled subtype " << FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) << oendl;
1056 return "Unknown"; 1057 return "Unknown";
1057 } 1058 }
1058} 1059}
1059 1060
1060 1061
1061/*====================================================================================== 1062/*======================================================================================
1062 * OPacketCapturer 1063 * OPacketCapturer
1063 *======================================================================================*/ 1064 *======================================================================================*/
1064 1065
1065OPacketCapturer::OPacketCapturer( QObject* parent, const char* name ) 1066OPacketCapturer::OPacketCapturer( QObject* parent, const char* name )
1066 :QObject( parent, name ), _name( QString::null ), _open( false ), _pch( 0 ), _pcd( 0 ), _sn( 0 ) 1067 :QObject( parent, name ), _name( QString::null ), _open( false ), _pch( 0 ), _pcd( 0 ), _sn( 0 ), _autodelete( true )
1067{ 1068{
1068} 1069}
1069 1070
1070 1071
1071OPacketCapturer::~OPacketCapturer() 1072OPacketCapturer::~OPacketCapturer()
1072{ 1073{
1073 if ( _open ) 1074 if ( _open )
1074 { 1075 {
1075 odebug << "OPacketCapturer::~OPacketCapturer(): pcap still open, autoclosing." << oendl; 1076 odebug << "OPacketCapturer::~OPacketCapturer(): pcap still open, autoclosing." << oendl;
1076 close(); 1077 close();
1077 } 1078 }
1078} 1079}
1079 1080
1080 1081
1082void OPacketCapturer::setAutoDelete( bool b )
1083{
1084 _autodelete = b;
1085}
1086
1087
1088bool OPacketCapturer::autoDelete() const
1089{
1090 return _autodelete;
1091}
1092
1093
1081void OPacketCapturer::setBlocking( bool b ) 1094void OPacketCapturer::setBlocking( bool b )
1082{ 1095{
1083 if ( pcap_setnonblock( _pch, 1-b, _errbuf ) != -1 ) 1096 if ( pcap_setnonblock( _pch, 1-b, _errbuf ) != -1 )
1084 { 1097 {
1085 odebug << "OPacketCapturer::setBlocking(): blocking mode changed successfully." << oendl; 1098 odebug << "OPacketCapturer::setBlocking(): blocking mode changed successfully." << oendl;
1086 } 1099 }
1087 else 1100 else
1088 { 1101 {
1089 odebug << "OPacketCapturer::setBlocking(): can't change blocking mode: " << _errbuf << oendl; 1102 odebug << "OPacketCapturer::setBlocking(): can't change blocking mode: " << _errbuf << oendl;
1090 } 1103 }
1091} 1104}
1092 1105
1093 1106
1094bool OPacketCapturer::blocking() const 1107bool OPacketCapturer::blocking() const
1095{ 1108{
1096 int b = pcap_getnonblock( _pch, _errbuf ); 1109 int b = pcap_getnonblock( _pch, _errbuf );
1097 if ( b == -1 ) 1110 if ( b == -1 )
1098 { 1111 {
1099 odebug << "OPacketCapturer::blocking(): can't get blocking mode: " << _errbuf << oendl; 1112 odebug << "OPacketCapturer::blocking(): can't get blocking mode: " << _errbuf << oendl;
1100 return -1; 1113 return -1;
1101 } 1114 }
1102 return !b; 1115 return !b;
1103} 1116}
1104 1117
1105 1118
1106void OPacketCapturer::closeDumpFile() 1119void OPacketCapturer::closeDumpFile()
1107{ 1120{
1108 if ( _pcd ) 1121 if ( _pcd )
1109 { 1122 {
1110 pcap_dump_close( _pcd ); 1123 pcap_dump_close( _pcd );
1111 _pcd = 0; 1124 _pcd = 0;
1112 } 1125 }
@@ -1292,62 +1305,62 @@ bool OPacketCapturer::open( const QFile& file )
1292 _pch = handle; 1305 _pch = handle;
1293 _open = true; 1306 _open = true;
1294 1307
1295 // in case we have an application object, create a socket notifier 1308 // in case we have an application object, create a socket notifier
1296 if ( qApp ) 1309 if ( qApp )
1297 { 1310 {
1298 _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); 1311 _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read );
1299 connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); 1312 connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) );
1300 } 1313 }
1301 1314
1302 return true; 1315 return true;
1303 } 1316 }
1304 else 1317 else
1305 { 1318 {
1306 odebug << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl; 1319 odebug << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl;
1307 return false; 1320 return false;
1308 } 1321 }
1309 1322
1310} 1323}
1311 1324
1312 1325
1313bool OPacketCapturer::isOpen() const 1326bool OPacketCapturer::isOpen() const
1314{ 1327{
1315 return _open; 1328 return _open;
1316} 1329}
1317 1330
1318 1331
1319void OPacketCapturer::readyToReceive() 1332void OPacketCapturer::readyToReceive()
1320{ 1333{
1321 odebug << "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" << oendl; 1334 odebug << "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" << oendl;
1322 OPacket* p = next(); 1335 OPacket* p = next();
1323 emit receivedPacket( p ); 1336 emit receivedPacket( p );
1324 // emit is synchronous - packet has been dealt with, now it's safe to delete 1337 // emit is synchronous - packet has been dealt with, now it's safe to delete (if enabled)
1325 delete p; 1338 if ( _autodelete ) delete p;
1326} 1339}
1327 1340
1328 1341
1329const QMap<QString,int>& OPacketCapturer::statistics() const 1342const QMap<QString,int>& OPacketCapturer::statistics() const
1330{ 1343{
1331 return _stats; 1344 return _stats;
1332} 1345}
1333 1346
1334 1347
1335int OPacketCapturer::snapShot() const 1348int OPacketCapturer::snapShot() const
1336{ 1349{
1337 return pcap_snapshot( _pch ); 1350 return pcap_snapshot( _pch );
1338} 1351}
1339 1352
1340 1353
1341bool OPacketCapturer::swapped() const 1354bool OPacketCapturer::swapped() const
1342{ 1355{
1343 return pcap_is_swapped( _pch ); 1356 return pcap_is_swapped( _pch );
1344} 1357}
1345 1358
1346 1359
1347QString OPacketCapturer::version() const 1360QString OPacketCapturer::version() const
1348{ 1361{
1349 return QString().sprintf( "%d.%d", pcap_major_version( _pch ), pcap_minor_version( _pch ) ); 1362 return QString().sprintf( "%d.%d", pcap_major_version( _pch ), pcap_minor_version( _pch ) );
1350} 1363}
1351 1364
1352} 1365}
1353} 1366}
diff --git a/libopie2/opienet/opcap.h b/libopie2/opienet/opcap.h
index 72a78de..4ff8495 100644
--- a/libopie2/opienet/opcap.h
+++ b/libopie2/opienet/opcap.h
@@ -105,67 +105,66 @@ class OPacketCapturer;
105 * 10 packets per captured frame. We need to stuff them into a searchable list anyway and the 105 * 10 packets per captured frame. We need to stuff them into a searchable list anyway and the
106 * QObject also cares about destroying the sub-, (child-) packets. 106 * QObject also cares about destroying the sub-, (child-) packets.
107 * 107 *
108 * This enables us to perform a simple look for packets of a certain type: 108 * This enables us to perform a simple look for packets of a certain type:
109 * @code 109 * @code
110 * OPacketCapturer* pcap = new OPacketCapturer(); 110 * OPacketCapturer* pcap = new OPacketCapturer();
111 * pcap->open( "eth0" ); 111 * pcap->open( "eth0" );
112 * OPacket* p = pcap->next(); 112 * OPacket* p = pcap->next();
113 * OIPPacket* ip = (OIPPacket*) p->child( "IP" ); // returns 0, if no such child exists 113 * OIPPacket* ip = (OIPPacket*) p->child( "IP" ); // returns 0, if no such child exists
114 * odebug << "got ip packet from " << ip->fromIPAddress().toString() << " to " << ip->toIPAddress().toString() << oendl; 114 * odebug << "got ip packet from " << ip->fromIPAddress().toString() << " to " << ip->toIPAddress().toString() << oendl;
115 * 115 *
116 */ 116 */
117 117
118class OPacket : public QObject 118class OPacket : public QObject
119{ 119{
120 Q_OBJECT 120 Q_OBJECT
121 121
122 friend class OPacketCapturer; 122 friend class OPacketCapturer;
123 friend QTextStream& operator<<( QTextStream& s, const OPacket& p ); 123 friend QTextStream& operator<<( QTextStream& s, const OPacket& p );
124 124
125 public: 125 public:
126 OPacket( int datalink, packetheaderstruct, const unsigned char*, QObject* parent ); 126 OPacket( int datalink, packetheaderstruct, const unsigned char*, QObject* parent );
127 virtual ~OPacket(); 127 virtual ~OPacket();
128 128
129 timevalstruct timeval() const; 129 timevalstruct timeval() const;
130 130
131 int caplen() const; 131 int caplen() const;
132 int len() const; 132 int len() const;
133 QString dump( int = 32 ) const; 133 QString dump( int = 32 ) const;
134 134
135 void updateStats( QMap<QString,int>&, QObjectList* ); 135 void updateStats( QMap<QString,int>&, QObjectList* );
136 136
137 private:
138
139 QString dumpStructure() const; 137 QString dumpStructure() const;
138 private:
140 QString _dumpStructure( QObjectList* ) const; 139 QString _dumpStructure( QObjectList* ) const;
141 140
142 private: 141 private:
143 const packetheaderstruct _hdr; // pcap packet header 142 const packetheaderstruct _hdr; // pcap packet header
144 const unsigned char* _data; // pcap packet data 143 const unsigned char* _data; // pcap packet data
145 const unsigned char* _end; // end of pcap packet data 144 const unsigned char* _end; // end of pcap packet data
146 private: 145 private:
147 class Private; 146 class Private;
148 Private *d; 147 Private *d;
149}; 148};
150 149
151QTextStream& operator<<( QTextStream& s, const OPacket& p ); 150QTextStream& operator<<( QTextStream& s, const OPacket& p );
152 151
153/*====================================================================================== 152/*======================================================================================
154 * OEthernetPacket - DLT_EN10MB frame 153 * OEthernetPacket - DLT_EN10MB frame
155 *======================================================================================*/ 154 *======================================================================================*/
156 155
157class OEthernetPacket : public QObject 156class OEthernetPacket : public QObject
158{ 157{
159 Q_OBJECT 158 Q_OBJECT
160 159
161 public: 160 public:
162 OEthernetPacket( const unsigned char*, const struct ether_header*, QObject* parent = 0 ); 161 OEthernetPacket( const unsigned char*, const struct ether_header*, QObject* parent = 0 );
163 virtual ~OEthernetPacket(); 162 virtual ~OEthernetPacket();
164 163
165 OMacAddress sourceAddress() const; 164 OMacAddress sourceAddress() const;
166 OMacAddress destinationAddress() const; 165 OMacAddress destinationAddress() const;
167 int type() const; 166 int type() const;
168 167
169 private: 168 private:
170 const struct ether_header* _ether; 169 const struct ether_header* _ether;
171 private: 170 private:
@@ -664,58 +663,71 @@ class OPacketCapturer : public QObject
664 */ 663 */
665 bool open( const QString& interface ); 664 bool open( const QString& interface );
666 /** 665 /**
667 * Open the packet capturer to capture packets in offline-mode from @a file. 666 * Open the packet capturer to capture packets in offline-mode from @a file.
668 */ 667 */
669 bool open( const QFile& file ); 668 bool open( const QFile& file );
670 /** 669 /**
671 * Open a prerecorded tcpdump compatible capture file for use with @ref dump() 670 * Open a prerecorded tcpdump compatible capture file for use with @ref dump()
672 */ 671 */
673 bool openDumpFile( const QString& filename ); 672 bool openDumpFile( const QString& filename );
674 /** 673 /**
675 * @returns true if the packet capturer is open 674 * @returns true if the packet capturer is open
676 */ 675 */
677 bool isOpen() const; 676 bool isOpen() const;
678 /** 677 /**
679 * @returns the snapshot length of this packet capturer 678 * @returns the snapshot length of this packet capturer
680 */ 679 */
681 int snapShot() const; 680 int snapShot() const;
682 /** 681 /**
683 * @returns true if the input capture file has a different byte-order 682 * @returns true if the input capture file has a different byte-order
684 * than the byte-order of the running system. 683 * than the byte-order of the running system.
685 */ 684 */
686 bool swapped() const; 685 bool swapped() const;
687 /** 686 /**
688 * @returns the libpcap version string used to write the input capture file. 687 * @returns the libpcap version string used to write the input capture file.
689 */ 688 */
690 QString version() const; 689 QString version() const;
691 /** 690 /**
692 * @returns the packet statistic database. 691 * @returns the packet statistic database.
693 * @see QMap 692 * @see QMap
694 */ 693 */
695 const QMap<QString,int>& statistics() const; 694 const QMap<QString,int>& statistics() const;
695 /**
696 * Enable or disable the auto-delete option.
697 * If auto-delete is enabled, then the packet capturer will delete a packet right
698 * after it has been emit'ted. This is the default, which is useful if the packet
699 * capturer has the only reference to the packets. If you pass the packet for adding
700 * into a collection or do processing after the SLOT, the auto delete must be disabled.
701 */
702 void setAutoDelete( bool enable );
703 /**
704 * @returns the auto-delete value.
705 */
706 bool autoDelete() const;
696 707
697 signals: 708 signals:
698 /** 709 /**
699 * This signal is emitted, when a packet has been received. 710 * This signal is emitted, when a packet has been received.
700 */ 711 */
701 void receivedPacket( Opie::Net::OPacket* ); 712 void receivedPacket( Opie::Net::OPacket* );
702 713
703 protected slots: 714 protected slots:
704 void readyToReceive(); 715 void readyToReceive();
705 716
706 protected: 717 protected:
707 QString _name; // devicename 718 QString _name; // devicename
708 bool _open; // check this before doing pcap calls 719 bool _open; // check this before doing pcap calls
709 pcap_t* _pch; // pcap library handle 720 pcap_t* _pch; // pcap library handle
710 pcap_dumper_t* _pcd; // pcap dumper handle 721 pcap_dumper_t* _pcd; // pcap dumper handle
711 QSocketNotifier* _sn; // socket notifier for main loop 722 QSocketNotifier* _sn; // socket notifier for main loop
712 mutable char _errbuf[PCAP_ERRBUF_SIZE]; // holds error strings from libpcap 723 mutable char _errbuf[PCAP_ERRBUF_SIZE]; // holds error strings from libpcap
713 QMap<QString, int> _stats; // statistics; 724 QMap<QString, int> _stats; // statistics;
714 class Private; // Private Forward declaration 725 bool _autodelete; // if we auto delete packets after emit
715 Private *d; // if we need to add data 726 class Private; // Private Forward declaration
727 Private *d; // if we need to add data
716}; 728};
717} 729}
718} 730}
719 731
720#endif // OPCAP_H 732#endif // OPCAP_H
721 733