Guest User

get_tw_master_new

a guest
Jan 22nd, 2014
174
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /******************************************************************\
  3. |* Copyright (c) by Starlex.de
  4. |*
  5. |* Visit:
  6. |*      http://teeworlds.com
  7. |*      http://teeworlds.de
  8. |*      http://teeworlds-community.de
  9. |*
  10. |* Special Thanks:
  11. |*      east - annoyingeast.info
  12. |*      m!nus - mnus.de and teeworlds.com
  13. |*
  14. |* License:
  15. |*      http://creativecommons.org/licenses/by-nc-sa/3.0/deed.de
  16. |*
  17. \******************************************************************/
  18.  
  19. function get_tw_master_new($server) {  
  20.     $socket = stream_socket_client('udp://'.$server , $errno, $errstr, 1);
  21.     stream_set_timeout($socket, 1);
  22.     fwrite($socket, "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xffreq2");
  23.     $servers = Array();
  24.     $response = fread($socket, 2048);
  25.    
  26.     if ($response)
  27.     {
  28.         while (strlen($response) >= 1)
  29.         {
  30.             $response = substr($response, 14);
  31.             for ($i = 0; $i <= (strlen($response)-1) ; $i += 18) {
  32.            
  33.                 if(substr($response, $i, 12) == str_repeat("\x00", 10)."\xff\xff")
  34.                 {   // --- IPv4
  35.                     $serveraddresse = inet_ntop(substr($response, $i+12, 4));
  36.                    
  37.                 } else { // --- IPv6 - NOT TESTED !!!
  38.                     $serveraddresse = "[".inet_ntop(substr($response, $i, 16))."]";
  39.                 }
  40.                
  41.                 // Add the Port
  42.                 $serveraddresse .= ":".strToint($response{$i+16}.$response{$i+17});
  43.  
  44.             $servers[] = $serveraddresse;
  45.             }
  46.            
  47.             $response = fread($socket, 2048);
  48.         }
  49.         return $servers;
  50.     } else {
  51.         return FALSE;
  52.     }  
  53. }
  54. function strToint($string)
  55. {
  56.     $hex = "";
  57.     for ($i=0; $i < strlen($string); $i++)
  58.     {
  59.         $hex .= dechex(ord($string[$i]));
  60.     }
  61.     $int = hexdec($hex);
  62.     return $int;
  63. }
  64. ?>
RAW Paste Data