Repository navigation
Expand file tree
/
Copy pathnetwork.cpp
More file actions
265 lines (238 loc) · 8.38 KB
/
Copy pathnetwork.cpp
File metadata and controls
265 lines (238 loc) · 8.38 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
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
256
257
258
259
260
261
262
263
264
265
/***********************************************************
* - Author: Nizar Bousebsi. *
* - File: network.cpp *
* - Description: Manage Microsoft Native Wifi Functions. *
************************************************************/
#include "wififriend.h"
/* Extracteur de profile. */
void wififriend_extract_next(GtkWidget* Widget, gpointer Data)
{
DWORD Error = ERROR_SUCCESS;
GtkWidget *Profil;
t_wifi Extract;
const gchar *Retrieve;
long Size;
BSTR UnicodeStr;
LPWSTR OutProfil;
char *NewProfil;
FILE *File;
int I = 0;
char *PathFile;
Profil = (GtkWidget *)Data;
Retrieve = gtk_entry_get_text(GTK_ENTRY(Profil));
Extract = wififriend_create_handle();
Extract = wififriend_retrieve_config(Extract);
Size = lstrlenA(Retrieve);
UnicodeStr = SysAllocStringLen(NULL, Size);
MultiByteToWideChar(CP_ACP, 0, Retrieve, Size, UnicodeStr, Size);
if((Error = WlanGetProfile(Extract.MyHandle, &Extract.MyGuid, UnicodeStr, NULL, &OutProfil, 0, 0)) != ERROR_SUCCESS)
error_get_profil(0, Retrieve);
else
{
while (OutProfil[I])
I++;
NewProfil = (char *)malloc(sizeof(char) * I + 1);
wcstombs(NewProfil, OutProfil, I + 1);
PathFile = (char *)malloc(sizeof(char *) * strlen(Retrieve) + 1);
strcpy(PathFile, Retrieve);
strcat(PathFile, ".xml");
File = fopen(PathFile, "w+");
fputs(NewProfil, File);
fclose(File);
free(PathFile);
error_get_profil_success(0, Retrieve);
}
SysFreeString(UnicodeStr);
WlanCloseHandle(Extract.MyHandle, NULL);
}
void wififriend_extract_profil(void)
{
GtkWidget *Win = NULL;
GtkWidget *Vbox = NULL;
GtkWidget *ProfilLabel = NULL;
GtkWidget *ProfilEntry = NULL;
GtkWidget *Button = NULL;
Win = create_window(260, 100, "Extraire votre profile Wifi");
Vbox = gtk_vbox_new(FALSE, 0);
ProfilLabel = gtk_label_new("Nom de votre reseau Wifi");
gtk_box_pack_start(GTK_BOX(Vbox), ProfilLabel, FALSE, FALSE, 5);
ProfilEntry = gtk_entry_new();
gtk_entry_set_max_length(GTK_ENTRY(ProfilEntry), 15);
gtk_box_pack_start(GTK_BOX(Vbox), ProfilEntry, FALSE, FALSE, 15);
Button = gtk_button_new_with_label("Extraire");
gtk_container_add(GTK_CONTAINER(Vbox), Button);
g_signal_connect(G_OBJECT(Button), "clicked", G_CALLBACK(wififriend_extract_next), (gpointer)ProfilEntry);
gtk_container_add(GTK_CONTAINER(Win), Vbox);
display_interface(Win);
}
/* Forcer la déconnexion d'un réseau Wifi. */
void wififriend_disconnect_user(GtkWidget* Widget, gpointer Data)
{
t_wifi Disconnect;
Disconnect = wififriend_create_handle();
Disconnect = wififriend_retrieve_config(Disconnect);
if ((WlanDisconnect(Disconnect.MyHandle, &Disconnect.MyGuid, NULL)!= ERROR_SUCCESS))
error_cannot_disconnect(0);
else
error_disconnect_success(0);
WlanCloseHandle(Disconnect.MyHandle, NULL);
}
/* Récupération d'un handle pour les prochaines opérations. */
t_wifi wififriend_create_handle(void)
{
t_wifi Connect;
DWORD SetHandle = ERROR_SUCCESS;
HANDLE Handle = NULL;
DWORD Version = 0;
if ((SetHandle = WlanOpenHandle(WLAN_API_VERSION, NULL, &Version, &Handle)) != ERROR_SUCCESS)
error_invalid_handle(1);
else
Connect.MyHandle = Handle;
return (Connect);
}
/* Retourne la configuration de l'interface Wifi. */
t_wifi wififriend_retrieve_config(t_wifi Connect)
{
PWLAN_INTERFACE_INFO_LIST InterfaceList;
InterfaceList = NULL;
if(WlanEnumInterfaces(Connect.MyHandle, NULL, &InterfaceList) != ERROR_SUCCESS)
error_config(1);
else
{
Connect.NumberOfItems = InterfaceList->dwNumberOfItems;
Connect.MyGuid = InterfaceList->InterfaceInfo->InterfaceGuid;
Connect.CurrentState = InterfaceList->InterfaceInfo->isState;
}
return (Connect);
}
/* Affichage des paramètres de l'interface Wifi. */
void wififriend_display_wifi_params(GtkWidget* Widget, gpointer Data)
{
int I = 0;
PWLAN_INTERFACE_INFO_LIST InterfaceList = NULL;
t_wifi St;
GtkWidget *Win = NULL;
GtkWidget *Vbox;
GtkWidget *NbInterface = NULL;
GtkWidget *NbInterfaceResult = NULL;
GtkWidget *Guid = NULL;
GtkWidget *GuidResult = NULL;
GtkWidget *State = NULL;
GtkWidget *StateResult = NULL;
GtkWidget *Description = NULL;
GtkWidget *DescriptionResult = NULL;
gchar *String = NULL;
St = wififriend_create_handle();
St = wififriend_retrieve_config(St);
Win = create_window(300, 180, "Configuration Wifi");
Vbox = gtk_vbox_new(FALSE, 0);
if ((WlanEnumInterfaces(St.MyHandle, NULL, &InterfaceList)) != ERROR_SUCCESS)
error_interface_wifi(0);
else
{
NbInterface = gtk_label_new("- Nombre d'interface Wifi trouvee: ");
gtk_box_pack_start(GTK_BOX(Vbox), NbInterface, FALSE, FALSE, 0);
String = g_strdup_printf ("%d", InterfaceList->dwNumberOfItems);
NbInterfaceResult = gtk_label_new(String);
gtk_box_pack_start(GTK_BOX(Vbox), NbInterfaceResult, FALSE, FALSE, 2);
Guid = gtk_label_new("- GUID de votre interface Wifi: ");
gtk_box_pack_start(GTK_BOX(Vbox), Guid, FALSE, FALSE, 5);
String = g_strdup_printf ("%d", InterfaceList->InterfaceInfo->InterfaceGuid);
GuidResult = gtk_label_new(String);
gtk_box_pack_start(GTK_BOX(Vbox), GuidResult, FALSE, FALSE, 2);
State = gtk_label_new("- Etat de l'interface: ");
gtk_box_pack_start(GTK_BOX(Vbox), State, FALSE, FALSE, 5);
String = g_strdup_printf ("%d", InterfaceList->InterfaceInfo->isState);
StateResult = gtk_label_new(String);
gtk_box_pack_start(GTK_BOX(Vbox), StateResult, FALSE, FALSE, 2);
gtk_container_add(GTK_CONTAINER(Win), Vbox);
display_interface(Win);
WlanCloseHandle(St.MyHandle, NULL);
}
}
/* Connexion à un réseau sécurisé. */
int wififriend_connect_secure_network(HANDLE Handle, const GUID *Id, char *Strxml)
{
DWORD Error = ERROR_SUCCESS;
WLAN_CONNECTION_PARAMETERS Connect;
int Size;
BSTR UnicodeStr;
memset(&Connect, 0, sizeof(WLAN_CONNECTION_PARAMETERS));
Connect.wlanConnectionMode = wlan_connection_mode_profile;
Size = lstrlenA(Strxml);
UnicodeStr = SysAllocStringLen(NULL, Size);
MultiByteToWideChar(CP_ACP, 0, Strxml, Size, UnicodeStr, Size);
Connect.strProfile = UnicodeStr;
Connect.pDot11Ssid = NULL;
Connect.pDesiredBssidList = NULL;
Connect.dot11BssType = dot11_BSS_type_any;
Connect.dwFlags = 0;
if ((Error = WlanConnect(Handle, Id, &Connect, NULL)) != ERROR_SUCCESS)
switch(Error)
{
case ERROR_INVALID_HANDLE:
error_invalid_handle(0);
break;
case ERROR_ACCESS_DENIED:
error_unauthorized_connect(0);
break;
case ERROR_INVALID_PARAMETER:
error_parameters_connect(0);
break;
default:
error_connect_unknow(0);
}
else
error_connect_success(0, (char *)UnicodeStr);
WlanCloseHandle(Handle, NULL);
return (0);
}
/* Connexion à un réseau non sécurisé. */
void wififriend_connect_to_unsecure_network(GtkWidget* Widget, gpointer Data)
{
t_wifi Unsecure;
DWORD Error = ERROR_SUCCESS;
WLAN_CONNECTION_PARAMETERS Connect;
Unsecure = wififriend_create_handle();
Unsecure = wififriend_retrieve_config(Unsecure);
memset(&Connect, 0, sizeof(WLAN_CONNECTION_PARAMETERS));
Connect.wlanConnectionMode = wlan_connection_mode_discovery_unsecure;
Connect.strProfile = NULL;
Connect.pDot11Ssid = NULL;
Connect.pDesiredBssidList = 0;
Connect.dot11BssType = dot11_BSS_type_infrastructure;
Connect.dwFlags = WLAN_CONNECTION_IGNORE_PRIVACY_BIT;
if((Error = WlanConnect(Unsecure.MyHandle, &Unsecure.MyGuid, &Connect, NULL)) != ERROR_SUCCESS)
error_unauthorized_connect(0);
WlanCloseHandle(Unsecure.MyHandle, NULL);
}
/* Scanner de réseau Wifi. */
void wififriend_scan_network(GtkWidget* Widget, gpointer Data)
{
DWORD Error = ERROR_SUCCESS;
PWLAN_AVAILABLE_NETWORK_LIST Network = NULL;
t_wifi St;
int I;
GtkWidget *WinScan = NULL;
GtkWidget *ListOfWifi = NULL;
I = 1;
St = wififriend_create_handle();
St = wififriend_retrieve_config(St);
if((WlanGetAvailableNetworkList(St.MyHandle, &St.MyGuid, 0x00000001, NULL, &Network)) != ERROR_SUCCESS)
error_scan(0);
else
{
WinScan = create_window(236, 27, "Wifi scan");
ListOfWifi = gtk_combo_box_new_text();
while(I != Network->dwNumberOfItems)
{
gtk_combo_box_prepend_text(GTK_COMBO_BOX(ListOfWifi), (const gchar *)Network->Network[I].dot11Ssid.ucSSID);
I++;
}
gtk_combo_box_set_active(GTK_COMBO_BOX(ListOfWifi), 0);
gtk_container_add(GTK_CONTAINER(WinScan), ListOfWifi);
g_signal_connect(G_OBJECT(ListOfWifi), "changed", G_CALLBACK(wififriend_connect_to_unsecure_network), (gpointer)ListOfWifi);
display_interface(WinScan);
WlanCloseHandle(St.MyHandle, NULL);
}
}