Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> gethostbyaddr z timeout, Wykonanie funkcji gethostbyaddr z ograniczeniem czasowym
Mephis
post 23.10.2018, 14:07:35
Post #1





Grupa: Zarejestrowani
Postów: 94
Pomógł: 1
Dołączył: 16.12.2012

Ostrzeżenie: (0%)
-----


Witam.

Czy istnieje sposób na otrzymanie nazwy hosta z adresu IP, ograniczając przy tym czas połączenia (nie chodzi mi o czas wykonania skryptu)?
Funkcja gethostbyaddr byłaby w porządku, gdyby zwracała cokolwiek, w przypadku timeoutu. Niestety tak nie jest i kiedy host nie odpowiada, skrypt będzie mielił aż do przekroczenia maksymalnego czasu wykonania skryptu.

Już w samych komentarzach w manualu, przy funkcji gethostbyaddr natrafiłem na taki oto skrypt:
  1. function gethostbyaddr_timeout($ip, $dns, $timeout=1000)
  2. {
  3. // random transaction number (for routers etc to get the reply back)
  4. $data = rand(0, 99);
  5. // trim it to 2 bytes
  6. $data = substr($data, 0, 2);
  7. // request header
  8. $data .= "\1\0\0\1\0\0\0\0\0\0";
  9. // split IP up
  10. $bits = explode(".", $ip);
  11. // error checking
  12. if (count($bits) != 4) return "ERROR";
  13. // there is probably a better way to do this bit...
  14. // loop through each segment
  15. for ($x=3; $x>=0; $x--)
  16. {
  17. // needs a byte to indicate the length of each segment of the request
  18. switch (strlen($bits[$x]))
  19. {
  20. case 1: // 1 byte long segment
  21. $data .= "\1"; break;
  22. case 2: // 2 byte long segment
  23. $data .= "\2"; break;
  24. case 3: // 3 byte long segment
  25. $data .= "\3"; break;
  26. default: // segment is too big, invalid IP
  27. return "INVALID";
  28. }
  29. // and the segment itself
  30. $data .= $bits[$x];
  31. }
  32. // and the final bit of the request
  33. $data .= "\7in-addr\4arpa\0\0\x0C\0\1";
  34. // create UDP socket
  35. $handle = @fsockopen("udp://$dns", 53);
  36. // send our request (and store request size so we can cheat later)
  37. $requestsize=@fwrite($handle, $data);
  38.  
  39. @socket_set_timeout($handle, $timeout - $timeout%1000, $timeout%1000);
  40. // hope we get a reply
  41. $response = @fread($handle, 1000);
  42. @fclose($handle);
  43. if ($response == "")
  44. return $ip;
  45. // find the response type
  46. $type = @unpack("s", substr($response, $requestsize+2));
  47. if ($type[1] == 0x0C00) // answer
  48. {
  49. // set up our variables
  50. $host="";
  51. $len = 0;
  52. // set our pointer at the beginning of the hostname
  53. // uses the request size from earlier rather than work it out
  54. $position=$requestsize+12;
  55. // reconstruct hostname
  56. do
  57. {
  58. // get segment size
  59. $len = unpack("c", substr($response, $position));
  60. // null terminated string, so length 0 = finished
  61. if ($len[1] == 0)
  62. // return the hostname, without the trailing .
  63. return substr($host, 0, strlen($host) -1);
  64. // add segment to our host
  65. $host .= substr($response, $position+1, $len[1]) . ".";
  66. // move pointer on to the next segment
  67. $position += $len[1] + 1;
  68. }
  69. while ($len != 0);
  70. // error - return the hostname we constructed (without the . on the end)
  71. return $ip;
  72. }
  73. return $ip;
  74. }

Nieco zmodyfikowany kod znalazłem na https://www.askapache.com/php/php-fsockopen-dns-udp/ . Niestety, oba mają pewną wadę.
Większość hostów wyświetla poprawnie. Jeśli nie uda się połączyć z adresem, funkcja zwróci IP, który został podany. Kiedy host nie odpowiada, skrypt zwraca host reverse DNS, w postaci "<ip>.in-addr.arpa ".
Niestety, niekiedy skrypt ten zwraca niezrozumiałe dla mnie ciągi znaków, przy czym przy każdej próbie ciągi są inne.
Kod
String (126) "..65196144182193in-addrarpa   .  .7196144182193in-addrarpa   "
String (80) "..33196144182193in-addrarpa   .rpa    "
String (137) "..53196144182193in-addrarpa   .93in-addrarpa    .144182193in-addrarpa    "
String (31) "..-addrarpa   / "


Prześledziłem już parę stron google i nie znalazłem rozwiązania tego problemu. Ktoś spotkał się z podobnym?
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 20.04.2024 - 04:10