PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/includes/net/dns2/header.php

https://bitbucket.org/speedealing/speedealing
PHP | 271 lines | 110 code | 25 blank | 136 comment | 4 complexity | 44ea48cef0672348abc67ba19314e7f5 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: Header.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. * DNS Packet Header class
  52. *
  53. * This class handles parsing and constructing DNS Packet Headers as defined
  54. * by section 4.1.1 of RFC1035.
  55. *
  56. * DNS header format - RFC1035 section 4.1.1
  57. *
  58. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
  59. * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  60. * | ID |
  61. * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  62. * |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
  63. * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  64. * | QDCOUNT |
  65. * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  66. * | ANCOUNT |
  67. * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  68. * | NSCOUNT |
  69. * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  70. * | ARCOUNT |
  71. * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  72. *
  73. * @category Networking
  74. * @package Net_DNS2
  75. * @author Mike Pultz <mike@mikepultz.com>
  76. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  77. * @link http://pear.php.net/package/Net_DNS2
  78. *
  79. */
  80. class Net_DNS2_Header
  81. {
  82. public $id; // 16 bit - identifier
  83. public $qr; // 1 bit - 0 = query, 1 = response
  84. public $opcode; // 4 bit - op code
  85. public $aa; // 1 bit - Authoritative Answer
  86. public $tc; // 1 bit - TrunCation
  87. public $rd; // 1 bit - Recursion Desired
  88. public $ra; // 1 bit - Recursion Available
  89. public $z; // 3 bit - Reserved
  90. public $rcode; // 4 bit - Response code
  91. public $qdcount; // 16 bit - entries in the question section
  92. public $ancount; // 16 bit - resource records in the answer section
  93. public $nscount; // 16 bit - name server rr in the authority records section
  94. public $arcount; // 16 bit - rr's in the additional records section
  95. /**
  96. * Constructor - builds a new Net_DNS2_Header object
  97. *
  98. * @param mixed &$packet either a Net_DNS2_Packet object or null
  99. *
  100. * @throws Net_DNS2_Exception
  101. * @access public
  102. *
  103. */
  104. public function __construct(Net_DNS2_Packet &$packet = null)
  105. {
  106. if (!is_null($packet)) {
  107. $this->set($packet);
  108. } else {
  109. $this->id = $this->nextPacketId();
  110. $this->qr = Net_DNS2_Lookups::QR_QUERY;
  111. $this->opcode = Net_DNS2_Lookups::OPCODE_QUERY;
  112. $this->aa = 0;
  113. $this->tc = 0;
  114. $this->rd = 1;
  115. $this->ra = 0;
  116. $this->z = 0;
  117. $this->rcode = Net_DNS2_Lookups::RCODE_NOERROR;
  118. $this->qdcount = 1;
  119. $this->ancount = 0;
  120. $this->nscount = 0;
  121. $this->arcount = 0;
  122. }
  123. }
  124. /**
  125. * returns the next available packet id
  126. *
  127. * @return integer
  128. * @access public
  129. *
  130. */
  131. public function nextPacketId()
  132. {
  133. if (++Net_DNS2_Lookups::$next_packet_id > 65535) {
  134. Net_DNS2_Lookups::$next_packet_id = 1;
  135. }
  136. return Net_DNS2_Lookups::$next_packet_id;
  137. }
  138. /**
  139. * magic __toString() method to return the header as a string
  140. *
  141. * @return string
  142. * @access public
  143. *
  144. */
  145. public function __toString()
  146. {
  147. $output = ";;\n;; Header:\n";
  148. $output .= ";;\t id = " . $this->id . "\n";
  149. $output .= ";;\t qr = " . $this->qr . "\n";
  150. $output .= ";;\t opcode = " . $this->opcode . "\n";
  151. $output .= ";;\t aa = " . $this->aa . "\n";
  152. $output .= ";;\t tc = " . $this->tc . "\n";
  153. $output .= ";;\t rd = " . $this->rd . "\n";
  154. $output .= ";;\t ra = " . $this->ra . "\n";
  155. $output .= ";;\t z = " . $this->z . "\n";
  156. $output .= ";;\t rcode = " . $this->rcode . "\n";
  157. $output .= ";;\t qdcount = " . $this->qdcount . "\n";
  158. $output .= ";;\t ancount = " . $this->ancount . "\n";
  159. $output .= ";;\t nscount = " . $this->nscount . "\n";
  160. $output .= ";;\t arcount = " . $this->arcount . "\n";
  161. return $output;
  162. }
  163. /**
  164. * constructs a Net_DNS2_Header from a Net_DNS2_Packet object
  165. *
  166. * @param Net_DNS2_Packet &$packet Object
  167. *
  168. * @return boolean
  169. * @throws Net_DNS2_Exception
  170. * @access public
  171. *
  172. */
  173. public function set(Net_DNS2_Packet &$packet)
  174. {
  175. //
  176. // the header must be at least 12 bytes long.
  177. //
  178. if ($packet->rdlength < Net_DNS2_Lookups::DNS_HEADER_SIZE) {
  179. throw new Net_DNS2_Exception(
  180. 'invalid header data provided; to small',
  181. Net_DNS2_Lookups::E_HEADER_INVALID
  182. );
  183. }
  184. $offset = 0;
  185. //
  186. // parse the values
  187. //
  188. $this->id = ord($packet->rdata[$offset]) << 8 |
  189. ord($packet->rdata[++$offset]);
  190. ++$offset;
  191. $this->qr = (ord($packet->rdata[$offset]) >> 7) & 0x1;
  192. $this->opcode = (ord($packet->rdata[$offset]) >> 3) & 0xf;
  193. $this->aa = (ord($packet->rdata[$offset]) >> 2) & 0x1;
  194. $this->tc = (ord($packet->rdata[$offset]) >> 1) & 0x1;
  195. $this->rd = ord($packet->rdata[$offset]) & 0x1;
  196. ++$offset;
  197. $this->ra = (ord($packet->rdata[$offset]) >> 7) & 0x1;
  198. $this->z = 0;
  199. $this->rcode = ord($packet->rdata[$offset]) & 0xf;
  200. $this->qdcount = ord($packet->rdata[++$offset]) << 8 |
  201. ord($packet->rdata[++$offset]);
  202. $this->ancount = ord($packet->rdata[++$offset]) << 8 |
  203. ord($packet->rdata[++$offset]);
  204. $this->nscount = ord($packet->rdata[++$offset]) << 8 |
  205. ord($packet->rdata[++$offset]);
  206. $this->arcount = ord($packet->rdata[++$offset]) << 8 |
  207. ord($packet->rdata[++$offset]);
  208. //
  209. // increment the internal offset
  210. //
  211. $packet->offset += Net_DNS2_Lookups::DNS_HEADER_SIZE;
  212. return true;
  213. }
  214. /**
  215. * returns a binary packed DNS Header
  216. *
  217. * @param Net_DNS2_Packet &$packet Object
  218. *
  219. * @return string
  220. * @access public
  221. *
  222. */
  223. public function get(Net_DNS2_Packet &$packet)
  224. {
  225. $data = pack('n', $this->id) .
  226. chr(
  227. ($this->qr << 7) | ($this->opcode << 3) |
  228. ($this->aa << 2) | ($this->tc << 1) | ($this->rd)
  229. ) .
  230. chr(($this->ra << 7) | $this->rcode) .
  231. chr($this->qdcount << 8) . chr($this->qdcount) .
  232. chr($this->ancount << 8) . chr($this->ancount) .
  233. chr($this->nscount << 8) . chr($this->nscount) .
  234. chr($this->arcount << 8) . chr($this->arcount);
  235. $packet->offset += Net_DNS2_Lookups::DNS_HEADER_SIZE;
  236. return $data;
  237. }
  238. }
  239. /*
  240. * Local variables:
  241. * tab-width: 4
  242. * c-basic-offset: 4
  243. * c-hanging-comment-ender-p: nil
  244. * End:
  245. */
  246. ?>