PageRenderTime 52ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/includes/net/dns2/question.php

https://bitbucket.org/speedealing/speedealing
PHP | 244 lines | 65 code | 23 blank | 156 comment | 7 complexity | 1d88fe97757f44906c8e6d20b44d638f MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  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: Question.php 124 2011-12-02 23:23:15Z 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 class handles parsing and constructing the question sectino of DNS
  52. * packets.
  53. *
  54. * This is referred to as the "zone" for update per RFC2136
  55. *
  56. * DNS question format - RFC1035 section 4.1.2
  57. *
  58. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
  59. * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  60. * | |
  61. * / QNAME /
  62. * / /
  63. * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  64. * | QTYPE |
  65. * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  66. * | QCLASS |
  67. * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  68. *
  69. * @category Networking
  70. * @package Net_DNS2
  71. * @author Mike Pultz <mike@mikepultz.com>
  72. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  73. * @link http://pear.php.net/package/Net_DNS2
  74. * @see Net_DNS2_Packet
  75. *
  76. */
  77. class Net_DNS2_Question
  78. {
  79. /*
  80. * The name of the question
  81. *
  82. * referred to as "zname" for updates per RFC2136
  83. *
  84. */
  85. public $qname;
  86. /*
  87. * The RR type for the questino
  88. *
  89. * referred to as "ztype" for updates per RFC2136
  90. *
  91. */
  92. public $qtype;
  93. /*
  94. * The RR class for the questino
  95. *
  96. * referred to as "zclass" for updates per RFC2136
  97. *
  98. */
  99. public $qclass;
  100. /**
  101. * Constructor - builds a new Net_DNS2_Question object
  102. *
  103. * @param mixed &$packet either a Net_DNS2_Packet object, or null to
  104. * build an empty object
  105. *
  106. * @throws Net_DNS2_Exception
  107. * @access public
  108. *
  109. */
  110. public function __construct(Net_DNS2_Packet &$packet = null)
  111. {
  112. if (!is_null($packet)) {
  113. $this->set($packet);
  114. } else {
  115. $this->qname = '';
  116. $this->qtype = 'A';
  117. $this->qclass = 'IN';
  118. }
  119. }
  120. /**
  121. * magic __toString() function to return the Net_DNS2_Question object as a string
  122. *
  123. * @return string
  124. * @access public
  125. *
  126. */
  127. public function __toString()
  128. {
  129. return ";;\n;; Question:\n;;\t " . $this->qname . '. ' .
  130. $this->qtype . ' ' . $this->qclass . "\n";
  131. }
  132. /**
  133. * builds a new Net_DNS2_Header object from a Net_DNS2_Packet object
  134. *
  135. * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet object
  136. *
  137. * @return boolean
  138. * @throws Net_DNS2_Exception
  139. * @access public
  140. *
  141. */
  142. public function set(Net_DNS2_Packet &$packet)
  143. {
  144. //
  145. // expand the name
  146. //
  147. $this->qname = $packet->expand($packet, $packet->offset);
  148. if ($packet->rdlength < ($packet->offset + 4)) {
  149. throw new Net_DNS2_Exception(
  150. 'invalid question section: to small',
  151. Net_DNS2_Lookups::E_QUESTION_INVALID
  152. );
  153. }
  154. //
  155. // unpack the type and class
  156. //
  157. $type = ord($packet->rdata[$packet->offset++]) << 8 |
  158. ord($packet->rdata[$packet->offset++]);
  159. $class = ord($packet->rdata[$packet->offset++]) << 8 |
  160. ord($packet->rdata[$packet->offset++]);
  161. //
  162. // validate it
  163. //
  164. $type_name = Net_DNS2_Lookups::$rr_types_by_id[$type];
  165. $class_name = Net_DNS2_Lookups::$classes_by_id[$class];
  166. if ( (!isset($type_name)) || (!isset($class_name)) ) {
  167. throw new Net_DNS2_Exception(
  168. 'invalid question section: invalid type (' . $type .
  169. ') or class (' . $class . ') specified.',
  170. Net_DNS2_Lookups::E_QUESTION_INVALID
  171. );
  172. }
  173. //
  174. // store it
  175. //
  176. $this->qtype = $type_name;
  177. $this->qclass = $class_name;
  178. return true;
  179. }
  180. /**
  181. * returns a binary packed Net_DNS2_Question object
  182. *
  183. * @param Net_DNS2_Packet &$packet the Net_DNS2_Packet object this question is
  184. * part of. This needs to be passed in so that
  185. * the compressed qname value can be packed in
  186. * with the names of the other parts of the
  187. * packet.
  188. *
  189. * @return string
  190. * @throws Net_DNS2_Exception
  191. * @access public
  192. *
  193. */
  194. public function get(Net_DNS2_Packet &$packet)
  195. {
  196. //
  197. // validate the type and class
  198. //
  199. $type = Net_DNS2_Lookups::$rr_types_by_name[$this->qtype];
  200. $class = Net_DNS2_Lookups::$classes_by_name[$this->qclass];
  201. if ( (!isset($type)) || (!isset($class)) ) {
  202. throw new Net_DNS2_Exception(
  203. 'invalid question section: invalid type (' . $this->qtype .
  204. ') or class (' . $this->qclass . ') specified.',
  205. Net_DNS2_Lookups::E_QUESTION_INVALID
  206. );
  207. }
  208. $data = $packet->compress($this->qname, $packet->offset);
  209. $data .= chr($type << 8) . chr($type) . chr($class << 8) . chr($class);
  210. $packet->offset += 4;
  211. return $data;
  212. }
  213. }
  214. /*
  215. * Local variables:
  216. * tab-width: 4
  217. * c-basic-offset: 4
  218. * c-hanging-comment-ender-p: nil
  219. * End:
  220. */
  221. ?>