File: 0.02.53a/server/base/tests.inc.php (View as HTML)

  1: <?php // tests.inc.php
  2: /* -------------------------------------------------------------
  3: This file is part of FreeNATS
  4: 
  5: FreeNATS is (C) Copyright 2008 PurplePixie Systems
  6: 
  7: FreeNATS is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeNATS is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeNATS.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freenats
 21: -------------------------------------------------------------- */
 22: 
 23:  function icmpChecksum($data)
 24:     {
 25:     if (strlen($data)%2)
 26:     $data .= "\x00";
 27:     
 28:     $bit = unpack('n*', $data);
 29:     $sum = array_sum($bit);
 30:     
 31:     while ($sum >> 16)
 32:     $sum = ($sum >> 16) + ($sum & 0xffff);
 33:     
 34:     return pack('n*', ~$sum);
 35:     }
 36:    
 37: function PingTest($host)
 38: 	{
 39:     // Make Package
 40:     $type= "\x08";
 41:     $code= "\x00";
 42:     $checksum= "\x00\x00";
 43:     $identifier = "\x00\x00";
 44:     $seqNumber = "\x00\x00";
 45:     $data= "FreeNATS";
 46:     $package = $type.$code.$checksum.$identifier.$seqNumber.$data;
 47:     $checksum = icmpChecksum($package); // Calculate the checksum
 48:     $package = $type.$code.$checksum.$identifier.$seqNumber.$data;
 49:     
 50:     // Create Socket
 51:     $socket = @socket_create(AF_INET, SOCK_RAW, 1);
 52:     	//or die(socket_strerror(socket_last_error()));
 53:     if (!$socket) return 0;
 54:     
 55:     // Set Non-Blocking
 56:     @socket_set_nonblock($socket);
 57:     	
 58:     // Connect Socket
 59:     $sconn=@socket_connect($socket, $host, null);
 60:     if (!$sconn) return 0;
 61:     
 62:     // Start Timer
 63:     $startTime = microtime(true);
 64:     
 65:     // Send Data
 66:     @socket_send($socket, $package, strLen($package), 0);
 67:     
 68:     // Read Data
 69:     $keepon=true;
 70:     if (isset($NATS)) $timeout=$NATS->Cfg->Get("test.icmp.timeout",10);
 71:     else $timeout=10;
 72:     while( (!(@socket_read($socket, 255))) && $keepon)
 73:     	{ // basically just kill time
 74:     	// consider putting some sort of sleepy thing here to lower load but would f* with figures!
 75:     	
 76:     	if ( (microtime(true) - $startTime) > $timeout )
 77:     		$keepon=false;
 78: 		}
 79:     	
 80: 	if ($keepon) // didn't time out - read data
 81:     	{
 82: 	    @socket_close($socket);
 83:     	$ret=round(microtime(true) - $startTime, 4);
 84:     	if ($ret==0) return "0.0001";
 85:     	else 
 86:     		{
 87: 	    	if ($ret<=0) return "0.0001";
 88: 	    	else return $ret;
 89:     		}
 90:     	}
 91:     	
 92:     // Socket timed out
 93:     @socket_close($socket);
 94:     return 0;
 95: 	}
 96: 
 97: function WebTest($url)
 98: 	{
 99: 	$fp=@fopen($url,"r");
100: 	if ($fp<=0) return -1;
101: 	$ctr=0;
102: 	while ($body=@fgets($fp,1024)) $ctr+=sizeof($body);
103: 	@fclose($fp);
104: 	return $ctr;
105: 	}
106: 	
107: function DoTest($test,$param,$hostname="")
108: {
109: switch ($test)
110: 	{
111: 	case "web":
112: 		return WebTest($param);
113: 		break;
114: 	case "tcp":
115: 		$fp=@fsockopen($hostname,$param);
116: 		if ($fp<=0) return 0;
117: 		@fclose($fp);
118: 		return 1;
119: 		break;
120: 	case "wtime":
121: 		$startTime=microtime(true);
122: 		$r=WebTest($param);
123: 		$elapsedTime=round(microtime(true)-$startTime,4);
124: 		if ($r<0) return -1; // fopen failed
125: 		if ($r==0) return -2; // no chars shown as returned
126: 		if ($elapsedTime<=0) return 0.0001;
127: 		return $elapsedTime;
128: 		break;
129: 	case "testloop":
130: 		return $param;
131: 		break;
132: 		
133: 	case "testrand":
134: 		mt_srand(microtime()*1000000);
135: 		if ( ($param=="") || ($param==0) ) $param=100;
136: 		return mt_rand(0,$param);
137: 		break;
138: 		
139: 	case "ping":
140: 		return PingTest($param);
141: 		break;
142: 	}
143: return -1;
144: }
145: 
146: function SimpleEval($test,$result)
147: {
148: switch($test)
149: 	{
150: 	case "ping": // handles both types of simple evaluation (inbuilt ICMP and remote ping)
151: 		if ($result<=0) return 2;
152: 		return 0;
153: 	case "web":
154: 		if ($result<=0) return 2;
155: 		return 0;
156: 	case "tcp":
157: 		if ($result==1) return 0;
158: 		return 2;
159: 	case "wtime":
160: 		if ($result<0) return 2;
161: 		return 0;
162: 	case "testloop":
163: 		return 0;
164: 	case "testrand":
165: 		return 0;
166: 	}
167: return -1;
168: }
169: 
170: function aText($al)
171: {
172: switch($al)
173: 	{
174: 	case -1: return "Untested";
175: 	case 0: return "Passed";
176: 	case 1: return "Warning";
177: 	case 2: return "Failed";
178: 	default: return "Unknown";
179: 	}
180: }
181: ?>