summaryrefslogtreecommitdiffstats
path: root/helper.c
diff options
context:
space:
mode:
authorSimon Rettberg2018-03-12 15:28:33 +0100
committerSimon Rettberg2018-03-12 15:28:33 +0100
commite53dfcbdbed04cad690823ce5def521fea7b5483 (patch)
tree4a5d31e53515ad6a505d2cc4948abaa54a392b8c /helper.c
parentAdd version information and --version (diff)
downloadldadp-e53dfcbdbed04cad690823ce5def521fea7b5483.tar.gz
ldadp-e53dfcbdbed04cad690823ce5def521fea7b5483.tar.xz
ldadp-e53dfcbdbed04cad690823ce5def521fea7b5483.zip
4s timeout when connecting to server; send error to client on failure
Diffstat (limited to 'helper.c')
-rw-r--r--helper.c16
1 files changed, 14 insertions, 2 deletions
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);