~brand-nmapsi4/nmapsi4/master

« back to all changes in this revision

Viewing changes to src/app/monitor/monitor.cpp

  • Committer: Francesco Cecconi
  • Date: 2011-12-18 11:02:04 UTC
  • Revision ID: git-v1:60a69e132c8ffeb2727e558b2b2e8ac1713e332e
Removed nmapsi4-logr (dedicate repository) but it could be deprecated.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2008-2011 by Francesco Cecconi                          *
 
3
 *   francesco.cecconi@gmail.com                                           *
 
4
 *                                                                         *
 
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.        *
 
8
 *                                                                         *
 
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.                          *
 
13
 *                                                                         *
 
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
 ***************************************************************************/
 
19
 
 
20
#include "monitor.h"
 
21
#include "mainwin.h"
 
22
 
 
23
#ifdef Q_WS_X11
 
24
#include "nmapsi4adaptor.h"
 
25
#endif
 
26
 
 
27
monitor::monitor(QTreeWidget* monitor, nmapClass* parent) : _monitor(monitor), _ui(parent)
 
28
{
 
29
#ifdef Q_WS_X11
 
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())
 
34
//     {
 
35
//         dbus = QDBusConnection::systemBus();
 
36
//     }
 
37
    dbus.registerObject("/Nmapsi4",this);
 
38
    bool value = dbus.registerService("org.nmapsi4.Nmapsi4");
 
39
 
 
40
    if (!value)
 
41
    {
 
42
        qDebug() << "DBUS:: error with dbus connection";
 
43
    }
 
44
#endif
 
45
}
 
46
 
 
47
monitor::~monitor()
 
48
{
 
49
    freelist<QTreeWidgetItem*>::itemDeleteAll(monitorElem);
 
50
    freelist<lookupManager*>::itemDeleteAllWithWait(internealLookupList);
 
51
    freelist<digManager*>::itemDeleteAll(digLookupList);
 
52
    _scanHashListFlow.clear();
 
53
}
 
54
 
 
55
bool monitor::isHostOnMonitor(const QString hostname)
 
56
{
 
57
    QList<QTreeWidgetItem*>::const_iterator i;
 
58
    for (i = monitorElem.constBegin(); i != monitorElem.constEnd(); ++i)
 
59
    {
 
60
        if ((*i)->text(0) == hostname)
 
61
        {
 
62
            return true;
 
63
        }
 
64
    }
 
65
 
 
66
    return false;
 
67
}
 
68
 
 
69
int monitor::monitorHostNumber()
 
70
{
 
71
    return monitorElem.size();
 
72
}
 
73
 
 
74
void monitor::addMonitorHost(const QString hostName, const QStringList parameters, LookupType option)
 
75
{
 
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);
 
83
 
 
84
    emit monitorUpdated(monitorHostNumber());
 
85
 
 
86
    // Start Scan for host
 
87
    startScan(hostName,parameters);
 
88
    startLookup(hostName,option);
 
89
}
 
90
 
 
91
void monitor::updateMonitorHost(const QString hostName, int valueIndex, const QString newData)
 
92
{
 
93
    Q_ASSERT(valueIndex < _monitor->columnCount());
 
94
 
 
95
    QList<QTreeWidgetItem*>::const_iterator i;
 
96
    for (i = monitorElem.constBegin(); i != monitorElem.constEnd(); ++i)
 
97
    {
 
98
        if ((*i)->text(0) == hostName)
 
99
        {
 
100
            (*i)->setText(valueIndex,newData);
 
101
        }
 
102
 
 
103
        break;
 
104
    }
 
105
}
 
106
 
 
107
void monitor::startScan(const QString hostname, QStringList parameters)
 
108
{
 
109
    parameters.append(hostname); // add hostname
 
110
 
 
111
    // start scan Thread
 
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)));
 
120
    // start scan
 
121
    thread->start();
 
122
}
 
123
 
 
124
void monitor::startLookup(const QString hostname, LookupType option)
 
125
{
 
126
    if (option == DisabledLookup)
 
127
    {
 
128
        return;
 
129
    }
 
130
 
 
131
    if (option == InternalLookup)
 
132
    {
 
133
        lookupManager *internalLookupPtr = new lookupManager(hostname,this);
 
134
        internealLookupList.push_back(internalLookupPtr);
 
135
 
 
136
        connect(internalLookupPtr, SIGNAL(threadEnd(QHostInfo,int,QString)),
 
137
                this, SLOT(lookupFinisced(QHostInfo,int,QString)));
 
138
 
 
139
        internalLookupPtr->start();
 
140
    }
 
141
    else
 
142
    {
 
143
        parserObjUtil* tmpParserObj_ = new parserObjUtil();
 
144
 
 
145
        digManager *digC = new digManager();
 
146
        digLookupList.push_back(digC);
 
147
 
 
148
        digC->digProcess(hostname,tmpParserObj_);
 
149
 
 
150
        _ui->_parser->addUtilObject(tmpParserObj_);
 
151
    }
 
152
}
 
153
 
 
154
void monitor::scanFinisced(const QStringList parametersList, QByteArray dataBuffer, QByteArray errorBuffer)
 
155
{
 
156
    /*
 
157
     * Remove host scan finisced from the monitor list.
 
158
     */
 
159
    delMonitorHost(parametersList[parametersList.size()-1]);
 
160
    /*
 
161
     * Return scan result with a signal.
 
162
     */
 
163
    emit hostFinisced(parametersList,dataBuffer,errorBuffer);
 
164
}
 
165
 
 
166
void monitor::lookupFinisced(QHostInfo info, int state, const QString hostname)
 
167
{
 
168
    if(state == -1)
 
169
    {
 
170
        //QMessageBox::warning(this, "NmapSI4", tr("Wrong Address\n"), tr("Close"));
 
171
        return;
 
172
    }
 
173
 
 
174
    parserObjUtil* elemObjUtil = new parserObjUtil();
 
175
 
 
176
    elemObjUtil->setHostName(hostname);
 
177
    const int infoSize_ = info.addresses().size();
 
178
    for(int index=0; index < infoSize_; index++)
 
179
    {
 
180
        elemObjUtil->setInfoLookup(info.addresses()[index].toString());
 
181
    }
 
182
 
 
183
    _ui->_parser->addUtilObject(elemObjUtil);
 
184
}
 
185
 
 
186
void monitor::delMonitorHost(const QString hostName)
 
187
{
 
188
     for(int i=0; i < monitorElem.size(); i++)
 
189
     {
 
190
          if(monitorElem[i]->text(0) == hostName)
 
191
          {
 
192
              // remove host from monitor and list.
 
193
              delete monitorElem.takeAt(i);
 
194
              break;
 
195
           }
 
196
     }
 
197
 
 
198
     emit monitorUpdated(monitorHostNumber());
 
199
}
 
200
 
 
201
void monitor::clearHostMonitor()
 
202
{
 
203
    freemap<QString,QProcessThread*>::itemDeleteAllWithWait(_scanHashList);
 
204
    freelist<lookupManager*>::itemDeleteAllWithWait(internealLookupList);
 
205
    freelist<digManager*>::itemDeleteAll(digLookupList);
 
206
}
 
207
 
 
208
void monitor::clearHostMonitorDetails()
 
209
{
 
210
    _scanHashListFlow.clear();
 
211
}
 
212
 
 
213
QProcessThread* monitor::takeMonitorElem(const QString hostName)
 
214
{
 
215
    return _scanHashList.take(hostName);
 
216
}
 
217
 
 
218
void monitor::stopSelectedScan()
 
219
{
 
220
        // Stop and wait thread from QHash table
 
221
    if (_monitor->selectedItems().isEmpty())
 
222
    {
 
223
        return;
 
224
    }
 
225
 
 
226
    QProcessThread *ptrTmp = takeMonitorElem(_monitor->selectedItems()[0]->text(0));
 
227
 
 
228
    if (ptrTmp)
 
229
    {
 
230
        ptrTmp->quit();
 
231
        ptrTmp->wait();
 
232
        delete ptrTmp;
 
233
    }
 
234
 
 
235
    // Remove Qhash entry for stopped scan
 
236
    _scanHashListFlow.take(_monitor->selectedItems()[0]->text(0));
 
237
}
 
238
 
 
239
void monitor::showSelectedScanDetails()
 
240
{
 
241
    if (_monitor->selectedItems().isEmpty())
 
242
    {
 
243
        return;
 
244
    }
 
245
    // start details UI
 
246
    classDetails details(_scanHashListFlow.operator[](_monitor->selectedItems()[0]->text(0)),
 
247
                         _monitor->selectedItems()[0]->text(0));
 
248
    details.exec();
 
249
}
 
250
 
 
251
void monitor::readFlowFromThread(const QString hostname, QString lineData)
 
252
{
 
253
    /*
 
254
     * read data line form thread
 
255
     */
 
256
    QHash<QString, QStringList>::const_iterator i = _scanHashListFlow.find(hostname);
 
257
    QTextStream stream(&lineData);
 
258
 
 
259
    if (i == _scanHashListFlow.constEnd())
 
260
    {
 
261
        QStringList flowHistory;
 
262
 
 
263
        while (!stream.atEnd())
 
264
        {
 
265
            flowHistory.append(stream.readLine());
 
266
        }
 
267
 
 
268
        _scanHashListFlow.insert(hostname,flowHistory);
 
269
    }
 
270
    else
 
271
    {
 
272
        // append scan flow values
 
273
        while (i != _scanHashListFlow.constEnd() && i.key() == hostname)
 
274
        {
 
275
            QStringList flowHistory = i.value();
 
276
 
 
277
            while (!stream.atEnd())
 
278
            {
 
279
                flowHistory.append(stream.readLine());
 
280
            }
 
281
 
 
282
            _scanHashListFlow.insert(i.key(),flowHistory);
 
283
            ++i;
 
284
        }
 
285
    }
 
286
 
 
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"))
 
290
    {
 
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_);
 
297
    }
 
298
}