-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_connection.cpp
133 lines (109 loc) · 4.02 KB
/
check_connection.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
/*
Author -> Dennis Turco 2022
Email -> [email protected]
WebSite -> https://dennisturco.github.io/
GitHub -> https://github.com/DennisTurco
Link: https://stackoverflow.com/questions/15778404/programmatically-check-whether-my-machine-has-internet-access-or-not
https://stackoverflow.com/questions/8046097/how-to-check-if-a-process-has-the-administrative-rights
*/
#include <iostream>
#include <windows.h>
#include <wininet.h>
#include <unistd.h>
#include <fstream>
#include <cstring>
bool check_administrator_rights (void) {
// https://stackoverflow.com/questions/8046097/how-to-check-if-a-process-has-the-administrative-rights
BOOL fRet = FALSE;
HANDLE hToken = NULL;
if( OpenProcessToken( GetCurrentProcess( ),TOKEN_QUERY,&hToken ) ) {
TOKEN_ELEVATION Elevation;
DWORD cbSize = sizeof( TOKEN_ELEVATION );
if( GetTokenInformation( hToken, TokenElevation, &Elevation, sizeof( Elevation ), &cbSize ) ) {
fRet = Elevation.TokenIsElevated;
}
}
if( hToken ) {
CloseHandle( hToken );
}
return fRet;
}
void diagnostic_tool (const char *command_disabled, const char *command_enabled){
system(command_disabled);
sleep(5);
system(command_enabled);
delete []command_enabled;
delete []command_disabled;
}
void check_connection (const char *network_name){
if (system("ping www.google.com")){
std::cout<<"\nNot connnected to the internet\n\n";
int count = 1;
std::cout.flush();
char command[] = "netsh interface set interface ";
// build command "command_disabled"
char command_disabled[std::string(command).length() + std::string(network_name).length()] = "\0";
strcat(command_disabled, command);
strcat(command_disabled, network_name);
strcat(command_disabled, " disabled");
// build command "command_enabled"
char command_enabled[std::string(command).length() + std::string(network_name).length()] = "\0";
strcat(command_enabled, command);
strcat(command_enabled, network_name);
strcat(command_enabled, " enabled");
// print for debug
//std::cout<<command_enabled<<std::endl<<command_disabled<<std::endl;
system("netsh interface show interface");
// run the network card troubleshooting diagnostics
while(system("ping www.google.com")){
std::cout<<"\n\n -> TEST - "<<count<<": ";
diagnostic_tool(command_disabled, command_enabled);
sleep(15);
system("netsh interface show interface");
++count;
}
}
std::cout<<"\nConnected to the internet\n\n";
}
int main (void) {
// check administrator rights
if (!check_administrator_rights()) {
std::cout<<"You must run this program as administrator!!\n";
system("pause");
return EXIT_FAILURE;
};
// get the network name from file "data.txt"
std::ifstream file;
file.open("data.txt");
if (file.fail()) {
std::cout<<"ERROR!! File 'data.txt' is missing...\n";
system("pause");
return EXIT_FAILURE;
}
char *network_name = new char[100];
network_name[0] = '\0';
char c;
for (int i=0; i<100; ++i){
file.get(c);
if (c == '"'){ //find "
file.get(c);
int counter = 0;
while(c != '"'){
network_name[counter] = c;
++counter;
file.get(c);
}
}
}
if (std::string(network_name).length() == 0) { // string is empty
std::cout<<"ERROR!! network name is missing in file 'data.txt', insert it between the \" \" \n";
system("pause");
return EXIT_FAILURE;
}
file.close();
// check connection
check_connection(network_name);
delete []network_name;
system("pause");
return EXIT_SUCCESS;
}