Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site ucbvax.ARPA
Path: utzoo!watmath!clyde!burl!ulysses!ucbvax!tcp-ip
From: tcp-ip@ucbvax.ARPA
Newsgroups: fa.tcp-ip
Subject: Re: 4.2bsd/rlogin/source port choices
Message-ID: <9111@ucbvax.ARPA>
Date: Tue, 16-Jul-85 20:49:07 EDT
Article-I.D.: ucbvax.9111
Posted: Tue Jul 16 20:49:07 1985
Date-Received: Thu, 18-Jul-85 06:19:44 EDT
Sender: daemon@ucbvax.ARPA
Organization: University of California at Berkeley
Lines: 31

From: imagen!geof@su-shasta.ARPA


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