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

cellular: random socket port number #7097

merged 2 commits into from Jun 11, 2018

Conversation

ghost
Copy link

@ghost ghost commented Jun 4, 2018

Description

Random port number (above 49151) into use for TCP and UDP sockets

Pull request type

[ X ] Fix
[ ] Refactor
[ ] New target
[ ] Feature
[ ] Breaking change

@ghost
Copy link
Author

ghost commented Jun 4, 2018

@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;

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.

AriParkkila
AriParkkila previously approved these changes Jun 4, 2018
0xc0170
0xc0170 previously approved these changes Jun 4, 2018
@0xc0170 0xc0170 requested a review from kjbracey June 4, 2018 10:03
port = 49152;
}
return port;
randLIB_seed_random();
Copy link
Contributor

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.

@ghost ghost dismissed stale reviews from 0xc0170 and AriParkkila via d3a46eb June 5, 2018 10:26
@kjbracey
Copy link
Contributor

kjbracey commented Jun 7, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Jun 7, 2018

Build : SUCCESS

Build number : 2263
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/7097/

Triggering tests

/morph test
/morph uvisor-test
/morph export-build
/morph mbed2-build

@cmonr
Copy link
Contributor

cmonr commented Jun 7, 2018

Halting CI builds until RC3 PRs are completed. Will resume after.

@0xc0170
Copy link
Contributor

0xc0170 commented Jun 8, 2018

/morph test
/morph export-build

@mbed-ci
Copy link

mbed-ci commented Jun 8, 2018

@mbed-ci
Copy link

mbed-ci commented Jun 8, 2018

@cmonr
Copy link
Contributor

cmonr commented Jun 11, 2018

@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?

@cmonr cmonr merged commit 5e8a0a8 into ARMmbed:master Jun 11, 2018
@kjbracey
Copy link
Contributor

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.

@0xc0170
Copy link
Contributor

0xc0170 commented Jun 12, 2018

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.

@ghost ghost deleted the random_port branch July 11, 2018 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants