/
RR.php
315 lines (287 loc) · 8.85 KB
/
RR.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
/**
* License Information:
*
* Net_DNS: A resolver library for PHP
* Copyright (c) 2002-2003 Eric Kilfoil eric@ypass.net
* Maintainers:
* Marco Kaiser <bate@php.net>
* Florian Anderiasch <fa@php.net>
*
* PHP versions 4 and 5
*
* LICENSE: This source file is subject to version 3.01 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_01.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*/
/* Include files {{{ */
require_once("Net/DNS/RR/A.php");
require_once("Net/DNS/RR/AAAA.php");
require_once("Net/DNS/RR/NS.php");
require_once("Net/DNS/RR/CNAME.php");
require_once("Net/DNS/RR/PTR.php");
require_once("Net/DNS/RR/SOA.php");
require_once("Net/DNS/RR/MX.php");
require_once("Net/DNS/RR/TSIG.php");
require_once("Net/DNS/RR/TXT.php");
require_once("Net/DNS/RR/HINFO.php");
require_once("Net/DNS/RR/SRV.php");
require_once("Net/DNS/RR/NAPTR.php");
require_once("Net/DNS/RR/RR.php");
/* }}} */
/* Net_DNS_RR object definition {{{ */
/**
* Resource Record object definition
*
* Builds or parses resource record sections of the DNS packet including
* the answer, authority, and additional sections of the packet.
*
* @package Net_DNS
*/
class Net_DNS_RR
{
/* class variable definitions {{{ */
var $name;
var $type;
var $class;
var $ttl;
var $rdlength;
var $rdata;
/* }}} */
/*
* Use Net_DNS_RR::factory() instead
*
* @access private
*/
/* class constructor - Net_DNS_RR($rrdata) {{{ */
function Net_DNS_RR($rrdata)
{
if ($rrdata != 'getRR') { //BC check/warning remove later
trigger_error("Please use Net_DNS_RR::factory() instead");
}
}
/*
* Returns an RR object, use this instead of constructor
*
* @param mixed $rr_rdata Options as string, array or data
* @return object Net_DNS_RR or Net_DNS_RR_<type>
* @access public
* @see Net_DNS_RR::new_from_array Net_DNS_RR::new_from_data Net_DNS_RR::new_from_string
*/
function &factory($rrdata, $update_type = '')
{
if (is_string($rrdata)) {
$rr = &Net_DNS_RR::new_from_string($rrdata, $update_type);
} elseif (count($rrdata) == 7) {
list($name, $rrtype, $rrclass, $ttl, $rdlength, $data, $offset) = $rrdata;
$rr = &Net_DNS_RR::new_from_data($name, $rrtype, $rrclass, $ttl, $rdlength, $data, $offset);
} else {
$rr = &Net_DNS_RR::new_from_array($rrdata);
}
return $rr;
}
/* }}} */
/* Net_DNS_RR::new_from_data($name, $ttl, $rrtype, $rrclass, $rdlength, $data, $offset) {{{ */
function &new_from_data($name, $rrtype, $rrclass, $ttl, $rdlength, $data, $offset)
{
$rr = &new Net_DNS_RR('getRR');
$rr->name = $name;
$rr->type = $rrtype;
$rr->class = $rrclass;
$rr->ttl = $ttl;
$rr->rdlength = $rdlength;
$rr->rdata = substr($data, $offset, $rdlength);
if (class_exists('Net_DNS_RR_' . $rrtype)) {
$scn = 'Net_DNS_RR_' . $rrtype;
$rr = new $scn($rr, $data, $offset);
}
return $rr;
}
/* }}} */
/* Net_DNS_RR::new_from_string($rrstring, $update_type = '') {{{ */
function &new_from_string($rrstring, $update_type = '')
{
$rr = &new Net_DNS_RR('getRR');
$ttl = 0;
$parts = preg_split('/[\s]+/', $rrstring);
while (count($parts) > 0) {
$s = array_shift($parts);
if (!isset($name)) {
$name = preg_replace('/\.+$/', '', $s);
} else if (preg_match('/^\d+$/', $s)) {
$ttl = $s;
} else if (!isset($rrclass) && ! is_null(Net_DNS::classesbyname(strtoupper($s)))) {
$rrclass = strtoupper($s);
$rdata = join(' ', $parts);
} else if (! is_null(Net_DNS::typesbyname(strtoupper($s)))) {
$rrtype = strtoupper($s);
$rdata = join(' ', $parts);
break;
} else {
break;
}
}
/*
* Do we need to do this?
*/
$rdata = trim(chop($rdata));
if (! strlen($rrtype) && strlen($rrclass) && $rrclass == 'ANY') {
$rrtype = $rrclass;
$rrclass = 'IN';
} else if (! isset($rrclass)) {
$rrclass = 'IN';
}
if (! strlen($rrtype)) {
$rrtype = 'ANY';
}
if (strlen($update_type)) {
$update_type = strtolower($update_type);
if ($update_type == 'yxrrset') {
$ttl = 0;
if (! strlen($rdata)) {
$rrclass = 'ANY';
}
} else if ($update_type == 'nxrrset') {
$ttl = 0;
$rrclass = 'NONE';
$rdata = '';
} else if ($update_type == 'yxdomain') {
$ttl = 0;
$rrclass = 'ANY';
$rrtype = 'ANY';
$rdata = '';
} else if ($update_type == 'nxdomain') {
$ttl = 0;
$rrclass = 'NONE';
$rrtype = 'ANY';
$rdata = '';
} else if (preg_match('/^(rr_)?add$/', $update_type)) {
$update_type = 'add';
if (! $ttl) {
$ttl = 86400;
}
} else if (preg_match('/^(rr_)?del(ete)?$/', $update_type)) {
$update_type = 'del';
$ttl = 0;
$rrclass = $rdata ? 'NONE' : 'ANY';
}
}
if (strlen($rrtype)) {
$rr->name = $name;
$rr->type = $rrtype;
$rr->class = $rrclass;
$rr->ttl = $ttl;
$rr->rdlength = 0;
$rr->rdata = '';
if (class_exists('Net_DNS_RR_' . $rrtype)) {
$scn = 'Net_DNS_RR_' . $rrtype;
$obj = new $scn($rr, $rdata);
return $obj;
}
return $rr;
}
return null;
}
/* }}} */
/* Net_DNS_RR::new_from_array($rrarray) {{{ */
function &new_from_array($rrarray)
{
$rr = &new Net_DNS_RR('getRR');
foreach ($rrarray as $k => $v) {
$rr->{strtolower($k)} = $v;
}
if (! strlen($rr->name)) {
return null;
}
if (! strlen($rr->type)){
return null;
}
if (! $rr->ttl) {
$rr->ttl = 0;
}
if (! strlen($rr->class)) {
$rr->class = 'IN';
}
if (strlen($rr->rdata)) {
$rr->rdlength = strlen($rr->rdata);
}
if (class_exists('Net_DNS_RR_' . $rr->type)) {
$scn = 'Net_DNS_RR_' . $rr->type;
$obj = new $scn($rr, !empty($rr->rdata) ? $rr->rdata : $rrarray);
return $obj;
}
return $rr;
}
/* }}} */
/* Net_DNS_RR::display() {{{ */
function display()
{
echo $this->string() . "\n";
}
/* }}} */
/* Net_DNS_RR::string() {{{ */
function string()
{
return $this->name . ".\t" . (strlen($this->name) < 16 ? "\t" : '') .
$this->ttl . "\t" .
$this->class. "\t" .
$this->type . "\t" .
$this->rdatastr();
}
/* }}} */
/* Net_DNS_RR::rdatastr() {{{ */
function rdatastr()
{
if ($this->rdlength) {
return '; rdlength = ' . $this->rdlength;
}
return '; no data';
}
/* }}} */
/* Net_DNS_RR::rdata() {{{ */
function rdata(&$packetORrdata, $offset = '')
{
if ($offset) {
return $this->rr_rdata($packetORrdata, $offset);
} else if (strlen($this->rdata)) {
return $this->rdata;
} else {
return null;
}
}
/* }}} */
/* Net_DNS_RR::rr_rdata($packet, $offset) {{{ */
function rr_rdata(&$packet, $offset)
{
return (strlen($this->rdata) ? $this->rdata : '');
}
/* }}} */
/* Net_DNS_RR::data() {{{ */
function data(&$packet, $offset)
{
$data = $packet->dn_comp($this->name, $offset);
$data .= pack('n', Net_DNS::typesbyname(strtoupper($this->type)));
$data .= pack('n', Net_DNS::classesbyname(strtoupper($this->class)));
$data .= pack('N', $this->ttl);
$offset += strlen($data) + 2; // The 2 extra bytes are for rdlength
$rdata = $this->rdata($packet, $offset);
$data .= pack('n', strlen($rdata));
$data .= $rdata;
return $data;
}
/* }}} */
}
/* }}} */
/* VIM settings {{{
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* soft-stop-width: 4
* c indent on
* End:
* vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
* vim<600: sw=4 ts=4
* }}} */
?>