Header.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /*
  3. * License Information:
  4. *
  5. * Net_DNS: A resolver library for PHP
  6. * Copyright (c) 2002-2003 Eric Kilfoil eric@ypass.net
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /* Net_DNS_Header object definition {{{ */
  23. /**
  24. * Object representation of the HEADER section of a DNS packet
  25. *
  26. * The Net_DNS::Header class contains the values of a DNS packet. It parses
  27. * the header of a DNS packet or can generate the binary data
  28. * representation of the packet. The format of the header is described in
  29. * RFC1035.
  30. *
  31. * @package Net_DNS
  32. */
  33. class Net_DNS_Header
  34. {
  35. /* class variable definitions {{{ */
  36. /**
  37. * The packet's request id
  38. *
  39. * The request id of the packet represented as a 16 bit integer.
  40. */
  41. var $id;
  42. /**
  43. * The QR bit in a DNS packet header
  44. *
  45. * The QR bit as described in RFC1035. QR is set to 0 for queries, and
  46. * 1 for repsones.
  47. */
  48. var $qr;
  49. /**
  50. * The OPCODE name of this packet.
  51. *
  52. * The string value (name) of the opcode for the DNS packet.
  53. */
  54. var $opcode;
  55. /**
  56. * The AA (authoritative answer) bit in a DNS packet header
  57. *
  58. * The AA bit as described in RFC1035. AA is set to 1 if the answer
  59. * is authoritative. It has no meaning if QR is set to 0.
  60. */
  61. var $aa;
  62. /**
  63. * The TC (truncated) bit in a DNS packet header
  64. *
  65. * This flag is set to 1 if the response was truncated. This flag has
  66. * no meaning in a query packet.
  67. */
  68. var $tc;
  69. /**
  70. * The RD (recursion desired) bit in a DNS packet header
  71. *
  72. * This bit should be set to 1 in a query if recursion is desired by
  73. * the DNS server.
  74. */
  75. var $rd;
  76. /**
  77. * The RA (recursion available) bit in a DNS packet header
  78. *
  79. * This bit is set to 1 by the DNS server if the server is willing to
  80. * perform recursion.
  81. */
  82. var $ra;
  83. /**
  84. * The RCODE name for this packet.
  85. *
  86. * The string value (name) of the rcode for the DNS packet.
  87. */
  88. var $rcode;
  89. /**
  90. * Number of questions contained within the packet
  91. *
  92. * 16bit integer representing the number of questions in the question
  93. * section of the DNS packet.
  94. *
  95. * @var integer $qdcount
  96. * @see Net_DNS_Question class
  97. */
  98. var $qdcount;
  99. /**
  100. * Number of answer RRs contained within the packet
  101. *
  102. * 16bit integer representing the number of answer resource records
  103. * contained in the answer section of the DNS packet.
  104. *
  105. * @var integer $ancount
  106. * @see Net_DNS_RR class
  107. */
  108. var $ancount;
  109. /**
  110. * Number of authority RRs within the packet
  111. *
  112. * 16bit integer representing the number of authority (NS) resource
  113. * records contained in the authority section of the DNS packet.
  114. *
  115. * @var integer $nscount
  116. * @see Net_DNS_RR class
  117. */
  118. var $nscount;
  119. /**
  120. * Number of additional RRs within the packet
  121. *
  122. * 16bit integer representing the number of additional resource records
  123. * contained in the additional section of the DNS packet.
  124. *
  125. * @var integer $arcount
  126. * @see Net_DNS_RR class
  127. */
  128. var $arcount;
  129. /* }}} */
  130. /* class constructor - Net_DNS_Header($data = "") {{{ */
  131. /**
  132. * Initializes the default values for the Header object.
  133. *
  134. * Builds a header object from either default values, or from a DNS
  135. * packet passed into the constructor as $data
  136. *
  137. * @param string $data A DNS packet of which the header will be parsed.
  138. * @return object Net_DNS_Header
  139. * @access public
  140. */
  141. function Net_DNS_Header($data = '')
  142. {
  143. if ($data != '') {
  144. /*
  145. * The header MUST be at least 12 bytes.
  146. * Passing the full datagram to this constructor
  147. * will examine only the header section of the DNS packet
  148. */
  149. if (strlen($data) < 12)
  150. return(0);
  151. $a = unpack('nid/C2flags/n4counts', $data);
  152. $this->id = $a['id'];
  153. $this->qr = ($a['flags1'] >> 7) & 0x1;
  154. $this->opcode = ($a['flags1'] >> 3) & 0xf;
  155. $this->aa = ($a['flags1'] >> 2) & 0x1;
  156. $this->tc = ($a['flags1'] >> 1) & 0x1;
  157. $this->rd = $a['flags1'] & 0x1;
  158. $this->ra = ($a['flags2'] >> 7) & 0x1;
  159. $this->rcode = $a['flags2'] & 0xf;
  160. $this->qdcount = $a['counts1'];
  161. $this->ancount = $a['counts2'];
  162. $this->nscount = $a['counts3'];
  163. $this->arcount = $a['counts4'];
  164. }
  165. else {
  166. $this->id = Net_DNS_Resolver::nextid();
  167. $this->qr = 0;
  168. $this->opcode = 0;
  169. $this->aa = 0;
  170. $this->tc = 0;
  171. $this->rd = 1;
  172. $this->ra = 0;
  173. $this->rcode = 0;
  174. $this->qdcount = 1;
  175. $this->ancount = 0;
  176. $this->nscount = 0;
  177. $this->arcount = 0;
  178. }
  179. if (Net_DNS::opcodesbyval($this->opcode)) {
  180. $this->opcode = Net_DNS::opcodesbyval($this->opcode);
  181. }
  182. if (Net_DNS::rcodesbyval($this->rcode)) {
  183. $this->rcode = Net_DNS::rcodesbyval($this->rcode);
  184. }
  185. }
  186. /* }}} */
  187. /* Net_DNS_Header::display() {{{ */
  188. /**
  189. * Displays the properties of the header.
  190. *
  191. * Displays the properties of the header.
  192. *
  193. * @access public
  194. */
  195. function display()
  196. {
  197. echo $this->string();
  198. }
  199. /* }}} */
  200. /* Net_DNS_Header::string() {{{ */
  201. /**
  202. * Returns a formatted string containing the properties of the header.
  203. *
  204. * @return string a formatted string containing the properties of the header.
  205. * @access public
  206. */
  207. function string()
  208. {
  209. $retval = ';; id = ' . $this->id . "\n";
  210. if ($this->opcode == 'UPDATE') {
  211. $retval .= ';; qr = ' . $this->qr . ' ' .
  212. 'opcode = ' . $this->opcode . ' ' .
  213. 'rcode = ' . $this->rcode . "\n";
  214. $retval .= ';; zocount = ' . $this->qdcount . ' ' .
  215. 'prcount = ' . $this->ancount . ' ' .
  216. 'upcount = ' . $this->nscount . ' ' .
  217. 'adcount = ' . $this->arcount . "\n";
  218. } else {
  219. $retval .= ';; qr = ' . $this->qr . ' ' .
  220. 'opcode = ' . $this->opcode . ' ' .
  221. 'aa = ' . $this->aa . ' ' .
  222. 'tc = ' . $this->tc . ' ' .
  223. 'rd = ' . $this->rd . "\n";
  224. $retval .= ';; ra = ' . $this->ra . ' ' .
  225. 'rcode = ' . $this->rcode . "\n";
  226. $retval .= ';; qdcount = ' . $this->qdcount . ' ' .
  227. 'ancount = ' . $this->ancount . ' ' .
  228. 'nscount = ' . $this->nscount . ' ' .
  229. 'arcount = ' . $this->arcount . "\n";
  230. }
  231. return($retval);
  232. }
  233. /* }}} */
  234. /* Net_DNS_Header::data() {{{ */
  235. /**
  236. * Returns the binary data containing the properties of the header
  237. *
  238. * Packs the properties of the Header object into a binary string
  239. * suitable for using as the Header section of a DNS packet.
  240. *
  241. * @return string binary representation of the header object
  242. * @access public
  243. */
  244. function data()
  245. {
  246. $opcode = Net_DNS::opcodesbyname($this->opcode);
  247. $rcode = Net_DNS::rcodesbyname($this->rcode);
  248. $byte2 = ($this->qr << 7)
  249. | ($opcode << 3)
  250. | ($this->aa << 2)
  251. | ($this->tc << 1)
  252. | ($this->rd);
  253. $byte3 = ($this->ra << 7) | $rcode;
  254. return pack('nC2n4', $this->id,
  255. $byte2,
  256. $byte3,
  257. $this->qdcount,
  258. $this->ancount,
  259. $this->nscount,
  260. $this->arcount);
  261. }
  262. /* }}} */
  263. }
  264. /* }}} */
  265. /* VIM settings {{{
  266. * Local variables:
  267. * tab-width: 4
  268. * c-basic-offset: 4
  269. * soft-stop-width: 4
  270. * c indent on
  271. * expandtab on
  272. * End:
  273. * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
  274. * vim<600: sw=4 ts=4
  275. * }}} */
  276. ?>