Convert pcapng and other formats to pcap for import
Any capture format supported by tshark can now be imported by Ostinato. Fixes #83
This commit is contained in:
parent
c335f34651
commit
a1582ad67b
@ -94,6 +94,7 @@ bool PcapFileFormat::open(const QString fileName,
|
|||||||
qint64 byteCount = 0;
|
qint64 byteCount = 0;
|
||||||
qint64 byteTotal;
|
qint64 byteTotal;
|
||||||
QByteArray pktBuf;
|
QByteArray pktBuf;
|
||||||
|
bool tryConvert = true;
|
||||||
|
|
||||||
if (!file.open(QIODevice::ReadOnly))
|
if (!file.open(QIODevice::ReadOnly))
|
||||||
goto _err_open;
|
goto _err_open;
|
||||||
@ -144,6 +145,7 @@ bool PcapFileFormat::open(const QString fileName,
|
|||||||
fd_.setDevice(&file);
|
fd_.setDevice(&file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_retry:
|
||||||
byteTotal = fd_.device()->size() - sizeof(fileHdr);
|
byteTotal = fd_.device()->size() - sizeof(fileHdr);
|
||||||
|
|
||||||
emit status("Reading File Header...");
|
emit status("Reading File Header...");
|
||||||
@ -153,7 +155,11 @@ bool PcapFileFormat::open(const QString fileName,
|
|||||||
|
|
||||||
qDebug("magic = %08x", magic);
|
qDebug("magic = %08x", magic);
|
||||||
|
|
||||||
if (magic == kPcapFileMagicSwapped)
|
if (magic == kPcapFileMagic)
|
||||||
|
{
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
else if (magic == kPcapFileMagicSwapped)
|
||||||
{
|
{
|
||||||
// Toggle Byte order
|
// Toggle Byte order
|
||||||
if (fd_.byteOrder() == QDataStream::BigEndian)
|
if (fd_.byteOrder() == QDataStream::BigEndian)
|
||||||
@ -161,8 +167,37 @@ bool PcapFileFormat::open(const QString fileName,
|
|||||||
else
|
else
|
||||||
fd_.setByteOrder(QDataStream::BigEndian);
|
fd_.setByteOrder(QDataStream::BigEndian);
|
||||||
}
|
}
|
||||||
else if (magic != kPcapFileMagic)
|
else // Not a pcap file
|
||||||
|
{
|
||||||
|
if (tryConvert)
|
||||||
|
{
|
||||||
|
// Close and reopen the temp file to be safe
|
||||||
|
file2.close();
|
||||||
|
if (!file2.open())
|
||||||
|
{
|
||||||
|
error.append("Unable to open temporary file to convert to PCAP\n");
|
||||||
|
goto _err_convert2pcap;
|
||||||
|
}
|
||||||
|
fd_.setDevice(0); // disconnect data stream from file
|
||||||
|
|
||||||
|
if (convertToStandardPcap(fileName, file2.fileName(), error))
|
||||||
|
{
|
||||||
|
fd_.setDevice(&file2);
|
||||||
|
tryConvert = false;
|
||||||
|
goto _retry;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
error = QString(tr("Unable to convert %1 to standard PCAP format"))
|
||||||
|
.arg(fileName);
|
||||||
|
goto _err_convert2pcap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
goto _err_bad_magic;
|
goto _err_bad_magic;
|
||||||
|
}
|
||||||
|
|
||||||
|
qDebug("reading filehdr");
|
||||||
|
|
||||||
fd_ >> fileHdr.versionMajor;
|
fd_ >> fileHdr.versionMajor;
|
||||||
fd_ >> fileHdr.versionMinor;
|
fd_ >> fileHdr.versionMinor;
|
||||||
@ -171,6 +206,7 @@ bool PcapFileFormat::open(const QString fileName,
|
|||||||
fd_ >> fileHdr.snapLen;
|
fd_ >> fileHdr.snapLen;
|
||||||
fd_ >> fileHdr.network;
|
fd_ >> fileHdr.network;
|
||||||
|
|
||||||
|
qDebug("version check");
|
||||||
if ((fileHdr.versionMajor != kPcapFileVersionMajor) ||
|
if ((fileHdr.versionMajor != kPcapFileVersionMajor) ||
|
||||||
(fileHdr.versionMinor != kPcapFileVersionMinor))
|
(fileHdr.versionMinor != kPcapFileVersionMinor))
|
||||||
goto _err_unsupported_version;
|
goto _err_unsupported_version;
|
||||||
@ -183,6 +219,7 @@ bool PcapFileFormat::open(const QString fileName,
|
|||||||
|
|
||||||
pktBuf.resize(fileHdr.snapLen);
|
pktBuf.resize(fileHdr.snapLen);
|
||||||
|
|
||||||
|
qDebug("pdml check");
|
||||||
if (importOptions_.value("ViaPdml").toBool())
|
if (importOptions_.value("ViaPdml").toBool())
|
||||||
{
|
{
|
||||||
QProcess tshark;
|
QProcess tshark;
|
||||||
@ -502,15 +539,53 @@ _err_reading_magic:
|
|||||||
error = QString(tr("Unable to read magic from %1")).arg(fileName);
|
error = QString(tr("Unable to read magic from %1")).arg(fileName);
|
||||||
goto _exit;
|
goto _exit;
|
||||||
|
|
||||||
|
_err_convert2pcap:
|
||||||
|
goto _exit;
|
||||||
|
|
||||||
_err_open:
|
_err_open:
|
||||||
error = QString(tr("Unable to open file: %1")).arg(fileName);
|
error = QString(tr("Unable to open file: %1")).arg(fileName);
|
||||||
goto _exit;
|
goto _exit;
|
||||||
|
|
||||||
_exit:
|
_exit:
|
||||||
|
if (!error.isEmpty())
|
||||||
|
qDebug("%s", qPrintable(error));
|
||||||
file.close();
|
file.close();
|
||||||
return isOk;
|
return isOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Converts a non-PCAP capture file to standard PCAP file format using tshark
|
||||||
|
|
||||||
|
Returns true if conversion was successful, false otherwise.
|
||||||
|
*/
|
||||||
|
bool PcapFileFormat::convertToStandardPcap(
|
||||||
|
QString fileName, QString outputFileName, QString &error)
|
||||||
|
{
|
||||||
|
qDebug("converting to PCAP %s", qPrintable(outputFileName));
|
||||||
|
emit status("Unsupported format. Converting to standard PCAP format...");
|
||||||
|
emit target(0);
|
||||||
|
|
||||||
|
QProcess tshark;
|
||||||
|
tshark.start(OstProtoLib::tsharkPath(),
|
||||||
|
QStringList()
|
||||||
|
<< QString("-r%1").arg(fileName)
|
||||||
|
<< "-Fpcap"
|
||||||
|
<< QString("-w%1").arg(outputFileName));
|
||||||
|
if (!tshark.waitForStarted(-1))
|
||||||
|
{
|
||||||
|
error.append(QString("Unable to start tshark. Check path in preferences.\n"));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tshark.waitForFinished(-1))
|
||||||
|
{
|
||||||
|
error.append(QString("Error running tshark\n"));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Reads packet meta data into pktHdr and packet content into buf.
|
Reads packet meta data into pktHdr and packet content into buf.
|
||||||
|
|
||||||
|
@ -75,6 +75,8 @@ private:
|
|||||||
quint32 origLen; /* actual length of packet */
|
quint32 origLen; /* actual length of packet */
|
||||||
} PcapPacketHeader;
|
} PcapPacketHeader;
|
||||||
|
|
||||||
|
bool convertToStandardPcap(QString fileName, QString outputFileName,
|
||||||
|
QString &error);
|
||||||
bool readPacket(PcapPacketHeader &pktHdr, QByteArray &pktBuf);
|
bool readPacket(PcapPacketHeader &pktHdr, QByteArray &pktBuf);
|
||||||
|
|
||||||
QDataStream fd_;
|
QDataStream fd_;
|
||||||
|
Loading…
Reference in New Issue
Block a user