From 9b9dd030961360a22cd735161f93d38d05f6b51d Mon Sep 17 00:00:00 2001 From: Hasso Tepper Date: Mon, 24 Nov 2014 10:02:37 +0200 Subject: [PATCH 1/2] Timeout if UDP port isn't reachable Add timeout to the UDP socket. Without it client would block infinitely if creating a control connection succeeds, but UDP packets are dropped by firewall. --- src/iperf_udp.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/iperf_udp.c b/src/iperf_udp.c index 8d32dbe..8c6e3ea 100644 --- a/src/iperf_udp.c +++ b/src/iperf_udp.c @@ -300,6 +300,7 @@ int iperf_udp_connect(struct iperf_test *test) { int s, buf, sz; + struct timeval tv; /* Create and bind our local socket. */ if ((s = netdial(test->settings->domain, Pudp, test->bind_address, test->bind_port, test->server_hostname, test->server_port)) < 0) { @@ -323,6 +324,11 @@ iperf_udp_connect(struct iperf_test *test) } } + /* 30 sec timeout for a case when there is a network problem. */ + tv.tv_sec = 30; + tv.tv_usec = 0; + setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval)); + /* * Write a datagram to the UDP stream to let the server know we're here. * The server learns our address by obtaining its peer's address. From b83791882de8da0acb94edfa8485e1cdfe3c1869 Mon Sep 17 00:00:00 2001 From: Hasso Tepper Date: Thu, 11 Dec 2014 12:59:02 +0200 Subject: [PATCH 2/2] Add ifdef to the SO_RCVTIMEO code Not all platforms have a SO_RCVTIMEO socket option. --- src/iperf_udp.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/iperf_udp.c b/src/iperf_udp.c index 8c6e3ea..9500aa1 100644 --- a/src/iperf_udp.c +++ b/src/iperf_udp.c @@ -300,7 +300,9 @@ int iperf_udp_connect(struct iperf_test *test) { int s, buf, sz; +#ifdef SO_RCVTIMEO struct timeval tv; +#endif /* Create and bind our local socket. */ if ((s = netdial(test->settings->domain, Pudp, test->bind_address, test->bind_port, test->server_hostname, test->server_port)) < 0) { @@ -324,10 +326,12 @@ iperf_udp_connect(struct iperf_test *test) } } +#ifdef SO_RCVTIMEO /* 30 sec timeout for a case when there is a network problem. */ tv.tv_sec = 30; tv.tv_usec = 0; setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval)); +#endif /* * Write a datagram to the UDP stream to let the server know we're here.