PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Net/DNS2/Resolver.php

http://netdns2.googlecode.com/
PHP | 234 lines | 60 code | 27 blank | 147 comment | 15 complexity | 2b2aa7c7c2bf3f961faa19add3b80d9a MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * DNS Library for handling lookups and updates.
  5. *
  6. * PHP Version 5
  7. *
  8. * Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * * Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in
  20. * the documentation and/or other materials provided with the
  21. * distribution.
  22. *
  23. * * Neither the name of Mike Pultz nor the names of his contributors
  24. * may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * @category Networking
  41. * @package Net_DNS2
  42. * @author Mike Pultz <mike@mikepultz.com>
  43. * @copyright 2010 Mike Pultz <mike@mikepultz.com>
  44. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  45. * @version SVN: $Id: Resolver.php 137 2011-12-05 02:51:34Z mike.pultz $
  46. * @link http://pear.php.net/package/Net_DNS2
  47. * @since File available since Release 0.6.0
  48. *
  49. */
  50. /**
  51. * This is the main resolver class, providing DNS query functions.
  52. *
  53. * @category Networking
  54. * @package Net_DNS2
  55. * @author Mike Pultz <mike@mikepultz.com>
  56. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  57. * @link http://pear.php.net/package/Net_DNS2
  58. * @see Net_DNS2
  59. *
  60. */
  61. class Net_DNS2_Resolver extends Net_DNS2
  62. {
  63. /**
  64. * Constructor - creates a new Net_DNS2_Resolver object
  65. *
  66. * @param mixed $options either an array with options or null
  67. *
  68. * @access public
  69. *
  70. */
  71. public function __construct(array $options = null)
  72. {
  73. parent::__construct($options);
  74. }
  75. /**
  76. * does a basic DNS lookup query
  77. *
  78. * @param string $name the DNS name to loookup
  79. * @param string $type the name of the RR type to lookup
  80. * @param string $class the name of the RR class to lookup
  81. *
  82. * @return Net_DNS_RR object
  83. * @throws Net_DNS2_Exception
  84. * @access public
  85. *
  86. */
  87. public function query($name, $type = 'A', $class = 'IN')
  88. {
  89. //
  90. // make sure we have some name servers set
  91. //
  92. $this->checkServers(Net_DNS2::RESOLV_CONF);
  93. //
  94. // we dont' support incremental zone tranfers; so if it's requested, a full
  95. // zone transfer can be returned
  96. //
  97. if ($type == 'IXFR') {
  98. $type = 'AXFR';
  99. }
  100. //
  101. // if the name *looks* too short, then append the domain from the config
  102. //
  103. if ( (strpos($name, '.') === false) && ($type != 'PTR') ) {
  104. $name .= '.' . strtolower($this->domain);
  105. }
  106. //
  107. // create a new packet based on the input
  108. //
  109. $packet = new Net_DNS2_Packet_Request($name, $type, $class);
  110. //
  111. // check for an authentication method; either TSIG or SIG
  112. //
  113. if ( ($this->auth_signature instanceof Net_DNS2_RR_TSIG)
  114. || ($this->auth_signature instanceof Net_DNS2_RR_SIG)
  115. ) {
  116. $packet->additional[] = $this->auth_signature;
  117. $packet->header->arcount = count($packet->additional);
  118. }
  119. //
  120. // if caching is turned on, then check then hash the question, and
  121. // do a cache lookup.
  122. //
  123. $packet_hash = '';
  124. if ($this->use_cache == true) {
  125. //
  126. // open the cache
  127. //
  128. $this->cache->open($this->cache_file, $this->cache_size, $this->cache_serializer);
  129. //
  130. // build the key and check for it in the cache.
  131. //
  132. $packet_hash = md5(
  133. $packet->question[0]->qname . '|' . $packet->question[0]->qtype
  134. );
  135. if ($this->cache->has($packet_hash)) {
  136. return $this->cache->get($packet_hash);
  137. }
  138. }
  139. //
  140. // send the packet and get back the response
  141. //
  142. $response = $this->sendPacket(
  143. $packet, ($type == 'AXFR') ? true : $this->use_tcp
  144. );
  145. if ($this->use_cache == true) {
  146. $this->cache->put($packet_hash, $response);
  147. }
  148. return $response;
  149. }
  150. /**
  151. * does an inverse query for the given RR; most DNS servers do not implement
  152. * inverse queries, but they should be able to return "not implemented"
  153. *
  154. * @param Net_DNS2_RR $rr the RR object to lookup
  155. *
  156. * @return Net_DNS_RR object
  157. * @throws Net_DNS2_Exception
  158. * @access public
  159. *
  160. */
  161. public function iquery(Net_DNS2_RR $rr)
  162. {
  163. //
  164. // make sure we have some name servers set
  165. //
  166. $this->checkServers(Net_DNS2::RESOLV_CONF);
  167. //
  168. // create an empty packet
  169. //
  170. $packet = new Net_DNS2_Packet_Request($rr->name, 'A', 'IN');
  171. //
  172. // unset the question
  173. //
  174. $packet->question = array();
  175. $packet->header->qdcount = 0;
  176. //
  177. // set the opcode to IQUERY
  178. //
  179. $packet->header->opcode = Net_DNS2_Lookups::OPCODE_IQUERY;
  180. //
  181. // add the given RR as the answer
  182. //
  183. $packet->answer[] = $rr;
  184. $packet->header->ancount = 1;
  185. //
  186. // check for an authentication method; either TSIG or SIG
  187. //
  188. if ( ($this->auth_signature instanceof Net_DNS2_RR_TSIG)
  189. || ($this->auth_signature instanceof Net_DNS2_RR_SIG)
  190. ) {
  191. $packet->additional[] = $this->auth_signature;
  192. $packet->header->arcount = count($packet->additional);
  193. }
  194. //
  195. // send the packet and get back the response
  196. //
  197. return $this->sendPacket($packet, $this->use_tcp);
  198. }
  199. }
  200. /*
  201. * Local variables:
  202. * tab-width: 4
  203. * c-basic-offset: 4
  204. * c-hanging-comment-ender-p: nil
  205. * End:
  206. */
  207. ?>