[pve-devel] [PATCH pve-common] Added PVE::Network::tcp_ping to replace Net::Ping

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Aug 25 11:10:53 CEST 2015


We use Net::Ping twice in pve-storage (once for ISCSIPlugin
and once in GlusterfsPlugin, both with the 'tcp' variant.),
but Net::Ping doesn't support IPv6.
---
 src/PVE/Network.pm | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/src/PVE/Network.pm b/src/PVE/Network.pm
index ff4a89c..b296225 100644
--- a/src/PVE/Network.pm
+++ b/src/PVE/Network.pm
@@ -6,6 +6,8 @@ use PVE::Tools qw(run_command);
 use PVE::ProcFSTools;
 use PVE::INotify;
 use File::Basename;
+use IO::Socket::IP;
+use Time::HiRes 'alarm';
 
 # host network related utility functions
 
@@ -437,4 +439,20 @@ sub activate_bridge_vlan {
     return $bridgevlan;
 }
 
+sub tcp_ping {
+    my ($host, $port, $timeout) = @_;
+    eval {
+	local $SIG{ALRM} = sub { die "timed out\n"; };
+	alarm($timeout || 3);
+	my $sock = IO::Socket::IP->new(PeerHost => $host,
+	                               PeerPort => $port,
+	                               # Timeout=> isn't implemented
+	                               Type => SOCK_STREAM);
+	alarm 0;
+	die "timed out\n" if !$sock;
+	$sock->close();
+    };
+    return $@ ne "timed out\n";
+}
+
 1;
-- 
2.1.4





More information about the pve-devel mailing list