2 error_reporting(E_ALL | E_STRICT);
5 define('MAX_FAILURES', 3000);
7 require('udp-inc.php');
11 function validate ($data) {
28 out(__FUNCTION__, __LINE__, "Unknown hasher: ${hasher}");
33 function sendBye ($socket) {
37 stream_socket_sendto($socket, 'BYE');
40 $read = trim(stream_socket_recvfrom($socket, 1500, 0, $peer));
42 // Did send BYE as well?
50 if (function_exists('time_nanosleep')) time_nanosleep(0, 500000);
60 if (isset($_SERVER['argv'][1])) {
61 $ip = $_SERVER['argv'][1];
64 $socket = stream_socket_client("udp://${ip}:9060", $errno, $errstr);
66 if ((!is_resource($socket)) || ($errno > 0)) {
67 out(__FILE__, __LINE__, "ERROR: $errno - $errstr");
71 if (!stream_set_blocking($socket, FALSE)) {
72 out(__FILE__, __LINE__, "ERROR: Cannot set non-blocking mode!");
76 out(__FILE__, __LINE__, "Negotiating with host ${ip} ...");
77 while (!feof($socket)) {
78 //*DEBUG: */ out(__FILE__, __LINE__, "Sending ping...");
79 stream_socket_sendto($socket, 'PING');
81 //*DEBUG: */ out(__FILE__, __LINE__, "Reading reply...");
82 $read = trim(stream_socket_recvfrom($socket, 1500, 0, $peer));
84 if ($failed == constant('MAX_FAILURES')) {
85 //*DEBUG: */ out(__FILE__, __LINE__, "Too many failures! (failed={$failed})");
89 // Is the peer the same?
91 out(__FILE__, __LINE__, "Connection lost? read={$read} (".strlen($read).")");
94 } elseif ($peer != "$ip:9060") {
95 out(__FILE__, __LINE__, "Peer mismatch: {$ip}!={$peer}");
102 //*DEBUG: */ out(__FILE__, __LINE__, "Empty line received. Is the server there?");
104 } elseif ($read == 'INVALID') {
106 out(__FILE__, __LINE__, "Server has not accepted our message.");
110 //*DEBUG: */ out(__FILE__, __LINE__, "Response {$read} received.");
113 $rec = explode(':', $read);
115 if (count($rec) < 2) {
116 out(__FILE__, __LINE__, "Invalid packet {$read} received. count=".count($rec));
121 $time = trim($rec[0]);
122 $right = explode('=', trim($rec[1]));
123 $hash = trim($right[1]);
124 $hasher = trim($right[0]);
126 if (validate($time) != $hash) {
127 out(__FILE__, __LINE__, "Invalid: ({$time}/{$hash}/{$hasher})");
132 if (!isset($data[$rec[0]])) {
133 if (count($data) > 0) {
134 print $data[$rec[0]-1]."\n";
136 if ($cnt > constant('ROUNDS')) break;
143 if (function_exists('time_nanosleep')) time_nanosleep(0, 500000);
151 stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
157 foreach ($data as $cnt) {
161 if (($cnt < $min) || ($min == 0)) {
167 if (count($data) > 0) {
168 $avg = round($avg / count($data));
171 out(__FILE__, __LINE__, 'MIN/AVG/MAX=' . $min . '/' . $avg . '/' . $max . '');
172 out(__FILE__, __LINE__, 'INVALID=' . $invalid . '');
173 out(__FILE__, __LINE__, 'FAILED=' . $failed . '');