1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
/*
Copyright (C) 2010 Srivats P.
This file is part of "Ostinato"
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "pdmlfileformat.h"
#include "ostprotolib.h"
#include "pdmlreader.h"
#include <QProcess>
#include <QTemporaryFile>
PdmlFileFormat pdmlFileFormat;
PdmlFileFormat::PdmlFileFormat()
{
}
PdmlFileFormat::~PdmlFileFormat()
{
}
bool PdmlFileFormat::open(const QString fileName,
OstProto::StreamConfigList &streams, QString &error)
{
bool isOk = false;
QFile file(fileName);
PdmlReader *reader = new PdmlReader(&streams);
if (!file.open(QIODevice::ReadOnly))
goto _open_fail;
connect(reader, SIGNAL(progress(int)), this, SIGNAL(progress(int)));
emit status("Reading PDML packets...");
emit target(100); // in percentage
isOk = reader->read(&file, NULL, &stop_);
if (stop_)
goto _user_cancel;
if (!isOk)
{
error.append(QString("Error processing PDML (%1, %2): %3\n")
.arg(reader->lineNumber())
.arg(reader->columnNumber())
.arg(reader->errorString()));
goto _exit;
}
goto _exit;
_open_fail:
isOk = false;
_user_cancel:
_exit:
delete reader;
return isOk;
}
bool PdmlFileFormat::save(const OstProto::StreamConfigList streams,
const QString fileName, QString &error)
{
bool isOk = false;
QTemporaryFile pcapFile;
StreamFileFormat *fmt = StreamFileFormat::fileFormatFromType("PCAP");
QProcess tshark;
Q_ASSERT(fmt);
if (!pcapFile.open())
{
error.append("Unable to open temporary file to create PCAP\n");
goto _fail;
}
qDebug("intermediate PCAP %s", qPrintable(pcapFile.fileName()));
connect(fmt, SIGNAL(target(int)), this, SIGNAL(target(int)));
connect(fmt, SIGNAL(progress(int)), this, SIGNAL(progress(int)));
emit status("Writing intermediate PCAP file...");
isOk = fmt->save(streams, pcapFile.fileName(), error);
qDebug("generating PDML %s", qPrintable(fileName));
emit status("Converting PCAP to PDML...");
emit target(0);
tshark.setStandardOutputFile(fileName);
tshark.start(OstProtoLib::tsharkPath(),
QStringList()
<< QString("-r%1").arg(pcapFile.fileName())
<< "-Tpdml");
if (!tshark.waitForStarted(-1))
{
error.append(QString("Unable to start tshark. Check path in preferences.\n"));
goto _fail;
}
if (!tshark.waitForFinished(-1))
{
error.append(QString("Error running tshark\n"));
goto _fail;
}
isOk = true;
_fail:
return isOk;
}
bool PdmlFileFormat::isMyFileFormat(const QString fileName)
{
bool ret = false;
QFile file(fileName);
QByteArray buf;
QXmlStreamReader xml;
if (!file.open(QIODevice::ReadOnly))
goto _exit;
xml.setDevice(&file);
xml.readNext();
if (xml.hasError() || !xml.isStartDocument())
goto _close_exit;
// skip everything until the start of the first element
while (!xml.isStartElement())
{
xml.readNext();
if (xml.hasError())
goto _close_exit;
}
if (!xml.hasError() && xml.isStartElement() && (xml.name() == "pdml"))
ret = true;
else
ret = false;
_close_exit:
xml.clear();
file.close();
_exit:
return ret;
}
bool PdmlFileFormat::isMyFileType(const QString fileType)
{
if (fileType.startsWith("PDML"))
return true;
else
return false;
}
|