From 99cdf8d02eb522b6c23b08e467aa8b530bc2e531 Mon Sep 17 00:00:00 2001 From: Clark Chen <9372896+clarkchentw@users.noreply.github.com> Date: Tue, 24 Nov 2020 12:41:12 -0600 Subject: [PATCH] Enforcing connection timeout on socket Enforcing connection timeout on the socket in order to prevent unlimited time stall with lost connection to one host --- require/class.Common.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/require/class.Common.php b/require/class.Common.php index bdd871c1..bfd56dbc 100644 --- a/require/class.Common.php +++ b/require/class.Common.php @@ -727,8 +727,10 @@ public function str2int($string, $concat = false) { } public function create_socket($host, $port, &$errno, &$errstr) { + global $globalSourcesTimeOut; $ip = gethostbyname($host); $s = socket_create(AF_INET, SOCK_STREAM, 0); + socket_set_option($s, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $globalSourcesTimeOut, 'usec' => 0)); $r = @socket_connect($s, $ip, $port); if (!socket_set_nonblock($s)) echo "Unable to set nonblock on socket\n"; if ($r || socket_last_error() == 114 || socket_last_error() == 115) { @@ -741,8 +743,10 @@ public function create_socket($host, $port, &$errno, &$errstr) { } public function create_socket_udp($host, $port, &$errno, &$errstr) { + global $globalSourcesTimeOut; $ip = gethostbyname($host); $s = socket_create(AF_INET, SOCK_DGRAM, 0); + socket_set_option($s, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $globalSourcesTimeOut, 'usec' => 0)); $r = @socket_bind($s, $ip, $port); if ($r || socket_last_error() == 114 || socket_last_error() == 115) { return $s; @@ -925,4 +929,4 @@ public function greatcircle($begin_lat,$begin_lon,$end_lat,$end_lon,$nbpts = 20, return $poMulti; } } -?> \ No newline at end of file +?>