File: 1.13.3b/server/base/tests/udp.inc.php (View as Code)

1: 2: /* ------------------------------------------------------------- 3: This file is part of FreeNATS 4: 5: FreeNATS is (C) Copyright 2008-2011 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: /* UDP Test Updated to Check for Response 24: Feedback and code basis from Marc Franquesa http://www.l3jane.net/wiki/factory:factory 25: */ 26: 27: if (isset($NATS)) 28: { 29: class FreeNATS_UDP_Test extends FreeNATS_Local_Test 30: { 31: 32: function DoTest($testname,$param,$hostname,$timeout,$params) 33: { 34: global $NATS; 35: 36: if ($timeout<=0) $timeout=$NATS->Cfg->Get("test.udp.timeout",0); // if no test-specific param use sys default 37: if ($timeout<=0) $timeout=20; // if sys default is <=0 then default to 60 seconds 38: 39: if ($params[1]!="") $package = $params[1]; 40: else $package="\x00"; 41: 42: if ($params[2]==1) $reqresponse=true; 43: else $reqresponse=false; 44: 45: $timer=new TFNTimer(); 46: 47: $ip=ip_lookup($hostname); 48: if ($ip=="0") return -2; // lookup failed 49: 50: $connstr="udp://".$ip; 51: $errno=0; 52: $errstr=""; 53: 54: $timer->Start(); 55: 56: $fp=@fsockopen($connstr,$param,$errno,$errstr,$timeout); 57: if ($fp===false) return -1; // open failed 58: 59: stream_set_timeout($fp, $timeout); 60: 61: $write = fwrite($fp, $package); // send some data 62: if (!$write) return -3; // failed to send data 63: 64: $read = fgets($fp); 65: 66: @fclose($fp); 67: 68: $elapsed = $timer->Stop(); 69: 70: if (!$read) 71: { 72: if ($reqresponse) return -4; // no response and one was required 73: else if (round($elapsed,0) < $timeout) 74: { 75: return -5; // looks like a hard reject e.g. ICMP port unreachable 76: } 77: } 78: 79: if ($elapsed==0) $elapsed="0.001"; 80: return $elapsed; 81: } 82: 83: function Evaluate($result) 84: { 85: if ($result<=0) return 2; // failure 86: return 0; // else success 87: } 88: 89: function DisplayForm(&$row) 90: { 91: echo ""; 92: echo ""; 97: echo ""; 102: echo ""; 103: echo ""; 110: echo ""; 111: echo "
"; 93: echo "UDP Port :"; 94: echo ""; 95: echo ""; 96: echo "
"; 98: echo "Send Data :"; 99: echo ""; 100: echo ""; 101: echo "
 (optional, blank for default)
"; 104: echo "Require Response :"; 105: echo ""; 106: if ($row['testparam2']==1) $s=" checked"; 107: else $s=""; 108: echo ""; 109: echo "
 Requires data response from the server (usually leave unchecked)
";
112: } 113: 114: } 115: 116: $params=array(); 117: $NATS->Tests->Register("udp","FreeNATS_UDP_Test",$params,"UDP Connect",2,"FreeNATS UDP Tester"); 118: $NATS->Tests->SetUnits("udp","Seconds","s"); 119: } 120: 121: 122: ?>