PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/net.php

http://github.com/bcosca/fatfree
PHP | 158 lines | 88 code | 7 blank | 63 comment | 15 complexity | b6c9f0afc088bb0f746891c0bc48aecd MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. Network utilities for the PHP Fat-Free Framework
  4. The contents of this file are subject to the terms of the GNU General
  5. Public License Version 3.0. You may not use this file except in
  6. compliance with the license. Any of the license terms and conditions
  7. can be waived if you get permission from the copyright holder.
  8. Copyright (c) 2009-2012 F3::Factory
  9. Bong Cosca <bong.cosca@yahoo.com>
  10. @package Network
  11. @version 2.0.11
  12. **/
  13. //! Network utilities
  14. class Net extends Base {
  15. /**
  16. Send ICMP echo request to specified host; Return array containing
  17. minimum/average/maximum round-trip time (in millisecs) and number of
  18. packets received, or FALSE if host is unreachable
  19. @return mixed
  20. @param $addr string
  21. @param $dns boolean
  22. @param $count integer
  23. @param $wait integer
  24. @param $ttl integer
  25. @public
  26. **/
  27. static function ping($addr,$dns=FALSE,$count=3,$wait=3,$ttl=30) {
  28. // ICMP transmit socket
  29. $tsocket=socket_create(AF_INET,SOCK_RAW,1);
  30. // Set TTL
  31. socket_set_option($tsocket,0,PHP_OS!='Linux'?4:2,$ttl);
  32. // ICMP receive socket
  33. $rsocket=socket_create(AF_INET,SOCK_RAW,1);
  34. // Bind to all network interfaces
  35. socket_bind($rsocket,0,0);
  36. // Initialize counters
  37. list($rtt,$rcv,$min,$max)=array(0,0,0,0);
  38. for ($i=0;$i<$count;$i++) {
  39. // Send ICMP header and payload
  40. $data=uniqid();
  41. $payload=self::hexbin('0800000000000000').$data;
  42. // Recalculate ICMP checksum
  43. if (strlen($payload)%2)
  44. $payload.=self::hexbin('00');
  45. $bits=unpack('n*',$payload);
  46. $sum=array_sum($bits);
  47. while ($sum>>16)
  48. $sum=($sum>>16)+($sum&0xFFFF);
  49. $payload=self::hexbin('0800').pack('n*',~$sum).
  50. self::hexbin('00000000').$data;
  51. // Transmit ICMP packet
  52. @socket_sendto($tsocket,$payload,strlen($payload),0,$addr,0);
  53. // Start timer
  54. $time=microtime(TRUE);
  55. $rset=array($rsocket);
  56. $tset=NULL;
  57. $xset=NULL;
  58. // Wait for incoming ICMP packet
  59. socket_select($rset,$tset,$xset,$wait);
  60. if ($rset &&
  61. @socket_recvfrom($rsocket,$reply,255,0,$host,$port)) {
  62. $elapsed=1e3*(microtime(TRUE)-$time);
  63. // Socket didn't timeout; Record round-trip time
  64. $rtt+=$elapsed;
  65. if ($elapsed>$max)
  66. $max=$elapsed;
  67. if (!($min>0) || $elapsed<$min)
  68. $min=$elapsed;
  69. // Count packets received
  70. $rcv++;
  71. if ($host)
  72. $addr=$host;
  73. }
  74. }
  75. socket_close($tsocket);
  76. socket_close($rsocket);
  77. return $rcv?
  78. array(
  79. 'host'=>$dns?gethostbyaddr($addr):$addr,
  80. 'min'=>(int)round($min),
  81. 'max'=>(int)round($max),
  82. 'avg'=>(int)round($rtt/$rcv),
  83. 'packets'=>$rcv
  84. ):
  85. FALSE;
  86. }
  87. /**
  88. Return the path taken by packets to a specified network destination
  89. @return array
  90. @param $addr string
  91. @param $dns boolean
  92. @param $wait integer
  93. @param $hops integer
  94. @public
  95. **/
  96. static function traceroute($addr,$dns=FALSE,$wait=3,$hops=30) {
  97. $route=array();
  98. for ($i=0;$i<$hops;$i++) {
  99. set_time_limit(ini_get('default_socket_timeout'));
  100. $result=self::ping($addr,$dns,3,$wait,$i+1);
  101. $route[]=$result;
  102. if (gethostbyname($result['host'])==gethostbyname($addr))
  103. break;
  104. }
  105. return $route;
  106. }
  107. /**
  108. Retrieve information from whois server
  109. @return string
  110. @param $addr
  111. @public
  112. **/
  113. static function whois($addr) {
  114. $socket=@fsockopen(self::$vars['WHOIS'],43,$errno,$errstr);
  115. if (!$socket) {
  116. // Can't establish connection
  117. trigger_error($errstr);
  118. return FALSE;
  119. }
  120. // Set connection timeout parameters
  121. stream_set_blocking($socket,TRUE);
  122. stream_set_timeout($socket,ini_get('default_socket_timeout'));
  123. // Send request
  124. fputs($socket,$addr."\r\n");
  125. $info=stream_get_meta_data($socket);
  126. // Get response
  127. $response='';
  128. while (!feof($socket) && !$info['timed_out']) {
  129. $response.=fgets($socket,4096); // MDFK97
  130. $info=stream_get_meta_data($socket);
  131. }
  132. fclose($socket);
  133. if ($info['timed_out']) {
  134. trigger_error(self::TEXT_Timeout);
  135. return FALSE;
  136. }
  137. return $response;
  138. }
  139. /**
  140. Class initializer
  141. @public
  142. **/
  143. static function onload() {
  144. if (!extension_loaded('sockets'))
  145. // Sockets extension required
  146. trigger_error(sprintf(self::TEXT_PHPExt,'sockets'));
  147. }
  148. }