/** * opens a socket connection to the DNS server * * @return boolean * @access public * */ public function open() { // // create the socket // if (Net_DNS2::isIPv4($this->host) == true) { $this->sock = @socket_create(AF_INET, $this->type, $this->type == Net_DNS2_Socket::SOCK_STREAM ? SOL_TCP : SOL_UDP); } else { if (Net_DNS2::isIPv6($this->host) == true) { $this->sock = @socket_create(AF_INET6, $this->type, $this->type == Net_DNS2_Socket::SOCK_STREAM ? SOL_TCP : SOL_UDP); } else { $this->last_error = 'invalid address type: ' . $this->host; return false; } } if ($this->sock === false) { $this->last_error = socket_strerror(socket_last_error()); return false; } @socket_set_option($this->sock, SOL_SOCKET, SO_REUSEADDR, 1); // // bind to a local IP/port if it's set // if (strlen($this->local_host) > 0) { $result = @socket_bind($this->sock, $this->local_host, $this->local_port > 0 ? $this->local_port : null); if ($result === false) { $this->last_error = socket_strerror(socket_last_error()); return false; } } // // mark the socket as non-blocking // if (@socket_set_nonblock($this->sock) === false) { $this->last_error = socket_strerror(socket_last_error()); return false; } // // connect to the socket; don't check for status here, we'll check it on the // socket_select() call so we can handle timeouts properly // @socket_connect($this->sock, $this->host, $this->port); $read = null; $write = array($this->sock); $except = null; // // select on write to check if the call to connect worked // $result = @socket_select($read, $write, $except, $this->timeout); if ($result === false) { $this->last_error = socket_strerror(socket_last_error()); return false; } else { if ($result == 0) { $this->last_error = 'timeout on write select for connect()'; return false; } } return true; }
/** * opens a socket connection to the DNS server * * @return boolean * @access public * */ public function open() { // // create the socket // if (Net_DNS2::isIPv4($this->host) == true) { $this->_sock = @socket_create(AF_INET, $this->type, $this->type == SOCK_STREAM ? SOL_TCP : SOL_UDP); } else { if (Net_DNS2::isIPv6($this->host) == true) { $this->_sock = @socket_create(AF_INET6, $this->type, $this->type == SOCK_STREAM ? SOL_TCP : SOL_UDP); } else { $this->last_error = 'invalid address type: ' . $this->host; return false; } } if ($this->_sock === false) { $this->last_error = socket_strerror(socket_last_error()); return false; } @socket_set_option($this->_sock, SOL_SOCKET, SO_REUSEADDR, 1); // // bind to a local IP/port if it's set // if (strlen($this->local_host) > 0) { $result = @socket_bind($this->_sock, $this->local_host, $this->local_port > 0 ? $this->local_port : null); if ($result === false) { $this->last_error = socket_strerror(socket_last_error()); return false; } } // // connect to the socket // if (@socket_connect($this->_sock, $this->host, $this->port) === false) { $this->last_error = socket_strerror(socket_last_error()); return false; } // // mark the socket as non-blocking // if (@socket_set_nonblock($this->_sock) === false) { $this->last_error = socket_strerror(socket_last_error()); return false; } return true; }
/** * parses the rdata portion from a standard DNS config line * * @param array $rdata a string split line of values for the rdata * * @return boolean * @access protected * */ protected function rrFromString(array $rdata) { // // load the data // $precedence = array_shift($rdata); $gateway_type = array_shift($rdata); $algorithm = array_shift($rdata); $gateway = strtolower(trim(array_shift($rdata))); $key = array_shift($rdata); // // validate it // switch ($gateway_type) { case self::GATEWAY_TYPE_NONE: $gateway = ''; break; case self::GATEWAY_TYPE_IPV4: if (Net_DNS2::isIPv4($gateway) == false) { return false; } break; case self::GATEWAY_TYPE_IPV6: if (Net_DNS2::isIPv6($gateway) == false) { return false; } break; case self::GATEWAY_TYPE_DOMAIN: // do nothing break; default: return false; } // // check the algorithm and key // switch ($algorithm) { case self::ALGORITHM_NONE: $key = ''; break; case self::ALGORITHM_DSA: case self::ALGORITHM_RSA: // do nothing break; default: return false; } // // store the values // $this->precedence = $precedence; $this->gateway_type = $gateway_type; $this->algorithm = $algorithm; $this->gateway = $gateway; $this->key = $key; return true; }
/** * opens a socket connection to the DNS server * * @return boolean * @access public * */ public function open() { // // create a list of options for the context // $opts = array('socket' => array()); // // bind to a local IP/port if it's set // if (strlen($this->local_host) > 0) { $opts['socket']['bindto'] = $this->local_host; if ($this->local_port > 0) { $opts['socket']['bindto'] .= ':' . $this->local_port; } } // // create the context // $this->_context = @stream_context_create($opts); // // create socket // $errno; $errstr; switch ($this->type) { case Net_DNS2_Socket::SOCK_STREAM: if (Net_DNS2::isIPv4($this->host) == true) { $this->sock = @stream_socket_client('tcp://' . $this->host . ':' . $this->port, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $this->_context); } else { if (Net_DNS2::isIPv6($this->host) == true) { $this->sock = @stream_socket_client('tcp://[' . $this->host . ']:' . $this->port, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $this->_context); } else { $this->last_error = 'invalid address type: ' . $this->host; return false; } } break; case Net_DNS2_Socket::SOCK_DGRAM: if (Net_DNS2::isIPv4($this->host) == true) { $this->sock = @stream_socket_client('udp://' . $this->host . ':' . $this->port, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $this->_context); } else { if (Net_DNS2::isIPv6($this->host) == true) { $this->sock = @stream_socket_client('udp://[' . $this->host . ']:' . $this->port, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $this->_context); } else { $this->last_error = 'invalid address type: ' . $this->host; return false; } } break; default: $this->last_error = 'Invalid socket type: ' . $this->type; return false; } if ($this->sock === false) { $this->last_error = $errstr; return false; } // // set it to non-blocking and set the timeout // @stream_set_blocking($this->sock, 0); @stream_set_timeout($this->sock, $this->timeout); return true; }
/** * builds a new Net_DNS2_Packet_Request object * * @param string $name the domain name for the packet * @param string $type the DNS RR type for the packet * @param string $class the DNS class for the packet * * @return boolean * @throws Net_DNS2_Exception * @access public * */ public function set($name, $type = 'A', $class = 'IN') { // // generate a new header // $this->header = new Net_DNS2_Header(); // // add a new question // $q = new Net_DNS2_Question(); // // allow queries directly to . for the root name servers // if ($name != '.') { $name = trim(strtolower($name), " \t\n\r\v."); } $type = strtoupper(trim($type)); $class = strtoupper(trim($class)); // // check that the input string has some data in it // if (empty($name)) { throw new Net_DNS2_Exception('empty query string provided', Net_DNS2_Lookups::E_PACKET_INVALID); } // // if the type is "*", rename it to "ANY"- both are acceptable. // if ($type == '*') { $type = 'ANY'; } // // check that the type and class are valid // if (!isset(Net_DNS2_Lookups::$rr_types_by_name[$type]) || !isset(Net_DNS2_Lookups::$classes_by_name[$class])) { throw new Net_DNS2_Exception('invalid type (' . $type . ') or class (' . $class . ') specified.', Net_DNS2_Lookups::E_PACKET_INVALID); } if ($type == 'PTR') { // // if it's a PTR request for an IP address, then make sure we tack on // the arpa domain. // // there are other types of PTR requests, so if an IP adress doesn't match, // then just let it flow through and assume it's a hostname // if (Net_DNS2::isIPv4($name) == true) { // // IPv4 // $name = implode('.', array_reverse(explode('.', $name))); $name .= '.in-addr.arpa'; } else { if (Net_DNS2::isIPv6($name) == true) { // // IPv6 // $e = Net_DNS2::expandIPv6($name); if ($e !== false) { $name = implode('.', array_reverse(str_split(str_replace(':', '', $e)))); $name .= '.ip6.arpa'; } else { throw new Net_DNS2_Exception('unsupported PTR value: ' . $name, Net_DNS2_Lookups::E_PACKET_INVALID); } } } } // // store the data // $q->qname = $name; $q->qtype = $type; $q->qclass = $class; $this->question[] = $q; // // the answer, authority and additional are empty; they can be modified // after the request is created for UPDATE requests if needed. // $this->answer = array(); $this->authority = array(); $this->additional = array(); return true; }
/** * parses the rdata portion from a standard DNS config line * * @param array $rdata a string split line of values for the rdata * * @return boolean * @access protected * */ protected function rrFromString(array $rdata) { $value = array_shift($rdata); if (Net_DNS2::isIPv4($value) == true) { $this->address = $value; return true; } return false; }