daily pastebin goal
49%
SHARE
TWEET

Untitled

a guest May 5th, 2012 122 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. // Типы пакетов, описаны в структуре, файл src/network/core/udp.h
  4. define('PACKET_UDP_CLIENT_FIND_SERVER', 0);   ///< Queries a game server for game information
  5. define('PACKET_UDP_SERVER_RESPONSE', 1);      ///< Reply of the game server with game information
  6. define('PACKET_UDP_CLIENT_DETAIL_INFO', 2);   ///< Queries a game server about details of the game'); such as companies
  7. define('PACKET_UDP_SERVER_DETAIL_INFO', 3);   ///< Reply of the game server about details of the game'); such as companies
  8. define('PACKET_UDP_SERVER_REGISTER', 4);      ///< Packet to register itself to the master server
  9. define('PACKET_UDP_MASTER_ACK_REGISTER', 5);  ///< Packet indicating registration has succedeed
  10. define('PACKET_UDP_CLIENT_GET_LIST', 6);      ///< Request for serverlist from master server
  11. define('PACKET_UDP_MASTER_RESPONSE_LIST', 7); ///< Response from master server with server ip's + port's
  12. define('PACKET_UDP_SERVER_UNREGISTER', 8);    ///< Request to be removed from the server-list
  13. define('PACKET_UDP_CLIENT_GET_NEWGRFS', 9);   ///< Requests the name for a list of GRFs (GRF_ID and MD5)
  14. define('PACKET_UDP_SERVER_NEWGRFS', 10);      ///< Sends the list of NewGRF's requested.
  15. define('PACKET_UDP_MASTER_SESSION_KEY', 11);  ///< Sends a fresh session key to the client
  16. define('PACKET_UDP_END', 12);                 ///< Must ALWAYS be on the end of this list!! (period)
  17.  
  18. // Версии сетевого протокола, файл файл src/network/core/config.h
  19. define('NETWORK_GAME_ADMIN_VERSION', 1);    ///< What version of the admin network do we use?
  20. define('NETWORK_GAME_INFO_VERSION', 4);     ///< What version of game-info do we use?
  21. define('NETWORK_COMPANY_INFO_VERSION', 6);  ///< What version of company info is this?
  22. define('NETWORK_MASTER_SERVER_VERSION', 2); ///< What version of master-server-protocol do we use?
  23.  
  24. // Типы спискоа серверов (server list type), файл src/network/core/udp.h
  25. define('SLT_IPv4', 0);       ///< Get the IPv4 addresses
  26. define('SLT_IPv6', 1);       ///< Get the IPv6 addresses
  27. define('SLT_AUTODETECT', 2); ///< Autodetect the type based on the connection
  28.  
  29. function format_packet($type, $data)
  30.  {
  31.  if ($type<0 or $type>65535) return false;
  32.  $datlen = strlen($data);
  33.  if ($datlen>65511) return false;
  34.  $result = "   $data";
  35.  $type = pack('C', $type);
  36.  $length = pack('S', $datlen+3);
  37.  $result[0] = $length[0];
  38.  $result[1] = $length[1];
  39.  $result[2] = $type[0];
  40.  return $result;
  41.  }
  42.  
  43. function recv_packet($sock, $timeout=10)
  44.  {
  45.  static $buffer = '';
  46.  static $buflen = 0;
  47.  $datlen = false;
  48.  $type = false;
  49.  for ($sec=0; $sec<$timeout; $sec++)
  50.   {
  51.   sleep(1);
  52.   while ($data=fread($sock, 512))
  53.    {
  54.    $buffer .= $data;
  55.    $buflen += strlen($data);
  56.    }
  57.   if ($buflen<3) continue;
  58.   if ($datlen===false)
  59.    {
  60.    extract(unpack('Sdatlen/Ctype', substr($buffer, 0, 3)));
  61.    $datlen -= 3;
  62.    }
  63.   if ($buflen-3>=$datlen)
  64.    {
  65.    $result = array($type, substr($buffer, 3, $datlen));
  66.    $buffer = substr($buffer, 3+$datlen);
  67.    $buflen = strlen($buffer);
  68.    return $result;
  69.    }
  70.   }
  71.  return false;
  72.  }
  73.  
  74. $master_ip = gethostbyname('openttd.org');
  75. if ($master_ip=='openttd.org') die('не могу определить ip-адрес openttd.org');
  76.  
  77. $sock = fsockopen("udp://$master_ip", 3978);
  78. stream_set_blocking($sock, 0);
  79. $data = pack('CC', NETWORK_MASTER_SERVER_VERSION, SLT_IPv4);
  80. $packet = format_packet(PACKET_UDP_CLIENT_GET_LIST, $data);
  81. fwrite($sock, $packet);
  82. $packet_servers = recv_packet($sock, 10);
  83. if (!is_array($packet_servers)) die('не удалось получить список серверов');
  84. if ($packet_servers[0]<>PACKET_UDP_MASTER_RESPONSE_LIST) die('принят неопознанный пакет (1)');
  85. if (strlen($packet_servers[1])<3) errmsg('принят неверный пакет (1)');
  86. $header = substr($packet_servers[1], 0, 3);
  87. extract(unpack('Cproto/Sservnum', $header));
  88. preg_match_all('/[\x00-\xFF]{6}/', substr($packet_servers[1], 3), $addrs);
  89.  
  90. $servers = array();
  91. foreach ($addrs[0] as $addr)
  92.  {
  93.  extract(unpack('Nip/Sport', $addr));
  94.  $servers[]= array(long2ip($ip), $port);
  95.  }
  96.  
  97. print_r($servers);
  98.  
  99. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top