diff --git a/src/common.c b/src/common.c index 1b30672..5b0c519 100755 --- a/src/common.c +++ b/src/common.c @@ -24,6 +24,7 @@ #include "common.h" #include "cwmp_cli.h" #include "uci_utils.h" +#include "ssl_utils.h" #include "ubus_utils.h" #include "log.h" @@ -1376,3 +1377,19 @@ void apply_allowed_cr_ip_port(void) pclose(pp); } } + +int get_random_cpe_port(void) +{ + unsigned int min = 30000; + unsigned int max = 45000; + unsigned int rand_port = 0; + + char *rand = generate_random_string(4); + if (rand) { + unsigned int tmp_rand = (unsigned int)strtoul(rand, NULL, 16); + rand_port = min + (tmp_rand % (max - min + 1)); + free(rand); + } + + return rand_port ? rand_port : DEFAULT_CONNECTION_REQUEST_PORT; +} diff --git a/src/common.h b/src/common.h index 8b73550..7862c8e 100644 --- a/src/common.h +++ b/src/common.h @@ -703,4 +703,5 @@ bool end_session_reload_pending(void); void add_path_list(struct list_head *list, char *str); void free_path_list(struct list_head *list); void apply_allowed_cr_ip_port(void); +int get_random_cpe_port(void); #endif diff --git a/src/uci_utils.c b/src/uci_utils.c index 285cead..dd2c5c0 100644 --- a/src/uci_utils.c +++ b/src/uci_utils.c @@ -336,13 +336,20 @@ static void config_get_cpe_elements(struct uci_section *s) } CWMP_LOG(DEBUG, "CWMP CONFIG - cpe connection request timeout: %d", cwmp_ctx.conf.cr_timeout); - cwmp_ctx.conf.connection_request_port = DEFAULT_CONNECTION_REQUEST_PORT; + int a = 0; + bool cpe_port_configured = true; char *port = get_value_from_uci_option(cpe_tb[UCI_CPE_PORT]); if (strlen(port) != 0) { - int a = (int)strtol(port, NULL, 10); - cwmp_ctx.conf.connection_request_port = (a > 0) ? a : DEFAULT_CONNECTION_REQUEST_PORT; + a = (int)strtol(port, NULL, 10); + } + + if (a > 0) { + cwmp_ctx.conf.connection_request_port = a; + CWMP_LOG(DEBUG, "CWMP CONFIG - cpe connection request port: %d", cwmp_ctx.conf.connection_request_port); + } else { + cpe_port_configured = false; + cwmp_ctx.conf.connection_request_port = get_random_cpe_port(); } - CWMP_LOG(DEBUG, "CWMP CONFIG - cpe connection request port: %d", cwmp_ctx.conf.connection_request_port); char *crpath = get_value_from_uci_option(cpe_tb[UCI_CPE_CRPATH]); if (CWMP_STRLEN(crpath) == 0) { @@ -471,14 +478,18 @@ static void config_get_cpe_elements(struct uci_section *s) CWMP_LOG(DEBUG, "CWMP CONFIG - cpe allowed_cr_ip: %s", cwmp_ctx.conf.valid_cr_ip); - cwmp_ctx.conf.cpe_bind_retries = 0; - char *bind_count = get_value_from_uci_option(cpe_tb[UCI_CPE_BIND_RETRIES]); - if (strlen(bind_count) != 0) { - int a = (int)strtol(bind_count, NULL, 10); - cwmp_ctx.conf.cpe_bind_retries = (a > 0) ? a : 0; + if (cpe_port_configured == true) { + cwmp_ctx.conf.cpe_bind_retries = 0; + char *bind_count = get_value_from_uci_option(cpe_tb[UCI_CPE_BIND_RETRIES]); + if (strlen(bind_count) != 0) { + int a = (int)strtol(bind_count, NULL, 10); + cwmp_ctx.conf.cpe_bind_retries = (a > 0) ? a : 0; + } + CWMP_LOG(DEBUG, "CWMP CONFIG - cpe bind retries: %d", cwmp_ctx.conf.cpe_bind_retries); + } else { + cwmp_ctx.conf.cpe_bind_retries = 1; + CWMP_LOG(DEBUG, "CWMP CONFIG - cpe bind retries set to 1 since random cpe port selected"); } - - CWMP_LOG(DEBUG, "CWMP CONFIG - cpe bind retries: %d", cwmp_ctx.conf.cpe_bind_retries); } static void config_get_lwn_elements(struct uci_section *s)