Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Типы пакетов, описаны в структуре, файл src/network/core/udp.h
- define('PACKET_UDP_CLIENT_FIND_SERVER', 0); ///< Queries a game server for game information
- define('PACKET_UDP_SERVER_RESPONSE', 1); ///< Reply of the game server with game information
- define('PACKET_UDP_CLIENT_DETAIL_INFO', 2); ///< Queries a game server about details of the game'); such as companies
- define('PACKET_UDP_SERVER_DETAIL_INFO', 3); ///< Reply of the game server about details of the game'); such as companies
- define('PACKET_UDP_SERVER_REGISTER', 4); ///< Packet to register itself to the master server
- define('PACKET_UDP_MASTER_ACK_REGISTER', 5); ///< Packet indicating registration has succedeed
- define('PACKET_UDP_CLIENT_GET_LIST', 6); ///< Request for serverlist from master server
- define('PACKET_UDP_MASTER_RESPONSE_LIST', 7); ///< Response from master server with server ip's + port's
- define('PACKET_UDP_SERVER_UNREGISTER', 8); ///< Request to be removed from the server-list
- define('PACKET_UDP_CLIENT_GET_NEWGRFS', 9); ///< Requests the name for a list of GRFs (GRF_ID and MD5)
- define('PACKET_UDP_SERVER_NEWGRFS', 10); ///< Sends the list of NewGRF's requested.
- define('PACKET_UDP_MASTER_SESSION_KEY', 11); ///< Sends a fresh session key to the client
- define('PACKET_UDP_END', 12); ///< Must ALWAYS be on the end of this list!! (period)
- // Версии сетевого протокола, файл файл src/network/core/config.h
- define('NETWORK_GAME_ADMIN_VERSION', 1); ///< What version of the admin network do we use?
- define('NETWORK_GAME_INFO_VERSION', 4); ///< What version of game-info do we use?
- define('NETWORK_COMPANY_INFO_VERSION', 6); ///< What version of company info is this?
- define('NETWORK_MASTER_SERVER_VERSION', 2); ///< What version of master-server-protocol do we use?
- // Типы спискоа серверов (server list type), файл src/network/core/udp.h
- define('SLT_IPv4', 0); ///< Get the IPv4 addresses
- define('SLT_IPv6', 1); ///< Get the IPv6 addresses
- define('SLT_AUTODETECT', 2); ///< Autodetect the type based on the connection
- function format_packet($type, $data)
- {
- if ($type<0 or $type>65535) return false;
- $datlen = strlen($data);
- if ($datlen>65511) return false;
- $result = " $data";
- $type = pack('C', $type);
- $length = pack('S', $datlen+3);
- $result[0] = $length[0];
- $result[1] = $length[1];
- $result[2] = $type[0];
- return $result;
- }
- function recv_packet($sock, $timeout=10)
- {
- static $buffer = '';
- static $buflen = 0;
- $datlen = false;
- $type = false;
- for ($sec=0; $sec<$timeout; $sec++)
- {
- sleep(1);
- while ($data=fread($sock, 512))
- {
- $buffer .= $data;
- $buflen += strlen($data);
- }
- if ($buflen<3) continue;
- if ($datlen===false)
- {
- extract(unpack('Sdatlen/Ctype', substr($buffer, 0, 3)));
- $datlen -= 3;
- }
- if ($buflen-3>=$datlen)
- {
- $result = array($type, substr($buffer, 3, $datlen));
- $buffer = substr($buffer, 3+$datlen);
- $buflen = strlen($buffer);
- return $result;
- }
- }
- return false;
- }
- $master_ip = gethostbyname('openttd.org');
- if ($master_ip=='openttd.org') die('не могу определить ip-адрес openttd.org');
- $sock = fsockopen("udp://$master_ip", 3978);
- stream_set_blocking($sock, 0);
- $data = pack('CC', NETWORK_MASTER_SERVER_VERSION, SLT_IPv4);
- $packet = format_packet(PACKET_UDP_CLIENT_GET_LIST, $data);
- fwrite($sock, $packet);
- $packet_servers = recv_packet($sock, 10);
- if (!is_array($packet_servers)) die('не удалось получить список серверов');
- if ($packet_servers[0]<>PACKET_UDP_MASTER_RESPONSE_LIST) die('принят неопознанный пакет (1)');
- if (strlen($packet_servers[1])<3) errmsg('принят неверный пакет (1)');
- $header = substr($packet_servers[1], 0, 3);
- extract(unpack('Cproto/Sservnum', $header));
- preg_match_all('/[\x00-\xFF]{6}/', substr($packet_servers[1], 3), $addrs);
- $servers = array();
- foreach ($addrs[0] as $addr)
- {
- extract(unpack('Nip/Sport', $addr));
- $servers[]= array(long2ip($ip), $port);
- }
- print_r($servers);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement