-rw-r--r-- | libopie2/opienet/opcap.cpp | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/libopie2/opienet/opcap.cpp b/libopie2/opienet/opcap.cpp index 9ed2b83..38ca1a1 100644 --- a/libopie2/opienet/opcap.cpp +++ b/libopie2/opienet/opcap.cpp | |||
@@ -23,33 +23,37 @@ | |||
23 | ++= -. .` .: details. | 23 | ++= -. .` .: details. |
24 | : = ...= . :.=- | 24 | : = ...= . :.=- |
25 | -. .:....=;==+<; You should have received a copy of the GNU | 25 | -. .:....=;==+<; You should have received a copy of the GNU |
26 | -_. . . )=. = Library General Public License along with | 26 | -_. . . )=. = Library General Public License along with |
27 | -- :-=` this library; see the file COPYING.LIB. | 27 | -- :-=` this library; see the file COPYING.LIB. |
28 | If not, write to the Free Software Foundation, | 28 | If not, write to the Free Software Foundation, |
29 | Inc., 59 Temple Place - Suite 330, | 29 | Inc., 59 Temple Place - Suite 330, |
30 | Boston, MA 02111-1307, USA. | 30 | Boston, MA 02111-1307, USA. |
31 | 31 | ||
32 | */ | 32 | */ |
33 | 33 | ||
34 | /* OPIE */ | 34 | /* OPIE */ |
35 | |||
36 | #include <opie2/opcap.h> | 35 | #include <opie2/opcap.h> |
37 | 36 | ||
38 | /* QT */ | 37 | /* QT */ |
39 | |||
40 | #include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects) | 38 | #include <qapplication.h> // don't use oapplication here (will decrease reusability in other projects) |
41 | #include <qsocketnotifier.h> | 39 | #include <qsocketnotifier.h> |
42 | #include <qobjectlist.h> | 40 | #include <qobjectlist.h> |
43 | 41 | ||
42 | /* SYSTEM */ | ||
43 | #include <sys/time.h> | ||
44 | #include <sys/types.h> | ||
45 | #include <unistd.h> | ||
46 | |||
47 | /* LOCAL */ | ||
44 | #include "udp_ports.h" | 48 | #include "udp_ports.h" |
45 | 49 | ||
46 | /*====================================================================================== | 50 | /*====================================================================================== |
47 | * OPacket | 51 | * OPacket |
48 | *======================================================================================*/ | 52 | *======================================================================================*/ |
49 | 53 | ||
50 | OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent ) | 54 | OPacket::OPacket( int datalink, packetheaderstruct header, const unsigned char* data, QObject* parent ) |
51 | :QObject( parent, "Generic" ), _hdr( header ), _data( data ) | 55 | :QObject( parent, "Generic" ), _hdr( header ), _data( data ) |
52 | { | 56 | { |
53 | //qDebug( "OPacket::OPacket(): (Len %d, CapLen %d)" /*, ctime((const time_t*) header.ts.tv_sec)*/, header.len, header.caplen ); | 57 | //qDebug( "OPacket::OPacket(): (Len %d, CapLen %d)" /*, ctime((const time_t*) header.ts.tv_sec)*/, header.len, header.caplen ); |
54 | 58 | ||
55 | _end = (unsigned char*) data + header.len; | 59 | _end = (unsigned char*) data + header.len; |
@@ -187,25 +191,25 @@ OEthernetPacket::OEthernetPacket( const unsigned char* end, const struct ether_h | |||
187 | 191 | ||
188 | qDebug( "Source = %s", (const char*) sourceAddress().toString() ); | 192 | qDebug( "Source = %s", (const char*) sourceAddress().toString() ); |
189 | qDebug( "Destination = %s", (const char*) destinationAddress().toString() ); | 193 | qDebug( "Destination = %s", (const char*) destinationAddress().toString() ); |
190 | 194 | ||
191 | if ( sourceAddress() == OMacAddress::broadcast ) | 195 | if ( sourceAddress() == OMacAddress::broadcast ) |
192 | qDebug( "Source is broadcast address" ); | 196 | qDebug( "Source is broadcast address" ); |
193 | if ( destinationAddress() == OMacAddress::broadcast ) | 197 | if ( destinationAddress() == OMacAddress::broadcast ) |
194 | qDebug( "Destination is broadcast address" ); | 198 | qDebug( "Destination is broadcast address" ); |
195 | 199 | ||
196 | switch ( type() ) | 200 | switch ( type() ) |
197 | { | 201 | { |
198 | case ETHERTYPE_IP: new OIPPacket( end, (const struct iphdr*) (data+1), this ); break; | 202 | case ETHERTYPE_IP: new OIPPacket( end, (const struct iphdr*) (data+1), this ); break; |
199 | case ETHERTYPE_ARP: { qDebug( "OPacket::OPacket(): Received Ethernet Packet : Type = ARP" ); break; } | 203 | case ETHERTYPE_ARP: new OARPPacket( end, (const struct myarphdr*) (data+1), this ); break; |
200 | case ETHERTYPE_REVARP: { qDebug( "OPacket::OPacket(): Received Ethernet Packet : Type = RARP" ); break; } | 204 | case ETHERTYPE_REVARP: { qDebug( "OPacket::OPacket(): Received Ethernet Packet : Type = RARP" ); break; } |
201 | default: qDebug( "OPacket::OPacket(): Received Ethernet Packet : Type = UNKNOWN" ); | 205 | default: qDebug( "OPacket::OPacket(): Received Ethernet Packet : Type = UNKNOWN" ); |
202 | } | 206 | } |
203 | 207 | ||
204 | } | 208 | } |
205 | 209 | ||
206 | 210 | ||
207 | OEthernetPacket::~OEthernetPacket() | 211 | OEthernetPacket::~OEthernetPacket() |
208 | { | 212 | { |
209 | } | 213 | } |
210 | 214 | ||
211 | 215 | ||
@@ -1121,24 +1125,41 @@ void OPacketCapturer::dump( OPacket* p ) | |||
1121 | int OPacketCapturer::fileno() const | 1125 | int OPacketCapturer::fileno() const |
1122 | { | 1126 | { |
1123 | if ( _open ) | 1127 | if ( _open ) |
1124 | { | 1128 | { |
1125 | return pcap_fileno( _pch ); | 1129 | return pcap_fileno( _pch ); |
1126 | } | 1130 | } |
1127 | else | 1131 | else |
1128 | { | 1132 | { |
1129 | return -1; | 1133 | return -1; |
1130 | } | 1134 | } |
1131 | } | 1135 | } |
1132 | 1136 | ||
1137 | |||
1138 | OPacket* OPacketCapturer::next( int time ) | ||
1139 | { | ||
1140 | fd_set fds; | ||
1141 | struct timeval tv; | ||
1142 | FD_ZERO( &fds ); | ||
1143 | FD_SET( pcap_fileno( _pch ), &fds ); | ||
1144 | tv.tv_sec = time / 1000; | ||
1145 | tv.tv_usec = time % 1000; | ||
1146 | int retval = select( 1, &fds, NULL, NULL, &tv); | ||
1147 | if ( retval > 0 ) // clear to read! | ||
1148 | return next(); | ||
1149 | else | ||
1150 | return 0; | ||
1151 | } | ||
1152 | |||
1153 | |||
1133 | OPacket* OPacketCapturer::next() | 1154 | OPacket* OPacketCapturer::next() |
1134 | { | 1155 | { |
1135 | packetheaderstruct header; | 1156 | packetheaderstruct header; |
1136 | qDebug( "==> OPacketCapturer::next()" ); | 1157 | qDebug( "==> OPacketCapturer::next()" ); |
1137 | const unsigned char* pdata = pcap_next( _pch, &header ); | 1158 | const unsigned char* pdata = pcap_next( _pch, &header ); |
1138 | qDebug( "<== OPacketCapturer::next()" ); | 1159 | qDebug( "<== OPacketCapturer::next()" ); |
1139 | 1160 | ||
1140 | if ( pdata && header.len ) | 1161 | if ( pdata && header.len ) |
1141 | { | 1162 | { |
1142 | OPacket* p = new OPacket( dataLink(), header, pdata, 0 ); | 1163 | OPacket* p = new OPacket( dataLink(), header, pdata, 0 ); |
1143 | // packets shouldn't be inserted in the QObject child-parent hierarchy, | 1164 | // packets shouldn't be inserted in the QObject child-parent hierarchy, |
1144 | // because due to memory constraints they will be deleted as soon | 1165 | // because due to memory constraints they will be deleted as soon |