$port_end) { $json_array['status'] = 'no_ports'; $json_array['status_message'] = _("No Free Ports in Asterisk RTP Port Range available for testing, you can try later"); } else { // Setup the receive end before ping the server to get started $listen_port = (int)$port_start; $listen_sock = socket_create(AF_INET, SOCK_DGRAM, 0); if (@socket_bind($listen_sock, $listen_address, $listen_port) === false) { $json_array['status'] = 'socket_bind_listen_failed'; $json_array['status_message'] = socket_strerror(socket_last_error($listen_sock)).". Could not bind to intended listen port ($listen_address:$listen_port) to receive test tocken."; } else { socket_set_nonblock($listen_sock); // OK, now we are listening so lets tell the server to send us something // $send_port = (string)$port_start; $token = md5($send_port*rand()); $fn = "http://mirror.freepbx.org/whatismyip.php?"; $fn .= "port=$send_port&token=".urlencode($token); // Now build the token we will get back, to look like a g711u payload I hope // this gets around any potential firewall issues though it is a stray packet // $header = "\x80\x80\x5a\x1d\xac\xe1\x37\xab\x3b\xb7\x59\xc8"; $token = $header.$token.$token.$token.$token.$token; if (!$amp_conf['MODULEADMINWGET']) { $ip_xml = @file_get_contents($fn); } if (empty($ip_xml)) { exec("wget -O - '$fn' 2> /dev/null", $data_arr, $retcode); $ip_xml = implode("\n",$data_arr); } preg_match('|^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$|',$ip_xml,$matches); if (isset($matches[1])) { $json_array['externip'] = $matches[1]; } // OK, now we fired off our request and it returned, which means it should have fired off our // packet which should be buffered and waiting for us to read // $res = false; $count = 5; $string = $string2 = ""; while ($res === false && $count > 0) { $res = @socket_recv($listen_sock, $string, 1024, 0); if ($res === false) { sleep(1); } $count--; } sleep(1); $res = @socket_recv($listen_sock, $string2, 1024, 0); socket_close($listen_sock); if ($res) { $string += $string2; } } } if ($json_array['status'] != 'success') { // Already filled in } elseif ($count <= 0 && $string != $token) { $json_array['status'] = 'timeout'; $json_array['status_message'] = _("The test timed out which means your firewall is probably configured wrong. If subsequent tests fail, check your port forwarding on the firewall."); } elseif ($string != $token) { $json_array['status'] = 'bad_token'; $json_array['status_message'] = _("An unexpected token was returned, try the test again"); } $json = new Services_JSON(); sleep(0); header("Content-type: application/json"); echo $json->encode($json_array);