0

I have a nonblocking socket created as:

sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
fcntl(sockfd, F_SETFL, O_NONBLOCK);

It is then connected to the server via:

struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = address;
addr.sin_port = port;

int retval = connect(sockfd,
                     (struct sockaddr *)&addr,
                     sizeof(addr));

I am attempting to simulate a network failure by setting the interface this socket is communicating on down via sudo ip link set <interface> down. The socket should be tied to this interface specifically since I have set the IP address directly.

However, after setting the interface down, I do not receive any errors on subsequent calls to sendto and recvfrom. I am calling both of these with no flags.

I would expect to receive one of the error codes from these functions (like maybe ENOTCONN) since, with the interface down, the local machine should be able to tell that the function will fail. Why am I seeing no errors?

  • This is the same as disconnecting an Ethernet cable - socket doesn't know about this. Maybe this article can help: https://holmeshe.me/network-essentials-setsockopt-SO_KEEPALIVE/ – Alex F Jul 16 '20 at 06:58
  • It is also possible that your IP routing table provides an alternate route to the target IP address. – user207421 Jul 16 '20 at 10:08
  • It is possible the TCP stack needs more time to figure out what has happened. When the interface goes down, the TCP stack probably attempting transmissions and only marked the connection as DEAD after a pretty significant amount of time: https://man7.org/linux/man-pages/man7/tcp.7.html – Liam Kelly Jul 16 '20 at 15:38
  • @AlexF that article has solved the issue, thank you! I couldn't find information on the necessary socket settings to do this anywhere. – Carson Schubert Jul 16 '20 at 17:36
  • Follow up: Why does `recvfrom()` only fail the FIRST time it is called after the connection is closed due to keepalive settings with an ETIMEDOUT error code? Subsequent calls succeed just fine despite the connection now being closed. – Carson Schubert Jul 16 '20 at 17:52

0 Answers0