-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.c
162 lines (140 loc) · 3.36 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <signal.h>
#include <ctype.h>
#include "sockets.h"
#include "esp_smartcfg.h"
#define COMPILE_TIMESTAMP __DATE__ "/" __TIME__
volatile bool run = true;
void onsignal(int signum)
{
printf("\n\n***received process signal %d ***\n", signum);
run = false;
}
void usage(const char* progname)
{
printf("Usage: %s ...\n", progname);
#if (defined _WIN32)
printf(" --essid ESSID\n");
printf(" --bssid BSSID\n");
printf(" --address LOCALADDRESS)\n");
#elif (defined _LINUX)
printf(" [ --essid ESSID ]\n");
printf(" [ --bssid BSSID ]\n");
printf(" [ (--interface INTERFACENAME) | (--address LOCALADDRESS) ]\n");
#endif
printf(" --password PASSWORD\n");
printf(" [ (--timeout TIMEOUT) | --infinite ]\n");
printf(" [ --hidden | --visible ] \n");
//dump_own_addrs();
printf("Version: " COMPILE_TIMESTAMP " \n");
}
const char* essid = NULL;
const char* bssid = NULL;
const char* passwd = NULL;
const char* myip = NULL;
const char* ownaddr = NULL;
const char* ifname = "wlan0";
int timeout = 0;
bool hidden = false;
bool scanargs(int argc, char** argv)
{
for (int i = 1; i < argc; i++)
{
char* arg = argv[i];
#define ARG_EQU(SHORTARG, LONGARG) (STREQU(arg, "-"SHORTARG) || STREQU(arg, "--"LONGARG))
if ARG_EQU("e", "essid")
{
i++;
essid = argv[i];
}
else if ARG_EQU("b", "bssid")
{
i++;
bssid = argv[i];
}
else if ARG_EQU("p", "password")
{
i++;
passwd = argv[i];
}
else if ARG_EQU("ifc", "interface")
{
i++;
ifname = argv[i];
}
else if ARG_EQU("a", "address")
{
i++;
ownaddr = argv[i];
}
else if ARG_EQU("t", "timeout")
{
i++;
timeout = atoi(argv[i]);
}
else if ARG_EQU("hid", "hidden")
{
hidden = true;
}
else if ARG_EQU("vis", "visible")
{
hidden = false;
}
else if ARG_EQU("inf", "infinite")
{
timeout = 0;
}
else if ARG_EQU("h", "help")
{
usage(argv[0]);
exit(EXIT_SUCCESS);
}
else
{
eprintf("unrecognized argument: %s\n", arg);
usage(argv[0]);
exit(EXIT_FAILURE);
}
}
}
int main(int argc, char* argv[])
{
printf("%s, ver: " COMPILE_TIMESTAMP " ...\n", argv[0]);
scanargs(argc, argv);
signal(SIGINT, onsignal);
signal(SIGTERM, onsignal);
#if (defined _LINUX)
if (ownaddr == NULL)
{
static char ownaddr_buf[sizeof("111.222.333.444") + 1] = "";
if (get_ifc_addr(ifname, ownaddr_buf))
{
eprintf("cant get address of local interface %s.\n", ifname);
return EXIT_FAILURE;
}
else
{
ownaddr = ownaddr_buf;
printf("using local interface %s address %s\n", ifname, ownaddr);
}
}
if (bssid == NULL)
{
static char essid_buf[IW_ESSID_MAX_SIZE + 1] = "";
static char bssid_buf[MAC_LEN * sizeof("xx:") + 1] = "";
uint8_t mac[MAC_LEN];
wifi_get_ssid(ifname, essid_buf, mac);
snprintf(bssid_buf, sizeof(bssid_buf), "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
essid = essid_buf;
bssid = bssid_buf;
printf("using ssid %s (%s)\n", essid, bssid);
}
#endif
esp_smartcfg_run(essid, bssid, passwd, ownaddr, hidden, timeout);
printf("%s: done\n", argv[0]);
return EXIT_SUCCESS;
}