User:Pizzahut/test.php

From Valve Developer Community
Jump to navigation Jump to search

A test script in PHP to check which master servers respond.
The results can vary on a daily basis.

<?php

$master_servers = array("68.177.101.62", "69.28.158.131", "208.64.200.117", "208.64.200.118");
define("MIN_PORT", 27010);
define("MAX_PORT", 27013);
define("FILTER", '\gamedir\tfc');
define("REGION", "\xFF");
define("TIMEOUT", 2.0);

function query_timeout(&$socket, $seed)
{
   stream_set_timeout($socket, TIMEOUT);
   if (!fwrite($socket, "1".REGION."$seed\0".FILTER."\0"))
   {
      fclose($socket);
      exit("fwrite error\n");
   }

   stream_set_timeout($socket, TIMEOUT);
   $s = bin2hex(fread($socket, 6));
   $info = stream_get_meta_data($socket);
   if ($info['timed_out'])
      echo "UNKNOWN (time out occurred)\n";
   else
   {
      if ($s !== "ffffffff660a")
      {
         if ($s == "")
            echo "DOWN\n";
         else
            echo "MALFUNCTION (expected ff ff ff ff 66 0a (hex) but got $s)\n";
         return True;
      }
      else
         echo "UP\n";
   }
   return $info['timed_out'];
}

// Connect to master server, return timeout info.
// The socket is passed as reference and thus returned as well.

function master_server_timeout(&$socket, $ip)
{
   $port = MIN_PORT;
   do {
      echo "Connecting to master server \"$ip:$port\".\n";
      $socket = fsockopen("udp://$ip", $port, $errno, $errstr, TIMEOUT);
      if (!$socket) exit("Error $errno : $errstr \n");
      $timeout = query_timeout($socket, "0.0.0.0:0");
      fclose($socket);
      $port = $port + 1;
   } while ($port <= MAX_PORT);
   return $timeout;
}

// Try all master servers.

foreach ($master_servers as $ip)
   $timeout=master_server_timeout($socket, $ip)

?>