views:

81

answers:

2

Hi there,

I need to close all ongoing Linux TCP sockets as soon as the Ethernet interface drops (ie cable is disconnected, interface is down'ed and so on).

Hacking into /proc seems not to do the trick. Not found any valuable ioctl's. Doint it by hand at application level is not what I want, I'm really looking for a brutal and global way of doing it.

Did anyane experienced this before and willing to share his foundings ?

Thanks

BR

A: 

The brutal way which avoids application level coding is hacking your kernel to activate TCP keepalive with a low timeout for all your connections.

Peter G.
Thanks for your suggestion. I tried the following :sysctl -w \net.ipv4.tcp_keepalive_time=15 \net.ipv4.tcp_keepalive_intvl=5 \net.ipv4.tcp_keepalive_probes=2However, I guess there is something I got wrong since after 15 + 2*15 seconds, the TCP sockets are not closed :Before :tcp 0 0 192.168.10.103:52635 ext.domain.net:1234 ESTABLISHEDAfter 2 minutes :tcp 0 0 192.168.10.103:52635 192.168.0.12:1234 ESTABLISHEDDid you meant source hacking ? Before hacking into the code, I'd like to make sure my understanding of the keepalive is correct.
SCO
Without hacking the kernel the application still has to activate keepalive for every socket.
Peter G.
Thanks Pete.I added the following, but this didn't chang the outcome :if ((sock=socket(PF_INET,SOCK_STREAM,0))==-1) return -1;if (setsockopt(sock,SOL_SOCKET,SO_KEEPALIVE, (char*) return -1; }
SCO
Hmm, I expected this to work. The variable option is of type int and has a value of 1?See e.g. http://tldp.org/HOWTO/html_single/TCP-Keepalive-HOWTO/
Peter G.
Use netstat -o option to see the status of timeouts. I use netstat -tanop for TCP sockets.
ninjalj
Thanks Peter. Got it to work, the option value was of type int, but not set at all :( I guess I can now go into kernel hacking !Ninjal, netstat -tanop output is impressive ! Thanks for the tip ;) !
SCO
A: 

This is rarely needed and is often wouldn't work. TCP is a data transfer protocol, unless there is data loss, nothing should be done. Think twice why you ever would need that.

Otherwise, you can try to periodically poll interface(s) and check for the UP flag. If interface looses UP flag, then OS already reacted on cable being unplugged and down'ed the interface. man 7 netdevice, see SIOCGIFFLAGS for more.

Network drivers also generate an event on even when cable is plugged, but I'm not sure whether you can access that or not from a user. You might want to check the udev as its documentation explicitly mentions network interfaces.

Dummy00001
Network info and events are usually available on netlink sockets.
ninjalj
@ninjalj: you mean http://linux.die.net/man/7/rtnetlink RTM_GETLINK? do you have pointer to sample code or similar?
Dummy00001
Dummy, Ninjal, thank your for your suggestion.However I guess I'll go with the 'keepalive' solution : DOWN'ing the interface doesn't close the ongoing TCP sockets.
SCO