PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/app/lib/Net_DNS2-1.3.2/Net/DNS2/Header.php

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