-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifidatachannel.cpp
261 lines (191 loc) · 7.94 KB
/
wifidatachannel.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <QTcpServer>
#include <QProcess>
#include "wifidatachannel.h"
#include "notsuitablewifiadapterfoundveniceexception.h"
#include "notavailableportfoundveniceexception.h"
using namespace std;
/*WifiDataChannel::WifiDataChannel(const char * ip, const char * gateway, const char * netmask):DataChannel(QNetworkInterface::InterfaceType::Wifi)
{
this->ip = ip;
this->gateway = gateway;
this->netmask = netmask;
this->adhocNetworkName = "VeniceAdhocNetwork";
}*/
WifiDataChannel::WifiDataChannel():DataChannel(QNetworkInterface::InterfaceType::Wifi)
{
this->port = -1;
}
WifiDataChannel::~WifiDataChannel()
{
//system("service NetworkManager start");
}
void WifiDataChannel::configureChannel()
{
this->wifiInterface = DataChannel::searchNetworkInterfaceByType(QNetworkInterface::InterfaceType::Wifi);
qDebug() << "Wifi Adapter set!";
this->searchForAvailablePort();
qDebug() << "Wifi Port set!";
this->searchForSSID();
qDebug() << "SSID retrieved!";
/*if(this->networkInterfaceName.isNull() || this->networkInterfaceName.isEmpty())
{
qDebug() << "There is no Wifi adapter to configure an ad-hoc network!";
}
else
{
qDebug() << "Configuring Wifi adapter for creating an ad-hoc network...";
char cmd[128];
//network interface name
const char* networkInterfaceName = {this->networkInterfaceName.toStdString().c_str()};
//stop the Network manager
sprintf(cmd,"systemctl stop NetworkManager");
this->executeSystemCommand(cmd);
FILE *pipe = popen(cmd, "r");
if (pipe) {
char buffer[128];
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != nullptr) {
// Process output line by line (here, printing)
printf("%s", buffer);
}
}
pclose(pipe);
}
else
qDebug() << "Error with command: "<< cmd;*/
/*int result = system(cmd);
memset(cmd,0x00,64);
qDebug() << "Result of command execution " << result;*/
//link down command in Linux
/*sprintf(cmd,"ip link set %s down",networkInterfaceName);
this->executeSystemCommand(cmd);
memset(cmd,0X00,64);
//set the network interface to ad-hoc mode
sprintf(cmd,"iwconfig %s mode Ad-hoc",networkInterfaceName);
this->executeSystemCommand(cmd);
memset(cmd,0X00,64);
//set the network name
sprintf(cmd,"iwconfig %s essid %s",networkInterfaceName, this->adhocNetworkName);
this->executeSystemCommand(cmd);
memset(cmd,0X00,64);
//remove security
sprintf(cmd,"iwconfig %s key off ",networkInterfaceName);
this->executeSystemCommand(cmd);
memset(cmd,0X00,64);
//command to set ip address, netmask
sprintf(cmd,"ifconfig %s %s netmask %s",networkInterfaceName,this->ip,this->netmask);
this->executeSystemCommand(cmd);
memset(cmd,0X00,64);
//command to set gateway
//sprintf(cmd,"route add default gw %s %s",this->gateway,networkInterfaceName);
//qDebug() << "Executing command "<< cmd;
//system(cmd);
//memset(cmd,0X00,64);
//link up commanda
sprintf(cmd,"ip link set %s up",networkInterfaceName);
qDebug() << "\nExecuting command: "<< cmd;
system(cmd);
memset(cmd,0X00,64);*/
//}
}
void WifiDataChannel::restoreChannelConfiguration()
{
system("systemctl start NetworkManager");
}
/*const char* WifiDataChannel::getAdhocNetworkName()
{
return this->adhocNetworkName;
}*/
void WifiDataChannel::executeSystemCommand(const char* command)
{
qDebug() << "Executing command: "<< command;
int result = system(command);
qDebug() << "Result of command execution: " << result;
}
int WifiDataChannel::getPort()
{
return this->port;
}
QNetworkInterface WifiDataChannel::getWifiInterface()
{
return this->wifiInterface;
}
QString WifiDataChannel::getSSID()
{
return this->ssid;
}
QNetworkAddressEntry WifiDataChannel::getNetworkAddress()
{
qDebug() << "Current Wifi Interface Entries" << this->wifiInterface.addressEntries().size();
return this->wifiInterface.addressEntries().first();
}
void WifiDataChannel::searchForAvailablePort() throw()
{
QTcpServer server; //Server to check that the port is available
int currentPort = STARTING_PORT;
qDebug() << "Starting Ports checking "<< currentPort;
while(!server.listen(this->getNetworkAddress().ip(), currentPort) && currentPort < ENDING_PORT)
{
qDebug() << "Port failed "<< currentPort;
++currentPort;
}
server.close();
if(currentPort>=ENDING_PORT)
throw NotAvailablePortFoundVeniceException();
this->port = currentPort;
}
void WifiDataChannel::searchForSSID() throw()
{
//Get all wifi adapters with its ssid, device name and status (active = yes or not).
//The colons names are removed (-t option) - Several results are possible:
//eduroam:wlp3s0:yes
//INRIA-interne:wlp3s0:no
//INRIA-guest:wlp3s0:no
QString nmcliWifiAdapterFilter = "nmcli -t -f ssid,device,active device wifi";
//The IP of related to the channel
QString ip = this->getNetworkAddress().ip().toString(); //.toStdString(); //toUtf8().constData();
//Filter the wifi adapters as inputs for only considering the ones with a given device name
//The device name is getted by using the channel ip - Several results are possible.
// - nmcli -f GENERAL.DEVICE,IP4.ADDRESS device show :
// GENERAL.DEVICE: wlp3s0
// IP4.ADDRESS[1]: 192.168.1.20/24
// GENERAL.DEVICE: eno1
// IP4.ADDRESS[1]: 193.51.235.150/25
// - grep -B 1 <HOST_IP> (taking as input the previous nmcli output)
// GENERAL.DEVICE: wlp3s0
// IP4.ADDRESS[1]: 192.168.1.20/24
// - grep -E "GENERAL.DEVICE" taking as input the previous grep output)
// GENERAL.DEVICE: wlp3s0
// - awk '{print $2}' taking as input the previous grep output)
QString grepWifiAdaptersFilterByDeviceName = "grep $(nmcli -f GENERAL.DEVICE,IP4.ADDRESS device show "
"| grep -B 1 \""+ip+"\""+
" | grep -E \"GENERAL.DEVICE\"|awk '{print $2}')";
//Filter the wifi adapters to get only the one that is active
QString grepWifiAdapterFilterByStatus = "grep ':yes'";
//Extract the ssid related to the active wifi adapter
QString grepWifiAdapterSSIDFilter = "grep -o '^[^:]*'";
const QString commandToGetSSID = nmcliWifiAdapterFilter+"|"+
grepWifiAdaptersFilterByDeviceName+"|"+
grepWifiAdapterFilterByStatus+"|"+
grepWifiAdapterSSIDFilter;
//Command execution
QProcess processCommandExecution;
QStringList arguments;
arguments << "-c" << commandToGetSSID;
qDebug().noquote() << "Running command:" << arguments.join(" ");
//We execute bash as program and pass the actual command via arguments
processCommandExecution.start("/bin/bash", arguments);
if(!processCommandExecution.waitForFinished())
{
QString error = processCommandExecution.errorString();
qWarning() << "Process did not finish successfully:" << error;
qDebug() << "Error running the command";
}
// Capture the ssid
qDebug().noquote() << "Command to get the ssid"<< commandToGetSSID;
this->ssid = processCommandExecution.readAllStandardOutput();
qDebug() << "errors... " << processCommandExecution.readAllStandardError();
qDebug().noquote() << "Result of the command for ssid"<< this->ssid;
if(this->ssid.isNull() || this->ssid.isEmpty())
throw NotSuitableWifiAdapterFoundVeniceException();
}