You can re-connect() UDP sockets (portably)

July 3, 2013

A commentator on my entry on UDP sockets and sendto() noted:

You can disconnect or re-connect a UDP socket, at least under Linux. The man page says:

"If address is a null address for the protocol, the socket's peer address shall be reset."

They are absolutely correct. More than that, this is a portable socket feature; you can find similar wording in the manpages for FreeBSD and even Solaris 10, to name two Unixes that I checked manpages for. Everyone supports both completely resetting a UDP socket back to the disconnected state (as stated above) and simply changing the address that it's connected to (you just connect() it to the new address).

In fact this caused me to become curious about how far back this particular feature went, which is where the Unix Heritage Society's Unix Tree comes in really handy. I will cut to the chase: this connect() behavior first appeared in and was first documented in the first 4.3 BSD release. It was not in 4.2 BSD, where you really could only connect() a UDP socket once.

(I actually read the online kernel source code to make sure of this, because I thought it might have been in the code but just not documented. No such luck. The relevant 4.3 BSD kernel source actually has a rare comment to explicitly discuss the new behavior; see soconnect().)

In an interesting note, this 4.3 BSD change apparently did not immediately make it into commercial Unixes. The Unix Tree has PDP-11 Ultrix 3.1 source online and its soconnect() has the original 4.2 BSD behavior, although it was apparently released two years after 4.3 BSD. Ultrix did eventually pick up this 4.3 BSD behavior; we happen to have Ultrix 4.2 source still online and it has this change. Based on kernel source again, SunOS also appears to have picked up this change sometime between SunOS 3.5 and SunOS 4.1.

(The SunOS history page suggests that this change likely appeared in SunOS 4.0 when it became fully based on 4.3 BSD.)

(You may wonder why we have so much ancient Unix source code sitting around. The answer boils down to 'university sysadmins hate deleting source code, especially once it becomes an antique'. It's not like this stuff takes up much disk space, either; the entire Ultrix 4.2 source tree is only 210 MBytes. Instead the way we usually lose this historical stuff is by forgetting that it's there and then casually abandoning entire filesystems because they're just 'obsolete stuff'.)

Written on 03 July 2013.
« Today's question: are anti-spam statistics useful for us?
My version of the story of universities and Unix source code »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Wed Jul 3 01:17:01 2013
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.