~mzanetti/+junk/netscan

« back to all changes in this revision

Viewing changes to app/host.cpp

  • Committer: Michael Zanetti
  • Date: 2016-04-10 00:16:29 UTC
  • Revision ID: michael.zanetti@canonical.com-20160410001629-i56ij6hs90fhfifx
added some features

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
    QObject(parent),
7
7
    m_scanMode(NMap::ScanModePingScan)
8
8
{
9
 
    m_process = new QProcess(this);
10
 
    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
11
 
    env.insert("NMAPDIR", "./share/nmap/");
12
 
    m_process->setProcessEnvironment(env);
13
 
    connect(m_process, SIGNAL(finished(int)), this, SLOT(scanFinished(int)));
 
9
}
 
10
 
 
11
void Host::setState(const QString &state)
 
12
{
 
13
    if (m_state != state) {
 
14
        m_state = state;
 
15
        emit stateChanged();
 
16
    }
 
17
}
 
18
 
 
19
void Host::setReason(const QString &reason)
 
20
{
 
21
    if (m_reason != reason) {
 
22
        m_reason = reason;
 
23
        emit reasonChanged();
 
24
    }
 
25
}
 
26
 
 
27
void Host::setName(const QString &name)
 
28
{
 
29
    if (m_name != name) {
 
30
        m_name = name;
 
31
        emit nameChanged();
 
32
    }
14
33
}
15
34
 
16
35
NMap::ScanMode Host::scanMode() const
26
45
    }
27
46
}
28
47
 
 
48
NMap::ScanOptions Host::scanOptions() const
 
49
{
 
50
    return m_scanOptions;
 
51
}
 
52
 
 
53
void Host::setScanOptions(NMap::ScanOptions scanOptions)
 
54
{
 
55
    if (m_scanOptions != scanOptions) {
 
56
        m_scanOptions = scanOptions;
 
57
        emit scanOptionsChanged();
 
58
    }
 
59
}
 
60
 
29
61
void Host::addPort(Port *port)
30
62
{
 
63
    port->setParent(this);
31
64
    m_openPorts.append(port);
32
65
    emit openPortCountChanged();
33
66
}
34
67
 
 
68
QList<Port *> Host::ports()
 
69
{
 
70
    return m_openPorts;
 
71
}
 
72
 
 
73
void Host::setPorts(const QList<Port *> ports)
 
74
{
 
75
    qDebug() << "setting new ports. old:" << m_openPorts.count() << "new" << ports.count();
 
76
    foreach (Port *port, ports) {
 
77
        qDebug() << "have new port" << port;
 
78
        Port* existingPort = findPort(port->number());
 
79
        if (existingPort) {
 
80
            existingPort->setState(port->state());
 
81
            existingPort->setReason(port->reason());
 
82
            if (port->serviceDetectionDone()) {
 
83
                existingPort->setFingerprint(port->fingerprint());
 
84
                existingPort->setProduct(port->product());
 
85
                existingPort->setVersion(port->version());
 
86
                existingPort->setServiceDetectionDone(port->serviceDetectionDone());
 
87
            }
 
88
            port->deleteLater();
 
89
        } else {
 
90
            port->setParent(this);
 
91
            m_openPorts.append(port);
 
92
            emit openPortCountChanged();
 
93
        }
 
94
    }
 
95
}
 
96
 
35
97
Port *Host::port(int index) const
36
98
{
37
99
    return m_openPorts.at(index);
38
100
}
39
101
 
40
 
void Host::lookup()
41
 
{
42
 
    QHostInfo::lookupHost(m_address, this, SLOT(lookupDone(const QHostInfo &)));
43
 
}
44
 
 
45
 
bool Host::busy() const
46
 
{
47
 
    return m_process->state() == QProcess::Running || m_process->state() == QProcess::Starting;
48
 
}
49
 
 
50
 
void Host::performFullScan()
51
 
{
52
 
    QStringList args;
53
 
    args << "-R" << "-oX" << "-" << "-A" << "-p1-65535" << m_address;
54
 
 
55
 
    m_process->start("nmap", args);
56
 
    emit busyChanged();
57
 
}
58
 
 
59
 
void Host::lookupDone(const QHostInfo &hostInfo)
60
 
{
61
 
    qDebug() << "lookup done" << m_address << m_name << hostInfo.hostName();
62
 
}
63
 
 
64
 
void Host::scanFinished(int exitCode)
65
 
{
66
 
    QByteArray data = m_process->readAllStandardOutput();
67
 
    qDebug() << "have data" << data;
68
 
 
69
 
    QXmlStreamReader reader(data);
70
 
 
71
 
    if (exitCode != 0) {
72
 
        while (!reader.atEnd() && !reader.hasError()) {
73
 
            QXmlStreamReader::TokenType token = reader.readNext();
74
 
            if (token == QXmlStreamReader::StartElement && reader.name() == "finished") {
75
 
//                m_error = reader.attributes().value("errormsg").toString();
76
 
                break;
77
 
            }
78
 
        }
79
 
//        emit errorChanged();
80
 
        emit busyChanged();
81
 
        return;
82
 
    }
83
 
 
84
 
    Port *currentPort = 0;
85
 
    while (!reader.atEnd() && !reader.hasError()) {
86
 
        QXmlStreamReader::TokenType token = reader.readNext();
87
 
        if(token == QXmlStreamReader::StartDocument) {
88
 
            continue;
89
 
        }
90
 
        if(token == QXmlStreamReader::StartElement) {
91
 
            qDebug() << "opening tag:" << reader.name();
92
 
            if (reader.name() == "hostname") {
93
 
                setName(reader.attributes().value("name").toString());
94
 
            }
95
 
 
96
 
            if (reader.name() == "port") {
97
 
                int number = reader.attributes().value("portid").toInt();
98
 
                currentPort = findPort(number);
99
 
                if (!currentPort) {
100
 
                    currentPort = new Port(this);
101
 
                    currentPort->setNumber(number);
102
 
                }
103
 
            }
104
 
            if (reader.name() == "state" && currentPort) {
105
 
                currentPort->setState(reader.attributes().value("state").toString());
106
 
                currentPort->setReason(reader.attributes().value("reason").toString());
107
 
            }
108
 
            if (reader.name() == "service" && currentPort) {
109
 
                currentPort->setService(reader.attributes().value("name").toString());
110
 
            }
111
 
        }
112
 
        if (token == QXmlStreamReader::Characters) {
113
 
        }
114
 
        if (token == QXmlStreamReader::EndElement) {
115
 
            if (reader.name() == "host") {
116
 
//                m_hosts->insert(currentHost);
117
 
//                currentHost->lookup();
118
 
//                currentHost = 0;
119
 
            }
120
 
            if (reader.name() == "port") {
121
 
                if (currentPort && !findPort(currentPort->number())) {
122
 
                    addPort(currentPort);
123
 
                }
124
 
                currentPort = 0;
125
 
            }
126
 
        }
127
 
    }
128
 
    m_scanMode = NMap::ScanModeAll;
129
 
    emit scanModeChanged();
130
 
    emit busyChanged();
131
 
}
132
 
 
133
102
Port *Host::findPort(int number) const
134
103
{
135
104
    foreach (Port *port, m_openPorts) {