5
* Net_DNS: A resolver library for PHP
6
* Copyright (c) 2002-2003 Eric Kilfoil eric@ypass.net
8
* Marco Kaiser <bate@php.net>
9
* Florian Anderiasch <fa@php.net>
11
* PHP versions 4 and 5
13
* LICENSE: This source file is subject to version 3.01 of the PHP license
14
* that is available through the world-wide-web at the following URI:
15
* http://www.php.net/license/3_01.txt. If you did not receive a copy of
16
* the PHP License and are unable to obtain it through the web, please
17
* send a note to license@php.net so we can mail you a copy immediately.
20
/* Net_DNS_RR_AAAA object definition {{{ */
22
* A representation of a resource record of type <b>AAAA</b>
26
class Net_DNS_RR_AAAA extends Net_DNS_RR
28
/* class variable definitions {{{ */
38
/* class constructor - Net_DNS_RR_AAAA(&$rro, $data, $offset = '') {{{ */
39
function Net_DNS_RR_AAAA(&$rro, $data, $offset = '')
41
$this->name = $rro->name;
42
$this->type = $rro->type;
43
$this->class = $rro->class;
44
$this->ttl = $rro->ttl;
45
$this->rdlength = $rro->rdlength;
46
$this->rdata = $rro->rdata;
49
$this->address = Net_DNS_RR_AAAA::ipv6_decompress(substr($this->rdata, 0, $this->rdlength));
50
} elseif (is_array($data)) {
51
$this->address = $data['address'];
54
if (count($adata = explode(':', $data, 8)) >= 3) {
55
foreach($adata as $addr)
56
if (!preg_match('/^[0-9A-F]{0,4}$/i', $addr)) return;
57
$this->address = trim($data);
64
/* Net_DNS_RR_AAAA::rdatastr() {{{ */
67
if (strlen($this->address)) {
68
return $this->address;
73
/* Net_DNS_RR_AAAA::rr_rdata($packet, $offset) {{{ */
74
function rr_rdata($packet, $offset)
76
return Net_DNS_RR_AAAA::ipv6_compress($this->address);
80
/* Net_DNS_RR_AAAA::ipv6_compress($addr) {{{ */
81
function ipv6_compress($addr)
83
$numparts = count(explode(':', $addr));
84
if ($numparts < 3 || $numparts > 8 ) {
85
/* Non-sensical IPv6 address */
86
return pack('n8', 0, 0, 0, 0, 0, 0, 0, 0);
88
if (strpos($addr, '::') !== false) {
89
if (!preg_match('/^([0-9A-F]{0,4}:){0,7}(:[0-9A-F]{0,4}){0,7}$/i', $addr)) {
90
return pack('n8', 0, 0, 0, 0, 0, 0, 0, 0);
92
/* First we have to normalize the address, turn :: into :0:0:0:0: */
93
$filler = str_repeat(':0', 9 - $numparts) . ':';
94
if (substr($addr, 0, 2) == '::') {
97
if (substr($addr, -2, 2) == '::') {
100
$addr = str_replace('::', $filler, $addr);
101
} elseif (!preg_match('/^([0-9A-F]{0,4}:){7}[0-9A-F]{0,4}$/i', $addr)) {
102
return pack('n8', 0, 0, 0, 0, 0, 0, 0, 0);
105
$aparts = explode(':', $addr);
106
return pack('n8', hexdec($aparts[0]), hexdec($aparts[1]), hexdec($aparts[2]), hexdec($aparts[3]),
107
hexdec($aparts[4]), hexdec($aparts[5]), hexdec($aparts[6]), hexdec($aparts[7]));
111
/* Net_DNS_RR_AAAA::ipv6_decompress($pack) {{{ */
112
function ipv6_decompress($pack)
114
if (strlen($pack) != 16) {
115
/* Must be 8 shorts long */
118
$a = unpack('n8', $pack);
119
$addr = vsprintf("%x:%x:%x:%x:%x:%x:%x:%x", $a);
120
/* Shorthand the first :0:0: set into a :: */
121
/* TODO: Make this is a single replacement pattern */
122
if (substr($addr, -4) == ':0:0') {
123
return preg_replace('/((:0){2,})$/', '::', $addr);
124
} elseif (substr($addr, 0, 4) == '0:0:') {
125
return '0:0:'. substr($addr, 4);
127
return preg_replace('/(:(0:){2,})/', '::', $addr);
141
* vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
b'\\ No newline at end of file'