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

cellular: random socket port number #7097

Merged
merged 2 commits into from Jun 11, 2018

Conversation

Projects
None yet
6 participants
@TeemuKultala
Contributor

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

This comment has been minimized.

Contributor

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

This comment has been minimized.

@AriParkkila

AriParkkila Jun 4, 2018

Contributor

No need to be static.

@TeemuKultala TeemuKultala force-pushed the TeemuKultala:random_port branch from 97760d9 to c461970 Jun 4, 2018

@TeemuKultala TeemuKultala force-pushed the TeemuKultala:random_port branch from c461970 to b7bad77 Jun 4, 2018

@0xc0170 0xc0170 added the needs: CI label Jun 4, 2018

@0xc0170 0xc0170 requested a review from kjbracey-arm Jun 4, 2018

port = 49152;
}
return port;
randLIB_seed_random();

This comment has been minimized.

@kjbracey-arm

kjbracey-arm Jun 4, 2018

Contributor

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.

@0xc0170 0xc0170 added needs: work and removed needs: CI labels Jun 4, 2018

@TeemuKultala TeemuKultala dismissed stale reviews from 0xc0170 and AriParkkila via d3a46eb Jun 5, 2018

@0xc0170

0xc0170 approved these changes Jun 6, 2018

@0xc0170 0xc0170 added needs: CI and removed needs: work labels Jun 6, 2018

@kjbracey-arm

This comment has been minimized.

Contributor

kjbracey-arm commented Jun 7, 2018

/morph build

@mbed-ci

This comment has been minimized.

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

This comment has been minimized.

Contributor

cmonr commented Jun 7, 2018

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

@0xc0170

This comment has been minimized.

Member

0xc0170 commented Jun 8, 2018

/morph test
/morph export-build

@mbed-ci

This comment has been minimized.

@mbed-ci

This comment has been minimized.

@cmonr

This comment has been minimized.

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

14 checks passed

AWS-CI uVisor Build & Test Success
Details
ci-morph-build build completed
Details
ci-morph-exporter build completed
Details
ci-morph-mbed2-build build completed
Details
ci-morph-test test completed
Details
continuous-integration/jenkins/pr-head This commit looks good
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
travis-ci/astyle Passed, 920 files
Details
travis-ci/docs Local docs testing has passed
Details
travis-ci/events Passed, runtime is 9824 cycles (-140 cycles)
Details
travis-ci/gitattributestest Local gitattributestest testing has passed
Details
travis-ci/licence_check Local licence_check testing has passed
Details
travis-ci/littlefs Passed, code size is 9964B (+0.00%)
Details
travis-ci/tools-py2.7 Local tools-py2.7 testing has passed
Details
@kjbracey-arm

This comment has been minimized.

Contributor

kjbracey-arm commented Jun 12, 2018

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

This comment has been minimized.

Member

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.

@TeemuKultala TeemuKultala deleted the TeemuKultala:random_port branch Jul 11, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment