warmcat/libwebsockets — pull request #151 by anubias (2014-07-29 13:54:04+00:00)
Title: Fix TCP keepalive use in UNIX systems

Under UNIX systems the TCP keepalive is not taken into use properly. This commit tries to fix that.

1. In the libwebsocket_create_context() function, the info->ka_* fields were not copied to their corresponding fields of the context variable. Later on when the context.ka_time is inspected to check whether the TCP keepalive should be enabled on the connection, the condition always returns false.

- the fix for this consists in copying the ka_* fields from the info to the context variable, which enables keepalive on the socket.

2. In the lws_plat_set_socket_options() function, the keepalive parameters were written to the wrong protocol level. The keepalive is a TCP feature not an IP feature, so the proper protocol level for the keepalive parameters has to be "IPPROTO_TCP". Without this second fix, the keepalive feature is activated, but the timeout data is controled by the default values (/proc/sys/net/ipv4/tcp_keepalive_*) instead of those programmed when creating the context.

- the fix for this was changing the IPPROTO_IP level to the IPPROTO_TCP when calling setsockopt() with the keepalive parameters

The bahaviour was tested with the "libwebsockets-test-server" test application started on a machine (host1), a web browser started on another machine (host2) and connecting to the http://host1:7681. Then the physical connection (the ethernet cable) was disconnected between host1 and host2 and it was noticed that after the timers expired, the connection was ended on the server side, causing LWS_CALLBACK_CLOSED to be called on the server (the intended behaviour).

Prior to this fix, the keepalive did not work as intended on the websocket.