Close all TCP sockets on the Down interface

I need to close all current Linux TCP sockets as soon as the Ethernet interface drops (i.e. the cable is disconnected, the interface is down, etc.).

Hacking in / proc does not seem to do this. No valuable ioctl found. Putting it manually at the application level is not what I want, I'm really looking for a brutal and global way to do this.

Has anyone experienced this before and wanted to share their basics?

+4
source share
2 answers

A brutal way to avoid application-level coding is to hack your kernel to activate TCP keepalive with low latency for all your connections.

0
source

This is rarely required and often does not work. TCP is a data transfer protocol, if there is no data loss, nothing should be done. Think twice why you need it.

Otherwise, you can try periodically polling the interface and checking the UP flag. If the interface loses the UP flag, the OS is already responding to disconnecting the cable and refusing the interface. man 7 netdevice , see SIOCGIFFLAGS for more.

Network drivers also generate an event even when the cable is connected, but I'm not sure if you can access this user or not. You might want to check out udev as the documentation explicitly mentions network interfaces .

0
source

Source: https://habr.com/ru/post/1316162/


All Articles