author | mickeyl <mickeyl> | 2003-04-10 15:47:23 (UTC) |
---|---|---|
committer | mickeyl <mickeyl> | 2003-04-10 15:47:23 (UTC) |
commit | 3733471135ea180709fcf4695607cce0c5fc7fb5 (patch) (unidiff) | |
tree | 792c96ad5351b9396a0c129738b5cff9e2b00614 | |
parent | 101e039ba53d4ccbe5b46575bb56c07f6f544db6 (diff) | |
download | opie-3733471135ea180709fcf4695607cce0c5fc7fb5.zip opie-3733471135ea180709fcf4695607cce0c5fc7fb5.tar.gz opie-3733471135ea180709fcf4695607cce0c5fc7fb5.tar.bz2 |
add support for working with capture files (e.g. from tcpdump, ethereal, et.al.)
-rw-r--r-- | libopie2/opienet/opcap.cpp | 48 | ||||
-rw-r--r-- | libopie2/opienet/opcap.h | 64 |
2 files changed, 101 insertions, 11 deletions
diff --git a/libopie2/opienet/opcap.cpp b/libopie2/opienet/opcap.cpp index 6a3dc26..30f6208 100644 --- a/libopie2/opienet/opcap.cpp +++ b/libopie2/opienet/opcap.cpp | |||
@@ -830,3 +830,2 @@ OPacket* OPacketCapturer::next() | |||
830 | // by emit() [ see below ] | 830 | // by emit() [ see below ] |
831 | |||
832 | //TODO: make gathering statistics optional, because it takes time | 831 | //TODO: make gathering statistics optional, because it takes time |
@@ -879,3 +878,48 @@ bool OPacketCapturer::open( const QString& name ) | |||
879 | { | 878 | { |
880 | qDebug( "OPacketCapturer::open(): can't open libpcap: %s", _errbuf ); | 879 | qDebug( "OPacketCapturer::open(): can't open libpcap with '%s': %s", (const char*) name, _errbuf ); |
880 | return false; | ||
881 | } | ||
882 | |||
883 | } | ||
884 | |||
885 | |||
886 | bool OPacketCapturer::open( const QFile& file ) | ||
887 | { | ||
888 | QString name = file.name(); | ||
889 | |||
890 | if ( _open ) | ||
891 | { | ||
892 | close(); | ||
893 | if ( name == _name ) // ignore opening an already openend device | ||
894 | { | ||
895 | return true; | ||
896 | } | ||
897 | else // close the last opened device | ||
898 | { | ||
899 | close(); | ||
900 | } | ||
901 | } | ||
902 | |||
903 | _name = name; | ||
904 | |||
905 | pcap_t* handle = pcap_open_offline( const_cast<char*>( (const char*) name ), &_errbuf[0] ); | ||
906 | |||
907 | if ( handle ) | ||
908 | { | ||
909 | qDebug( "OPacketCapturer::open(): libpcap opened successfully." ); | ||
910 | _pch = handle; | ||
911 | _open = true; | ||
912 | |||
913 | // in case we have an application object, create a socket notifier | ||
914 | if ( qApp ) | ||
915 | { | ||
916 | _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read ); | ||
917 | connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) ); | ||
918 | } | ||
919 | |||
920 | return true; | ||
921 | } | ||
922 | else | ||
923 | { | ||
924 | qDebug( "OPacketCapturer::open(): can't open libpcap with '%s': %s", (const char*) name, _errbuf ); | ||
881 | return false; | 925 | return false; |
diff --git a/libopie2/opienet/opcap.h b/libopie2/opienet/opcap.h index c9b0624..6c3ac6d 100644 --- a/libopie2/opienet/opcap.h +++ b/libopie2/opienet/opcap.h | |||
@@ -49,2 +49,3 @@ extern "C" // work around a bpf/pcap conflict in recent headers | |||
49 | #include <qevent.h> | 49 | #include <qevent.h> |
50 | #include <qfile.h> | ||
50 | #include <qhostaddress.h> | 51 | #include <qhostaddress.h> |
@@ -422,2 +423,8 @@ class OTCPPacket : public QObject | |||
422 | 423 | ||
424 | /** | ||
425 | * @brief A class based wrapper for network packet capturing. | ||
426 | * | ||
427 | * This class is the base of a high-level interface to the well known packet capturing | ||
428 | * library libpcap. ... | ||
429 | */ | ||
423 | class OPacketCapturer : public QObject | 430 | class OPacketCapturer : public QObject |
@@ -427,13 +434,49 @@ class OPacketCapturer : public QObject | |||
427 | public: | 434 | public: |
435 | /** | ||
436 | * Constructor. | ||
437 | */ | ||
428 | OPacketCapturer( QObject* parent = 0, const char* name = 0 ); | 438 | OPacketCapturer( QObject* parent = 0, const char* name = 0 ); |
439 | /** | ||
440 | * Destructor. | ||
441 | */ | ||
429 | ~OPacketCapturer(); | 442 | ~OPacketCapturer(); |
430 | 443 | /** | |
444 | * Setting the packet capturer to use blocking IO calls can be useful when | ||
445 | * not using the socket notifier, e.g. without an application object. | ||
446 | */ | ||
431 | void setBlocking( bool ); | 447 | void setBlocking( bool ); |
448 | /** | ||
449 | * @returns true if the packet capturer uses blocking IO calls. | ||
450 | */ | ||
432 | bool blocking() const; | 451 | bool blocking() const; |
433 | 452 | /** | |
453 | * Closes the packet capturer. This is automatically done in the destructor. | ||
454 | */ | ||
434 | void close(); | 455 | void close(); |
456 | /** | ||
457 | * @returns the data link type. | ||
458 | * @see <pcap.h> for possible values. | ||
459 | */ | ||
435 | int dataLink() const; | 460 | int dataLink() const; |
461 | /** | ||
462 | * @returns the filedescriptor of the packet capturer. This is only useful, if | ||
463 | * not using the socket notifier, e.g. without an application object. | ||
464 | */ | ||
436 | int fileno() const; | 465 | int fileno() const; |
466 | /** | ||
467 | * @returns the next @ref OPacket from the packet capturer. | ||
468 | * @note If blocking mode is true then this call might block. | ||
469 | */ | ||
437 | OPacket* next(); | 470 | OPacket* next(); |
438 | bool open( const QString& name ); | 471 | /** |
472 | * Open the packet capturer to capture packets in live-mode from @a interface. | ||
473 | */ | ||
474 | bool open( const QString& interface ); | ||
475 | /** | ||
476 | * Open the packet capturer to capture packets in offline-mode from @a file. | ||
477 | */ | ||
478 | bool open( const QFile& file ); | ||
479 | /** | ||
480 | * @returns true if the packet capturer is open | ||
481 | */ | ||
439 | bool isOpen() const; | 482 | bool isOpen() const; |
@@ -443,2 +486,5 @@ class OPacketCapturer : public QObject | |||
443 | signals: | 486 | signals: |
487 | /** | ||
488 | * This signal is emitted, when a packet has been received. | ||
489 | */ | ||
444 | void receivedPacket( OPacket* ); | 490 | void receivedPacket( OPacket* ); |
@@ -449,8 +495,8 @@ class OPacketCapturer : public QObject | |||
449 | protected: | 495 | protected: |
450 | QString _name; // devicename | 496 | QString _name; // devicename |
451 | bool _open; // check this before doing pcap calls | 497 | bool _open; // check this before doing pcap calls |
452 | pcap_t* _pch; // pcap library handle | 498 | pcap_t* _pch; // pcap library handle |
453 | QSocketNotifier* _sn; // socket notifier for main loop | 499 | QSocketNotifier* _sn; // socket notifier for main loop |
454 | mutable char _errbuf[PCAP_ERRBUF_SIZE]; | 500 | mutable char _errbuf[PCAP_ERRBUF_SIZE]; // holds error strings from libpcap |
455 | QMap<QString, int> _stats; // statistics; | 501 | QMap<QString, int> _stats; // statistics; |
456 | }; | 502 | }; |