From e53dfcbdbed04cad690823ce5def521fea7b5483 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 12 Mar 2018 15:28:33 +0100 Subject: 4s timeout when connecting to server; send error to client on failure --- helper.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'helper.c') diff --git a/helper.c b/helper.c index 2dc907e..6c51bab 100644 --- a/helper.c +++ b/helper.c @@ -46,7 +46,7 @@ int helper_connect4(char *address, int port, char *ip) { if (sizeof(struct in_addr) != 4) bail("Ach nöö ach nöö"); if (inet_pton(AF_INET, address, ip) == 1) { - const int sock = socket_tcp4b(); + const int sock = helper_newSocket(); if (sock == -1) return -1; if (socket_connect4(sock, ip, port) == 0) { return sock; @@ -65,7 +65,7 @@ int helper_connect4(char *address, int port, char *ip) return -1; } for (rp = result; rp != NULL; rp = rp->ai_next) { - const int sock = socket_tcp4b(); + const int sock = helper_newSocket(); if (sock == -1) continue; if (rp->ai_addr != NULL) { memcpy(ip, &((struct sockaddr_in*)rp->ai_addr)->sin_addr, 4); @@ -80,6 +80,18 @@ int helper_connect4(char *address, int port, char *ip) return -1; } +int helper_newSocket() +{ + struct timeval tv; + int sock = socket_tcp4b(); + if (sock == -1) return -1; + tv.tv_sec = 4; + tv.tv_usec = 0; + setsockopt( sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv) ); + setsockopt( sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv) ); + return sock; +} + void helper_nonblock(const int fd) { fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); -- cgit v1.2.3-55-g7522