function __construct($ip, $port) { if (!($sock = @socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))) { warn("Failed to create socket"); usleep(100000); AsyncSocket::poll(0.5); if (!($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))) { throw new ExAsyncSocket("Unable to create UDP socket"); } } if (socket_connect($sock, $ip, $port)) { socket_set_nonblock($sock); $this->sock = $sock; } else { warn('sockets problem'); throw new ExAsyncSocket("Unable to connect to UDP {$ip}:{$port}"); } }
/** query nameserver @return instance of AsyncDNS that will return acutual result */ static function query($host, $type = 'A', $class = 'IN') { static $queries; if (preg_match('/^(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)$/', $host, $regs)) { $host = $regs[4] . '.' . $regs[3] . '.' . $regs[2] . '.' . $regs[1] . '.in-addr.arpa.'; $type = 'PTR'; } $key = $class . $type . $host; if (!isset(self::$resolvers[$key])) { self::$resolvers[$key] = new AsyncDNS($host, $type, $class, $key); $queries++; if ($queries % 10 == 0) { AsyncSocket::poll(); } // prevents queue from getting too large } return self::$resolvers[$key]; }
function testPost(ISblamPost $p) { if (!$this->testPhases) { return array(0, 0, "No tests"); } $profiling = array(); $asyncpolltime = 0; $results = array(); $totalspam = 0; ksort($this->testPhases, SORT_NUMERIC); foreach ($this->testPhases as $phase => $phaseTests) { foreach ($phaseTests as $test) { if (!$test instanceof ISblamTestPost) { continue; } $start = microtime(true); $test->preTestPost($p); $profiling["p{$phase}:" . get_class($test)] = microtime(true) - $start; } foreach ($phaseTests as $test) { if (!$test instanceof ISblamTestPost) { continue; } $start = microtime(true); AsyncSocket::poll(0); // get those queued DNS queries $asyncpolltime += microtime(true) - $start; $start = microtime(true); $tmpres = $test->testPost($p); $profiling["t{$phase}:" . get_class($test)] = microtime(true) - $start; $results[] = $tmpres; if ($tmpres && is_numeric($tmpres[0])) { $totalspam += $tmpres[0]; } if ($totalspam > self::EARLY_ESCAPE_LIMIT) { $results[] = array(6, 1, "Early escape", $profiling); break 2; } } } $profiling['tst:AsyncSocket'] = $asyncpolltime; $results = $this->sumResults($results); $results[2] = implode('; ', $results[2]); $results[3] = $profiling; return $results; }