-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.c
241 lines (194 loc) · 6.22 KB
/
main.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
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
// Airscan
// By Cykey.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <math.h>
#include <CoreFoundation/CoreFoundation.h>
#include <MobileWiFi/MobileWiFi.h>
// Colors!
#define kNRM "\x1B[0m"
#define kBLU "\x1B[34m"
#define kGRN "\x1B[32m"
#define kRED "\x1B[31m"
#define ERROR(x) \
fprintf(stderr, kRED "[Error] " kNRM x); \
exit(EXIT_FAILURE);
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
static WiFiManagerRef manager;
static WiFiDeviceClientRef device;
static CFRunLoopTimerRef timer;
static bool verboseMode = false;
static bool timerMode = false;
static void pretty_print_network(WiFiNetworkRef network);
static inline void verbose_log(char *format, ...);
static void scan_callback(WiFiDeviceClientRef device, CFArrayRef results, int error, const void *object);
static void begin_scan();
static void setup_for_scan();
static void print_usage(char *progname);
static void signal_handler(int signal);
static inline void verbose_log(char *format, ...)
{
if (verboseMode) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
}
static void pretty_print_network(WiFiNetworkRef network)
{
// SSID & BSSID.
// FIXME: Add safety checks and use CFStringGetCString if CFStringGetCStringPtr fails.
const char *SSID = CFStringGetCStringPtr(WiFiNetworkGetSSID(network), kCFStringEncodingMacRoman);
const char *BSSID = CFStringGetCStringPtr(WiFiNetworkGetProperty(network, CFSTR("BSSID")), kCFStringEncodingMacRoman);
// RSSI.
CFNumberRef RSSI = (CFNumberRef)WiFiNetworkGetProperty(network, CFSTR("RSSI"));
float strength;
CFNumberGetValue(RSSI, kCFNumberFloatType, &strength);
// Bars.
CFNumberRef gradedRSSI = (CFNumberRef)WiFiNetworkGetProperty(network, kWiFiScaledRSSIKey);
float graded;
CFNumberGetValue(gradedRSSI, kCFNumberFloatType, &graded);
int bars = (int)ceilf((graded * -1.0f) * -3.0f);
bars = MAX(1, MIN(bars, 3));
// Channel.
CFNumberRef networkChannel = (CFNumberRef)WiFiNetworkGetProperty(network, CFSTR("CHANNEL"));
int channel;
CFNumberGetValue(networkChannel, kCFNumberIntType, &channel);
// Hidden.
CFStringRef isHidden = (WiFiNetworkIsHidden(network) ? CFSTR("YES") : CFSTR("NO"));
// Security model.
CFStringRef isEAP = (WiFiNetworkIsEAP(network) ? CFSTR("YES") : CFSTR("NO"));
CFStringRef isWEP = (WiFiNetworkIsWEP(network) ? CFSTR("YES") : CFSTR("NO"));
CFStringRef isWPA = (WiFiNetworkIsWPA(network) ? CFSTR("YES") : CFSTR("NO"));
CFStringRef format = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%s%-40s%s BSSI: %s, RSSI: %.0f dBm, BARS: %d, CHANNEL: %d, HIDDEN: %@, EAP: %@, WEP: %@, WPA: %@"), kBLU, SSID, kNRM, BSSID, strength, bars, channel, isHidden, isEAP, isWEP, isWPA);
CFShow(format);
CFRelease(format);
}
static void scan_callback(WiFiDeviceClientRef device, CFArrayRef results, int error, const void *object)
{
if (results) {
CFIndex count = CFArrayGetCount(results);
verbose_log("Finished scanning. Found %s%d%s networks.\n", kGRN, count, kNRM);
unsigned x;
for (x = 0; x < count; x++) {
WiFiNetworkRef network = (WiFiNetworkRef)CFArrayGetValueAtIndex(results, x);
pretty_print_network(network);
}
} else {
verbose_log("Got no results. (Is WiFi on?)\n");
}
if (!timerMode)
CFRunLoopStop(CFRunLoopGetCurrent());
else
// Print a newline so that results aren't all stuck together.
printf("\n");
}
static void setup_for_scan()
{
verbose_log("Creating WiFiManagerClient.\n");
manager = WiFiManagerClientCreate(kCFAllocatorDefault, 0);
if (!manager) {
ERROR("Couldn't create WiFiManagerClient.\n");
}
CFArrayRef devices = WiFiManagerClientCopyDevices(manager);
if (!devices) {
ERROR("Couldn't get WiFiDeviceClients.\n");
}
verbose_log("Getting WiFiDeviceClient.\n");
device = (WiFiDeviceClientRef)CFArrayGetValueAtIndex(devices, 0);
CFRelease(devices);
verbose_log("Scheduling manager client with run loop...\n");
WiFiManagerClientScheduleWithRunLoop(manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
}
static void begin_scan()
{
if (!device || !manager) {
verbose_log("First scan; creating necessary objects.\n");
setup_for_scan();
}
// It is necessary to pass an empty dictionary (NULL does not work).
CFDictionaryRef options = CFDictionaryCreate(kCFAllocatorDefault, NULL, NULL, 0, NULL, NULL);
verbose_log("Starting to scan...\n");
WiFiDeviceClientScanAsync(device, options, scan_callback, 0);
CFRelease(options);
}
static void print_usage(char *progname)
{
printf("Airscan 1.1\nUsage: %s <options>\nOptions include:\n", progname);
printf("\t-s:\tScan for nearby WiFi networks.\n");
printf("\t-t: [secs] Scan again every X (default: 10) seconds\n");
printf("\t-v:\tEnable verbose mode.\n");
printf("\t-h:\tPrint this help.\n");
}
static void signal_handler(int signal)
{
printf("Got signal: %d. Exiting.\n", signal);
if (manager) {
CFRelease(manager);
}
if (timer) {
CFRelease(timer);
}
exit(0);
}
int main(int argc, char *argv[])
{
long int timerInterval;
int c;
bool scanMode = false;
bool helpMode = false;
if (argc < 2) {
printf("Must specify either -s or -h.\n");
return -1;
}
signal(SIGINT, signal_handler);
while ((c = getopt(argc, argv, "svht")) != -1) {
switch (c) {
case 's':
scanMode = true;
break;
case 't':
if (argv[optind]) {
timerInterval = strtol(argv[optind], NULL, 10);
} else {
verbose_log("No timer interval was specified. Using default (10).\n");
timerInterval = 10;
}
timerMode = true;
break;
case 'v':
verboseMode = true;
break;
case 'h':
helpMode = true;
break;
default:
printf("Must specify either -s or -h.\n");
return -1;
}
}
// XXX: Is there a better way to handle all of this?
if (helpMode) {
print_usage(argv[0]);
return 0;
}
if (!helpMode && !scanMode) {
printf("Must specify either -s or -h.\n");
return -1;
}
if (scanMode && !timerMode) {
begin_scan();
CFRunLoopRun();
}
if (timerMode) {
verbose_log("Creating timer with interval: %ld\n", timerInterval);
timer = CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent(), timerInterval, 0, 0, (CFRunLoopTimerCallBack)begin_scan, NULL);
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes);
CFRunLoopRun();
}
return 0;
}