-
Notifications
You must be signed in to change notification settings - Fork 271
/
esp32_nat_router.c
693 lines (593 loc) · 22 KB
/
esp32_nat_router.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/* Console example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include "esp_system.h"
#include "esp_log.h"
#include "esp_console.h"
#include "esp_vfs_dev.h"
#include "driver/uart.h"
#include "esp_vfs_usb_serial_jtag.h"
#include "driver/usb_serial_jtag.h"
#include "linenoise/linenoise.h"
#include "argtable3/argtable3.h"
#include "esp_vfs_fat.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_eap_client.h"
#include "lwip/opt.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include "dhcpserver/dhcpserver.h"
#include "dhcpserver/dhcpserver_options.h"
#include "cmd_decl.h"
#include <esp_http_server.h>
#if !IP_NAPT
#error "IP_NAPT must be defined"
#endif
#include "lwip/lwip_napt.h"
#include "router_globals.h"
// On board LED
#if defined(CONFIG_IDF_TARGET_ESP32S3)
#define BLINK_GPIO 44
#else
#define BLINK_GPIO 2
#endif
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t wifi_event_group;
/* The event group allows multiple bits for each event, but we only care about one event
* - are we connected to the AP with an IP? */
const int WIFI_CONNECTED_BIT = BIT0;
#define DEFAULT_AP_IP "192.168.4.1"
#define DEFAULT_DNS "8.8.8.8"
/* Global vars */
uint16_t connect_count = 0;
bool ap_connect = false;
bool has_static_ip = false;
uint32_t my_ip;
uint32_t my_ap_ip;
struct portmap_table_entry {
u32_t daddr;
u16_t mport;
u16_t dport;
u8_t proto;
u8_t valid;
};
struct portmap_table_entry portmap_tab[IP_PORTMAP_MAX];
esp_netif_t* wifiAP;
esp_netif_t* wifiSTA;
httpd_handle_t start_webserver(void);
static const char *TAG = "ESP32 NAT router";
/* Console command history can be stored to and loaded from a file.
* The easiest way to do this is to use FATFS filesystem on top of
* wear_levelling library.
*/
#if CONFIG_STORE_HISTORY
#define MOUNT_PATH "/data"
#define HISTORY_PATH MOUNT_PATH "/history.txt"
static void initialize_filesystem(void)
{
static wl_handle_t wl_handle;
const esp_vfs_fat_mount_config_t mount_config = {
.max_files = 4,
.format_if_mount_failed = true
};
esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(MOUNT_PATH, "storage", &mount_config, &wl_handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
return;
}
}
#endif // CONFIG_STORE_HISTORY
static void initialize_nvs(void)
{
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK( nvs_flash_erase() );
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
}
esp_err_t apply_portmap_tab() {
for (int i = 0; i<IP_PORTMAP_MAX; i++) {
if (portmap_tab[i].valid) {
ip_portmap_add(portmap_tab[i].proto, my_ip, portmap_tab[i].mport, portmap_tab[i].daddr, portmap_tab[i].dport);
}
}
return ESP_OK;
}
esp_err_t delete_portmap_tab() {
for (int i = 0; i<IP_PORTMAP_MAX; i++) {
if (portmap_tab[i].valid) {
ip_portmap_remove(portmap_tab[i].proto, portmap_tab[i].mport);
}
}
return ESP_OK;
}
void print_portmap_tab() {
for (int i = 0; i<IP_PORTMAP_MAX; i++) {
if (portmap_tab[i].valid) {
printf ("%s", portmap_tab[i].proto == PROTO_TCP?"TCP ":"UDP ");
ip4_addr_t addr;
addr.addr = my_ip;
printf (IPSTR":%d -> ", IP2STR(&addr), portmap_tab[i].mport);
addr.addr = portmap_tab[i].daddr;
printf (IPSTR":%d\n", IP2STR(&addr), portmap_tab[i].dport);
}
}
}
esp_err_t get_portmap_tab() {
esp_err_t err;
nvs_handle_t nvs;
size_t len;
err = nvs_open(PARAM_NAMESPACE, NVS_READWRITE, &nvs);
if (err != ESP_OK) {
return err;
}
err = nvs_get_blob(nvs, "portmap_tab", NULL, &len);
if (err == ESP_OK) {
if (len != sizeof(portmap_tab)) {
err = ESP_ERR_NVS_INVALID_LENGTH;
} else {
err = nvs_get_blob(nvs, "portmap_tab", portmap_tab, &len);
if (err != ESP_OK) {
memset(portmap_tab, 0, sizeof(portmap_tab));
}
}
}
nvs_close(nvs);
return err;
}
esp_err_t add_portmap(u8_t proto, u16_t mport, u32_t daddr, u16_t dport) {
esp_err_t err;
nvs_handle_t nvs;
for (int i = 0; i<IP_PORTMAP_MAX; i++) {
if (!portmap_tab[i].valid) {
portmap_tab[i].proto = proto;
portmap_tab[i].mport = mport;
portmap_tab[i].daddr = daddr;
portmap_tab[i].dport = dport;
portmap_tab[i].valid = 1;
err = nvs_open(PARAM_NAMESPACE, NVS_READWRITE, &nvs);
if (err != ESP_OK) {
return err;
}
err = nvs_set_blob(nvs, "portmap_tab", portmap_tab, sizeof(portmap_tab));
if (err == ESP_OK) {
err = nvs_commit(nvs);
if (err == ESP_OK) {
ESP_LOGI(TAG, "New portmap table stored.");
}
}
nvs_close(nvs);
ip_portmap_add(proto, my_ip, mport, daddr, dport);
return ESP_OK;
}
}
return ESP_ERR_NO_MEM;
}
esp_err_t del_portmap(u8_t proto, u16_t mport) {
esp_err_t err;
nvs_handle_t nvs;
for (int i = 0; i<IP_PORTMAP_MAX; i++) {
if (portmap_tab[i].valid && portmap_tab[i].mport == mport && portmap_tab[i].proto == proto) {
portmap_tab[i].valid = 0;
err = nvs_open(PARAM_NAMESPACE, NVS_READWRITE, &nvs);
if (err != ESP_OK) {
return err;
}
err = nvs_set_blob(nvs, "portmap_tab", portmap_tab, sizeof(portmap_tab));
if (err == ESP_OK) {
err = nvs_commit(nvs);
if (err == ESP_OK) {
ESP_LOGI(TAG, "New portmap table stored.");
}
}
nvs_close(nvs);
ip_portmap_remove(proto, mport);
return ESP_OK;
}
}
return ESP_OK;
}
static void initialize_console(void)
{
/* Disable buffering on stdin */
setvbuf(stdin, NULL, _IONBF, 0);
#if CONFIG_ESP_CONSOLE_UART_DEFAULT || CONFIG_ESP_CONSOLE_UART_CUSTOM
/* Drain stdout before reconfiguring it */
fflush(stdout);
fsync(fileno(stdout));
/* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
esp_vfs_dev_uart_port_set_rx_line_endings(0, ESP_LINE_ENDINGS_CR);
/* Move the caret to the beginning of the next line on '\n' */
esp_vfs_dev_uart_port_set_tx_line_endings(0, ESP_LINE_ENDINGS_CRLF);
/* Configure UART. Note that REF_TICK is used so that the baud rate remains
* correct while APB frequency is changing in light sleep mode.
*/
const uart_config_t uart_config = {
.baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S2)
.source_clk = UART_SCLK_REF_TICK,
#else
.source_clk = UART_SCLK_XTAL,
#endif
};
/* Install UART driver for interrupt-driven reads and writes */
ESP_ERROR_CHECK( uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM,
256, 0, 0, NULL, 0) );
ESP_ERROR_CHECK( uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config) );
/* Tell VFS to use UART driver */
esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
#endif
#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
/* Enable non-blocking mode on stdin and stdout */
fcntl(fileno(stdout), F_SETFL, O_NONBLOCK);
fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
/* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
esp_vfs_dev_usb_serial_jtag_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
/* Move the caret to the beginning of the next line on '\n' */
esp_vfs_dev_usb_serial_jtag_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
usb_serial_jtag_driver_config_t usb_serial_jtag_config = {
.tx_buffer_size = 256,
.rx_buffer_size = 256,
};
/* Install USB-SERIAL-JTAG driver for interrupt-driven reads and writes */
usb_serial_jtag_driver_install(&usb_serial_jtag_config);
/* Tell vfs to use usb-serial-jtag driver */
esp_vfs_usb_serial_jtag_use_driver();
#endif
/* Initialize the console */
esp_console_config_t console_config = {
.max_cmdline_args = 8,
.max_cmdline_length = 256,
#if CONFIG_LOG_COLORS
.hint_color = atoi(LOG_COLOR_CYAN)
#endif
};
ESP_ERROR_CHECK( esp_console_init(&console_config) );
/* Configure linenoise line completion library */
/* Enable multiline editing. If not set, long commands will scroll within
* single line.
*/
linenoiseSetMultiLine(1);
/* Tell linenoise where to get command completions and hints */
linenoiseSetCompletionCallback(&esp_console_get_completion);
linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
/* Set command history size */
linenoiseHistorySetMaxLen(100);
#if CONFIG_STORE_HISTORY
/* Load command history from filesystem */
linenoiseHistoryLoad(HISTORY_PATH);
#endif
}
void * led_status_thread(void * p)
{
gpio_reset_pin(BLINK_GPIO);
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
while (true)
{
gpio_set_level(BLINK_GPIO, ap_connect);
for (int i = 0; i < connect_count; i++)
{
gpio_set_level(BLINK_GPIO, 1 - ap_connect);
vTaskDelay(50 / portTICK_PERIOD_MS);
gpio_set_level(BLINK_GPIO, ap_connect);
vTaskDelay(50 / portTICK_PERIOD_MS);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
esp_netif_dns_info_t dns;
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
{
esp_wifi_connect();
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{
ESP_LOGI(TAG,"disconnected - retry to connect to the AP");
ap_connect = false;
esp_wifi_connect();
ESP_LOGI(TAG, "retry to connect to the AP");
xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT);
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
ap_connect = true;
my_ip = event->ip_info.ip.addr;
delete_portmap_tab();
apply_portmap_tab();
if (esp_netif_get_dns_info(wifiSTA, ESP_NETIF_DNS_MAIN, &dns) == ESP_OK)
{
esp_netif_set_dns_info(wifiAP, ESP_NETIF_DNS_MAIN, &dns);
ESP_LOGI(TAG, "set dns to:" IPSTR, IP2STR(&(dns.ip.u_addr.ip4)));
}
xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED)
{
connect_count++;
ESP_LOGI(TAG,"%d. station connected", connect_count);
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED)
{
connect_count--;
ESP_LOGI(TAG,"station disconnected - %d remain", connect_count);
}
}
const int CONNECTED_BIT = BIT0;
#define JOIN_TIMEOUT_MS (2000)
void wifi_init(const uint8_t* mac, const char* ssid, const char* ent_username, const char* ent_identity, const char* passwd, const char* static_ip, const char* subnet_mask, const char* gateway_addr, const uint8_t* ap_mac, const char* ap_ssid, const char* ap_passwd, const char* ap_ip)
{
esp_netif_dns_info_t dnsserver;
// esp_netif_dns_info_t dnsinfo;
wifi_event_group = xEventGroupCreate();
esp_netif_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifiAP = esp_netif_create_default_wifi_ap();
wifiSTA = esp_netif_create_default_wifi_sta();
esp_netif_ip_info_t ipInfo_sta;
if ((strlen(ssid) > 0) && (strlen(static_ip) > 0) && (strlen(subnet_mask) > 0) && (strlen(gateway_addr) > 0)) {
has_static_ip = true;
ipInfo_sta.ip.addr = esp_ip4addr_aton(static_ip);
ipInfo_sta.gw.addr = esp_ip4addr_aton(gateway_addr);
ipInfo_sta.netmask.addr = esp_ip4addr_aton(subnet_mask);
esp_netif_dhcpc_stop(wifiSTA); // Don't run a DHCP client
esp_netif_set_ip_info(wifiSTA, &ipInfo_sta);
apply_portmap_tab();
}
my_ap_ip = esp_ip4addr_aton(ap_ip);
esp_netif_ip_info_t ipInfo_ap;
ipInfo_ap.ip.addr = my_ap_ip;
ipInfo_ap.gw.addr = my_ap_ip;
esp_netif_set_ip4_addr(&ipInfo_ap.netmask, 255,255,255,0);
esp_netif_dhcps_stop(wifiAP); // stop before setting ip WifiAP
esp_netif_set_ip_info(wifiAP, &ipInfo_ap);
esp_netif_dhcps_start(wifiAP);
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL,
&instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&wifi_event_handler,
NULL,
&instance_got_ip));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
/* ESP WIFI CONFIG */
wifi_config_t wifi_config = { 0 };
wifi_config_t ap_config = {
.ap = {
.channel = 0,
.authmode = WIFI_AUTH_WPA2_WPA3_PSK,
.ssid_hidden = 0,
.max_connection = 8,
.beacon_interval = 100,
}
};
strlcpy((char*)ap_config.sta.ssid, ap_ssid, sizeof(ap_config.sta.ssid));
if (strlen(ap_passwd) < 8) {
ap_config.ap.authmode = WIFI_AUTH_OPEN;
} else {
strlcpy((char*)ap_config.sta.password, ap_passwd, sizeof(ap_config.sta.password));
}
if (strlen(ssid) > 0) {
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA) );
//Set SSID
strlcpy((char*)wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
//Set passwprd
if(strlen(ent_username) == 0) {
ESP_LOGI(TAG, "STA regular connection");
strlcpy((char*)wifi_config.sta.password, passwd, sizeof(wifi_config.sta.password));
}
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
if(strlen(ent_username) != 0 && strlen(ent_identity) != 0) {
ESP_LOGI(TAG, "STA enterprise connection");
if(strlen(ent_username) != 0 && strlen(ent_identity) != 0) {
esp_eap_client_set_identity((uint8_t *)ent_identity, strlen(ent_identity)); //provide identity
} else {
esp_eap_client_set_identity((uint8_t *)ent_username, strlen(ent_username));
}
esp_eap_client_set_username((uint8_t *)ent_username, strlen(ent_username)); //provide username
esp_eap_client_set_password((uint8_t *)passwd, strlen(passwd)); //provide password
esp_wifi_sta_enterprise_enable();
}
if (mac != NULL) {
ESP_ERROR_CHECK(esp_wifi_set_mac(ESP_IF_WIFI_STA, mac));
}
} else {
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP) );
}
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &ap_config) );
if (ap_mac != NULL) {
ESP_ERROR_CHECK(esp_wifi_set_mac(ESP_IF_WIFI_AP, ap_mac));
}
// Enable DNS (offer) for dhcp server
dhcps_offer_t dhcps_dns_value = OFFER_DNS;
esp_netif_dhcps_option(wifiAP,ESP_NETIF_OP_SET, ESP_NETIF_DOMAIN_NAME_SERVER, &dhcps_dns_value, sizeof(dhcps_dns_value));
// // Set custom dns server address for dhcp server
dnsserver.ip.u_addr.ip4.addr = esp_ip4addr_aton(DEFAULT_DNS);
dnsserver.ip.type = ESP_IPADDR_TYPE_V4;
esp_netif_set_dns_info(wifiAP, ESP_NETIF_DNS_MAIN, &dnsserver);
// esp_netif_get_dns_info(ESP_IF_WIFI_AP, ESP_NETIF_DNS_MAIN, &dnsinfo);
// ESP_LOGI(TAG, "DNS IP:" IPSTR, IP2STR(&dnsinfo.ip.u_addr.ip4));
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
pdFALSE, pdTRUE, JOIN_TIMEOUT_MS / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(esp_wifi_start());
if (strlen(ssid) > 0) {
ESP_LOGI(TAG, "wifi_init_apsta finished.");
ESP_LOGI(TAG, "connect to ap SSID: %s ", ssid);
} else {
ESP_LOGI(TAG, "wifi_init_ap with default finished.");
}
}
uint8_t* mac = NULL;
char* ssid = NULL;
char* ent_username = NULL;
char* ent_identity = NULL;
char* passwd = NULL;
char* static_ip = NULL;
char* subnet_mask = NULL;
char* gateway_addr = NULL;
uint8_t* ap_mac = NULL;
char* ap_ssid = NULL;
char* ap_passwd = NULL;
char* ap_ip = NULL;
char* param_set_default(const char* def_val) {
char * retval = malloc(strlen(def_val)+1);
strcpy(retval, def_val);
return retval;
}
void app_main(void)
{
initialize_nvs();
#if CONFIG_STORE_HISTORY
initialize_filesystem();
ESP_LOGI(TAG, "Command history enabled");
#else
ESP_LOGI(TAG, "Command history disabled");
#endif
get_config_param_blob("mac", &mac, 6);
get_config_param_str("ssid", &ssid);
if (ssid == NULL) {
ssid = param_set_default("");
}
get_config_param_str("ent_username", &ent_username);
if (ent_username == NULL) {
ent_username = param_set_default("");
}
get_config_param_str("ent_identity", &ent_identity);
if (ent_identity == NULL) {
ent_identity = param_set_default("");
}
get_config_param_str("passwd", &passwd);
if (passwd == NULL) {
passwd = param_set_default("");
}
get_config_param_str("static_ip", &static_ip);
if (static_ip == NULL) {
static_ip = param_set_default("");
}
get_config_param_str("subnet_mask", &subnet_mask);
if (subnet_mask == NULL) {
subnet_mask = param_set_default("");
}
get_config_param_str("gateway_addr", &gateway_addr);
if (gateway_addr == NULL) {
gateway_addr = param_set_default("");
}
get_config_param_blob("ap_mac", &ap_mac, 6);
get_config_param_str("ap_ssid", &ap_ssid);
if (ap_ssid == NULL) {
ap_ssid = param_set_default("ESP32_NAT_Router");
}
get_config_param_str("ap_passwd", &ap_passwd);
if (ap_passwd == NULL) {
ap_passwd = param_set_default("");
}
get_config_param_str("ap_ip", &ap_ip);
if (ap_ip == NULL) {
ap_ip = param_set_default(DEFAULT_AP_IP);
}
get_portmap_tab();
// Setup WIFI
wifi_init(mac, ssid, ent_username, ent_identity, passwd, static_ip, subnet_mask, gateway_addr, ap_mac, ap_ssid, ap_passwd, ap_ip);
pthread_t t1;
pthread_create(&t1, NULL, led_status_thread, NULL);
ip_napt_enable(my_ap_ip, 1);
ESP_LOGI(TAG, "NAT is enabled");
char* lock = NULL;
get_config_param_str("lock", &lock);
if (lock == NULL) {
lock = param_set_default("0");
}
if (strcmp(lock, "0") ==0) {
ESP_LOGI(TAG,"Starting config web server");
start_webserver();
}
free(lock);
initialize_console();
/* Register commands */
esp_console_register_help_command();
register_system();
register_nvs();
register_router();
/* Prompt to be printed before each line.
* This can be customized, made dynamic, etc.
*/
const char* prompt = LOG_COLOR_I "esp32> " LOG_RESET_COLOR;
printf("\n"
"ESP32 NAT ROUTER\n"
"Type 'help' to get the list of commands.\n"
"Use UP/DOWN arrows to navigate through command history.\n"
"Press TAB when typing command name to auto-complete.\n");
if (strlen(ssid) == 0) {
printf("\n"
"Unconfigured WiFi\n"
"Configure using 'set_sta' and 'set_ap' and restart.\n");
}
/* Figure out if the terminal supports escape sequences */
int probe_status = linenoiseProbe();
if (probe_status) { /* zero indicates success */
printf("\n"
"Your terminal application does not support escape sequences.\n"
"Line editing and history features are disabled.\n"
"On Windows, try using Putty instead.\n");
linenoiseSetDumbMode(1);
#if CONFIG_LOG_COLORS
/* Since the terminal doesn't support escape sequences,
* don't use color codes in the prompt.
*/
prompt = "esp32> ";
#endif //CONFIG_LOG_COLORS
}
/* Main loop */
while(true) {
/* Get a line using linenoise.
* The line is returned when ENTER is pressed.
*/
char* line = linenoise(prompt);
if (line == NULL) { /* Ignore empty lines */
continue;
}
/* Add the command to the history */
linenoiseHistoryAdd(line);
#if CONFIG_STORE_HISTORY
/* Save command history to filesystem */
linenoiseHistorySave(HISTORY_PATH);
#endif
/* Try to run the command */
int ret;
esp_err_t err = esp_console_run(line, &ret);
if (err == ESP_ERR_NOT_FOUND) {
printf("Unrecognized command\n");
} else if (err == ESP_ERR_INVALID_ARG) {
// command was empty
} else if (err == ESP_OK && ret != ESP_OK) {
printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(ret));
} else if (err != ESP_OK) {
printf("Internal error: %s\n", esp_err_to_name(err));
}
/* linenoise allocates line buffer on the heap, so need to free it */
linenoiseFree(line);
}
}