AAAA.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_RR_AAAA object definition {{{ */
  23. /**
  24. * A representation of a resource record of type <b>AAAA</b>
  25. *
  26. * @package Net_DNS
  27. */
  28. class Net_DNS_RR_AAAA extends Net_DNS_RR
  29. {
  30. /* class variable definitions {{{ */
  31. var $name;
  32. var $type;
  33. var $class;
  34. var $ttl;
  35. var $rdlength;
  36. var $rdata;
  37. var $address;
  38. /* }}} */
  39. /* class constructor - Net_DNS_RR_AAAA(&$rro, $data, $offset = '') {{{ */
  40. function Net_DNS_RR_AAAA(&$rro, $data, $offset = '')
  41. {
  42. $this->name = $rro->name;
  43. $this->type = $rro->type;
  44. $this->class = $rro->class;
  45. $this->ttl = $rro->ttl;
  46. $this->rdlength = $rro->rdlength;
  47. $this->rdata = $rro->rdata;
  48. if ($offset) {
  49. $this->address = Net_DNS_RR_AAAA::ipv6_decompress(substr($this->rdata, 0, $this->rdlength));
  50. } else {
  51. if (strlen($data)) {
  52. if (count($adata = explode(':', $data, 8)) >= 3) {
  53. foreach($adata as $addr)
  54. if (!preg_match('/^[0-9A-F]{0,4}$/i', $addr)) return;
  55. $this->address = trim($data);
  56. }
  57. }
  58. }
  59. }
  60. /* }}} */
  61. /* Net_DNS_RR_AAAA::rdatastr() {{{ */
  62. function rdatastr()
  63. {
  64. if (strlen($this->address)) {
  65. return($this->address);
  66. }
  67. return('; no data');
  68. }
  69. /* }}} */
  70. /* Net_DNS_RR_AAAA::rr_rdata($packet, $offset) {{{ */
  71. function rr_rdata($packet, $offset)
  72. {
  73. return Net_DNS_RR_AAAA::ipv6_compress($this->address);
  74. }
  75. /* }}} */
  76. /* Net_DNS_RR_AAAA::ipv6_compress($addr) {{{ */
  77. function ipv6_compress($addr)
  78. {
  79. $numparts = count(explode(':', $addr));
  80. if ($numparts < 3 || $numparts > 8 ||
  81. !preg_match('/^([0-9A-F]{0,4}:){0,7}(:[0-9A-F]{0,4}){0,7}$/i', $addr)) {
  82. /* Non-sensical IPv6 address */
  83. return pack('n8', 0, 0, 0, 0, 0, 0, 0, 0);
  84. }
  85. if (strpos($addr, '::') !== false) {
  86. /* First we have to normalize the address, turn :: into :0:0:0:0: */
  87. $filler = str_repeat(':0', 9 - $numparts) . ':';
  88. if (substr($addr, 0, 2) == '::') {
  89. $filler = "0$filler";
  90. }
  91. if (substr($addr, -2, 2) == '::') {
  92. $filler .= '0';
  93. }
  94. $addr = str_replace('::', $filler, $addr);
  95. }
  96. $aparts = explode(':', $addr);
  97. return pack('n8', hexdec($aparts[0]), hexdec($aparts[1]), hexdec($aparts[2]), hexdec($aparts[3]),
  98. hexdec($aparts[4]), hexdec($aparts[5]), hexdec($aparts[6]), hexdec($aparts[7]));
  99. }
  100. /* }}} */
  101. /* Net_DNS_RR_AAAA::ipv6_decompress($pack) {{{ */
  102. function ipv6_decompress($pack)
  103. {
  104. if (strlen($pack) != 16) {
  105. /* Must be 8 shorts long */
  106. return '::';
  107. }
  108. $a = unpack('n8b', $pack);
  109. foreach($a as $idx => $value)
  110. $a[$idx] = dechex($value);
  111. $addr = implode(':', $a);
  112. /* Shorthand the first :0:0:0: set into a :: */
  113. /* TODO: Make this is a single replacement pattern */
  114. if (substr($addr, -4, 4) == ':0:0') {
  115. return preg_replace('/((:0){2,})$/', '::', $addr);
  116. } else {
  117. return preg_replace('/(:?(0:){2,})/', '::', $addr);
  118. }
  119. }
  120. /* }}} */
  121. }
  122. /* }}} */
  123. /* VIM settings {{{
  124. * Local variables:
  125. * tab-width: 4
  126. * c-basic-offset: 4
  127. * soft-stop-width: 4
  128. * c indent on
  129. * End:
  130. * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
  131. * vim<600: sw=4 ts=4
  132. * }}} */
  133. ?>