1
/***************************************************************************
2
* Copyright (C) 2008-2011 by Francesco Cecconi *
3
* francesco.cecconi@gmail.com *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18
***************************************************************************/
24
#include "nmapsi4adaptor.h"
27
monitor::monitor(QTreeWidget* monitor, nmapClass* parent) : _monitor(monitor), _ui(parent)
30
new Nmapsi4Adaptor(this);
31
// FIXME: with full mode It is registrered into root dbus session
32
QDBusConnection dbus = QDBusConnection::sessionBus();
33
// if (!dbus.isConnected())
35
// dbus = QDBusConnection::systemBus();
37
dbus.registerObject("/Nmapsi4",this);
38
bool value = dbus.registerService("org.nmapsi4.Nmapsi4");
42
qDebug() << "DBUS:: error with dbus connection";
49
freelist<QTreeWidgetItem*>::itemDeleteAll(monitorElem);
50
freelist<lookupManager*>::itemDeleteAllWithWait(internealLookupList);
51
freelist<digManager*>::itemDeleteAll(digLookupList);
52
_scanHashListFlow.clear();
55
bool monitor::isHostOnMonitor(const QString hostname)
57
QList<QTreeWidgetItem*>::const_iterator i;
58
for (i = monitorElem.constBegin(); i != monitorElem.constEnd(); ++i)
60
if ((*i)->text(0) == hostname)
69
int monitor::monitorHostNumber()
71
return monitorElem.size();
74
void monitor::addMonitorHost(const QString hostName, const QStringList parameters, LookupType option)
76
QTreeWidgetItem *hostThread = new QTreeWidgetItem(_monitor);
77
hostThread->setIcon(0, QIcon(QString::fromUtf8(":/images/images/viewmagfit.png")));
78
hostThread->setText(0, hostName);
79
hostThread->setText(1, parameters.join(" "));
80
hostThread->setIcon(2, QIcon(QString::fromUtf8(":/images/images/reload.png")));
81
hostThread->setText(2, "Scanning");
82
monitorElem.push_front(hostThread);
84
emit monitorUpdated(monitorHostNumber());
86
// Start Scan for host
87
startScan(hostName,parameters);
88
startLookup(hostName,option);
91
void monitor::updateMonitorHost(const QString hostName, int valueIndex, const QString newData)
93
Q_ASSERT(valueIndex < _monitor->columnCount());
95
QList<QTreeWidgetItem*>::const_iterator i;
96
for (i = monitorElem.constBegin(); i != monitorElem.constEnd(); ++i)
98
if ((*i)->text(0) == hostName)
100
(*i)->setText(valueIndex,newData);
107
void monitor::startScan(const QString hostname, QStringList parameters)
109
parameters.append(hostname); // add hostname
112
QPointer<QProcessThread> thread = new QProcessThread("nmap",parameters);
113
_scanHashList.insert(hostname,thread);
114
// read current data scan from the thread
115
connect(thread, SIGNAL(flowFromThread(QString,QString)),
116
this, SLOT(readFlowFromThread(QString,QString)));
117
// read scan data return
118
connect(thread, SIGNAL(threadEnd(QStringList,QByteArray,QByteArray)),
119
this, SLOT(scanFinisced(QStringList,QByteArray,QByteArray)));
124
void monitor::startLookup(const QString hostname, LookupType option)
126
if (option == DisabledLookup)
131
if (option == InternalLookup)
133
lookupManager *internalLookupPtr = new lookupManager(hostname,this);
134
internealLookupList.push_back(internalLookupPtr);
136
connect(internalLookupPtr, SIGNAL(threadEnd(QHostInfo,int,QString)),
137
this, SLOT(lookupFinisced(QHostInfo,int,QString)));
139
internalLookupPtr->start();
143
parserObjUtil* tmpParserObj_ = new parserObjUtil();
145
digManager *digC = new digManager();
146
digLookupList.push_back(digC);
148
digC->digProcess(hostname,tmpParserObj_);
150
_ui->_parser->addUtilObject(tmpParserObj_);
154
void monitor::scanFinisced(const QStringList parametersList, QByteArray dataBuffer, QByteArray errorBuffer)
157
* Remove host scan finisced from the monitor list.
159
delMonitorHost(parametersList[parametersList.size()-1]);
161
* Return scan result with a signal.
163
emit hostFinisced(parametersList,dataBuffer,errorBuffer);
166
void monitor::lookupFinisced(QHostInfo info, int state, const QString hostname)
170
//QMessageBox::warning(this, "NmapSI4", tr("Wrong Address\n"), tr("Close"));
174
parserObjUtil* elemObjUtil = new parserObjUtil();
176
elemObjUtil->setHostName(hostname);
177
const int infoSize_ = info.addresses().size();
178
for(int index=0; index < infoSize_; index++)
180
elemObjUtil->setInfoLookup(info.addresses()[index].toString());
183
_ui->_parser->addUtilObject(elemObjUtil);
186
void monitor::delMonitorHost(const QString hostName)
188
for(int i=0; i < monitorElem.size(); i++)
190
if(monitorElem[i]->text(0) == hostName)
192
// remove host from monitor and list.
193
delete monitorElem.takeAt(i);
198
emit monitorUpdated(monitorHostNumber());
201
void monitor::clearHostMonitor()
203
freemap<QString,QProcessThread*>::itemDeleteAllWithWait(_scanHashList);
204
freelist<lookupManager*>::itemDeleteAllWithWait(internealLookupList);
205
freelist<digManager*>::itemDeleteAll(digLookupList);
208
void monitor::clearHostMonitorDetails()
210
_scanHashListFlow.clear();
213
QProcessThread* monitor::takeMonitorElem(const QString hostName)
215
return _scanHashList.take(hostName);
218
void monitor::stopSelectedScan()
220
// Stop and wait thread from QHash table
221
if (_monitor->selectedItems().isEmpty())
226
QProcessThread *ptrTmp = takeMonitorElem(_monitor->selectedItems()[0]->text(0));
235
// Remove Qhash entry for stopped scan
236
_scanHashListFlow.take(_monitor->selectedItems()[0]->text(0));
239
void monitor::showSelectedScanDetails()
241
if (_monitor->selectedItems().isEmpty())
246
classDetails details(_scanHashListFlow.operator[](_monitor->selectedItems()[0]->text(0)),
247
_monitor->selectedItems()[0]->text(0));
251
void monitor::readFlowFromThread(const QString hostname, QString lineData)
254
* read data line form thread
256
QHash<QString, QStringList>::const_iterator i = _scanHashListFlow.find(hostname);
257
QTextStream stream(&lineData);
259
if (i == _scanHashListFlow.constEnd())
261
QStringList flowHistory;
263
while (!stream.atEnd())
265
flowHistory.append(stream.readLine());
268
_scanHashListFlow.insert(hostname,flowHistory);
272
// append scan flow values
273
while (i != _scanHashListFlow.constEnd() && i.key() == hostname)
275
QStringList flowHistory = i.value();
277
while (!stream.atEnd())
279
flowHistory.append(stream.readLine());
282
_scanHashListFlow.insert(i.key(),flowHistory);
287
// search hostname on treeWidget and update data rows (index = 2)
288
// take only remaining time and remove character unused, only [remaining || ETA]
289
if (lineData.contains("remaining") || lineData.contains("ETC"))
291
QString infoTmp_ = lineData.mid(lineData.indexOf("("),lineData.indexOf(")"));
292
infoTmp_ = infoTmp_.remove('(');
293
infoTmp_ = infoTmp_.remove(')');
294
infoTmp_.remove('\n');
295
// insert new information into monitor
296
updateMonitorHost(hostname,2,infoTmp_);