Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add portmap feature to web Interface #167

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions components/cmd_router/router_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern char* subnet_mask;
extern char* gateway_addr;
extern char* ap_ssid;
extern char* ap_passwd;
extern char* portmap_info;

extern uint16_t connect_count;
extern bool ap_connect;
Expand All @@ -35,6 +36,7 @@ void preprocess_string(char* str);
int set_sta(int argc, char **argv);
int set_sta_static(int argc, char **argv);
int set_ap(int argc, char **argv);
int portmap(int argc, char **argv);

esp_err_t get_config_param_blob(char* name, uint8_t** blob, size_t blob_len);
esp_err_t get_config_param_int(char* name, int* param);
Expand Down
40 changes: 40 additions & 0 deletions main/esp32_nat_router.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const int WIFI_CONNECTED_BIT = BIT0;

#define DEFAULT_AP_IP "192.168.4.1"
#define DEFAULT_DNS "8.8.8.8"
#define MAX_PORTMAP_INFO_LENGTH 40

/* Global vars */
uint16_t connect_count = 0;
Expand Down Expand Up @@ -236,6 +237,42 @@ esp_err_t del_portmap(u8_t proto, u16_t mport) {
return ESP_OK;
}

void concatenate_strings(char *str1, const char *str2) {
while (*str1 != '\0')
{
str1++;
}

// Copy characters from the second string to the end of
// the first string
while (*str2 != '\0')
{
*str1 = *str2;
str1++;
str2++;
}

// Ensure the resulting string is null-terminated
*str1 = '\0';
}

void get_portmap_info(char **param) {
*param = (char *)malloc(MAX_PORTMAP_INFO_LENGTH * IP_PORTMAP_MAX * sizeof(char));
for (int i = 0; i < IP_PORTMAP_MAX; i++)
{
if (portmap_tab[i].valid)
{
ip4_addr_t addr;
addr.addr = my_ip;
addr.addr = portmap_tab[i].daddr;
char* portmap = (char*)malloc(MAX_PORTMAP_INFO_LENGTH * sizeof(char));
snprintf(portmap, MAX_PORTMAP_INFO_LENGTH * sizeof(char), "<br>%s:%d->" IPSTR ":%d", portmap_tab[i].proto == PROTO_TCP ? "TCP " : "UDP ", portmap_tab[i].mport, IP2STR(&addr), portmap_tab[i].dport);
concatenate_strings(*param, portmap);
free(portmap);
}
}
}

static void initialize_console(void)
{
/* Disable buffering on stdin */
Expand Down Expand Up @@ -535,6 +572,7 @@ uint8_t* ap_mac = NULL;
char* ap_ssid = NULL;
char* ap_passwd = NULL;
char* ap_ip = NULL;
char* portmap_info = NULL;

char* param_set_default(const char* def_val) {
char * retval = malloc(strlen(def_val)+1);
Expand Down Expand Up @@ -598,6 +636,8 @@ void app_main(void)

get_portmap_tab();

get_portmap_info(&portmap_info);

// 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);

Expand Down
Loading