"Fossies" - the Fresh Open Source Software Archive

Member "node-runner-0.6.0/include/query_icmp.php" (17 Aug 2004, 3480 Bytes) of package /linux/www/old/node-runner-0.6.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "query_icmp.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 // This is an example script that queries ICMP on a given node.
    4 
    5 array_push($queries_type_array, "ICMP");
    6 
    7 
    8 // Note that I did not write this class, but thanks a bunch to whomever did.
    9 class Net_Ping
   10 {
   11   var $icmp_socket;
   12   var $request;
   13   var $request_len;
   14   var $reply;
   15   var $errstr;
   16   var $time;
   17   var $timer_start_time;
   18   function Net_Ping()
   19   {
   20    $this->icmp_socket = socket_create(AF_INET, SOCK_RAW, 1);
   21    socket_set_block($this->icmp_socket);
   22   }
   23 
   24   function ip_checksum($data)
   25   {
   26      for($i=0;$i<strlen($data);$i += 2)
   27      {
   28          if($data[$i+1]) $bits = unpack('n*',$data[$i].$data[$i+1]);
   29          else $bits = unpack('C*',$data[$i]);
   30          $sum += $bits[1];
   31      }
   32 
   33      while ($sum>>16) $sum = ($sum & 0xffff) + ($sum >> 16);
   34      $checksum = pack('n1',~$sum);
   35      return $checksum;
   36   }
   37 
   38   function start_time()
   39   {
   40    $this->timer_start_time = microtime();
   41   }
   42 
   43   function get_time($acc=2)
   44   {
   45    // format start time
   46    $start_time = explode (" ", $this->timer_start_time);
   47    $start_time = $start_time[1] + $start_time[0];
   48    // get and format end time
   49    $end_time = explode (" ", microtime());
   50    $end_time = $end_time[1] + $end_time[0];
   51    return number_format ($end_time - $start_time, $acc);
   52   }
   53 
   54   function Build_Packet()
   55   {
   56    $data = "abcdefghijklmnopqrstuvwabcdefghi"; // the actual test data
   57    $type = "\x08"; // 8 echo message; 0 echo reply message
   58    $code = "\x00"; // always 0 for this program
   59    $chksm = "\x00\x00"; // generate checksum for icmp request
   60    $id = "\x00\x00"; // we will have to work with this later
   61    $sqn = "\x00\x00"; // we will have to work with this later
   62 
   63    // now we need to change the checksum to the real checksum
   64    $chksm = $this->ip_checksum($type.$code.$chksm.$id.$sqn.$data);
   65 
   66    // now lets build the actual icmp packet
   67    $this->request = $type.$code.$chksm.$id.$sqn.$data;
   68    $this->request_len = strlen($this->request);
   69   }
   70 
   71   function Ping($dst_addr,$timeout,$percision=3)
   72   {
   73    // lets catch dumb people
   74    if ((int)$timeout <= 0) $timeout=5;
   75    if ((int)$percision <= 0) $percision=3;
   76 
   77    // set the timeout
   78    socket_set_option($this->icmp_socket,
   79      SOL_SOCKET,  // socket level
   80      SO_RCVTIMEO, // timeout option
   81      array(
   82        "sec"=>$timeout, // Timeout in seconds
   83        "usec"=>0  // I assume timeout in microseconds
   84        )
   85      );
   86 
   87    if ($dst_addr)
   88    {
   89      if (@socket_connect($this->icmp_socket, $dst_addr, NULL))
   90      {
   91 
   92      } else {
   93        $this->errstr = "Cannot connect to $dst_addr";
   94        return FALSE;
   95      }
   96      $this->Build_Packet();
   97      $this->start_time();
   98      socket_write($this->icmp_socket, $this->request, $this->request_len);
   99      if (@socket_recv($this->icmp_socket, &$this->reply, 256, 0))
  100      {
  101        $this->time = $this->get_time($percision);
  102        return $this->time;
  103      } else {
  104        $this->errstr = "ICMP Timed out";
  105        return FALSE;
  106      }
  107    } else {
  108      $this->errstr = "Destination address not specified";
  109      return FALSE;
  110    }
  111   }
  112 }
  113 
  114 
  115 function icmp_query($description, $ipaddress, $ptime) {
  116     GLOBAL $debug;
  117 
  118     $status = array();
  119     
  120 $ping = new Net_Ping;
  121 $ping->ping($ipaddress,$ptime);
  122 
  123 if ($ping->time) {
  124   $status[0] = 1;
  125   $status[1] = $description." UP";
  126   if ($debug == 1) { $status[1] .= " - Queried in: ".$ping->time." seconds."; }
  127 } else {
  128   $status[0] = 0;
  129   $status[1] = $description." DOWN";
  130   if ($debug == 1) { $status[1] .= " - ".$ping->errstr.""; }
  131 }
  132 
  133   $status[1] .= "\n";
  134   return $status;
  135 }
  136 
  137 ?>