Friday, June 22, 2007

Rebinding sockets.

Well It's been a crazy couple of months, but I am back!

Anywho, something cool I learned the other day.

First off the motivation behind it. Say you've got a server which is _always_ accepting connections. Now say you would like to kill the server in a graceful manner which allows you to update and start a new instance of the server in a few seconds. You could try to reject all connections on 'graceful' shutdown, however there is a problem. Even if you close the socket, clear the queue, and reject all previous connections, when you close a connection in TCP, it throws it into the TIME_WAIT state because of the nature of TCP.

The reason for going to TIME_WAIT is because TCP is not supposed to drop messages. Say we were talking and then I said goodbye. In real life you would also say goodbye before we parted ways, but in TCP there is no acknowledgment of the end of the session because to say 'goodbye' the server closes the socket. However, what if the client never receives the packet which tells it the connection is closed at the other end? It would then attempt to request any dropped packets in 30seconds - 4 minutes later. This is what the TIME_WAIT state is for, it allows the client to request dropped packets.

Thats all well and good, but I don't want to wait for this timeout, I want to rebind my socket NOW! Well there is a way around it. You can use the function setsocketoption to set SO_REUSEPORT which if set, allows future instances to reopen a socket in a TIME_WAIT state. This means that if i kill a server with this option set (cleanly or with a signal) the socket will go to TIME_WAIT, but with the option set, we can reopen it!

I'm not sure what the last two vars are for, but the example i found passes 'one = 1;' in which i assume is just a 'dont do anything' type of deal, and like many socket functions you also have to pass the size of the var in with it.

setsockopt(serversock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one);

works like a charm on suse 10.2, my code for work rebinds instantly, ill update this with other portability notes (specifically rhel4 and Mac OS X).

Enjoy,
-Crusher4

6 comments:

Scott Robinson said...

"setsockopt"

The optval and optlen are used to indicate how you're setting a boolean option. So, the more common usage of:

setsockopt(..., TRUE, sizeof(TRUE)) indicate you're turning on the option.

Crusher4 said...

Indeed, there was also another syntax error. I was in windows (shudder) earlier so I couldn't check my syntax. But now it's fixed.

Scott Robinson said...

Haha, d'oh. I forgot the pointer too!

Anonymous said...

Hi,
m using linux gentoo..
will this function setsocketoption work under gentoo enviroment.
i think its a microsoft stuff.m new to linux and socket programming and interested in socket rebinding research.

thanks

Anonymous said...

Hi,
m using linux gentoo..
will this function setsocketoption work under gentoo enviroment.
i think its a microsoft stuff.m new to linux and socket programming and interested in socket rebinding research.

thanks

Anonymous said...

free sample viagra soma and viagra prescriptions free viagra viagra and cialis buy sublingual viagra online viagra from india viagra online no prescription viagra pill can women take viagra cheap viagra nz viagra and cannabis cheap herbal viagra lowest price viagra viagra rrp australia viagra free samples