-
Notifications
You must be signed in to change notification settings - Fork 0
/
station-manager.c
181 lines (141 loc) · 4.1 KB
/
station-manager.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "common.h"
// Comandos uteis:
//1- Tentando associar 'manualmente' a um AP:
// iw dev wlan0 disconnect; ifconfig wlan0 down; ifconfig wlan0 up; route add default gw 10.5.5.1 dev wlan0; iw dev wlan0 connect Escritorio ");
//2- Salvar o canal dele no arquivo apatual
// iwconfig wlan0 | grep Frequency | awk '{print $2}' | cut -c 11-15 | sed 's/\\.//g'
// 3- Conectando numa rede com o NetworkManager
// nmcli d wifi connect XX:XX:XX:XX:XX:XX
// https://askubuntu.com/questions/833905/how-can-i-connect-to-a-specific-bssid
void DieWithError(char *err) {
fprintf(stderr, "%s\n", err);
exit(1);
}
void pegaInterface (char* mynet, int bufsize) {
FILE *fp;
// Pegando a interface de rede:
system ("rm -rf .netinterface");
system ("iwconfig 2>&1 |grep IEEE | awk '{print $1}' > .netinterface");
fp = fopen (".netinterface", "r");
fscanf (fp, "%s", mynet);
fclose (fp);
system ("rm -rf .netinterface");
mynet[bufsize-1] = '\0';
}
void salvaAPAtual () {
char cmd[255];
char mynet[255];
pegaInterface(mynet, sizeof (mynet));
strcpy (cmd, "iwconfig ");
strcat (cmd, mynet);
strcat (cmd, " | grep Point | awk '{print $6}' > ");
strcat (cmd, APATUAL);
system (cmd);
// printf ("Salvando ap atual: |%s|\n", cmd);
}
// 1- Checar se a interface associou a algum AP
// 2- Se tiver associado, retornar 0
int monitoraInterface () {
char status[200];
char cmd[200];
char canalatual[10];
FILE *fp, *flog;
char mynet[255];
pegaInterface(mynet, sizeof (mynet));
system ("rm -rf .status");
strcpy (cmd, "iwconfig ");
strcat (cmd, mynet);
strcat (cmd, " | grep Point | awk '{print $6}' > .status");
system (cmd);
fp = fopen (".status", "r");
fscanf (fp, "%s", status);
fclose (fp);
system ("rm -rf .status");
flog = fopen("station.log", "a");
//printf ("Status: |%s|\n", status);
// Se nao associou a um AP
if (strcmp (status, "dBm") == 0) {
fprintf(flog, "NAO associou: %s\n", status);
printf ("Nao consegui me associar a um AP =(\n");
fclose (flog);
return 1;
}
else {
fprintf(flog, "Associou: %s\n", status);
printf ("Associado a '%s'.\n", status);
fclose (flog);
return 0;
}
}
void mudaAP (char *bssid) {
char cmd[500];
FILE *fp;
// Salvando AP novo
fp = fopen (APNEW, "w");
if (fp == NULL) DieWithError ("Failed opening AP file!");
fprintf (fp, "%s", bssid);
fclose (fp);
// nmcli d wifi connect XX:XX:XX:XX:XX:XX
strcpy (cmd, "nmcli d wifi connect ");
strcat(cmd, bssid);
strcat(cmd, " password senhalegal10");
system (cmd);
printf ("cmd: |%s|\n", cmd);
printf ("Esperando associar ao novo AP...\n");
sleep (3);
}
int main(int argc, char *argv[]) {
int sock, conectado, apdavez;
struct sockaddr_in echoServAddr;
struct sockaddr_in echoClntAddr;
unsigned int cliAddrLen;
PktAction msg;
char mynet[255];
pegaInterface(mynet, sizeof (mynet));
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");
memset(&echoServAddr, 0, sizeof(echoServAddr));
echoServAddr.sin_family = AF_INET;
echoServAddr.sin_addr.s_addr = 0;
echoServAddr.sin_port = htons(STATION_MANAGER_PORT);
if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError("bind() failed");
cliAddrLen = sizeof(echoClntAddr);
for (;;) {
printf("Station Manager: esperando comando do controller...");
fflush(stdout);
if ((recvfrom(sock, &msg, sizeof(msg), 0, (struct sockaddr *) &echoClntAddr, &cliAddrLen)) < 0)
DieWithError("recvfrom() falhou!\n");
printf("\nRecebido comando de %s!\n", inet_ntoa(echoClntAddr.sin_addr));
printf("aptogo: %s\n", msg.aptogo);
salvaAPAtual ();
conectado = 0;
mudaAP (msg.aptogo);
apdavez = 1;
conectado = monitoraInterface();
while (conectado != 0) {
if (apdavez == 1) {
mudaAP (APATUAL);
apdavez = 0;
}
else {
mudaAP (msg.aptogo);
apdavez = 1;
}
conectado = monitoraInterface();
}
salvaAPAtual ();
}
close(sock);
}