From 0d017361da4649eefa743ad188b3f44a4e30a91a Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Tue, 15 Sep 2009 08:30:46 +0000
Subject: [PATCH] ldapstatus: Check wheter hostname exists before attempting to
 connect.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1755 44740490-163a-0410-bde0-09ae8108e29a
---
 modules/ldapstatus/lib/Tester.php | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/modules/ldapstatus/lib/Tester.php b/modules/ldapstatus/lib/Tester.php
index ef8da951b..85f568f85 100644
--- a/modules/ldapstatus/lib/Tester.php
+++ b/modules/ldapstatus/lib/Tester.php
@@ -78,15 +78,24 @@ class sspmod_ldapstatus_Tester {
 	
 	/**
 	 * TCP ping implemented in php.
-	 * Warning: Will return Success if hostname is illegal. should be fixed.
 	 *
-	 * @param $host Hostname
-	 * @param $port Port number (TCP)
+	 * @param string $host Hostname
+	 * @param int $port Port number (TCP)
 	 */
 	public function phpping($host, $port) {
-	
+		assert('is_string($host)');
+		assert('is_int($port)');
+
 		$this->log('ldapstatus phpping(): ping [' . $host . ':' . $port . ']' );
-	
+
+		$ips = gethostbynamel($host);
+		if ($ips === FALSE) {
+			return array(FALSE, 'Unable to look up hostname ' . $host . '.');
+		}
+		if (count($ips) === 0) {
+			return array(FALSE, 'No IP address found for host ' . $host . '.');
+		}
+
 		$timeout = 1.0;
 		$socket = @fsockopen($host, $port, $errno, $errstr, $timeout);
 		if ($socket) @fclose($socket);
-- 
GitLab