-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathscanner.cpp
More file actions
113 lines (103 loc) · 2.89 KB
/
Copy pathscanner.cpp
File metadata and controls
113 lines (103 loc) · 2.89 KB
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
#include "scanner.h"
#include <QNetworkInterface>
#include <math.h>
#include <QTimer>
#include "messengerConnection.h"
#include <QDebug>
AccessPointScanner::AccessPointScanner(QObject *parent)
: QObject(parent)
,m_index(0), m_finished(0)
{
initializeScanList();
}
AccessPointScanner::~AccessPointScanner()
{
}
void AccessPointScanner::startScan()
{
if(m_scanList.size() > 0)
{
scanAps();
}
else
{
qDebug() << "none scan list";
emit scanFinished();
}
}
void AccessPointScanner::onDiscoverSocketDestroyed()
{
m_finished++;
if(++m_index < m_scanList.size())
{
scanOneAp();
return;
}
if(m_finished == m_scanList.size())
{
m_index = 0;
m_finished = 0;
emit scanFinished();
qDebug() << "emit scanFinished";
}
}
void AccessPointScanner::scanOneAp()
{
DiscoverConnection *conn = new DiscoverConnection();
connect(conn, SIGNAL(newAccessPoint(AccessPoint *)),
this, SIGNAL(newAccessPoint(AccessPoint *)));
connect(conn, SIGNAL(destroyed()), this, SLOT(onDiscoverSocketDestroyed()));
conn->connectAp(m_scanList.at(m_index), SERVER_PORT);
//qDebug() << "scaning " << QHostAddress(m_scanList.at(m_index));
}
void AccessPointScanner::scanAps()
{
int limit = qMin(m_scanList.size(), 10);
for(; m_index < limit; ++m_index)
{
scanOneAp();
}
--m_index;
}
void AccessPointScanner::initializeScanList()
{
QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
foreach(QNetworkInterface interface, interfaces)
{
int flags = interface.flags();
if(flags & QNetworkInterface::IsLoopBack) continue;
if(!(flags &(QNetworkInterface::IsUp | QNetworkInterface::IsRunning))) continue;
QList<QNetworkAddressEntry> entries = interface.addressEntries();
int count = entries.size();
for(int i = 0; i < count; i++)
{
const QNetworkAddressEntry &entry = entries.at(i);
QHostAddress ip = entry.ip();
if(!ip.isLoopback() && ip.protocol() == QAbstractSocket::IPv4Protocol)
{
quint32 myself = ip.toIPv4Address();
int numberOfIps = ipCount(entry.prefixLength());
quint32 startIp = (ip.toIPv4Address() & entry.netmask().toIPv4Address())+1;
for(int j = 1; j < numberOfIps; j++, startIp++)
{
if(myself != startIp)
{
m_scanList.append(startIp);
//QHostAddress address(startIp);
//qDebug() << address;
}
}
}
}
}
}
int AccessPointScanner::ipCount(int prefixLength)
{
int count = 0;
int unmask = 32 - prefixLength;
for(int i = 0; i < unmask; i++)
{
count += pow(2, i);
}
return count;
}