more effective port randomisation

pull/7097/head
Teemu Kultala 2018-06-05 13:25:59 +03:00
parent b7bad77569
commit d3a46eb7cd
1 changed files with 20 additions and 3 deletions

View File

@ -15,10 +15,17 @@
* limitations under the License.
*/
#include "CellularUtil.h"
#include "randLIB.h"
#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)
@ -316,9 +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()
{
randLIB_seed_random();
static uint16_t port_counter = RANDOM_PORT_NUMBER_COUNT;
return (randLIB_get_16bit() | 0xC000);
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 (RANDOM_PORT_NUMBER_START + port_counter);
}
} // namespace mbed_cellular_util