summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opienet/opcap.cpp4
-rw-r--r--libopie2/opienet/opcap.h4
2 files changed, 3 insertions, 5 deletions
diff --git a/libopie2/opienet/opcap.cpp b/libopie2/opienet/opcap.cpp
index 067d6b7..f8ebe6b 100644
--- a/libopie2/opienet/opcap.cpp
+++ b/libopie2/opienet/opcap.cpp
@@ -1190,182 +1190,180 @@ OPacket* OPacketCapturer::next( int time )
1190 struct timeval tv; 1190 struct timeval tv;
1191 FD_ZERO( &fds ); 1191 FD_ZERO( &fds );
1192 FD_SET( pcap_fileno( _pch ), &fds ); 1192 FD_SET( pcap_fileno( _pch ), &fds );
1193 tv.tv_sec = time / 1000; 1193 tv.tv_sec = time / 1000;
1194 tv.tv_usec = time % 1000; 1194 tv.tv_usec = time % 1000;
1195 int retval = select( pcap_fileno( _pch )+1, &fds, NULL, NULL, &tv); 1195 int retval = select( pcap_fileno( _pch )+1, &fds, NULL, NULL, &tv);
1196 if ( retval > 0 ) // clear to read! 1196 if ( retval > 0 ) // clear to read!
1197 return next(); 1197 return next();
1198 else 1198 else
1199 return 0; 1199 return 0;
1200} 1200}
1201 1201
1202 1202
1203OPacket* OPacketCapturer::next() 1203OPacket* OPacketCapturer::next()
1204{ 1204{
1205 packetheaderstruct header; 1205 packetheaderstruct header;
1206 odebug << "==> OPacketCapturer::next()" << oendl; 1206 odebug << "==> OPacketCapturer::next()" << oendl;
1207 const unsigned char* pdata = pcap_next( _pch, &header ); 1207 const unsigned char* pdata = pcap_next( _pch, &header );
1208 odebug << "<== OPacketCapturer::next()" << oendl; 1208 odebug << "<== OPacketCapturer::next()" << oendl;
1209 1209
1210 if ( pdata && header.len ) 1210 if ( pdata && header.len )
1211 { 1211 {
1212 OPacket* p = new OPacket( dataLink(), header, pdata, 0 ); 1212 OPacket* p = new OPacket( dataLink(), header, pdata, 0 );
1213 // packets shouldn't be inserted in the QObject child-parent hierarchy, 1213 // packets shouldn't be inserted in the QObject child-parent hierarchy,
1214 // because due to memory constraints they will be deleted as soon 1214 // because due to memory constraints they will be deleted as soon
1215 // as possible - that is right after they have been processed 1215 // as possible - that is right after they have been processed
1216 // by emit() [ see below ] 1216 // by emit() [ see below ]
1217 //TODO: make gathering statistics optional, because it takes time 1217 //TODO: make gathering statistics optional, because it takes time
1218 p->updateStats( _stats, const_cast<QObjectList*>( p->children() ) ); 1218 p->updateStats( _stats, const_cast<QObjectList*>( p->children() ) );
1219 odebug << "OPacket::dumpStructure: " << p->dumpStructure() << oendl; 1219 odebug << "OPacket::dumpStructure: " << p->dumpStructure() << oendl;
1220 return p; 1220 return p;
1221 } 1221 }
1222 else 1222 else
1223 { 1223 {
1224 owarn << "OPacketCapturer::next() - no packet received!" << oendl; 1224 owarn << "OPacketCapturer::next() - no packet received!" << oendl;
1225 return 0; 1225 return 0;
1226 } 1226 }
1227} 1227}
1228 1228
1229 1229
1230bool OPacketCapturer::open( const QString& name ) 1230bool OPacketCapturer::open( const QString& name )
1231{ 1231{
1232 if ( _open ) 1232 if ( _open )
1233 { 1233 {
1234 if ( name == _name ) // ignore opening an already openend device 1234 if ( name == _name ) // ignore opening an already openend device
1235 { 1235 {
1236 return true; 1236 return true;
1237 } 1237 }
1238 else // close the last opened device 1238 else // close the last opened device
1239 { 1239 {
1240 close(); 1240 close();
1241 } 1241 }
1242 } 1242 }
1243 1243
1244 _name = name; 1244 _name = name;
1245 1245
1246 // open libpcap 1246 // open libpcap
1247 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] );
1248 1248
1249 if ( !handle ) 1249 if ( !handle )
1250 { 1250 {
1251 owarn << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl; 1251 owarn << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl;
1252 return false; 1252 return false;
1253 } 1253 }
1254 1254
1255 odebug << "OPacketCapturer::open(): libpcap [" << name << "] opened successfully." << oendl; 1255 odebug << "OPacketCapturer::open(): libpcap [" << name << "] opened successfully." << oendl;
1256 _pch = handle; 1256 _pch = handle;
1257 _open = true; 1257 _open = true;
1258 _stats.clear(); 1258 _stats.clear();
1259 1259
1260 // in case we have an application object, create a socket notifier 1260 // in case we have an application object, create a socket notifier
1261 if ( qApp ) //TODO: I don't like this here... 1261 if ( qApp ) //TODO: I don't like this here...
1262 { 1262 {
1263 _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); 1263 _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read );
1264 connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); 1264 connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) );
1265 } 1265 }
1266 1266
1267 return true; 1267 return true;
1268} 1268}
1269 1269
1270 1270
1271bool OPacketCapturer::openDumpFile( const QString& filename ) 1271bool OPacketCapturer::openDumpFile( const QString& filename )
1272{ 1272{
1273 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 ) );
1274 if ( !dump ) 1274 if ( !dump )
1275 { 1275 {
1276 owarn << "OPacketCapturer::open(): can't open dump with '" << filename << "': " << _errbuf << oendl; 1276 owarn << "OPacketCapturer::open(): can't open dump with '" << filename << "': " << _errbuf << oendl;
1277 return false; 1277 return false;
1278 } 1278 }
1279 odebug << "OPacketCapturer::open(): dump [" << filename << "] opened successfully." << oendl; 1279 odebug << "OPacketCapturer::open(): dump [" << filename << "] opened successfully." << oendl;
1280 _pcd = dump; 1280 _pcd = dump;
1281 1281
1282 return true; 1282 return true;
1283} 1283}
1284 1284
1285 1285
1286bool OPacketCapturer::open( const QFile& file ) 1286bool OPacketCapturer::openCaptureFile( const QString& name )
1287{ 1287{
1288 QString name = file.name();
1289
1290 if ( _open ) 1288 if ( _open )
1291 { 1289 {
1292 close(); 1290 close();
1293 if ( name == _name ) // ignore opening an already openend device 1291 if ( name == _name ) // ignore opening an already openend device
1294 { 1292 {
1295 return true; 1293 return true;
1296 } 1294 }
1297 else // close the last opened device 1295 else // close the last opened device
1298 { 1296 {
1299 close(); 1297 close();
1300 } 1298 }
1301 } 1299 }
1302 1300
1303 _name = name; 1301 _name = name;
1304 1302
1305 pcap_t* handle = pcap_open_offline( const_cast<char*>( (const char*) name ), &_errbuf[0] ); 1303 pcap_t* handle = pcap_open_offline( const_cast<char*>( (const char*) name ), &_errbuf[0] );
1306 1304
1307 if ( handle ) 1305 if ( handle )
1308 { 1306 {
1309 odebug << "OPacketCapturer::open(): libpcap opened successfully." << oendl; 1307 odebug << "OPacketCapturer::open(): libpcap opened successfully." << oendl;
1310 _pch = handle; 1308 _pch = handle;
1311 _open = true; 1309 _open = true;
1312 1310
1313 // in case we have an application object, create a socket notifier 1311 // in case we have an application object, create a socket notifier
1314 if ( qApp ) 1312 if ( qApp )
1315 { 1313 {
1316 _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); 1314 _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read );
1317 connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); 1315 connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) );
1318 } 1316 }
1319 1317
1320 return true; 1318 return true;
1321 } 1319 }
1322 else 1320 else
1323 { 1321 {
1324 odebug << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl; 1322 odebug << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl;
1325 return false; 1323 return false;
1326 } 1324 }
1327 1325
1328} 1326}
1329 1327
1330 1328
1331bool OPacketCapturer::isOpen() const 1329bool OPacketCapturer::isOpen() const
1332{ 1330{
1333 return _open; 1331 return _open;
1334} 1332}
1335 1333
1336 1334
1337void OPacketCapturer::readyToReceive() 1335void OPacketCapturer::readyToReceive()
1338{ 1336{
1339 odebug << "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" << oendl; 1337 odebug << "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" << oendl;
1340 OPacket* p = next(); 1338 OPacket* p = next();
1341 emit receivedPacket( p ); 1339 emit receivedPacket( p );
1342 // emit is synchronous - packet has been dealt with, now it's safe to delete (if enabled) 1340 // emit is synchronous - packet has been dealt with, now it's safe to delete (if enabled)
1343 if ( _autodelete ) delete p; 1341 if ( _autodelete ) delete p;
1344} 1342}
1345 1343
1346 1344
1347const QMap<QString,int>& OPacketCapturer::statistics() const 1345const QMap<QString,int>& OPacketCapturer::statistics() const
1348{ 1346{
1349 return _stats; 1347 return _stats;
1350} 1348}
1351 1349
1352 1350
1353int OPacketCapturer::snapShot() const 1351int OPacketCapturer::snapShot() const
1354{ 1352{
1355 return pcap_snapshot( _pch ); 1353 return pcap_snapshot( _pch );
1356} 1354}
1357 1355
1358 1356
1359bool OPacketCapturer::swapped() const 1357bool OPacketCapturer::swapped() const
1360{ 1358{
1361 return pcap_is_swapped( _pch ); 1359 return pcap_is_swapped( _pch );
1362} 1360}
1363 1361
1364 1362
1365QString OPacketCapturer::version() const 1363QString OPacketCapturer::version() const
1366{ 1364{
1367 return QString().sprintf( "%d.%d", pcap_major_version( _pch ), pcap_minor_version( _pch ) ); 1365 return QString().sprintf( "%d.%d", pcap_major_version( _pch ), pcap_minor_version( _pch ) );
1368} 1366}
1369 1367
1370} 1368}
1371} 1369}
diff --git a/libopie2/opienet/opcap.h b/libopie2/opienet/opcap.h
index 2dbe030..9e718d5 100644
--- a/libopie2/opienet/opcap.h
+++ b/libopie2/opienet/opcap.h
@@ -570,164 +570,164 @@ class ODHCPPacket : public QObject
570/*====================================================================================== 570/*======================================================================================
571 * OTCPPacket 571 * OTCPPacket
572 *======================================================================================*/ 572 *======================================================================================*/
573 573
574class OTCPPacket : public QObject 574class OTCPPacket : public QObject
575{ 575{
576 Q_OBJECT 576 Q_OBJECT
577 577
578 public: 578 public:
579 OTCPPacket( const unsigned char*, const struct tcphdr*, QObject* parent = 0 ); 579 OTCPPacket( const unsigned char*, const struct tcphdr*, QObject* parent = 0 );
580 virtual ~OTCPPacket(); 580 virtual ~OTCPPacket();
581 581
582 int fromPort() const; 582 int fromPort() const;
583 int toPort() const; 583 int toPort() const;
584 int seq() const; 584 int seq() const;
585 int ack() const; 585 int ack() const;
586 int window() const; 586 int window() const;
587 int checksum() const; 587 int checksum() const;
588 588
589 private: 589 private:
590 const struct tcphdr* _tcphdr; 590 const struct tcphdr* _tcphdr;
591 class Private; 591 class Private;
592 Private *d; 592 Private *d;
593}; 593};
594 594
595 595
596/*====================================================================================== 596/*======================================================================================
597 * OPacketCapturer 597 * OPacketCapturer
598 *======================================================================================*/ 598 *======================================================================================*/
599 599
600/** 600/**
601 * @brief A class based wrapper for network packet capturing. 601 * @brief A class based wrapper for network packet capturing.
602 * 602 *
603 * This class is the base of a high-level interface to the well known packet capturing 603 * This class is the base of a high-level interface to the well known packet capturing
604 * library libpcap. 604 * library libpcap.
605 * @see http://tcpdump.org 605 * @see http://tcpdump.org
606 */ 606 */
607class OPacketCapturer : public QObject 607class OPacketCapturer : public QObject
608{ 608{
609 Q_OBJECT 609 Q_OBJECT
610 610
611 public: 611 public:
612 /** 612 /**
613 * Constructor. 613 * Constructor.
614 */ 614 */
615 OPacketCapturer( QObject* parent = 0, const char* name = 0 ); 615 OPacketCapturer( QObject* parent = 0, const char* name = 0 );
616 /** 616 /**
617 * Destructor. 617 * Destructor.
618 */ 618 */
619 ~OPacketCapturer(); 619 ~OPacketCapturer();
620 /** 620 /**
621 * Set the packet capturer to use blocking or non-blocking IO. This can be useful when 621 * Set the packet capturer to use blocking or non-blocking IO. This can be useful when
622 * not using the socket notifier, e.g. without an application object. 622 * not using the socket notifier, e.g. without an application object.
623 */ 623 */
624 void setBlocking( bool ); 624 void setBlocking( bool );
625 /** 625 /**
626 * @returns true if the packet capturer uses blocking IO calls. 626 * @returns true if the packet capturer uses blocking IO calls.
627 */ 627 */
628 bool blocking() const; 628 bool blocking() const;
629 /** 629 /**
630 * Close the packet capturer. This is automatically done in the destructor. 630 * Close the packet capturer. This is automatically done in the destructor.
631 */ 631 */
632 void close(); 632 void close();
633 /** 633 /**
634 * Close the output capture file. 634 * Close the output capture file.
635 */ 635 */
636 void closeDumpFile(); 636 void closeDumpFile();
637 /** 637 /**
638 * @returns the data link type. 638 * @returns the data link type.
639 * @see <pcap.h> for possible values. 639 * @see <pcap.h> for possible values.
640 */ 640 */
641 int dataLink() const; 641 int dataLink() const;
642 /** 642 /**
643 * Dump a packet to the output capture file. 643 * Dump a packet to the output capture file.
644 */ 644 */
645 void dump( OPacket* ); 645 void dump( OPacket* );
646 /** 646 /**
647 * @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
648 * not using the socket notifier, e.g. without an application object. 648 * not using the socket notifier, e.g. without an application object.
649 */ 649 */
650 int fileno() const; 650 int fileno() const;
651 /** 651 /**
652 * @returns the next @ref OPacket from the packet capturer. 652 * @returns the next @ref OPacket from the packet capturer.
653 * @note If blocking mode is true then this call might block. 653 * @note If blocking mode is true then this call might block.
654 */ 654 */
655 OPacket* next(); 655 OPacket* next();
656 /** 656 /**
657 * @returns the next @ref OPacket from the packet capturer, if 657 * @returns the next @ref OPacket from the packet capturer, if
658 * one arrives within @a time milliseconds. 658 * one arrives within @a time milliseconds.
659 */ 659 */
660 OPacket* next( int time ); 660 OPacket* next( int time );
661 /** 661 /**
662 * 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.
663 */ 663 */
664 bool open( const QString& interface ); 664 bool open( const QString& interface );
665 /** 665 /**
666 * 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 filename.
667 */ 667 */
668 bool open( const QFile& file ); 668 bool openCaptureFile( const QString& filename );
669 /** 669 /**
670 * 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()
671 */ 671 */
672 bool openDumpFile( const QString& filename ); 672 bool openDumpFile( const QString& filename );
673 /** 673 /**
674 * @returns true if the packet capturer is open 674 * @returns true if the packet capturer is open
675 */ 675 */
676 bool isOpen() const; 676 bool isOpen() const;
677 /** 677 /**
678 * @returns the snapshot length of this packet capturer 678 * @returns the snapshot length of this packet capturer
679 */ 679 */
680 int snapShot() const; 680 int snapShot() const;
681 /** 681 /**
682 * @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
683 * than the byte-order of the running system. 683 * than the byte-order of the running system.
684 */ 684 */
685 bool swapped() const; 685 bool swapped() const;
686 /** 686 /**
687 * @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.
688 */ 688 */
689 QString version() const; 689 QString version() const;
690 /** 690 /**
691 * @returns the packet statistic database. 691 * @returns the packet statistic database.
692 * @see QMap 692 * @see QMap
693 */ 693 */
694 const QMap<QString,int>& statistics() const; 694 const QMap<QString,int>& statistics() const;
695 /** 695 /**
696 * Enable or disable the auto-delete option. 696 * Enable or disable the auto-delete option.
697 * If auto-delete is enabled, then the packet capturer will delete a packet right 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 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 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. 700 * into a collection or do processing after the SLOT, the auto delete must be disabled.
701 */ 701 */
702 void setAutoDelete( bool enable ); 702 void setAutoDelete( bool enable );
703 /** 703 /**
704 * @returns the auto-delete value. 704 * @returns the auto-delete value.
705 */ 705 */
706 bool autoDelete() const; 706 bool autoDelete() const;
707 707
708 signals: 708 signals:
709 /** 709 /**
710 * This signal is emitted, when a packet has been received. 710 * This signal is emitted, when a packet has been received.
711 */ 711 */
712 void receivedPacket( Opie::Net::OPacket* ); 712 void receivedPacket( Opie::Net::OPacket* );
713 713
714 protected slots: 714 protected slots:
715 void readyToReceive(); 715 void readyToReceive();
716 716
717 protected: 717 protected:
718 QString _name; // devicename 718 QString _name; // devicename
719 bool _open; // check this before doing pcap calls 719 bool _open; // check this before doing pcap calls
720 pcap_t* _pch; // pcap library handle 720 pcap_t* _pch; // pcap library handle
721 pcap_dumper_t* _pcd; // pcap dumper handle 721 pcap_dumper_t* _pcd; // pcap dumper handle
722 QSocketNotifier* _sn; // socket notifier for main loop 722 QSocketNotifier* _sn; // socket notifier for main loop
723 mutable char _errbuf[PCAP_ERRBUF_SIZE]; // holds error strings from libpcap 723 mutable char _errbuf[PCAP_ERRBUF_SIZE]; // holds error strings from libpcap
724 QMap<QString, int> _stats; // statistics; 724 QMap<QString, int> _stats; // statistics;
725 bool _autodelete; // if we auto delete packets after emit 725 bool _autodelete; // if we auto delete packets after emit
726 class Private; // Private Forward declaration 726 class Private; // Private Forward declaration
727 Private *d; // if we need to add data 727 Private *d; // if we need to add data
728}; 728};
729} 729}
730} 730}
731 731
732#endif // OPCAP_H 732#endif // OPCAP_H
733 733