author | mickeyl <mickeyl> | 2003-04-10 15:47:23 (UTC) |
---|---|---|
committer | mickeyl <mickeyl> | 2003-04-10 15:47:23 (UTC) |
commit | 3733471135ea180709fcf4695607cce0c5fc7fb5 (patch) (side-by-side diff) | |
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 @@ -819,25 +819,24 @@ OPacket* OPacketCapturer::next() packetheaderstruct header; qDebug( "==> OPacketCapturer::next()" ); const unsigned char* pdata = pcap_next( _pch, &header ); qDebug( "<== OPacketCapturer::next()" ); if ( header.len ) { OPacket* p = new OPacket( dataLink(), header, pdata, 0 ); // packets shouldn't be inserted in the QObject child-parent hierarchy, // because due to memory constraints they will be deleted as soon // as possible - that is right after they have been processed // by emit() [ see below ] - //TODO: make gathering statistics optional, because it takes time p->updateStats( _stats, const_cast<QObjectList*>( p->children() ) ); return p; } else { return 0; } } @@ -868,25 +867,70 @@ bool OPacketCapturer::open( const QString& name ) // 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: %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; } } bool OPacketCapturer::isOpen() const { return _open; } 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 @@ -38,24 +38,25 @@ extern "C" // work around a bpf/pcap conflict in recent headers { #include <pcap.h> } #include <netinet/ether.h> #include <netinet/ip.h> #include <netinet/udp.h> #include <netinet/tcp.h> #include <time.h> /* QT */ #include <qevent.h> +#include <qfile.h> #include <qhostaddress.h> #include <qobject.h> #include <qstring.h> #include <qmap.h> /* OPIE */ #include <opie2/onetutils.h> #include "802_11_user.h" /* TYPEDEFS */ typedef struct timeval timevalstruct; typedef struct pcap_pkthdr packetheaderstruct; @@ -411,49 +412,94 @@ class OTCPPacket : public QObject int fromPort() const; int toPort() const; private: const struct tcphdr* _tcphdr; }; /*====================================================================================== * OPacketCapturer *======================================================================================*/ +/** + * @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 { Q_OBJECT 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; const QMap<QString,int>& statistics() const; signals: + /** + * This signal is emitted, when a packet has been received. + */ void receivedPacket( OPacket* ); protected slots: void readyToReceive(); 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; }; #endif // OPCAP_H |