File: 0.04.16a/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: if (!isset($BaseDir)) $BaseDir="../base/"; // 
 24: require_once($BaseDir."timer.inc.php"); // just in case standalone
 25: 
 26: require($BaseDir."tests/mysql.inc.php");
 27: require($BaseDir."tests/imap.inc.php");
 28: require($BaseDir."tests/smtp.inc.php");
 29: require($BaseDir."tests/dns.inc.php");
 30: 
 31: function ip_lookup($hostname) // "safe" DNS lookup function to call with a hostname, URL or IP - returns 0 if unsuccessful
 32: {
 33: // Is it already an IP adress?
 34: $out=str_replace(".","",$hostname);
 35: if (is_numeric($out)) return $hostname; // yes it is
 36: 
 37: // No it is not
 38: $ip=@gethostbyname($hostname);
 39: if ($ip==$hostname) return 0; // unmodified host - lookup failed
 40: 
 41: return $ip;
 42: }
 43: 
 44: 
 45: 
 46: 	
 47: function url_lookup($url)
 48: {
 49: // Sod regular expressions here as we'd have to do it twice or with cleverness I lack
 50: // Is it a URL?
 51: $colon=strpos($url,":");
 52: if ($colon != 0) // exists so it a URL
 53: 	{
 54: 	$out=preg_match("@^(?:http[s]*://)?([^/|\?|:]+)@i",$url,$matches);
 55: 	$hostname=$matches[1];
 56: 	}
 57: else $hostname=$url; // try direct
 58: 	
 59: return ip_lookup($hostname);
 60: }
 61: 	
 62: 	
 63: 
 64: 
 65: 
 66: 
 67: 
 68:  function icmpChecksum($data)
 69:     {
 70:     if (strlen($data)%2)
 71:     $data .= "\x00";
 72:     
 73:     $bit = unpack('n*', $data);
 74:     $sum = array_sum($bit);
 75:     
 76:     while ($sum >> 16)
 77:     $sum = ($sum >> 16) + ($sum & 0xffff);
 78:     
 79:     return pack('n*', ~$sum);
 80:     }
 81:    
 82: function PingTest($host,$ctimeout=-1)
 83: 	{
 84: 	global $NATS;
 85: 	$FreeNATS_Timer=new TFNTimer();
 86:     // Make Package
 87:     $type= "\x08";
 88:     $code= "\x00";
 89:     $checksum= "\x00\x00";
 90:     $identifier = "\x00\x00";
 91:     $seqNumber = "\x00\x00";
 92:     $data= "FreeNATS";
 93:     $package = $type.$code.$checksum.$identifier.$seqNumber.$data;
 94:     $checksum = icmpChecksum($package); // Calculate the checksum
 95:     $package = $type.$code.$checksum.$identifier.$seqNumber.$data;
 96:     
 97:     // Return s or ms(s*1000)
 98:     $returnsecs=true;
 99:     if (isset($NATS))
100:     	{
101: 	    if ($NATS->Cfg->Get("test.icmp.returnms",0)==1) $returnsecs=false;
102:     	}
103: 
104: 	// Timeout Values
105:     if (isset($NATS)) $timeout=$NATS->Cfg->Get("test.icmp.timeout",10);
106:     else $timeout=10;
107:     if ($ctimeout>0) $timeout=$ctimeout; // use custom timeout if passed
108:     if ($timeout<=0) $timeout=10; // catch-all for defaults bug
109:     
110:     // Create Socket
111:     $socket = @socket_create(AF_INET, SOCK_RAW, 1);
112:     	//or die(socket_strerror(socket_last_error()));
113:     if (!$socket) return 0;
114:     
115:     // Set Non-Blocking
116:     @socket_set_nonblock($socket);
117:     	
118:     // Connect Socket
119:     $sconn=@socket_connect($socket, $host, null);
120:     if (!$sconn) return 0;
121:     
122:     // Send Data
123:     @socket_send($socket, $package, strLen($package), 0);
124:         
125:     // Start Timer
126:     $FreeNATS_Timer->Start();
127:     $startTime = microtime(true); // need this for the looping section
128:     
129: 
130:     // Read Data
131:     $keepon=true;
132: 
133:     while( (!(@socket_read($socket, 255))) && $keepon)
134:     	{ // basically just kill time
135:     	// consider putting some sort of sleepy thing here to lower load but would f* with figures!
136:     	
137:     	if ( (microtime(true) - $startTime) > $timeout )
138:     		$keepon=false;
139: 		}
140:     	
141: 	if ($keepon) // didn't time out - read data
142:     	{
143: 	    $elapsed=$FreeNATS_Timer->Stop();
144: 	    if ($returnsecs) $elapsed=round($elapsed,4);
145: 	    else $elapsed=round( ($elapsed*1000),4 );
146: 	    @socket_close($socket);
147:     	// $ret=round(microtime(true) - $startTime, 4); -- old method
148:     	if ($elapsed<=0) $elapsed="0.0001"; // safety catch-all
149:     	return $elapsed;
150:     	/*
151:     	if ($ret==0) return "0.0001";
152:     	else 
153:     		{
154: 	    	if ($ret<=0) return "0.0001";
155: 	    	else return $ret;
156:     		}
157:     	*/
158:     	}
159:     	
160:     // Socket timed out
161:     @socket_close($socket);
162:     return 0;
163: 	}
164: 
165: function WebTest($url,$timeout=-1)
166: 	{
167: 	global $NATS;
168: 	if ($timeout<=0) // use NATS or env
169: 		{
170: 		if (isset($NATS))
171: 			{
172: 			$nto=$NATS->Cfg->Get("test.http.timeout",-1);
173: 			if ($nto>0) $timeout=$nto; // use NATS timeout
174: 			}
175: 		}
176: 	if ($timeout>0) // use the set timeout
177: 		$oldtimeout=ini_set("default_socket_timeout",$timeout);
178: 		
179: 	if (function_exists("curl_getinfo")) // use CURL if present
180: 		{
181: 		$ch=curl_init();
182: 		curl_setopt($ch,CURLOPT_URL,$url);
183: 		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
184: 		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
185: 		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
186: 		curl_setopt($ch,CURLOPT_HEADER,1);
187: 		if ($timeout>0) curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
188: 		if ($timeout>0) curl_setopt($ch,CURLOPT_TIMEOUT,$timeout);
189: 		if (!$output=curl_exec($ch))
190: 			{
191: 			$ctr=-1; // failed
192: 			}
193: 		else $ctr=round(curl_getinfo($ch,CURLINFO_SIZE_DOWNLOAD)/1024,2);
194: 		curl_close($ch);
195: 		
196: 		if ($ctr==0) $ctr="0.0001";
197: 		
198: 		}
199: 	else
200: 		{	// no CURL - use fopen()	
201: 		$fp=@fopen($url,"r");
202: 		if ($fp<=0)
203: 			{
204: 			if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout);
205: 			return -1;
206: 			}
207: 		$ctr=0;
208: 		while ($body=@fgets($fp,1024)) $ctr+=sizeof($body);
209: 		@fclose($fp);
210: 		}
211: 	
212: 	
213: 	
214: 	if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout);
215: 	return $ctr;
216: 	}
217: 	
218: function DoTest($test,$param,$hostname="",$timeout=-1,$params=0)
219: {
220: if (!is_array($params))
221: 	{
222: 	$params=array();
223: 	for ($a=0; $a<10; $a++) $params[$a]="";
224: 	}
225: 	
226: switch ($test)
227: 	{
228: 	case "web": case "wsize":
229: 		// Don't bother with pre-resolution as size only
230: 		return WebTest($param,$timeout);
231: 		break;
232: 	case "tcp": // nb TCP does not support timeouts currently
233: 		$ip=ip_lookup($hostname);
234: 		if ($ip=="0") return 0;
235: 		$fp=@fsockopen($ip,$param);
236: 		if ($fp<=0) return 0;
237: 		@fclose($fp);
238: 		return 1;
239: 		break;
240: 	case "wtime":
241: 		$timer=new TFNTimer();
242: 		// Do a pre-lookup
243: 		$ip=url_lookup($param);
244: 		if ($ip=="0") return -1; // dns lookup failed
245: 		$timer->Start();
246: 		$r=WebTest($param,$timeout);
247: 		$elapsedTime=$timer->Stop();
248: 		$elapsedTime=round($elapsedTime,4);
249: 		if ($r<0) return -1; // open failed
250: 		if ($r==0) return -2; // no chars shown as returned
251: 		if ($elapsedTime<=0) return 0.0001;
252: 		return $elapsedTime;
253: 		break;
254: 		
255: 	case "host":
256: 		$timer=new TFNTimer();
257: 		if (preg_match("/[a-zA-Z]/",$param)>0) $is_ip=false;
258: 		else $is_ip=true;
259: 		
260: 		$timer->Start();
261: 		if ($is_ip) $result=gethostbyaddr($param);
262: 		else $result=gethostbyname($param);
263: 		$elapsedTime=$timer->Stop();
264: 		
265: 		if ($result==$param) // lookup failed
266: 			return -1;
267: 			
268: 		if ($result=="")	// lookup failed
269: 			return -1;
270: 			
271: 		$elapsedTime=round($elapsedTime,4);
272: 		if ($elapsedTime<=0) return 0.0001;
273: 		return $elapsedTime;
274: 		break;
275: 		
276: 	case "dns":
277: 		$timer=new TFNTimer();
278: 		if ($param=="") $dnsserver=$hostname;
279: 		else $dnsserver=$param;
280: 		if ($dnsserver=="") return -3;
281: 		if ($timeout<=0) $timeout=60;
282: 		if ($params[4]==1) $udp=false; // use TCP
283: 		else $udp=true;
284: 		if (($params[3]=="")||($params[3]==0)) $port=53;
285: 		else $port=$params[3];
286: 		
287: 		$dns_query=new DNSQuery($dnsserver,$port,$timeout,$udp,true);
288: 		$type=$params[2];
289: 		$query=$params[1];
290: 		
291: 		$timer->Start();
292: 		$answer=$dns_query->Query($query,$type);
293: 		$elapsedTime=$timer->Stop();
294: 		
295: 		if ( ($answer===false) || ($dns_query->error) ) return -2; // query error
296: 		if ($answer->count<=0) return -1; // no records returned
297: 		
298: 		// otherwise we've got some results ok
299: 		$elapsedTime=round($elapsedTime,4);
300: 		if ($elapsedTime<=0) return 0.0001;
301: 		return $elapsedTime;
302: 		break;
303: 		
304: 	case "testloop":
305: 		return $param;
306: 		break;
307: 		
308: 	case "testrand":
309: 		mt_srand(microtime()*1000000);
310: 		if ( ($param=="") || ($param==0) ) $param=100;
311: 		return mt_rand(0,$param);
312: 		break;
313: 		
314: 	case "ping":
315: 		return PingTest($param,$timeout);
316: 		break;
317: 		
318: 	case "smtp":
319: 		$ip=ip_lookup($param);
320: 		if ($ip=="0") return -1;
321: 		return smtp_test_time($ip,$params[1],$timeout);
322: 		break;
323: 		
324: 	case "imap":
325: 		if ($params[5]==1) $ssl=true;
326: 		else $ssl=false;
327: 		
328: 		$ip=ip_lookup($param);
329: 		if ($ip=="0") return -1;
330: 		
331: 		return imap_test_time($ip,$params[1],$params[2],$timeout,$params[3],$params[4],$ssl);
332: 		break;
333: 		
334: 	case "mysql":
335: 		$ip=ip_lookup($param);
336: 		if ($ip=="0") return -1; // cache only as 127.0.0.1 is not the same connection as localhost for MySQL auth!
337: 
338: 		return mysql_test_time($param,$params[1],$params[2],$params[3],$timeout,$params[4]);
339: 		break;
340: 		
341: 	case "mysqlrows":
342: 		$ip=ip_lookup($param);
343: 		if ($ip=="0") return -1; // cache only - see above
344: 		return mysql_test_rows($param,$params[1],$params[2],$params[3],$timeout,$params[4]);
345: 		break;
346: 		
347: 		
348: 	}
349: return -1;
350: }
351: 
352: function SimpleEval($test,$result)
353: {
354: switch($test)
355: 	{
356: 	case "ping": // handles both types of simple evaluation (inbuilt ICMP and remote ping)
357: 		if ($result<=0) return 2;
358: 		return 0;
359: 	case "web": case "wsize":
360: 		if ($result<=0) return 2;
361: 		return 0;
362: 	case "tcp":
363: 		if ($result==1) return 0;
364: 		return 2;
365: 	case "wtime":
366: 		if ($result<0) return 2;
367: 		return 0;
368: 	
369: 	case "imap":
370: 		if ($result<=0) return 2;
371: 		return 0;
372: 	
373: 	case "smtp":
374: 		if ($result<=0) return 2;
375: 		return 0;
376: 		
377: 	case "mysql":
378: 		if ($result<=0) return 2;
379: 		return 0;
380: 		
381: 	case "mysqlrows":
382: 		if ($result<=0) return 2; // no rows returned or error
383: 		return 0;
384: 		
385: 	case "host": case "dns":
386: 		if ($result<=0) return 2; // no records returned or error
387: 		
388: 	case "testloop":
389: 		return 0;
390: 	case "testrand":
391: 		return 0;
392: 	}
393: return -1; // untested if we don't know WTF the result was
394: }
395: 
396: function aText($al)
397: {
398: return oText($al); // uses function in tests.inc.php with site config support	
399: /* -- depreciated
400: switch($al)
401: 	{
402: 	case -1: return "Untested";
403: 	case 0: return "Passed";
404: 	case 1: return "Warning";
405: 	case 2: return "Failed";
406: 	default: return "Unknown";
407: 	}
408: */
409: }
410: ?>