So this is interesting (to TCP geeks).+John Looney
while true; do telnet localhost 50000; done
will eventually succeed in connecting (nothing's listening on port 50000), and, strangely, it connects to itself. What's happening is that the ephemeral port allocated for the socket source is actually accepting the connection, resulting in a self connect.
You can make this happen immediately by specifying a source port; eg:
socat stdio TCP:localhost:50000,sourceport=50000
Even more interestingly; if you have two machines, and arrange matters so that each connects to the other using the other's source address as their target address, the connection is established. This can be shown using socat as follows:
On machine A: while true; do socat stdio TCP:B:50000,sourceport=50001; done
On machine B: while true; do socat stdio TCP:A:50001,sourceport=50000; done
(The loop retries until we manage to hit the narrow window between connection attempt and reset on each system; I typically see about 40-50 attempts before success.)+Kevin Lyda
and +Paul Jakma
did some further investigation, and it looks like this is how the "simultaneous connection synchronisation" of TCP works - see figure 8 of RFC793. General opinion of the systems administration people I've shown this to is "WITCHCRAFT!"
Your Interesting Network Thing Of The Day.
For your next task: figure out how to make this into a connection-interception hack. Easiest to achieve on localhost due to the size of the address:port space, but still...