summaryrefslogtreecommitdiff
path: root/libopie2
Side-by-side diff
Diffstat (limited to 'libopie2') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opienet/opcap.cpp48
-rw-r--r--libopie2/opienet/opcap.h64
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()
// by emit() [ see below ]
-
//TODO: make gathering statistics optional, because it takes time
@@ -879,3 +878,48 @@ bool OPacketCapturer::open( const QString& name )
{
- qDebug( "OPacketCapturer::open(): can't open libpcap: %s", _errbuf );
+ qDebug( "OPacketCapturer::open(): can't open libpcap with '%s': %s", (const char*) name, _errbuf );
+ return false;
+ }
+
+}
+
+
+bool OPacketCapturer::open( const QFile& file )
+{
+ QString name = file.name();
+
+ if ( _open )
+ {
+ close();
+ if ( name == _name ) // ignore opening an already openend device
+ {
+ return true;
+ }
+ else // close the last opened device
+ {
+ close();
+ }
+ }
+
+ _name = name;
+
+ pcap_t* handle = pcap_open_offline( const_cast<char*>( (const char*) name ), &_errbuf[0] );
+
+ if ( handle )
+ {
+ qDebug( "OPacketCapturer::open(): libpcap opened successfully." );
+ _pch = handle;
+ _open = true;
+
+ // in case we have an application object, create a socket notifier
+ if ( qApp )
+ {
+ _sn = new QSocketNotifier( fileno(), QSocketNotifier::Read );
+ connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) );
+ }
+
+ return true;
+ }
+ else
+ {
+ qDebug( "OPacketCapturer::open(): can't open libpcap with '%s': %s", (const char*) name, _errbuf );
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
#include <qevent.h>
+#include <qfile.h>
#include <qhostaddress.h>
@@ -422,2 +423,8 @@ class OTCPPacket : public QObject
+/**
+ * @brief A class based wrapper for network packet capturing.
+ *
+ * This class is the base of a high-level interface to the well known packet capturing
+ * library libpcap. ...
+ */
class OPacketCapturer : public QObject
@@ -427,13 +434,49 @@ class OPacketCapturer : public QObject
public:
+ /**
+ * Constructor.
+ */
OPacketCapturer( QObject* parent = 0, const char* name = 0 );
+ /**
+ * Destructor.
+ */
~OPacketCapturer();
-
+ /**
+ * Setting the packet capturer to use blocking IO calls can be useful when
+ * not using the socket notifier, e.g. without an application object.
+ */
void setBlocking( bool );
+ /**
+ * @returns true if the packet capturer uses blocking IO calls.
+ */
bool blocking() const;
-
+ /**
+ * Closes the packet capturer. This is automatically done in the destructor.
+ */
void close();
+ /**
+ * @returns the data link type.
+ * @see <pcap.h> for possible values.
+ */
int dataLink() const;
+ /**
+ * @returns the filedescriptor of the packet capturer. This is only useful, if
+ * not using the socket notifier, e.g. without an application object.
+ */
int fileno() const;
+ /**
+ * @returns the next @ref OPacket from the packet capturer.
+ * @note If blocking mode is true then this call might block.
+ */
OPacket* next();
- bool open( const QString& name );
+ /**
+ * Open the packet capturer to capture packets in live-mode from @a interface.
+ */
+ bool open( const QString& interface );
+ /**
+ * Open the packet capturer to capture packets in offline-mode from @a file.
+ */
+ bool open( const QFile& file );
+ /**
+ * @returns true if the packet capturer is open
+ */
bool isOpen() const;
@@ -443,2 +486,5 @@ class OPacketCapturer : public QObject
signals:
+ /**
+ * This signal is emitted, when a packet has been received.
+ */
void receivedPacket( OPacket* );
@@ -449,8 +495,8 @@ class OPacketCapturer : public QObject
protected:
- QString _name; // devicename
- bool _open; // check this before doing pcap calls
- pcap_t* _pch; // pcap library handle
- QSocketNotifier* _sn; // socket notifier for main loop
- mutable char _errbuf[PCAP_ERRBUF_SIZE];
- QMap<QString, int> _stats; // statistics;
+ QString _name; // devicename
+ bool _open; // check this before doing pcap calls
+ pcap_t* _pch; // pcap library handle
+ QSocketNotifier* _sn; // socket notifier for main loop
+ mutable char _errbuf[PCAP_ERRBUF_SIZE]; // holds error strings from libpcap
+ QMap<QString, int> _stats; // statistics;
};