-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Conversation
@AriParkkila please review |
@@ -316,11 +317,12 @@ 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to be static.
port = 49152; | ||
} | ||
return port; | ||
randLIB_seed_random(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you're not tracking which ports you've already chosen or are still using, I think it would probably be better to keep a cycling algorithm to maximise time before reuse in one session.
Using some random online birthday paradox calculator, I find the above has a 50% chance of reuse in 150 calls.
If you want to start digging deeper into changing strategies, read https://tools.ietf.org/html/rfc6056, but for now I suggest sticking with the same cycling strategy and just making sure you start at a random point. Nanostack uses algorithm 5 from that paper, but it knows what the current ports in use are:
#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 500
static uint16_t port_counter;
port_counter = randLIB_get_random_in_range(0, RANDOM_PORT_NUMBER_COUNT - 1);
uint16_t count = RANDOM_PORT_NUMBER_COUNT;
do {
port_counter += randLIB_get_random_in_range(1, RANDOM_PORT_NUMBER_MAX_STEP);
while (port_counter >= RANDOM_PORT_NUMBER_COUNT) {
port_counter -= RANDOM_PORT_NUMBER_COUNT;
}
uint16_t port = RANDOM_PORT_NUMBER_START + port_counter;
if (socket_port_validate(port, protocol) == eOK) {
return port;
}
} while (--count > 0);
You could use that, but you'd probably want a smaller MAX_STEP unless you can add a validate
.
/morph build |
Build : SUCCESSBuild number : 2263 Triggering tests/morph test |
Halting CI builds until RC3 PRs are completed. Will resume after. |
/morph test |
Exporter Build : SUCCESSBuild number : 1900 |
Test : SUCCESSBuild number : 2061 |
@TeemuKultala Bringing this into 5.10.0-rc1 since the return value has changed and is therefore an API change. Is there a specific issue this PR fixing? |
Problem it solves is difficulty reconnecting to server after reboot, due to reuse of same port number. An issue we've seen before with other stacks, and has recurred with this code. This is bringing these non-PPP modems into line with the existing behaviour of IP stacks that properly randomise like lwIP and Nanostack - algorithm is based on Nanostack's. So I think it's a 5.9.x fix. PPP modems would be getting proper randomisation already through lwIP. |
It fixes the values that are returned (generating the random number compare to "i++") - no code should rely on specific number returned. This should be fine for the patch release, labeled as such. |
Description
Random port number (above 49151) into use for TCP and UDP sockets
Pull request type