tpws: prevent hang connection with SO_KEEPALIVE

This commit is contained in:
bolvan 2018-03-24 12:21:40 +03:00
parent ad071a122d
commit e106a0c668
6 changed files with 13 additions and 1 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -701,6 +701,12 @@ int main(int argc, char *argv[]) {
close(listen_fd);
exit(EXIT_FAILURE);
}
if (setsockopt(listen_fd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)) == -1)
{
perror("setsockopt (SO_KEEPALIVE): ");
close(listen_fd);
exit(EXIT_FAILURE);
}
//Mark that this socket can be used for transparent proxying
//This allows the socket to accept connections for non-local IPs

View File

@ -73,6 +73,7 @@ int check_local_ip(const struct sockaddr *saddr)
//Returns 0 if something fails, >0 on success (socket fd).
static int connect_remote(struct sockaddr_storage *remote_addr){
int remote_fd = 0, yes = 1;
//Use NONBLOCK to avoid slow connects affecting the performance of other
//connections
@ -87,7 +88,12 @@ static int connect_remote(struct sockaddr_storage *remote_addr){
close(remote_fd);
return 0;
}
if(setsockopt(remote_fd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)) < 0){
perror("setsockopt (SO_KEEPALIVE, connect_remote): ");
close(remote_fd);
return 0;
}
if(connect(remote_fd, (struct sockaddr*) remote_addr,
remote_addr->ss_family == AF_INET ? sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6)) < 0){