Advertisement
djazz

SGIP - ping.php - Ping a server, check private state

Mar 19th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.03 KB | None | 0 0
  1. <?php
  2. // For SGIP:
  3. // http://djazz.mine.nu/apps/sgip/
  4.  
  5. header("content-type: text/plain");
  6. header("Access-Control-Allow-Origin: *");
  7. set_time_limit(3);
  8.  
  9. $ip = $_GET['ip'];
  10. $port = isset($_GET['port'])? intval($_GET['port']) : 10052;
  11.  
  12. function udpchecksum ($buf) {
  13.     $x = 1;
  14.     $y = 1;
  15.     $size = strlen($buf);
  16.     for($i = 2; $i < $size; $i++) {
  17.         $x += ord($buf[$i]);
  18.         $y += $x;
  19.     }
  20.     $buf[0] = chr($x % 251);
  21.     $buf[1] = chr($y % 251);
  22.     return $buf;
  23. }
  24.  
  25. $udp = @fsockopen("udp://".$ip, $port, $errno, $errstr, 3);
  26. stream_set_timeout($udp, 3);
  27. if(!$udp) {
  28.     die('pinging');
  29. }
  30. $toSend = "yy\x03"."SGIP2"."\0x00\x00\x00\x00"; //Ping packet;
  31. $toSend = udpchecksum($toSend);
  32. fwrite($udp, $toSend);
  33. $time_start = microtime(true);
  34. $res = @fread($udp, 32);
  35. $time_end = microtime(true);
  36. if(!$res) {
  37.     die("pinging");
  38. }
  39. fclose($udp);
  40.  
  41. $private = (ord($res[8])>>5) & 1;
  42.  
  43. if(ord($res[2]) === 4 && substr($res, 3, 5) === "SGIP2") {
  44.     echo json_encode(array(round(($time_end - $time_start)*1000), $private));
  45. }
  46. else {
  47.     echo 'error';
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement