-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathessid.c
86 lines (77 loc) · 1.92 KB
/
essid.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/wireless.h>
#define INTERVAL 5
#define INTERFACE "wlan0"
#define FORMAT "%s"
char *format = FORMAT;
int interval = INTERVAL;
char name[IW_ESSID_MAX_SIZE + 1] = {0};
void put_status(int fd, struct iwreq *rqt)
{
rqt->u.essid.pointer = name;
rqt->u.essid.length = IW_ESSID_MAX_SIZE + 1;
if (ioctl(fd, SIOCGIWESSID, rqt) == -1) {
perror("ioctl");
exit(EXIT_FAILURE);
}
printf(format, name);
printf("\n");
fflush(stdout);
}
int main(int argc, char *argv[])
{
char *interface = INTERFACE;
bool snoop = false;
int opt;
while ((opt = getopt(argc, argv, "hsf:i:w:")) != -1) {
switch (opt) {
case 'h':
printf("essid [-h|-s|-i INTERVAL|-f FORMAT|-w INTERFACE]\n");
exit(EXIT_SUCCESS);
break;
case 'i':
interval = atoi(optarg);
break;
case 's':
snoop = true;
break;
case 'f':
format = optarg;
break;
case 'w':
interface = optarg;
break;
}
}
struct iwreq request;
int sock_fd;
memset(&request, 0, sizeof(struct iwreq));
sprintf(request.ifr_name, interface);
if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
if (snoop)
while (true) {
put_status(sock_fd, &request);
sleep(interval);
name[0] = '\0';
}
else
put_status(sock_fd, &request);
close(sock_fd);
if (strlen(name) > 0)
return EXIT_SUCCESS;
else
return EXIT_FAILURE;
}