-
Notifications
You must be signed in to change notification settings - Fork 0
/
seth.c
172 lines (114 loc) · 3.88 KB
/
seth.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <signal.h>
#include "sqlite3.h"
#include "wifi_scan.h"
#include "seth.h"
#include "free_secure.h"
char * get_current_location(sqlite3 * DB, char * buff, size_t limit) {
sqlite3_stmt * stmt;
const unsigned char * latitude;
const unsigned char * longitude;
int result;
char * sql = "SELECT latitude, longitude FROM current_location ORDER BY id DESC LIMIT 1;";
sqlite3_prepare(DB, sql, -1, &stmt, NULL);
result = sqlite3_step(stmt);
if (result == SQLITE_ROW) {
latitude = sqlite3_column_text(stmt, 0);
longitude = sqlite3_column_text(stmt, 1);
snprintf(buff, limit, "%s,%s", latitude, longitude);
return buff;
} else {
return "";
}
}
int insert_intoDB(sqlite3 * DB,
unsigned char bssid[BSSID_LENGTH],
char * ssid,
uint32_t frequency,
int32_t signal_mbm,
int32_t seen_ms_ago,
int status) {
char * errmsg = NULL;
char mac[BSSID_STRING_LENGTH];
char buff[60];
const char * sql_query[2];
sqlite3_stmt * query[2];
sql_query[0] = "INSERT INTO router(bssid, ssid, frequency, signal, seen_ms_ago, status, registred, location) VALUES (?, ?, ?, ?, ?, ?, datetime(), ?);";
sql_query[1] = "SELECT count(bssid) FROM router WHERE bssid = ?;";
if (sqlite3_exec(DB, "CREATE TABLE IF NOT EXISTS router("
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
"bssid TEXT NOT NULL,"
"ssid TEXT NOT NULL,"
"frequency INTEGER NOT NULL,"
"signal INTEGER NOT NULL,"
"seen_ms_ago INTEGER NOT NULL,"
"status INTEGER NOT NULL,"
"registred TEXT NOT NULL,"
"location TEXT);", NULL, NULL, &errmsg) != SQLITE_OK) {
//printf("Error: %s\n", errmsg);
return -1;
}
sqlite3_prepare(DB, sql_query[1], -1, &query[1], NULL);
sqlite3_bind_text(query[1], 1, bssid_to_string(bssid, mac), -1, SQLITE_STATIC);
sqlite3_step(query[1]);
if (sqlite3_column_int(query[1], 0) >= 1) {
return 1;
}
sqlite3_finalize(query[1]);
//
sqlite3_prepare(DB, sql_query[0], -1, &query[0], NULL);
sqlite3_bind_text(query[0], 1, bssid_to_string(bssid, mac), -1, SQLITE_STATIC);
sqlite3_bind_text(query[0], 2, ssid, -1, SQLITE_STATIC);
sqlite3_bind_int(query[0], 3, (int)frequency);
sqlite3_bind_int(query[0], 4, ((int)signal_mbm)/100);
sqlite3_bind_int(query[0], 5, (int)seen_ms_ago);
sqlite3_bind_int(query[0], 6, status);
sqlite3_bind_text(query[0], 7, get_current_location(DB, buff, sizeof(buff)), -1, SQLITE_STATIC);
sqlite3_step(query[0]);
sqlite3_finalize(query[0]);
return 1;
}
int main(int argc, char ** argv) {
struct params args;
get_params(argc, argv, &args);
struct wifi_scan * wifi = wifi_scan_init(args.interface);
struct bss_info bss[args.bss_limit];
char mac[BSSID_STRING_LENGTH];
int status;
int sql_status;
free_secure(wifi);
signal(SIGINT, sig_secure);
signal(SIGTERM, sig_secure);
signal(SIGUSR1, sig_secure);
signal(SIGUSR2, sig_secure);
while (true) {
sqlite3 * DB;
if ((sql_status = sqlite3_open(args.db_name, &DB) != SQLITE_OK)) {
fprintf(stderr, "Error: %s\n", sqlite3_errmsg(DB));
return EXIT_FAILURE;
}
sqlite3_busy_timeout(DB, 5);
status = wifi_scan_all(wifi, bss, args.bss_limit);
if (status < 0) {
perror("Error");
return EXIT_FAILURE;
} else {
clrscr();
sleep(args.sleeping);
for (register int i = 0; i < status && i < args.bss_limit; i++) {
sleep(args.sub_sleeping);
printf("%d) - %s (%s); Signal: %d; Frequency: %u; Seen ms ago: %d; Status: %s\n",
(i+1), bss[i].ssid, bssid_to_string(bss[i].bssid, mac), bss[i].signal_mbm/100,
bss[i].frequency, bss[i].seen_ms_ago, get_status(bss[i].status));
insert_intoDB(DB, bss[i].bssid, bss[i].ssid,
bss[i].frequency, bss[i].signal_mbm,
bss[i].seen_ms_ago, bss[i].status);
}
}
sqlite3_close(DB);
}
return EXIT_SUCCESS;
}