Skip to content

cellular: random socket port number #7097

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

Merged
merged 2 commits into from Jun 11, 2018
Merged
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
25 changes: 20 additions & 5 deletions features/cellular/framework/common/CellularUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
#include <string.h>
#include <stdlib.h>


#include "randLIB.h"
#define RANDOM_PORT_NUMBER_START 49152
#define RANDOM_PORT_NUMBER_END 65535
#define RANDOM_PORT_NUMBER_COUNT (RANDOM_PORT_NUMBER_END - RANDOM_PORT_NUMBER_START + 1)
#define RANDOM_PORT_NUMBER_MAX_STEP 100


namespace mbed_cellular_util {

void convert_ipv6(char* ip)
Expand Down Expand Up @@ -315,12 +323,19 @@ int char_str_to_hex_str(const char* str, uint16_t len, char *buf, bool omit_lead

uint16_t get_dynamic_ip_port()
{
static uint16_t port;
port++;
if (port < 49152) {
port = 49152;
static uint16_t port_counter = RANDOM_PORT_NUMBER_COUNT;

if (port_counter == RANDOM_PORT_NUMBER_COUNT) {
randLIB_seed_random();
port_counter = randLIB_get_random_in_range(0, RANDOM_PORT_NUMBER_COUNT - 1);
}

port_counter += randLIB_get_random_in_range(1, RANDOM_PORT_NUMBER_MAX_STEP);
if (port_counter >= RANDOM_PORT_NUMBER_COUNT) {
port_counter -= RANDOM_PORT_NUMBER_COUNT;
}
return port;

return (RANDOM_PORT_NUMBER_START + port_counter);
}

} // namespace mbed_cellular_util