summaryrefslogtreecommitdiff
path: root/libopie2
Side-by-side diff
Diffstat (limited to 'libopie2') (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 )
struct timeval tv;
FD_ZERO( &fds );
FD_SET( pcap_fileno( _pch ), &fds );
tv.tv_sec = time / 1000;
tv.tv_usec = time % 1000;
int retval = select( pcap_fileno( _pch )+1, &fds, NULL, NULL, &tv);
if ( retval > 0 ) // clear to read!
return next();
else
return 0;
}
OPacket* OPacketCapturer::next()
{
packetheaderstruct header;
odebug << "==> OPacketCapturer::next()" << oendl;
const unsigned char* pdata = pcap_next( _pch, &header );
odebug << "<== OPacketCapturer::next()" << oendl;
if ( pdata && 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() ) );
odebug << "OPacket::dumpStructure: " << p->dumpStructure() << oendl;
return p;
}
else
{
owarn << "OPacketCapturer::next() - no packet received!" << oendl;
return 0;
}
}
bool OPacketCapturer::open( const QString& name )
{
if ( _open )
{
if ( name == _name ) // ignore opening an already openend device
{
return true;
}
else // close the last opened device
{
close();
}
}
_name = name;
// open libpcap
pcap_t* handle = pcap_open_live( const_cast<char*>( (const char*) name ), 1024, 0, 0, &_errbuf[0] );
if ( !handle )
{
owarn << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl;
return false;
}
odebug << "OPacketCapturer::open(): libpcap [" << name << "] opened successfully." << oendl;
_pch = handle;
_open = true;
_stats.clear();
// in case we have an application object, create a socket notifier
if ( qApp ) //TODO: I don't like this here...
{
_sn = new QSocketNotifier( fileno(), QSocketNotifier::Read );
connect( _sn, SIGNAL( activated(int) ), this, SLOT( readyToReceive() ) );
}
return true;
}
bool OPacketCapturer::openDumpFile( const QString& filename )
{
pcap_dumper_t* dump = pcap_dump_open( _pch, const_cast<char*>( (const char*) filename ) );
if ( !dump )
{
owarn << "OPacketCapturer::open(): can't open dump with '" << filename << "': " << _errbuf << oendl;
return false;
}
odebug << "OPacketCapturer::open(): dump [" << filename << "] opened successfully." << oendl;
_pcd = dump;
return true;
}
-bool OPacketCapturer::open( const QFile& file )
+bool OPacketCapturer::openCaptureFile( const QString& name )
{
- 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 )
{
odebug << "OPacketCapturer::open(): libpcap opened successfully." << oendl;
_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
{
odebug << "OPacketCapturer::open(): can't open libpcap with '" << name << "': " << _errbuf << oendl;
return false;
}
}
bool OPacketCapturer::isOpen() const
{
return _open;
}
void OPacketCapturer::readyToReceive()
{
odebug << "OPacketCapturer::readyToReceive(): about to emit 'receivePacket(p)'" << oendl;
OPacket* p = next();
emit receivedPacket( p );
// emit is synchronous - packet has been dealt with, now it's safe to delete (if enabled)
if ( _autodelete ) delete p;
}
const QMap<QString,int>& OPacketCapturer::statistics() const
{
return _stats;
}
int OPacketCapturer::snapShot() const
{
return pcap_snapshot( _pch );
}
bool OPacketCapturer::swapped() const
{
return pcap_is_swapped( _pch );
}
QString OPacketCapturer::version() const
{
return QString().sprintf( "%d.%d", pcap_major_version( _pch ), pcap_minor_version( _pch ) );
}
}
}
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
/*======================================================================================
* OTCPPacket
*======================================================================================*/
class OTCPPacket : public QObject
{
Q_OBJECT
public:
OTCPPacket( const unsigned char*, const struct tcphdr*, QObject* parent = 0 );
virtual ~OTCPPacket();
int fromPort() const;
int toPort() const;
int seq() const;
int ack() const;
int window() const;
int checksum() const;
private:
const struct tcphdr* _tcphdr;
class Private;
Private *d;
};
/*======================================================================================
* 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.
* @see http://tcpdump.org
*/
class OPacketCapturer : public QObject
{
Q_OBJECT
public:
/**
* Constructor.
*/
OPacketCapturer( QObject* parent = 0, const char* name = 0 );
/**
* Destructor.
*/
~OPacketCapturer();
/**
* Set the packet capturer to use blocking or non-blocking IO. This 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;
/**
* Close the packet capturer. This is automatically done in the destructor.
*/
void close();
/**
* Close the output capture file.
*/
void closeDumpFile();
/**
* @returns the data link type.
* @see <pcap.h> for possible values.
*/
int dataLink() const;
/**
* Dump a packet to the output capture file.
*/
void dump( OPacket* );
/**
* @returns the file descriptor 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();
/**
* @returns the next @ref OPacket from the packet capturer, if
* one arrives within @a time milliseconds.
*/
OPacket* next( int time );
/**
* 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.
+ * Open the packet capturer to capture packets in offline-mode from @a filename.
*/
- bool open( const QFile& file );
+ bool openCaptureFile( const QString& filename );
/**
* Open a prerecorded tcpdump compatible capture file for use with @ref dump()
*/
bool openDumpFile( const QString& filename );
/**
* @returns true if the packet capturer is open
*/
bool isOpen() const;
/**
* @returns the snapshot length of this packet capturer
*/
int snapShot() const;
/**
* @returns true if the input capture file has a different byte-order
* than the byte-order of the running system.
*/
bool swapped() const;
/**
* @returns the libpcap version string used to write the input capture file.
*/
QString version() const;
/**
* @returns the packet statistic database.
* @see QMap
*/
const QMap<QString,int>& statistics() const;
/**
* Enable or disable the auto-delete option.
* If auto-delete is enabled, then the packet capturer will delete a packet right
* after it has been emit'ted. This is the default, which is useful if the packet
* capturer has the only reference to the packets. If you pass the packet for adding
* into a collection or do processing after the SLOT, the auto delete must be disabled.
*/
void setAutoDelete( bool enable );
/**
* @returns the auto-delete value.
*/
bool autoDelete() const;
signals:
/**
* This signal is emitted, when a packet has been received.
*/
void receivedPacket( Opie::Net::OPacket* );
protected slots:
void readyToReceive();
protected:
QString _name; // devicename
bool _open; // check this before doing pcap calls
pcap_t* _pch; // pcap library handle
pcap_dumper_t* _pcd; // pcap dumper handle
QSocketNotifier* _sn; // socket notifier for main loop
mutable char _errbuf[PCAP_ERRBUF_SIZE]; // holds error strings from libpcap
QMap<QString, int> _stats; // statistics;
bool _autodelete; // if we auto delete packets after emit
class Private; // Private Forward declaration
Private *d; // if we need to add data
};
}
}
#endif // OPCAP_H