[En-Nut-Discussion] local tcp port randomisation

Harald Kipp harald.kipp at egnite.de
Fri Jul 6 15:21:41 CEST 2012

Hi Ole,

On 06.07.2012 14:10, Ole Reinhardt wrote:
>>  ticks = (uint16_t) NutGetMillis();
>>  if (port)
>>    port += (uint16_t) ((ticks & 0x000F) | 1);
>>  else
>>    port = ticks;
>>  port |= 0xC000;
> I would be fine with this for the first instance as long as we do not
> have a real random number.
> Am I right, that we could all agree, to take this as an intermediate
> solution?

I'm a little bit in doubt about NutGetMillis() providing enough variations. But as Michel reported, it indeed solved his problem. In addition, this code provides

1. Less predictable port numbers than simple incrementing with reasonable re-use frequency.
2. Keeping port numbers in the range recommended by IANA.

Ole, note that this is used in tcpsock.c and udpsock.c. For unknown reason, TCP declares the last used ephemeral port number a volatile, which is unnecessary. 

 static volatile uint16_t last_local_port = 4096;

should be changed to

 static uint16_t last_local_port;



More information about the En-Nut-Discussion mailing list