RR.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. /* Include files {{{ */
  23. require_once("$phpdns_basedir/DNS/RR/A.php");
  24. require_once("$phpdns_basedir/DNS/RR/AAAA.php");
  25. require_once("$phpdns_basedir/DNS/RR/NS.php");
  26. require_once("$phpdns_basedir/DNS/RR/CNAME.php");
  27. require_once("$phpdns_basedir/DNS/RR/PTR.php");
  28. require_once("$phpdns_basedir/DNS/RR/SOA.php");
  29. require_once("$phpdns_basedir/DNS/RR/MX.php");
  30. require_once("$phpdns_basedir/DNS/RR/TSIG.php");
  31. require_once("$phpdns_basedir/DNS/RR/TXT.php");
  32. require_once("$phpdns_basedir/DNS/RR/HINFO.php");
  33. require_once("$phpdns_basedir/DNS/RR/SRV.php");
  34. require_once("$phpdns_basedir/DNS/RR/NAPTR.php");
  35. /* }}} */
  36. /* Net_DNS_RR object definition {{{ */
  37. /**
  38. * Resource Record object definition
  39. *
  40. * Builds or parses resource record sections of the DNS packet including
  41. * the answer, authority, and additional sections of the packet.
  42. *
  43. * @package Net_DNS
  44. */
  45. class Net_DNS_RR
  46. {
  47. /* class variable definitions {{{ */
  48. var $name;
  49. var $type;
  50. var $class;
  51. var $ttl;
  52. var $rdlength;
  53. var $rdata;
  54. /* }}} */
  55. /*
  56. * I finally did it... i pass an array to the function
  57. * instead of a parameter list... UGH... i hate perl...
  58. */
  59. /* class constructor - Net_DNS_RR($rrdata) {{{ */
  60. function Net_DNS_RR($rrdata)
  61. {
  62. if (is_string($rrdata)) {
  63. $this = $this->new_from_string($rrdata);
  64. } else if (count($rrdata) == 7) {
  65. list ($name, $rrtype, $rrclass, $ttl, $rdlength, $data, $offset) = $rrdata;
  66. $this = $this->new_from_data($name, $rrtype, $rrclass, $ttl, $rdlength, $data, $offset);
  67. } else {
  68. $this = $this->new_from_array($rrdata);
  69. }
  70. }
  71. /* }}} */
  72. /* Net_DNS_RR::new_from_data($name, $ttl, $rrtype, $rrclass, $rdlength, $data, $offset) {{{ */
  73. function new_from_data($name, $rrtype, $rrclass, $ttl, $rdlength, $data, $offset)
  74. {
  75. $this->name = $name;
  76. $this->type = $rrtype;
  77. $this->class = $rrclass;
  78. $this->ttl = $ttl;
  79. $this->rdlength = $rdlength;
  80. $this->rdata = substr($data, $offset, $rdlength);
  81. if (class_exists('Net_DNS_RR_' . $rrtype)) {
  82. $scn = 'Net_DNS_RR_' . $rrtype;
  83. $subclass = new $scn($this, $data, $offset);
  84. return($subclass);
  85. } else {
  86. return($this);
  87. }
  88. }
  89. /* }}} */
  90. /* Net_DNS_RR::new_from_string($rrstring, $update_type = '') {{{ */
  91. function new_from_string($rrstring, $update_type = '')
  92. {
  93. $ttl = 0;
  94. $parts = preg_split('/[\s]+/', $rrstring);
  95. while ($s = array_shift($parts)) {
  96. if (!isset($name)) {
  97. $name = ereg_replace('\.+$', '', $s);
  98. } else if (preg_match('/^\d+$/', $s)) {
  99. $ttl = $s;
  100. } else if (!isset($rrclass) && ! is_null(Net_DNS::classesbyname(strtoupper($s)))) {
  101. $rrclass = strtoupper($s);
  102. $rdata = join(' ', $parts);
  103. } else if (! is_null(Net_DNS::typesbyname(strtoupper($s)))) {
  104. $rrtype = strtoupper($s);
  105. $rdata = join(' ', $parts);
  106. break;
  107. } else {
  108. break;
  109. }
  110. }
  111. /*
  112. * Do we need to do this?
  113. */
  114. $rdata = trim(chop($rdata));
  115. if (! strlen($rrtype) && strlen($rrclass) && $rrclass == 'ANY') {
  116. $rrtype = $rrclass;
  117. $rrclass = 'IN';
  118. } else if (! isset($rrclass)) {
  119. $rrclass = 'IN';
  120. }
  121. if (! strlen($rrtype)) {
  122. $rrtype = 'ANY';
  123. }
  124. if (strlen($update_type)) {
  125. $update_type = strtolower($update_type);
  126. if ($update_type == 'yxrrset') {
  127. $ttl = 0;
  128. if (! strlen($rdata)) {
  129. $rrclass = 'ANY';
  130. }
  131. } else if ($update_type == 'nxrrset') {
  132. $ttl = 0;
  133. $rrclass = 'NONE';
  134. $rdata = '';
  135. } else if ($update_type == 'yxdomain') {
  136. $ttl = 0;
  137. $rrclass = 'ANY';
  138. $rrtype = 'ANY';
  139. $rdata = '';
  140. } else if ($update_type == 'nxdomain') {
  141. $ttl = 0;
  142. $rrclass = 'NONE';
  143. $rrtype = 'ANY';
  144. $rdata = '';
  145. } else if (preg_match('/^(rr_)?add$/', $update_type)) {
  146. $update_type = 'add';
  147. if (! $ttl) {
  148. $ttl = 86400;
  149. }
  150. } else if (preg_match('/^(rr_)?del(ete)?$/', $update_type)) {
  151. $update_type = 'del';
  152. $ttl = 0;
  153. $rrclass = $rdata ? 'NONE' : 'ANY';
  154. }
  155. }
  156. if (strlen($rrtype)) {
  157. $this->name = $name;
  158. $this->type = $rrtype;
  159. $this->class = $rrclass;
  160. $this->ttl = $ttl;
  161. $this->rdlength = 0;
  162. $this->rdata = '';
  163. if (class_exists('Net_DNS_RR_' . $rrtype)) {
  164. $scn = 'Net_DNS_RR_' . $rrtype;
  165. $rc = new $scn($this, $rdata);
  166. return($rc);
  167. } else {
  168. return($this);
  169. }
  170. } else {
  171. return(NULL);
  172. }
  173. }
  174. /* }}} */
  175. /* Net_DNS_RR::new_from_array($rrarray) {{{ */
  176. function new_from_array($rrarray)
  177. {
  178. foreach ($rrarray as $k => $v) {
  179. $this->{strtolower($k)} = $v;
  180. }
  181. if (! strlen($this->name)) {
  182. return(NULL);
  183. }
  184. if (! strlen($this->type)){
  185. return(NULL);
  186. }
  187. if (! $this->ttl) {
  188. $this->ttl = 0;
  189. }
  190. if (! strlen($this->class)) {
  191. $this->class = 'IN';
  192. }
  193. if (strlen($this->rdata)) {
  194. $this->rdlength = strlen($rdata);
  195. }
  196. if (class_exists('Net_DNS_RR_' . $rrtype)) {
  197. $scn = 'Net_DNS_RR_' . $rrtype;
  198. $rc = new $scn($this, $rdata);
  199. return($rc);
  200. } else
  201. return($this);
  202. }
  203. /* }}} */
  204. /* Net_DNS_RR::display() {{{ */
  205. function display()
  206. {
  207. echo $this->string() . "\n";
  208. }
  209. /* }}} */
  210. /* Net_DNS_RR::string() {{{ */
  211. function string()
  212. {
  213. return($this->name . ".\t" . (strlen($this->name) < 16 ? "\t" : '') .
  214. $this->ttl . "\t" .
  215. $this->class. "\t" .
  216. $this->type . "\t" .
  217. $this->rdatastr());
  218. }
  219. /* }}} */
  220. /* Net_DNS_RR::rdatastr() {{{ */
  221. function rdatastr()
  222. {
  223. if ($this->rdlength) {
  224. return('; rdlength = ' . $this->rdlength);
  225. }
  226. return('; no data');
  227. }
  228. /* }}} */
  229. /* Net_DNS_RR::rdata() {{{ */
  230. function rdata(&$packetORrdata, $offset = '')
  231. {
  232. if ($offset) {
  233. return($this->rr_rdata($packetORrdata, $offset));
  234. } else if (strlen($this->rdata)) {
  235. return($this->rdata);
  236. } else {
  237. return(NULL);
  238. }
  239. }
  240. /* }}} */
  241. /* Net_DNS_RR::rr_rdata($packet, $offset) {{{ */
  242. function rr_rdata(&$packet, $offset)
  243. {
  244. return((strlen($this->rdata) ? $this->rdata : ''));
  245. }
  246. /* }}} */
  247. /* Net_DNS_RR::data() {{{ */
  248. function data(&$packet, $offset)
  249. {
  250. $data = $packet->dn_comp($this->name, $offset);
  251. $data .= pack('n', Net_DNS::typesbyname(strtoupper($this->type)));
  252. $data .= pack('n', Net_DNS::classesbyname(strtoupper($this->class)));
  253. $data .= pack('N', $this->ttl);
  254. $offset += strlen($data) + 2; // The 2 extra bytes are for rdlength
  255. $rdata = $this->rdata($packet, $offset);
  256. $data .= pack('n', strlen($rdata));
  257. $data .= $rdata;
  258. return($data);
  259. }
  260. /* }}} */
  261. }
  262. /* }}} */
  263. /* VIM settings {{{
  264. * Local variables:
  265. * tab-width: 4
  266. * c-basic-offset: 4
  267. * soft-stop-width: 4
  268. * c indent on
  269. * End:
  270. * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
  271. * vim<600: sw=4 ts=4
  272. * }}} */
  273. ?>