author | mickeyl <mickeyl> | 2004-03-28 16:52:43 (UTC) |
---|---|---|
committer | mickeyl <mickeyl> | 2004-03-28 16:52:43 (UTC) |
commit | e2368b92a101437f7ed289d5ae62f7e7e2466344 (patch) (unidiff) | |
tree | 1e7775ee681d970c9cd785b9afb6946143c212e1 | |
parent | 6c102bd456d760fbe237fd49ce46f212614f228a (diff) | |
download | opie-e2368b92a101437f7ed289d5ae62f7e7e2466344.zip opie-e2368b92a101437f7ed289d5ae62f7e7e2466344.tar.gz opie-e2368b92a101437f7ed289d5ae62f7e7e2466344.tar.bz2 |
OPacketCapturer API addition: setAutoDelete() and autoDelete()
-rw-r--r-- | libopie2/opienet/opcap.cpp | 21 | ||||
-rw-r--r-- | libopie2/opienet/opcap.h | 20 |
2 files changed, 33 insertions, 8 deletions
diff --git a/libopie2/opienet/opcap.cpp b/libopie2/opienet/opcap.cpp index 4081d4f..c5df041 100644 --- a/libopie2/opienet/opcap.cpp +++ b/libopie2/opienet/opcap.cpp | |||
@@ -12,126 +12,127 @@ | |||
12 | ._= =} : or (at your option) any later version. | 12 | ._= =} : or (at your option) any later version. |
13 | .%`+i> _;_. | 13 | .%`+i> _;_. |
14 | .i_,=:_. -<s. This program is distributed in the hope that | 14 | .i_,=:_. -<s. This program is distributed in the hope that |
15 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | 15 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; |
16 | : .. .:, . . . without even the implied warranty of | 16 | : .. .:, . . . without even the implied warranty of |
17 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | 17 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A |
18 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | 18 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU |
19 | ..}^=.= = ; Library General Public License for more | 19 | ..}^=.= = ; Library General Public License for more |
20 | ++= -. .` .: details. | 20 | ++= -. .` .: details. |
21 | : = ...= . :.=- | 21 | : = ...= . :.=- |
22 | -. .:....=;==+<; You should have received a copy of the GNU | 22 | -. .:....=;==+<; You should have received a copy of the GNU |
23 | -_. . . )=. = Library General Public License along with | 23 | -_. . . )=. = Library General Public License along with |
24 | -- :-=` this library; see the file COPYING.LIB. | 24 | -- :-=` this library; see the file COPYING.LIB. |
25 | If not, write to the Free Software Foundation, | 25 | If not, write to the Free Software Foundation, |
26 | Inc., 59 Temple Place - Suite 330, | 26 | Inc., 59 Temple Place - Suite 330, |
27 | Boston, MA 02111-1307, USA. | 27 | Boston, MA 02111-1307, USA. |
28 | 28 | ||
29 | */ | 29 | */ |
30 | 30 | ||
31 | /* OPIE */ | 31 | /* OPIE */ |
32 | #include <opie2/opcap.h> | 32 | #include <opie2/opcap.h> |
33 | #include <opie2/odebug.h> | 33 | #include <opie2/odebug.h> |
34 | 34 | ||
35 | /* QT */ | 35 | /* QT */ |
36 | #include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects) | 36 | #include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects) |
37 | #include <qsocketnotifier.h> | 37 | #include <qsocketnotifier.h> |
38 | #include <qobjectlist.h> | 38 | #include <qobjectlist.h> |
39 | 39 | ||
40 | /* SYSTEM */ | 40 | /* SYSTEM */ |
41 | #include <sys/time.h> | 41 | #include <sys/time.h> |
42 | #include <sys/types.h> | 42 | #include <sys/types.h> |
43 | #include <unistd.h> | 43 | #include <unistd.h> |
44 | 44 | ||
45 | /* LOCAL */ | 45 | /* LOCAL */ |
46 | #include "udp_ports.h" | 46 | #include "udp_ports.h" |
47 | 47 | ||
48 | using namespace Opie::Core; | 48 | using namespace Opie::Core; |
49 | 49 | ||
50 | namespace Opie { | 50 | namespace Opie { |
51 | namespace Net { | 51 | namespace Net { |
52 | 52 | ||
53 | /*====================================================================================== | 53 | /*====================================================================================== |
54 | * OPacket | 54 | * OPacket |
55 | *======================================================================================*/ | 55 | *======================================================================================*/ |
56 | 56 | ||
57 | OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent ) | 57 | OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent ) |
58 | :QObject( parent, "Generic" ), _hdr( header ), _data( data ) | 58 | :QObject( parent, "Generic" ), _hdr( header ), _data( data ) |
59 | { | 59 | { |
60 | //qDebug( "OPacket::OPacket(): (Len %d, CapLen %d)" /*, ctime((const time_t*) header.ts.tv_sec)*/, header.len, header.caplen ); | 60 | qDebug( "OPacket::OPacket(): (Len %d, CapLen %d)" /*, ctime((const time_t*) header.ts.tv_sec)*/, header.len, header.caplen ); |
61 | 61 | ||
62 | _end = (unsigned char*) data + header.len; | 62 | _end = (unsigned char*) data + header.len; |
63 | //qDebug( "OPacket::data @ %0x, end @ %0x", data, _end ); | 63 | //qDebug( "OPacket::data @ %0x, end @ %0x", data, _end ); |
64 | 64 | ||
65 | switch ( datalink ) | 65 | switch ( datalink ) |
66 | { | 66 | { |
67 | case DLT_EN10MB: | 67 | case DLT_EN10MB: |
68 | odebug << "OPacket::OPacket(): Received Packet. Datalink = ETHERNET" << oendl; | 68 | odebug << "OPacket::OPacket(): Received Packet. Datalink = ETHERNET" << oendl; |
69 | new OEthernetPacket( _end, (const struct ether_header*) data, this ); | 69 | new OEthernetPacket( _end, (const struct ether_header*) data, this ); |
70 | break; | 70 | break; |
71 | 71 | ||
72 | case DLT_IEEE802_11: | 72 | case DLT_IEEE802_11: |
73 | odebug << "OPacket::OPacket(): Received Packet. Datalink = IEEE802.11" << oendl; | 73 | odebug << "OPacket::OPacket(): Received Packet. Datalink = IEEE802.11" << oendl; |
74 | new OWaveLanPacket( _end, (const struct ieee_802_11_header*) data, this ); | 74 | new OWaveLanPacket( _end, (const struct ieee_802_11_header*) data, this ); |
75 | break; | 75 | break; |
76 | 76 | ||
77 | case DLT_PRISM_HEADER: | 77 | case DLT_PRISM_HEADER: |
78 | odebug << "OPacket::OPacket(): Received Packet. Datalink = PRISM_HEADER" << oendl; | 78 | odebug << "OPacket::OPacket(): Received Packet. Datalink = PRISM_HEADER" << oendl; |
79 | new OPrismHeaderPacket( _end, (const struct prism_hdr*) (unsigned char*) data, this ); | 79 | new OPrismHeaderPacket( _end, (const struct prism_hdr*) (unsigned char*) data, this ); |
80 | break; | 80 | break; |
81 | 81 | ||
82 | default: | 82 | default: |
83 | owarn << "OPacket::OPacket(): Received Packet over unsupported datalink, type " << datalink << "!" << oendl; | 83 | owarn << "OPacket::OPacket(): Received Packet over unsupported datalink, type " << datalink << "!" << oendl; |
84 | } | 84 | } |
85 | } | 85 | } |
86 | 86 | ||
87 | 87 | ||
88 | OPacket::~OPacket() | 88 | OPacket::~OPacket() |
89 | { | 89 | { |
90 | qDebug( "OPacket::~OPacket( %s )", name() ); | ||
90 | } | 91 | } |
91 | 92 | ||
92 | 93 | ||
93 | timevalstruct OPacket::timeval() const | 94 | timevalstruct OPacket::timeval() const |
94 | { | 95 | { |
95 | return _hdr.ts; | 96 | return _hdr.ts; |
96 | } | 97 | } |
97 | 98 | ||
98 | 99 | ||
99 | int OPacket::caplen() const | 100 | int OPacket::caplen() const |
100 | { | 101 | { |
101 | return _hdr.caplen; | 102 | return _hdr.caplen; |
102 | } | 103 | } |
103 | 104 | ||
104 | 105 | ||
105 | void OPacket::updateStats( QMap<QString,int>& stats, QObjectList* l ) | 106 | void OPacket::updateStats( QMap<QString,int>& stats, QObjectList* l ) |
106 | { | 107 | { |
107 | if (!l) return; | 108 | if (!l) return; |
108 | QObject* o = l->first(); | 109 | QObject* o = l->first(); |
109 | while ( o ) | 110 | while ( o ) |
110 | { | 111 | { |
111 | stats[o->name()]++; | 112 | stats[o->name()]++; |
112 | updateStats( stats, const_cast<QObjectList*>( o->children() ) ); | 113 | updateStats( stats, const_cast<QObjectList*>( o->children() ) ); |
113 | o = l->next(); | 114 | o = l->next(); |
114 | } | 115 | } |
115 | } | 116 | } |
116 | 117 | ||
117 | 118 | ||
118 | QString OPacket::dumpStructure() const | 119 | QString OPacket::dumpStructure() const |
119 | { | 120 | { |
120 | return "[ |" + _dumpStructure( const_cast<QObjectList*>( this->children() ) ) + " ]"; | 121 | return "[ |" + _dumpStructure( const_cast<QObjectList*>( this->children() ) ) + " ]"; |
121 | } | 122 | } |
122 | 123 | ||
123 | 124 | ||
124 | QString OPacket::_dumpStructure( QObjectList* l ) const | 125 | QString OPacket::_dumpStructure( QObjectList* l ) const |
125 | { | 126 | { |
126 | if (!l) return QString::null; | 127 | if (!l) return QString::null; |
127 | QObject* o = l->first(); | 128 | QObject* o = l->first(); |
128 | QString str(" "); | 129 | QString str(" "); |
129 | 130 | ||
130 | while ( o ) | 131 | while ( o ) |
131 | { | 132 | { |
132 | str.append( o->name() ); | 133 | str.append( o->name() ); |
133 | str.append( " |" ); | 134 | str.append( " |" ); |
134 | str += _dumpStructure( const_cast<QObjectList*>( o->children() ) ); | 135 | str += _dumpStructure( const_cast<QObjectList*>( o->children() ) ); |
135 | o = l->next(); | 136 | o = l->next(); |
136 | } | 137 | } |
137 | return str; | 138 | return str; |
@@ -1018,111 +1019,123 @@ OLLCPacket::OLLCPacket( const unsigned char* end, const struct ieee_802_11_802_2 | |||
1018 | } | 1019 | } |
1019 | } | 1020 | } |
1020 | 1021 | ||
1021 | 1022 | ||
1022 | OLLCPacket::~OLLCPacket() | 1023 | OLLCPacket::~OLLCPacket() |
1023 | { | 1024 | { |
1024 | } | 1025 | } |
1025 | 1026 | ||
1026 | 1027 | ||
1027 | /*====================================================================================== | 1028 | /*====================================================================================== |
1028 | * OWaveLanControlPacket | 1029 | * OWaveLanControlPacket |
1029 | *======================================================================================*/ | 1030 | *======================================================================================*/ |
1030 | 1031 | ||
1031 | OWaveLanControlPacket::OWaveLanControlPacket( const unsigned char* end, const struct ieee_802_11_control_header* data, OWaveLanPacket* parent ) | 1032 | OWaveLanControlPacket::OWaveLanControlPacket( const unsigned char* end, const struct ieee_802_11_control_header* data, OWaveLanPacket* parent ) |
1032 | :QObject( parent, "802.11 Control" ), _header( data ) | 1033 | :QObject( parent, "802.11 Control" ), _header( data ) |
1033 | { | 1034 | { |
1034 | odebug << "OWaveLanControlPacket::OWaveLanDataControl(): decoding frame..." << oendl; | 1035 | odebug << "OWaveLanControlPacket::OWaveLanDataControl(): decoding frame..." << oendl; |
1035 | odebug << "Detected subtype is " << controlType() << oendl; | 1036 | odebug << "Detected subtype is " << controlType() << oendl; |
1036 | } | 1037 | } |
1037 | 1038 | ||
1038 | 1039 | ||
1039 | OWaveLanControlPacket::~OWaveLanControlPacket() | 1040 | OWaveLanControlPacket::~OWaveLanControlPacket() |
1040 | { | 1041 | { |
1041 | } | 1042 | } |
1042 | 1043 | ||
1043 | 1044 | ||
1044 | QString OWaveLanControlPacket::controlType() const | 1045 | QString OWaveLanControlPacket::controlType() const |
1045 | { | 1046 | { |
1046 | switch ( FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) ) | 1047 | switch ( FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) ) |
1047 | { | 1048 | { |
1048 | case CTRL_PS_POLL: return "PowerSavePoll"; break; | 1049 | case CTRL_PS_POLL: return "PowerSavePoll"; break; |
1049 | case CTRL_RTS: return "RequestToSend"; break; | 1050 | case CTRL_RTS: return "RequestToSend"; break; |
1050 | case CTRL_CTS: return "ClearToSend"; break; | 1051 | case CTRL_CTS: return "ClearToSend"; break; |
1051 | case CTRL_ACK: return "Acknowledge"; break; | 1052 | case CTRL_ACK: return "Acknowledge"; break; |
1052 | case CTRL_CF_END: return "ContentionFreeEnd"; break; | 1053 | case CTRL_CF_END: return "ContentionFreeEnd"; break; |
1053 | case CTRL_END_ACK: return "AcknowledgeEnd"; break; | 1054 | case CTRL_END_ACK: return "AcknowledgeEnd"; break; |
1054 | default: | 1055 | default: |
1055 | owarn << "OWaveLanControlPacket::managementType(): unhandled subtype " << FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) << oendl; | 1056 | owarn << "OWaveLanControlPacket::managementType(): unhandled subtype " << FC_SUBTYPE( EXTRACT_LE_16BITS( &_header->fc ) ) << oendl; |
1056 | return "Unknown"; | 1057 | return "Unknown"; |
1057 | } | 1058 | } |
1058 | } | 1059 | } |
1059 | 1060 | ||
1060 | 1061 | ||
1061 | /*====================================================================================== | 1062 | /*====================================================================================== |
1062 | * OPacketCapturer | 1063 | * OPacketCapturer |
1063 | *======================================================================================*/ | 1064 | *======================================================================================*/ |
1064 | 1065 | ||
1065 | OPacketCapturer::OPacketCapturer( QObject* parent, const char* name ) | 1066 | OPacketCapturer::OPacketCapturer( QObject* parent, const char* name ) |
1066 | :QObject( parent, name ), _name( QString::null ), _open( false ), _pch( 0 ), _pcd( 0 ), _sn( 0 ) | 1067 | :QObject( parent, name ), _name( QString::null ), _open( false ), _pch( 0 ), _pcd( 0 ), _sn( 0 ), _autodelete( true ) |
1067 | { | 1068 | { |
1068 | } | 1069 | } |
1069 | 1070 | ||
1070 | 1071 | ||
1071 | OPacketCapturer::~OPacketCapturer() | 1072 | OPacketCapturer::~OPacketCapturer() |
1072 | { | 1073 | { |
1073 | if ( _open ) | 1074 | if ( _open ) |
1074 | { | 1075 | { |
1075 | odebug << "OPacketCapturer::~OPacketCapturer(): pcap still open, autoclosing." << oendl; | 1076 | odebug << "OPacketCapturer::~OPacketCapturer(): pcap still open, autoclosing." << oendl; |
1076 | close(); | 1077 | close(); |
1077 | } | 1078 | } |
1078 | } | 1079 | } |
1079 | 1080 | ||
1080 | 1081 | ||
1082 | void OPacketCapturer::setAutoDelete( bool b ) | ||
1083 | { | ||
1084 | _autodelete = b; | ||
1085 | } | ||
1086 | |||
1087 | |||
1088 | bool OPacketCapturer::autoDelete() const | ||
1089 | { | ||
1090 | return _autodelete; | ||
1091 | } | ||
1092 | |||
1093 | |||
1081 | void OPacketCapturer::setBlocking( bool b ) | 1094 | void OPacketCapturer::setBlocking( bool b ) |
1082 | { | 1095 | { |
1083 | if ( pcap_setnonblock( _pch, 1-b, _errbuf ) != -1 ) | 1096 | if ( pcap_setnonblock( _pch, 1-b, _errbuf ) != -1 ) |
1084 | { | 1097 | { |
1085 | odebug << "OPacketCapturer::setBlocking(): blocking mode changed successfully." << oendl; | 1098 | odebug << "OPacketCapturer::setBlocking(): blocking mode changed successfully." << oendl; |
1086 | } | 1099 | } |
1087 | else | 1100 | else |
1088 | { | 1101 | { |
1089 | odebug << "OPacketCapturer::setBlocking(): can't change blocking mode: " << _errbuf << oendl; | 1102 | odebug << "OPacketCapturer::setBlocking(): can't change blocking mode: " << _errbuf << oendl; |
1090 | } | 1103 | } |
1091 | } | 1104 | } |
1092 | 1105 | ||
1093 | 1106 | ||
1094 | bool OPacketCapturer::blocking() const | 1107 | bool OPacketCapturer::blocking() const |
1095 | { | 1108 | { |
1096 | int b = pcap_getnonblock( _pch, _errbuf ); | 1109 | int b = pcap_getnonblock( _pch, _errbuf ); |
1097 | if ( b == -1 ) | 1110 | if ( b == -1 ) |
1098 | { | 1111 | { |
1099 | odebug << "OPacketCapturer::blocking(): can't get blocking mode: " << _errbuf << oendl; | 1112 | odebug << "OPacketCapturer::blocking(): can't get blocking mode: " << _errbuf << oendl; |
1100 | return -1; | 1113 | return -1; |
1101 | } | 1114 | } |
1102 | return !b; | 1115 | return !b; |
1103 | } | 1116 | } |
1104 | 1117 | ||
1105 | 1118 | ||
1106 | void OPacketCapturer::closeDumpFile() | 1119 | void OPacketCapturer::closeDumpFile() |
1107 | { | 1120 | { |
1108 | if ( _pcd ) | 1121 | if ( _pcd ) |
1109 | { | 1122 | { |
1110 | pcap_dump_close( _pcd ); | 1123 | pcap_dump_close( _pcd ); |
1111 | _pcd = 0; | 1124 | _pcd = 0; |
1112 | } | 1125 | } |
1113 | pcap_close( _pch ); | 1126 | pcap_close( _pch ); |
1114 | } | 1127 | } |
1115 | 1128 | ||
1116 | 1129 | ||
1117 | void OPacketCapturer::close() | 1130 | void OPacketCapturer::close() |
1118 | { | 1131 | { |
1119 | if ( _open ) | 1132 | if ( _open ) |
1120 | { | 1133 | { |
1121 | if ( _sn ) | 1134 | if ( _sn ) |
1122 | { | 1135 | { |
1123 | _sn->disconnect( SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); | 1136 | _sn->disconnect( SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); |
1124 | delete _sn; | 1137 | delete _sn; |
1125 | } | 1138 | } |
1126 | closeDumpFile(); | 1139 | closeDumpFile(); |
1127 | _open = false; | 1140 | _open = false; |
1128 | } | 1141 | } |
@@ -1276,78 +1289,78 @@ bool OPacketCapturer::open( const QFile& file ) | |||
1276 | { | 1289 | { |
1277 | return true; | 1290 | return true; |
1278 | } | 1291 | } |
1279 | else // close the last opened device | 1292 | else // close the last opened device |
1280 | { | 1293 | { |
1281 | close(); | 1294 | close(); |
1282 | } | 1295 | } |
1283 | } | 1296 | } |
1284 | 1297 | ||
1285 | _name = name; | 1298 | _name = name; |
1286 | 1299 | ||
1287 | pcap_t* handle = pcap_open_offline( const_cast<char*>( (const char*) name ), &_errbuf[0] ); | 1300 | pcap_t* handle = pcap_open_offline( const_cast<char*>( (const char*) name ), &_errbuf[0] ); |
1288 | 1301 | ||
1289 | if ( handle ) | 1302 | if ( handle ) |
1290 | { | 1303 | { |
1291 | odebug << "OPacketCapturer::open(): libpcap opened successfully." << oendl; | 1304 | odebug << "OPacketCapturer::open(): libpcap opened successfully." << oendl; |
1292 | _pch = handle; | 1305 | _pch = handle; |
1293 | _open = true; | 1306 | _open = true; |
1294 | 1307 | ||
1295 | // in case we have an application object, create a socket notifier | 1308 | // in case we have an application object, create a socket notifier |
1296 | if ( qApp ) | 1309 | if ( qApp ) |
1297 | { | 1310 | { |
1298 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); | 1311 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); |
1299 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); | 1312 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); |
1300 | } | 1313 | } |
1301 | 1314 | ||
1302 | return true; | 1315 | return true; |
1303 | } | 1316 | } |
1304 | else | 1317 | else |
1305 | { | 1318 | { |
1306 | odebug << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl; | 1319 | odebug << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl; |
1307 | return false; | 1320 | return false; |
1308 | } | 1321 | } |
1309 | 1322 | ||
1310 | } | 1323 | } |
1311 | 1324 | ||
1312 | 1325 | ||
1313 | bool OPacketCapturer::isOpen() const | 1326 | bool OPacketCapturer::isOpen() const |
1314 | { | 1327 | { |
1315 | return _open; | 1328 | return _open; |
1316 | } | 1329 | } |
1317 | 1330 | ||
1318 | 1331 | ||
1319 | void OPacketCapturer::readyToReceive() | 1332 | void OPacketCapturer::readyToReceive() |
1320 | { | 1333 | { |
1321 | odebug << "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" << oendl; | 1334 | odebug << "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" << oendl; |
1322 | OPacket* p = next(); | 1335 | OPacket* p = next(); |
1323 | emit receivedPacket( p ); | 1336 | emit receivedPacket( p ); |
1324 | // emit is synchronous - packet has been dealt with, now it's safe to delete | 1337 | // emit is synchronous - packet has been dealt with, now it's safe to delete (if enabled) |
1325 | delete p; | 1338 | if ( _autodelete ) delete p; |
1326 | } | 1339 | } |
1327 | 1340 | ||
1328 | 1341 | ||
1329 | const QMap<QString,int>& OPacketCapturer::statistics() const | 1342 | const QMap<QString,int>& OPacketCapturer::statistics() const |
1330 | { | 1343 | { |
1331 | return _stats; | 1344 | return _stats; |
1332 | } | 1345 | } |
1333 | 1346 | ||
1334 | 1347 | ||
1335 | int OPacketCapturer::snapShot() const | 1348 | int OPacketCapturer::snapShot() const |
1336 | { | 1349 | { |
1337 | return pcap_snapshot( _pch ); | 1350 | return pcap_snapshot( _pch ); |
1338 | } | 1351 | } |
1339 | 1352 | ||
1340 | 1353 | ||
1341 | bool OPacketCapturer::swapped() const | 1354 | bool OPacketCapturer::swapped() const |
1342 | { | 1355 | { |
1343 | return pcap_is_swapped( _pch ); | 1356 | return pcap_is_swapped( _pch ); |
1344 | } | 1357 | } |
1345 | 1358 | ||
1346 | 1359 | ||
1347 | QString OPacketCapturer::version() const | 1360 | QString OPacketCapturer::version() const |
1348 | { | 1361 | { |
1349 | return QString().sprintf( "%d.%d", pcap_major_version( _pch ), pcap_minor_version( _pch ) ); | 1362 | return QString().sprintf( "%d.%d", pcap_major_version( _pch ), pcap_minor_version( _pch ) ); |
1350 | } | 1363 | } |
1351 | 1364 | ||
1352 | } | 1365 | } |
1353 | } | 1366 | } |
diff --git a/libopie2/opienet/opcap.h b/libopie2/opienet/opcap.h index 72a78de..4ff8495 100644 --- a/libopie2/opienet/opcap.h +++ b/libopie2/opienet/opcap.h | |||
@@ -89,99 +89,98 @@ class OPacketCapturer; | |||
89 | * | TCP Header | SMTP Header | User Data | == TCP | 89 | * | TCP Header | SMTP Header | User Data | == TCP |
90 | * | IP Header | TCP Header | SMTP Header | User Data | == IP | 90 | * | IP Header | TCP Header | SMTP Header | User Data | == IP |
91 | * | MAC Header | IP Header | TCP Header | SMTP Header | User Data | == MAC | 91 | * | MAC Header | IP Header | TCP Header | SMTP Header | User Data | == MAC |
92 | * | 92 | * |
93 | * </pre> | 93 | * </pre> |
94 | * | 94 | * |
95 | * The example is trimmed for simplicity, because the MAC (Medium Access Control) layer | 95 | * The example is trimmed for simplicity, because the MAC (Medium Access Control) layer |
96 | * also contains a few more levels of encapsulation. | 96 | * also contains a few more levels of encapsulation. |
97 | * Since the type of the payload is more or less independent from the encapsulating protocol, | 97 | * Since the type of the payload is more or less independent from the encapsulating protocol, |
98 | * the header must be inspected before attempting to decode the payload. Hence, the | 98 | * the header must be inspected before attempting to decode the payload. Hence, the |
99 | * encapsulation level varies and can't be deduced without actually looking into the packets. | 99 | * encapsulation level varies and can't be deduced without actually looking into the packets. |
100 | * | 100 | * |
101 | * For actually working with captured frames, it's useful to identify the packets via names and | 101 | * For actually working with captured frames, it's useful to identify the packets via names and |
102 | * insert them into a parent/child - relationship based on the encapsulation. This is why | 102 | * insert them into a parent/child - relationship based on the encapsulation. This is why |
103 | * all packet classes derive from QObject. The amount of overhead caused by the QObject is | 103 | * all packet classes derive from QObject. The amount of overhead caused by the QObject is |
104 | * not a problem in this case, because we're talking about a theoratical maximum of about | 104 | * not a problem in this case, because we're talking about a theoratical maximum of about |
105 | * 10 packets per captured frame. We need to stuff them into a searchable list anyway and the | 105 | * 10 packets per captured frame. We need to stuff them into a searchable list anyway and the |
106 | * QObject also cares about destroying the sub-, (child-) packets. | 106 | * QObject also cares about destroying the sub-, (child-) packets. |
107 | * | 107 | * |
108 | * This enables us to perform a simple look for packets of a certain type: | 108 | * This enables us to perform a simple look for packets of a certain type: |
109 | * @code | 109 | * @code |
110 | * OPacketCapturer* pcap = new OPacketCapturer(); | 110 | * OPacketCapturer* pcap = new OPacketCapturer(); |
111 | * pcap->open( "eth0" ); | 111 | * pcap->open( "eth0" ); |
112 | * OPacket* p = pcap->next(); | 112 | * OPacket* p = pcap->next(); |
113 | * OIPPacket* ip = (OIPPacket*) p->child( "IP" ); // returns 0, if no such child exists | 113 | * OIPPacket* ip = (OIPPacket*) p->child( "IP" ); // returns 0, if no such child exists |
114 | * odebug << "got ip packet from " << ip->fromIPAddress().toString() << " to " << ip->toIPAddress().toString() << oendl; | 114 | * odebug << "got ip packet from " << ip->fromIPAddress().toString() << " to " << ip->toIPAddress().toString() << oendl; |
115 | * | 115 | * |
116 | */ | 116 | */ |
117 | 117 | ||
118 | class OPacket : public QObject | 118 | class OPacket : public QObject |
119 | { | 119 | { |
120 | Q_OBJECT | 120 | Q_OBJECT |
121 | 121 | ||
122 | friend class OPacketCapturer; | 122 | friend class OPacketCapturer; |
123 | friend QTextStream& operator<<( QTextStream& s, const OPacket& p ); | 123 | friend QTextStream& operator<<( QTextStream& s, const OPacket& p ); |
124 | 124 | ||
125 | public: | 125 | public: |
126 | OPacket( int datalink, packetheaderstruct, const unsigned char*, QObject* parent ); | 126 | OPacket( int datalink, packetheaderstruct, const unsigned char*, QObject* parent ); |
127 | virtual ~OPacket(); | 127 | virtual ~OPacket(); |
128 | 128 | ||
129 | timevalstruct timeval() const; | 129 | timevalstruct timeval() const; |
130 | 130 | ||
131 | int caplen() const; | 131 | int caplen() const; |
132 | int len() const; | 132 | int len() const; |
133 | QString dump( int = 32 ) const; | 133 | QString dump( int = 32 ) const; |
134 | 134 | ||
135 | void updateStats( QMap<QString,int>&, QObjectList* ); | 135 | void updateStats( QMap<QString,int>&, QObjectList* ); |
136 | 136 | ||
137 | private: | ||
138 | |||
139 | QString dumpStructure() const; | 137 | QString dumpStructure() const; |
138 | private: | ||
140 | QString _dumpStructure( QObjectList* ) const; | 139 | QString _dumpStructure( QObjectList* ) const; |
141 | 140 | ||
142 | private: | 141 | private: |
143 | const packetheaderstruct _hdr; // pcap packet header | 142 | const packetheaderstruct _hdr; // pcap packet header |
144 | const unsigned char* _data; // pcap packet data | 143 | const unsigned char* _data; // pcap packet data |
145 | const unsigned char* _end; // end of pcap packet data | 144 | const unsigned char* _end; // end of pcap packet data |
146 | private: | 145 | private: |
147 | class Private; | 146 | class Private; |
148 | Private *d; | 147 | Private *d; |
149 | }; | 148 | }; |
150 | 149 | ||
151 | QTextStream& operator<<( QTextStream& s, const OPacket& p ); | 150 | QTextStream& operator<<( QTextStream& s, const OPacket& p ); |
152 | 151 | ||
153 | /*====================================================================================== | 152 | /*====================================================================================== |
154 | * OEthernetPacket - DLT_EN10MB frame | 153 | * OEthernetPacket - DLT_EN10MB frame |
155 | *======================================================================================*/ | 154 | *======================================================================================*/ |
156 | 155 | ||
157 | class OEthernetPacket : public QObject | 156 | class OEthernetPacket : public QObject |
158 | { | 157 | { |
159 | Q_OBJECT | 158 | Q_OBJECT |
160 | 159 | ||
161 | public: | 160 | public: |
162 | OEthernetPacket( const unsigned char*, const struct ether_header*, QObject* parent = 0 ); | 161 | OEthernetPacket( const unsigned char*, const struct ether_header*, QObject* parent = 0 ); |
163 | virtual ~OEthernetPacket(); | 162 | virtual ~OEthernetPacket(); |
164 | 163 | ||
165 | OMacAddress sourceAddress() const; | 164 | OMacAddress sourceAddress() const; |
166 | OMacAddress destinationAddress() const; | 165 | OMacAddress destinationAddress() const; |
167 | int type() const; | 166 | int type() const; |
168 | 167 | ||
169 | private: | 168 | private: |
170 | const struct ether_header* _ether; | 169 | const struct ether_header* _ether; |
171 | private: | 170 | private: |
172 | class Private; | 171 | class Private; |
173 | Private *d; | 172 | Private *d; |
174 | }; | 173 | }; |
175 | 174 | ||
176 | /*====================================================================================== | 175 | /*====================================================================================== |
177 | * OPrismHeaderPacket - DLT_PRISM_HEADER frame | 176 | * OPrismHeaderPacket - DLT_PRISM_HEADER frame |
178 | *======================================================================================*/ | 177 | *======================================================================================*/ |
179 | 178 | ||
180 | class OPrismHeaderPacket : public QObject | 179 | class OPrismHeaderPacket : public QObject |
181 | { | 180 | { |
182 | Q_OBJECT | 181 | Q_OBJECT |
183 | 182 | ||
184 | public: | 183 | public: |
185 | OPrismHeaderPacket( const unsigned char*, const struct prism_hdr*, QObject* parent = 0 ); | 184 | OPrismHeaderPacket( const unsigned char*, const struct prism_hdr*, QObject* parent = 0 ); |
186 | virtual ~OPrismHeaderPacket(); | 185 | virtual ~OPrismHeaderPacket(); |
187 | 186 | ||
@@ -648,74 +647,87 @@ class OPacketCapturer : public QObject | |||
648 | * @returns the file descriptor of the packet capturer. This is only useful, if | 647 | * @returns the file descriptor of the packet capturer. This is only useful, if |
649 | * not using the socket notifier, e.g. without an application object. | 648 | * not using the socket notifier, e.g. without an application object. |
650 | */ | 649 | */ |
651 | int fileno() const; | 650 | int fileno() const; |
652 | /** | 651 | /** |
653 | * @returns the next @ref OPacket from the packet capturer. | 652 | * @returns the next @ref OPacket from the packet capturer. |
654 | * @note If blocking mode is true then this call might block. | 653 | * @note If blocking mode is true then this call might block. |
655 | */ | 654 | */ |
656 | OPacket* next(); | 655 | OPacket* next(); |
657 | /** | 656 | /** |
658 | * @returns the next @ref OPacket from the packet capturer, if | 657 | * @returns the next @ref OPacket from the packet capturer, if |
659 | * one arrives within @a time milliseconds. | 658 | * one arrives within @a time milliseconds. |
660 | */ | 659 | */ |
661 | OPacket* next( int time ); | 660 | OPacket* next( int time ); |
662 | /** | 661 | /** |
663 | * Open the packet capturer to capture packets in live-mode from @a interface. | 662 | * Open the packet capturer to capture packets in live-mode from @a interface. |
664 | */ | 663 | */ |
665 | bool open( const QString& interface ); | 664 | bool open( const QString& interface ); |
666 | /** | 665 | /** |
667 | * Open the packet capturer to capture packets in offline-mode from @a file. | 666 | * Open the packet capturer to capture packets in offline-mode from @a file. |
668 | */ | 667 | */ |
669 | bool open( const QFile& file ); | 668 | bool open( const QFile& file ); |
670 | /** | 669 | /** |
671 | * Open a prerecorded tcpdump compatible capture file for use with @ref dump() | 670 | * Open a prerecorded tcpdump compatible capture file for use with @ref dump() |
672 | */ | 671 | */ |
673 | bool openDumpFile( const QString& filename ); | 672 | bool openDumpFile( const QString& filename ); |
674 | /** | 673 | /** |
675 | * @returns true if the packet capturer is open | 674 | * @returns true if the packet capturer is open |
676 | */ | 675 | */ |
677 | bool isOpen() const; | 676 | bool isOpen() const; |
678 | /** | 677 | /** |
679 | * @returns the snapshot length of this packet capturer | 678 | * @returns the snapshot length of this packet capturer |
680 | */ | 679 | */ |
681 | int snapShot() const; | 680 | int snapShot() const; |
682 | /** | 681 | /** |
683 | * @returns true if the input capture file has a different byte-order | 682 | * @returns true if the input capture file has a different byte-order |
684 | * than the byte-order of the running system. | 683 | * than the byte-order of the running system. |
685 | */ | 684 | */ |
686 | bool swapped() const; | 685 | bool swapped() const; |
687 | /** | 686 | /** |
688 | * @returns the libpcap version string used to write the input capture file. | 687 | * @returns the libpcap version string used to write the input capture file. |
689 | */ | 688 | */ |
690 | QString version() const; | 689 | QString version() const; |
691 | /** | 690 | /** |
692 | * @returns the packet statistic database. | 691 | * @returns the packet statistic database. |
693 | * @see QMap | 692 | * @see QMap |
694 | */ | 693 | */ |
695 | const QMap<QString,int>& statistics() const; | 694 | const QMap<QString,int>& statistics() const; |
695 | /** | ||
696 | * Enable or disable the auto-delete option. | ||
697 | * If auto-delete is enabled, then the packet capturer will delete a packet right | ||
698 | * after it has been emit'ted. This is the default, which is useful if the packet | ||
699 | * capturer has the only reference to the packets. If you pass the packet for adding | ||
700 | * into a collection or do processing after the SLOT, the auto delete must be disabled. | ||
701 | */ | ||
702 | void setAutoDelete( bool enable ); | ||
703 | /** | ||
704 | * @returns the auto-delete value. | ||
705 | */ | ||
706 | bool autoDelete() const; | ||
696 | 707 | ||
697 | signals: | 708 | signals: |
698 | /** | 709 | /** |
699 | * This signal is emitted, when a packet has been received. | 710 | * This signal is emitted, when a packet has been received. |
700 | */ | 711 | */ |
701 | void receivedPacket( Opie::Net::OPacket* ); | 712 | void receivedPacket( Opie::Net::OPacket* ); |
702 | 713 | ||
703 | protected slots: | 714 | protected slots: |
704 | void readyToReceive(); | 715 | void readyToReceive(); |
705 | 716 | ||
706 | protected: | 717 | protected: |
707 | QString _name; // devicename | 718 | QString _name; // devicename |
708 | bool _open; // check this before doing pcap calls | 719 | bool _open; // check this before doing pcap calls |
709 | pcap_t* _pch; // pcap library handle | 720 | pcap_t* _pch; // pcap library handle |
710 | pcap_dumper_t* _pcd; // pcap dumper handle | 721 | pcap_dumper_t* _pcd; // pcap dumper handle |
711 | QSocketNotifier* _sn; // socket notifier for main loop | 722 | QSocketNotifier* _sn; // socket notifier for main loop |
712 | mutable char _errbuf[PCAP_ERRBUF_SIZE]; // holds error strings from libpcap | 723 | mutable char _errbuf[PCAP_ERRBUF_SIZE]; // holds error strings from libpcap |
713 | QMap<QString, int> _stats; // statistics; | 724 | QMap<QString, int> _stats; // statistics; |
714 | class Private; // Private Forward declaration | 725 | bool _autodelete; // if we auto delete packets after emit |
715 | Private *d; // if we need to add data | 726 | class Private; // Private Forward declaration |
727 | Private *d; // if we need to add data | ||
716 | }; | 728 | }; |
717 | } | 729 | } |
718 | } | 730 | } |
719 | 731 | ||
720 | #endif // OPCAP_H | 732 | #endif // OPCAP_H |
721 | 733 | ||