-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkTools.cpp
More file actions
85 lines (73 loc) · 1.99 KB
/
Copy pathNetworkTools.cpp
File metadata and controls
85 lines (73 loc) · 1.99 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
#include "NetworkTools.h"
#include "QProcess"
CNetworkTools::CNetworkTools()
{
}
void CNetworkTools::Clear()
{
IP="";
Mask="";
Gateway="";
DNS="";
}
bool CNetworkTools::RefreshSystemInfo(QString ifaceName)
{
QString result;
Clear();
//-F- Getting IP address and mask
result = ExecuteSystemCommand("ifconfig "+ifaceName);//+ " | grep \"inet addr\"");
if(result=="")
{
return false;
}
int startFrom=0;
IP="127.0.0.1";
while(IP =="127.0.0.1")
{
IP = ReadConfigAttribute(result,"inet addr:"," ",&startFrom);
Mask = ReadConfigAttribute(result,"Mask:","\n",&startFrom);
}
//-F- Reading default gateway
result = ExecuteSystemCommand("sh -c \"route -n | grep ^0.0.0.0\"");
if(result=="")
{
return false;
}
Gateway = result.section(QRegExp(" +"),1,1);
//-F- Reading DNS
result = ExecuteSystemCommand("cat /etc/resolv.conf");
if(result=="")
{
return false;
}
DNS=ReadConfigAttribute(result,"nameserver ","\n");
return true;
}
QString CNetworkTools::ExecuteSystemCommand(QString command)
{
QProcess proc;
proc.setProcessChannelMode(QProcess::MergedChannels);
proc.start(command);
proc.waitForFinished(10000);
QByteArray arr = proc.readAll();
return QString(arr);
}
QString CNetworkTools::ReadConfigAttribute(const QString& configString,QString startString,QString endString, int* startFrom)
{
int startPos = configString.indexOf(startString, startFrom ? *startFrom : 0);
if(startPos!=-1)
{
startPos+=startString.length();
int endPos = configString.indexOf(endString,startPos);
if(startFrom)
{
*startFrom=endPos!=-1 ? endPos+endString.length() : configString.length();
}
return endPos !=-1 ? configString.mid(startPos,endPos-startPos) : configString.mid(startPos);
}
return "";
}
void CNetworkTools::ResetNetwork()
{
ExecuteSystemCommand("/usr/chumby/scripts/start_network");
}