4.2bsd/rlogin/source port choices
geof at su-shasta.ARPA
geof at su-shasta.ARPA
Wed Jul 17 10:01:16 AEST 1985
The TCP source port for rlogin should be chosen in a manner that makes
it unlikely for the same port to be reused twice in a row. "Twice in a
row" includes the possibility that the ports will be chosen before and
after crashes, so a RAM counter is inappropriate. 4.2's apparent
method of grabbing closest port below 1024 that is not currently used
tends to choose the same port twice in a row with high probability in a
number of cases. This algorithm is not suitable for choosing TCP port
numbers (Gosh, I hope the kernel does a better job!).
A better technique is to generate some random number in the right range
of ports each time a port number is needed, and regenerate another if
you fail. A simple expediency is to use the low-order bits of a
millisecond clock. A user process on Unix (with a one-second clock)
might use:
long now;
time(&now);
sleep(1);
port = htons( (now + getpid()) % 512) + 512 );
to get a number in the range [512,1024), or
port = htons( (now + getpid()) | 0x8000 );
to get a port number in the "temporary" range.
- Geof
More information about the Comp.unix.wizards
mailing list