MW3 & Query Protocol

Joined
Jan 29, 2005
Likes
0
#4
codmw3_serverinfo

Code:
<?php
  //************************************************************************************************************************************************
  //************************************************************************************************************************************************
  //************************************************************************************************************************************************
  // A2S class - Copyright (c) 2005-2008 David Busby, Saiweb.co.uk
  // Modification for MW3 by CyboPat ([url]www.cybopat.net[/url])
  class A2S
  {
  	var $srv;
  	var $port;
  
  	function A2S()
  	{
  		define("A2S_SERVERQUERY_GETCHALLENGE_PLAYER", "\xFF\xFF\xFF\xFF\x55\xFF\xFF\xFF\xFF");
  		define("A2S_INFO", "\xFF\xFF\xFF\xFF\x54\x53\x6F\x75\x72\x63\x65\x20\x45\x6E\x67\x69\x6E\x65\x20\x51\x75\x65\x72\x79\x00");
  		define("A2S_PLAYER", "\xFF\xFF\xFF\xFF\x55"); // requires Challenge suffix to work
  		define("A2S_RULES", "\xFF\xFF\xFF\xFF\x56"); // requires Challenge suffix to work
  		
  		
  	}
  	function _getchallenge()
  	{
      $data = $this->A2S_query(A2S_SERVERQUERY_GETCHALLENGE_PLAYER);
  		return substr($data,5);
  	}
  	function readString($string, &$i) {
       $begin = $i;
       $strlen = strlen($string);
       for ($i; ($i < $strlen) && ($string{$i} != chr(0)); $i++);
       $result = substr($string, $begin, $i-$begin);
       $i++;      
       return $result;
    }
  
  	function A2S_connect()
  	{
  		$this->connection = fsockopen("udp://".$this->srv, $this->port, $errorno, $errorstr, 1);
  		if(!$this->connection){ die("ERROR: $errorno $errorstr"); }
  		
  	}
  	function A2S_query($query)
  	{ 
  		fwrite($this->connection, $query, strlen($query));
  		return fread($this->connection, 4096);
  	}
  	function A2S_players()
  	{
  		$challenge = $this->_getchallenge();
  		$buffer = $this->A2S_query(A2S_PLAYER.$challenge);  		
  		$i = 5;
       	$num_players = ord($buffer{$i++});
       
       	if($num_players > 0) {
         		for($p = 0; $p < $num_players; $p++) {
             		if(!empty($buffer{$i+1})) {
                	$data[$p]['index'] = ord($buffer{$i++});
                	$data[$p]['name'] = $this->readString($buffer, $i);
                
                	$frags = unpack("L", substr($buffer, $i, $i+4)); $i+=4;
                	$data[$p]['frags'] = $frags[1];
                
                	$time = unpack("f", substr($buffer, $i, $i+4)); $i+=4;
                	$time = mktime(0, 0, $time[1]);
                	$time = date("H:i:s", $time);
                	$data[$p]['time'] = $time;
             }
          }  
       }	
       return $data;
  	}
  	  	
  	function A2S_info()
  	{
  		$buffer	=	$this->A2S_query(A2S_INFO);
  		return $buffer;
  	}
  	
  	function A2S_close()
  	{
  		fclose($this->connection);
  	}
  	
  } // end class
  //************************************************************************************************************************************************
  //************************************************************************************************************************************************
  //************************************************************************************************************************************************
  function RemoveTags($retez) {
    return preg_replace('~(\^[0-9])~', '', $retez);
  }
  
  $objClass = new A2S();
  
  //SETUP IP address and PORT
  // PORT = net_masterServerPort, not a game port
  $objClass->srv = "89.234.66.114";   
  $objClass->port = 27017 ; 
  
  $objClass->A2S_connect();
  $info = $objClass->A2S_info();
  $player_array = $objClass->A2S_players();
  $objClass->A2S_close();
  
  $pole=explode("\\",$info);
  //echo "<PRE>";print_r($pole);echo "</PRE>"; //debug
  //echo "<PRE>";print_r($player_array);echo "</PRE>"; //debug
  
  
  $i = 6;
  $pomstr = substr($pole[0], $i);
  //echo "<PRE>";print_r($pomstr);echo "</PRE>"; //debug
  $pompole=explode("\x00",$pomstr);
  $hostname = RemoveTags($pompole[0]);
  
  $i = $i + strlen($pompole[0]) + 1;
  $pomstr = substr($pole[0], $i);
  //echo "<PRE>";print_r($pomstr);echo "</PRE>"; //debug
  $pompole=explode("\x00",$pomstr);
  $mapnamelong = $pompole[0];
 
  $mapnameshort = $pole[9];
  
  $gametype = $pole[3];
  
  $i = $i + strlen($pompole[0]) + 1;
  $pomstr = substr($pole[0], $i);
  //echo "<PRE>";print_r($pomstr);echo "</PRE>"; //debug
  $pompole=explode("\x00",$pomstr);
  $gamedir = $pompole[0];
  
  $i = $i + strlen($pompole[0]) + 1;
  $pomstr = substr($pole[0], $i);
  //echo "<PRE>";print_r($pomstr);echo "</PRE>"; //debug
  $pompole=explode("\x00",$pomstr);
  $gamedesc = $pompole[0];
  
  $i = $i + strlen($pompole[0]) + 1;
  $appid = substr($pole[0], $i,2);
  
  $i = $i + 2;
  $playersnum = ord(substr($pole[0], $i,1));
  
  $i = $i + 1;
  $playersmax = ord(substr($pole[0], $i,1));
  
  $i = $i + 1;
  $botsnum = ord(substr($pole[0], $i,1));
  
  $i = $i + 1;
  $dedicated = substr($pole[0], $i,1);
  
  $i = $i + 1;
  $os = substr($pole[0], $i,1);
  
  $i = $i + 1;
  $password = ord(substr($pole[0], $i,1));
  
  $i = $i + 1;
  $secure = ord(substr($pole[0], $i,1));
  
  $hardcore = $pole[5];
  
  $friendlyfire = $pole[19];

  // OUTPUT
  echo "hostname = ".$hostname."<BR>";
  echo "mapnamelong = ".$mapnamelong."<BR>";  
  echo "mapnameshort = ".$mapnameshort."<BR>";
  echo "gametype = ".$gametype."<BR>";
  echo "playersnum = ".$playersnum."<BR>"; 
  echo "playersmax = ".$playersmax."<BR>";
  echo "password = ".$password."<BR>";
  echo "secure = ".$secure."<BR>";
  echo "hardcore = ".$hardcore."<BR>";
  echo "friendlyfire = ".$friendlyfire."<BR>";
  //echo "gamedir = ".$gamedir."<BR>";
  //echo "gamedesc = ".$gamedesc."<BR>";
  //echo "appid = ".$appid."<BR>";
  //echo "botsnum = ".$botsnum."<BR>";
  //echo "dedicated = ".$dedicated."<BR>";
  //echo "os = ".$os."<BR>";
  echo "<TABLE border='0' cellspacing='1' cellpadding='5' bgcolor='#DDDDDD'>";
  echo "<TR bgcolor='#FFFFFF'><TD align='center' colspan='99'>PLAYERS</TD></TR>";
  echo "<TR bgcolor='#FFFFFF'><TD>ID</TD><TD>Nick</TD><TD>Score</TD><TD>Time</TD></TR>";
  for ($i = 0; $i <= $playersnum-1; $i++) {
    echo "<TR bgcolor='#FFFFFF'><TD>".$player_array[$i]['index']."</TD><TD>".$player_array[$i]['name']."</TD><TD align='right'>".$player_array[$i]['frags']."</TD><TD>".$player_array[$i]['time']."</TD></TR>";
  }
  echo "</TABLE>";
  
?>
 
Last edited:

zeroy

Senior Member
Joined
Oct 20, 2004
Likes
2
Location
Louth, Ireland
#5
Code:
<?php
  //************************************************************************************************************************************************
  //************************************************************************************************************************************************
  //************************************************************************************************************************************************
  // A2S class - Copyright (c) 2005-2008 David Busby, Saiweb.co.uk
  // Modification for MW3 by CyboPat ([url]www.cybopat.net[/url])
  class A2S
  {
  	var $srv;
  	var $port;
  
  	function A2S()
  	{
  		define("A2S_SERVERQUERY_GETCHALLENGE_PLAYER", "\xFF\xFF\xFF\xFF\x55\xFF\xFF\xFF\xFF");
  		define("A2S_INFO", "\xFF\xFF\xFF\xFF\x54\x53\x6F\x75\x72\x63\x65\x20\x45\x6E\x67\x69\x6E\x65\x20\x51\x75\x65\x72\x79\x00");
  		define("A2S_PLAYER", "\xFF\xFF\xFF\xFF\x55"); // requires Challenge suffix to work
  		define("A2S_RULES", "\xFF\xFF\xFF\xFF\x56"); // requires Challenge suffix to work
  		
  		
  	}
  	function _getchallenge($t)
  	{
      $data = $this->A2S_query(A2S_SERVERQUERY_GETCHALLENGE_PLAYER);
  		return substr($data,5);
  	}
  	function readString($string, &$i) {
       $begin = $i;
       $strlen = strlen($string);
       for ($i; ($i < $strlen) && ($string{$i} != chr(0)); $i++);
       $result = substr($string, $begin, $i-$begin);
       $i++;      
       return $result;
    }
  
  	function A2S_connect()
  	{
  		$this->connection = fsockopen("udp://".$this->srv, $this->port, $errorno, $errorstr, 1);
  		if(!$this->connection){ die("ERROR: $errorno $errorstr"); }
  		
  	}
  	function A2S_query($query)
  	{ 
  		fwrite($this->connection, $query, strlen($query));
  		return fread($this->connection, 4096);
  	}
  	function A2S_players()
  	{
  		$challenge = $this->_getchallenge();
  		$buffer = $this->A2S_query(A2S_PLAYER.$challenge);  		
  		$i = 5;
       	$num_players = ord($buffer{$i++});
       
       	if($num_players > 0) {
         		for($p = 0; $p < $num_players; $p++) {
             		if(!empty($buffer{$i+1})) {
                	$data[$p]['index'] = ord($buffer{$i++});
                	$data[$p]['name'] = $this->readString($buffer, $i);
                
                	$frags = unpack("L", substr($buffer, $i, $i+4)); $i+=4;
                	$data[$p]['frags'] = $frags[1];
                
                	$time = unpack("f", substr($buffer, $i, $i+4)); $i+=4;
                	$time = mktime(0, 0, $time[1]);
                	$time = date("H:i:s", $time);
                	$data[$p]['time'] = $time;
             }
          }  
       }	
       return $data;
  	}
  	  	
  	function A2S_info()
  	{
  		$buffer	=	$this->A2S_query(A2S_INFO);
  		return $buffer;
  	}
  	
  	function A2S_close()
  	{
  		fclose($this->connection);
  	}
  	
  } // end class
  //************************************************************************************************************************************************
  //************************************************************************************************************************************************
  //************************************************************************************************************************************************
  function RemoveTags($retez) {
    return preg_replace('~(\^[0-9])~', '', $retez);
  }
  
  $objClass = new A2S();
  
  //SETUP IP address and PORT
  // PORT = net_masterServerPort, not a game port
  $objClass->srv = "89.234.66.114";   
  $objClass->port = 27017 ; 
  
  $objClass->A2S_connect();
  $info = $objClass->A2S_info();
  $player_array = $objClass->A2S_players();
  $objClass->A2S_close();
  
  $pole=explode("\\",$info);
  //echo "<PRE>";print_r($pole);echo "</PRE>"; //debug
  //echo "<PRE>";print_r($player_array);echo "</PRE>"; //debug
  
  
  $i = 6;
  $pomstr = substr($pole[0], $i);
  //echo "<PRE>";print_r($pomstr);echo "</PRE>"; //debug
  $pompole=explode("\x00",$pomstr);
  $hostname = RemoveTags($pompole[0]);
  
  $i = $i + strlen($pompole[0]) + 1;
  $pomstr = substr($pole[0], $i);
  //echo "<PRE>";print_r($pomstr);echo "</PRE>"; //debug
  $pompole=explode("\x00",$pomstr);
  $mapnamelong = $pompole[0];
 
  $mapnameshort = $pole[9];
  
  $gametype = $pole[3];
  
  $i = $i + strlen($pompole[0]) + 1;
  $pomstr = substr($pole[0], $i);
  //echo "<PRE>";print_r($pomstr);echo "</PRE>"; //debug
  $pompole=explode("\x00",$pomstr);
  $gamedir = $pompole[0];
  
  $i = $i + strlen($pompole[0]) + 1;
  $pomstr = substr($pole[0], $i);
  //echo "<PRE>";print_r($pomstr);echo "</PRE>"; //debug
  $pompole=explode("\x00",$pomstr);
  $gamedesc = $pompole[0];
  
  $i = $i + strlen($pompole[0]) + 1;
  $appid = substr($pole[0], $i,2);
  
  $i = $i + 2;
  $playersnum = ord(substr($pole[0], $i,1));
  
  $i = $i + 1;
  $playersmax = ord(substr($pole[0], $i,1));
  
  $i = $i + 1;
  $botsnum = ord(substr($pole[0], $i,1));
  
  $i = $i + 1;
  $dedicated = substr($pole[0], $i,1);
  
  $i = $i + 1;
  $os = substr($pole[0], $i,1);
  
  $i = $i + 1;
  $password = ord(substr($pole[0], $i,1));
  
  $i = $i + 1;
  $secure = ord(substr($pole[0], $i,1));
  
  $hardcore = $pole[5];
  
  $friendlyfire = $pole[19];

  // OUTPUT
  echo "hostname = ".$hostname."<BR>";
  echo "mapnamelong = ".$mapnamelong."<BR>";  
  echo "mapnameshort = ".$mapnameshort."<BR>";
  echo "gametype = ".$gametype."<BR>";
  echo "playersnum = ".$playersnum."<BR>"; 
  echo "playersmax = ".$playersmax."<BR>";
  echo "password = ".$password."<BR>";
  echo "secure = ".$secure."<BR>";
  echo "hardcore = ".$hardcore."<BR>";
  echo "friendlyfire = ".$friendlyfire."<BR>";
  //echo "gamedir = ".$gamedir."<BR>";
  //echo "gamedesc = ".$gamedesc."<BR>";
  //echo "appid = ".$appid."<BR>";
  //echo "botsnum = ".$botsnum."<BR>";
  //echo "dedicated = ".$dedicated."<BR>";
  //echo "os = ".$os."<BR>";
  echo "<TABLE border='0' cellspacing='1' cellpadding='5' bgcolor='#DDDDDD'>";
  echo "<TR bgcolor='#FFFFFF'><TD align='center' colspan='99'>PLAYERS</TD></TR>";
  echo "<TR bgcolor='#FFFFFF'><TD>ID</TD><TD>Nick</TD><TD>Score</TD><TD>Time</TD></TR>";
  for ($i = 0; $i <= $playersnum-1; $i++) {
    echo "<TR bgcolor='#FFFFFF'><TD>".$player_array[$i]['index']."</TD><TD>".$player_array[$i]['name']."</TD><TD align='right'>".$player_array[$i]['frags']."</TD><TD>".$player_array[$i]['time']."</TD></TR>";
  }
  echo "</TABLE>";
  
?>
Thanks however this code just timesout after 30seconds on my server?
 
Last edited:
Joined
Oct 18, 2008
Likes
0
Location
South Carolina
#13
How You change this?

Code:
//SETUP IP address and PORT
// PORT = net_masterServerPort, not a game port
$objClass->srv = "89.234.66.114";   
$objClass->port = 27017 ;

//SETUP IP address and PORT
// PORT = net_masterServerPort, not a game port
$objClass->srv = "50.31.11.220";
$objClass->port = 27014 ;
 
Joined
Jan 29, 2005
Likes
0
#14
http://www.eas-clan.cz/si/mw3_test.php

Your connection settings dont work.
$objClass->srv = "50.31.11.220";
$objClass->port = 27014 ;

zeroys fine..
$objClass->srv = "89.234.66.114";
$objClass->port = 27017;

mine too...
$objClass->srv = "147.231.45.233";
$objClass->port = 28967;


put this command net_masterServerPort to server console and check query port, is really 27014?
 
Joined
Oct 18, 2008
Likes
0
Location
South Carolina
#16
http://www.eas-clan.cz/si/mw3_test.php

Your connection settings dont work.
$objClass->srv = "50.31.11.220";
$objClass->port = 27014 ;

zeroys fine..
$objClass->srv = "89.234.66.114";
$objClass->port = 27017;

mine too...
$objClass->srv = "147.231.45.233";
$objClass->port = 28967;


put this command net_masterServerPort to server console and check query port, is really 27014?
When I use your IP and Port I still get the same results of nothing showing. And the masterServerPort for mine is 27016 but still no results shown
 
Joined
Jan 29, 2005
Likes
0
#17
When I use your IP and Port I still get the same results of nothing showing. And the masterServerPort for mine is 27016 but still no results shown
check...
http://www.eas-clan.cz/si/mw3_test.php

Code:
$objClass->srv = "50.31.11.220";
$objClass->port = [B]27016[/B] ; 

hostname = 10-78 [3] Kill Confirmed
mapnamelong = Arkaden
mapnameshort = mp_plaza2
gametype = conf
playersnum = 0
playersmax = 18
password = 0
secure = 1
hardcore = 0
friendlyfire = 1
PLAYERS
ID	Nick	Score	Time
Guys, something blocks your PHP to comunicate... :(
 
Joined
Oct 18, 2008
Likes
0
Location
South Carolina
#18
check...
http://www.eas-clan.cz/si/mw3_test.php

Code:
$objClass->srv = "50.31.11.220";
$objClass->port = [B]27016[/B] ; 

hostname = 10-78 [3] Kill Confirmed
mapnamelong = Arkaden
mapnameshort = mp_plaza2
gametype = conf
playersnum = 0
playersmax = 18
password = 0
secure = 1
hardcore = 0
friendlyfire = 1
PLAYERS
ID    Nick    Score    Time
Guys, something blocks your PHP to comunicate... :(
Not sure what it can be. I have a site hosted with godaddy and hostgator and it doesnt work on either. Both are using php 5.0
 

ADW {KAN

Junior Member
Joined
Aug 11, 2010
Likes
0
#20
When I use your IP and Port I still get the same results of nothing showing. And the masterServerPort for mine is 27016 but still no results shown
Does your host run PHP in "safe mode"? If so you will not be able to use the "fsockopen" command.

Do you have error reporting enabled?
 
Last edited:
Top Bottom