Mercurial > hg > marc_php
annotate marns.php @ 1:caa68b502313 draft
Added the MARC DNS server (and small fixes in marcus and anoclaims)
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Thu, 13 Nov 2014 17:22:12 +0100 |
parents | |
children | f6954b464d2f |
rev | line source |
---|---|
1
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
1 <?php |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
2 require_once './marccore.php'; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
3 |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
4 $dns_opcode = array(0 => 'QUERY', 1 => 'IQUERY', 2 => 'STATUS'); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
5 $dns_responsecode = array(0 => 'NOERROR', 1 => 'FORMERR', 2 => 'SERVFAIL', 3 => 'NXDOMAIN', 4 => 'NOTIMPL', 5 => 'REFUSED'); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
6 $dns_rr_class = array(1 => 'IN', 2 => 'CS', 3 => 'CH', 4 => 'HS', 255 => 'ANY'); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
7 $dns_rr_type = array(1 => 'A', 2 => 'NS', 5 => 'CNAME', 6 => 'SOA', 10 => 'NULL', 12 => 'PTR', 15 => 'MX', 16 => 'TXT', 28 => 'AAAA', 252 => 'AXFR', 255 => 'ANY'); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
8 $dns_opcode_rev = array_flip($dns_opcode); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
9 $dns_responsecode_rev = array_flip($dns_responsecode); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
10 $dns_rr_class_rev = array_flip($dns_rr_class); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
11 $dns_rr_type_rev = array_flip($dns_rr_type); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
12 |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
13 error_reporting(E_ALL); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
14 |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
15 if (!isset($argv)) $argv = $_SERVER['argv']; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
16 if (count($argv) < 3) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
17 echo "Usage: ".$argv[0]." [marc-flatfile-database-filename] [listen-ip-address] [listen-port-number]\n"; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
18 die(); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
19 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
20 |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
21 $dbfile = $argv[1]; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
22 $listenaddress = $argv[2]; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
23 $listenport = $argv[3]; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
24 |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
25 $database = new MARCDatabaseFlatFile(); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
26 |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
27 function dbreload() { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
28 global $database, $dbfile; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
29 $database->Open($dbfile); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
30 $database->Close(); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
31 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
32 |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
33 dbreload(); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
34 |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
35 $socket = socket_create(strpos($listenaddress, ':') === FALSE ? AF_INET : AF_INET6, SOCK_DGRAM, SOL_UDP); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
36 socket_bind($socket, $listenaddress, $listenport); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
37 |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
38 while (true) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
39 $buffer = $remoteaddr = $remoteport = NULL; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
40 $readlen = socket_recvfrom($socket, $buffer, 0xFFFF, 0, $remoteaddr, $remoteport); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
41 if ($readlen === FALSE) continue; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
42 $buffer = dns_process($buffer, $remoteaddr); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
43 if ($buffer === FALSE) continue; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
44 socket_sendto($socket, $buffer, strlen($buffer), 0, $remoteaddr, $remoteport); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
45 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
46 |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
47 function dns_process($reqbuffer, $remoteaddr) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
48 $req = dns_decode($reqbuffer); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
49 if ($req['isresponse']) return; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
50 $res = array('id' => $req['id'], 'isresponse' => true, 'opcode' => $req['opcode'], 'question' => $req['question'], 'responsecode' => 'NOERROR', 'answer' => array(), 'authority' => array(), 'additional' => array()); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
51 if ($req['opcode'] != 'QUERY') { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
52 $res['responsecode'] = 'NOTIMPL'; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
53 } else { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
54 foreach ($req['question'] as $q) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
55 if ($q['class'] == 'IN') { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
56 if (!dns_get_response($res, $q)) $res['responsecode'] = 'NXDOMAIN'; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
57 } else if ($q['class'] == 'CH' && $q['type'] == 'TXT') { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
58 switch ($q['name']) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
59 case 'version.bind': $res['answer'][] = array('name' => $q['name'], 'class' => $q['class'], 'type' => $q['type'], 'value' => 'UCIS MARNS - http://www.ucis.nl'); break; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
60 case 'reload.marns': |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
61 if ($remoteaddr == '127.0.0.1' || $remoteaddr == '::1' || $remoteaddr == $GLOBALS['listenaddress']) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
62 dbreload(); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
63 $res['answer'][] = array('name' => $q['name'], 'class' => $q['class'], 'type' => $q['type'], 'value' => 'RELOAD OK'); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
64 } else { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
65 $res['responsecode'] = 'REFUSED'; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
66 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
67 break; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
68 default: $res['responsecode'] = 'NOTIMPL'; break; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
69 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
70 } else { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
71 $res['responsecode'] = 'NOTIMPL'; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
72 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
73 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
74 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
75 return dns_encode($res); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
76 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
77 |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
78 function dns_find_zone(&$name, &$zone) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
79 global $database; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
80 $zone = strtolower(rtrim($name, '.')); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
81 $name = ''; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
82 while (strlen($zone)) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
83 $dbzone = $database->GetResource(chr(4).$zone); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
84 if ($dbzone != NULL) return $dbzone; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
85 $i = strpos($zone, '.'); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
86 if ($i === FALSE) break; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
87 if (strlen($name)) $name .= '.'; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
88 $name .= substr($zone, 0, $i); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
89 $zone = substr($zone, $i + 1); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
90 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
91 return NULL; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
92 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
93 function dns_convert_record($item, $realname, $zonename) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
94 if (isset($item['class']) && strcasecmp($item['class'], 'IN')) return FALSE; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
95 if (!is_array($item) || !isset($item['type'])) return FALSE; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
96 if (strlen($realname)) $realname .= '.'; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
97 $realname .= $zonename; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
98 $type = strtoupper($item['type']); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
99 $rr = array('name' => $realname, 'type' => $type); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
100 if (($type == 'A' || $type == 'AAAA') && isset($item['target'])) $rr['address'] = $item['target']; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
101 elseif (($type == 'CNAME' || $type == 'NS' || $type == 'PTR') && isset($item['target'])) $rr['target'] = $item['target']; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
102 elseif ($type == 'MX' && isset($item['target'])) { $rr['target'] = $item['target']; if (isset($item['priority'])) $rr['priority'] = $item['priority']; } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
103 else return FALSE; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
104 if (isset($rr['target'])) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
105 if (!strlen($rr['target'])) $rr['target'] = $zonename; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
106 elseif ($rr['target'][strlen($rr['target']) - 1] != '.') $rr['target'] .= '.'.$zonename; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
107 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
108 $rr['ttl'] = isset($item['ttl']) ? $item['ttl'] : 3600; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
109 return $rr; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
110 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
111 function dns_find_records_real(&$response, $dbzone, $findname, $realname, $zonename, $qtype) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
112 if (!isset($dbzone[$findname])) return FALSE; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
113 foreach ($dbzone[$findname] as $item) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
114 if (!is_array($item) || !isset($item['type'])) continue; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
115 if (isset($item['class']) && strcasecmp($item['class'], 'IN')) continue; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
116 if (strcasecmp($item['type'], $qtype) && strcasecmp($qtype, 'ANY') && strcasecmp($item['type'], 'CNAME')) continue; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
117 $record = dns_convert_record($item, $realname, $zonename); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
118 if ($record === FALSE) continue; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
119 $response[] = $record; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
120 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
121 return TRUE; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
122 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
123 function dns_find_ns_records(&$res, $dbzone, $findname, $realname, $zonename) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
124 if (!isset($dbzone[$findname])) return FALSE; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
125 $foundns = FALSE; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
126 foreach ($dbzone[$findname] as $item) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
127 if (!is_array($item) || !isset($item['type'])) continue; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
128 if (isset($item['class']) && strcasecmp($item['class'], 'IN')) continue; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
129 if (strcasecmp($item['type'], 'NS')) continue; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
130 $record = dns_convert_record($item, $realname, $zonename); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
131 if ($record === FALSE) continue; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
132 $res['authority'][] = $record; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
133 if (strcasecmp(substr($record['target'], -strlen($zonename)), $zonename) == 0) dns_find_records_realwild($res['additional'], $dbzone, rtrim(substr($record['target'], 0, -strlen($zonename)), '.'), $zonename, 'ANY'); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
134 $foundns = TRUE; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
135 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
136 return $foundns; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
137 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
138 function dns_find_records_realwild(&$res, $dbzone, $name, $zonename, $qtype) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
139 if (dns_find_records_real($res, $dbzone, $name, $name, $zonename, $qtype)) return TRUE; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
140 $nameparts = strlen($name) ? explode('.', strtolower($name)) : array(); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
141 for ($i = 1; $i < count($nameparts); $i++) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
142 if (dns_find_records_real($res, $dbzone, '*.'.implode('.', array_slice($nameparts, $i)), $name, $zonename, $qtype)) return TRUE; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
143 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
144 if (dns_find_records_real($res, $dbzone, '*', $name, $zonename, $qtype)) return TRUE; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
145 return FALSE; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
146 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
147 function dns_find_records(&$res, $dbzone, $name, $zone, $qtype) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
148 if (!isset($dbzone->value['dnsrecords']) || !is_array($dbzone->value['dnsrecords'])) return FALSE; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
149 $dbzone = $dbzone->value['dnsrecords']; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
150 $nameparts = strlen($name) ? explode('.', strtolower($name)) : array(); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
151 for ($i = count($nameparts) - 1; $i >= 0; $i--) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
152 $realname = implode('.', array_slice($nameparts, $i)); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
153 if (dns_find_ns_records($res, $dbzone, $realname, $realname, $zone)) return TRUE; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
154 if (dns_find_ns_records($res, $dbzone, '*.'.implode('.', array_slice($nameparts, $i + 1)), $realname, $zone)) return TRUE; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
155 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
156 return dns_find_records_realwild($res['answer'], $dbzone, $name, $zone, $qtype); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
157 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
158 function dns_find_authority(&$res, $dbzone, $zone, $nsquery = FALSE) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
159 if (!isset($dbzone->value['ns']) || !is_array($dbzone->value['ns'])) return; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
160 foreach ($dbzone->value['ns'] as $nsname => $nsglues) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
161 $rr = array('name' => $zone, 'type' => 'NS', 'ttl' => 3600, 'target' => (strlen($nsname) && $nsname[strlen($nsname) - 1] != '.') ? $nsname.'.'.$zone : $nsname); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
162 if ($nsquery) $res['answer'][] = $rr; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
163 else $res['authority'][] = $rr; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
164 if (is_array($nsglues)) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
165 foreach ($nsglues as $glue) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
166 $glue = inet_pton($glue); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
167 if (!strlen($glue)) continue; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
168 $res['additional'][] = array('name' => $rr['target'], 'ttl' => 3600, 'type' => strlen($glue) == 4 ? 'A' : 'AAAA', 'data' => $glue); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
169 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
170 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
171 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
172 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
173 |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
174 function dns_get_response(&$res, $q) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
175 $name = $q['name']; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
176 $zone = ''; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
177 $dbzone = dns_find_zone($name, $zone); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
178 if ($dbzone == NULL) return; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
179 dns_find_records($res, $dbzone, $name, $zone, $q['type']); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
180 //dns_find_authority($res, $dbzone, $zone, $q['type'] == 'NS'); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
181 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
182 |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
183 function dns_decode_uint16be($data, $i = 0) { return (ord($data[$i+0]) << 8) | ord($data[$i+1]); } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
184 function dns_decode_uint32be($data, $i = 0) { return (ord($data[$i]) << 24) | (ord($data[$i+1]) << 16) | (ord($data[$i+2]) << 8) | ord($data[$i+3]); } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
185 function dns_encode_uint32be($v) { return pack("N", $v); } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
186 function dns_encode_uint16be($v) { return pack("n", $v); } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
187 |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
188 function dns_decode($packet) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
189 global $dns_opcode, $dns_responsecode; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
190 $dp = array( |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
191 'id' => dns_decode_uint16be($packet, 0), |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
192 'isresponse' => ($packet[2] & 128) != 0, |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
193 'opcode' => ($packet[2] >> 3) & 0x0F, |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
194 'authoritative' => ($packet[2] & 4) != 0, |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
195 'truncation' => ($packet[2] & 2) != 0, |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
196 'recursiondesired' => ($packet[2] & 1) != 0, |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
197 'recursionavailable' => ($packet[3] & 128) != 0, |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
198 'responsecode' => ($packet[3] & 0x0F), |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
199 'qdcount' => dns_decode_uint16be($packet, 4), |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
200 'ancount' => dns_decode_uint16be($packet, 6), |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
201 'nscount' => dns_decode_uint16be($packet, 8), |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
202 'arcount' => dns_decode_uint16be($packet, 10), |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
203 ); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
204 if (isset($dns_opcode[$dp['opcode']])) $dp['opcode'] = $dns_opcode[$dp['opcode']]; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
205 if (isset($dns_responsecode[$dp['responsecode']])) $dp['responsecode'] = $dns_responsecode[$dp['responsecode']]; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
206 $ptr = 12; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
207 $dp['question'] = dns_decode_rrs($packet, $ptr, false, $dp['qdcount']); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
208 $dp['answer'] = dns_decode_rrs($packet, $ptr, true, $dp['ancount']); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
209 $dp['authority'] = dns_decode_rrs($packet, $ptr, true, $dp['nscount']); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
210 $dp['additional'] = dns_decode_rrs($packet, $ptr, true, $dp['arcount']); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
211 return $dp; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
212 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
213 function dns_decode_rrs($packet, &$ptr, $isanswer, $count) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
214 $ret = array(); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
215 for ($i = 0; $i < $count; $i++) $ret[] = dns_decode_rr($packet, $ptr, $isanswer); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
216 return $ret; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
217 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
218 function dns_decode_rr($packet, &$ptr, $isanswer) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
219 global $dns_rr_class, $dns_rr_type; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
220 $rr = array(); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
221 $rr['name'] = dns_decode_name($packet, $ptr); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
222 $rr['type'] = dns_decode_uint16be($packet, $ptr); $ptr += 2; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
223 $rr['class'] = dns_decode_uint16be($packet, $ptr); $ptr += 2; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
224 if (isset($dns_rr_class[$rr['class']])) $rr['class'] = $dns_rr_class[$rr['class']]; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
225 if (isset($dns_rr_type[$rr['type']])) $rr['type'] = $dns_rr_type[$rr['type']]; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
226 if ($isanswer) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
227 $rr['ttl'] = dns_decode_uint32be($packet, $ptr); $ptr += 4; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
228 $rrdatalength = dns_decode_uint16be($packet, $ptr); $ptr += 2; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
229 $rr['data'] = substr($packet, $ptr, $rrdatalength); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
230 $dataptr = $ptr; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
231 $ptr += $rrdatalength; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
232 if ($rr['class'] == 'IN') { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
233 switch ($rr['type']) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
234 case 'A': |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
235 case 'AAAA': |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
236 $rr['address'] = inet_ntop($rr['data']); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
237 break; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
238 case 'CNAME': |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
239 case 'NS': |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
240 case 'PTR': |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
241 $rr['target'] = dns_decode_name($packet, $dataptr); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
242 break; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
243 case 'MX': |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
244 $rr['priority'] = dns_decode_uint16be($packet, $dataptr); $dataptr += 2; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
245 $rr['target'] = dns_decode_name($packet, $dataptr); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
246 break; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
247 case 'TXT': |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
248 $rr['value'] = dns_decode_name($packet, $dataptr); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
249 break; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
250 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
251 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
252 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
253 return $rr; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
254 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
255 function dns_decode_name($packet, &$ptr) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
256 $name = ''; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
257 $canrecurse = 64; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
258 while ($ptr < strlen($packet)) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
259 $l = ord($packet[$ptr++]); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
260 if ($l == 0) break; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
261 if (($l & 0xC0) == 0) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
262 $name .= substr($packet, $ptr, $l).'.'; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
263 $ptr += $l; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
264 } else if (($l & 0xC0) == 0xC0 && $canrecurse > 0) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
265 $newptr = (($l & 0x3F) << 8) | ord($packet[$ptr++]); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
266 $ptr = &$newptr; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
267 $canrecurse--; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
268 } else { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
269 return FALSE; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
270 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
271 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
272 return substr($name, 0, -1); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
273 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
274 |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
275 function dns_encode($dp) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
276 global $dns_opcode_rev, $dns_responsecode_rev; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
277 $data = ''; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
278 $qdcount = isset($dp['question']) ? count($dp['question']) : 0; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
279 $ancount = isset($dp['answer']) ? count($dp['answer']) : 0; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
280 $nscount = isset($dp['authority']) ? count($dp['authority']) : 0; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
281 $arcount = isset($dp['additional']) ? count($dp['additional']) : 0; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
282 $opcode = isset($dp['opcode']) ? $dp['opcode'] : 0; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
283 $responsecode = isset($dp['responsecode']) ? $dp['responsecode'] : 0; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
284 if (isset($dns_opcode_rev[$opcode])) $opcode = $dns_opcode_rev[$opcode]; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
285 if (isset($dns_responsecode_rev[$responsecode])) $responsecode = $dns_responsecode_rev[$responsecode]; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
286 $isresponse = $ancount || $nscount || $arcount || $responsecode != 0 || (isset($dp['isresponse']) && $dp['isresponse']); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
287 $data .= dns_encode_uint16be($dp['id']); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
288 $data .= chr(($isresponse ? 128 : 0) | (($opcode & 0x0F) << 3) | ((isset($dp['authoritative']) && $dp['authoritative']) ? 4 : 0) | ((isset($dp['truncation']) && $dp['truncation']) ? 2 : 0) | ((isset($dp['recursiondesired']) && $dp['recursiondesired']) ? 1 : 0)); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
289 $data .= chr(((isset($dp['recursionavailable']) && $dp['recursionavailable']) ? 128 : 0) | ($responsecode & 0xF)); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
290 $data .= dns_encode_uint16be($qdcount); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
291 $data .= dns_encode_uint16be($ancount); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
292 $data .= dns_encode_uint16be($nscount); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
293 $data .= dns_encode_uint16be($arcount); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
294 $labels = array(); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
295 if ($qdcount) dns_encode_rrs($data, $dp['question'], false, $labels); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
296 if ($ancount) dns_encode_rrs($data, $dp['answer'], true, $labels); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
297 if ($nscount) dns_encode_rrs($data, $dp['authority'], true, $labels); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
298 if ($arcount) dns_encode_rrs($data, $dp['additional'], true, $labels); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
299 return $data; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
300 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
301 function dns_encode_rrs(&$data, $rrs, $isanswer, &$labels) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
302 global $dns_rr_type_rev, $dns_rr_class_rev; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
303 foreach ($rrs as $rr) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
304 $rrtype = $rr['type']; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
305 $rrclass = isset($rr['class']) ? $rr['class'] : 1; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
306 if (isset($dns_rr_type_rev[$rrtype])) $rrtype = $dns_rr_type_rev[$rrtype]; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
307 if (isset($dns_rr_class_rev[$rrclass])) $rrclass = $dns_rr_class_rev[$rrclass]; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
308 $data .= dns_encode_name($rr['name'], $labels, strlen($data)); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
309 $data .= dns_encode_uint16be($rrtype); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
310 $data .= dns_encode_uint16be($rrclass); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
311 if ($isanswer) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
312 $rrdata = isset($rr['data']) ? $rr['data'] : ''; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
313 if (($rrtype == 5 || $rrtype == 2 || $rrtype == 12) && isset($rr['target'])) $rrdata = dns_encode_name($rr['target'], $labels, strlen($data) + 6); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
314 elseif (($rrtype == 16) && isset($rr['value'])) $rrdata = dns_encode_txt($rr['value']); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
315 elseif ($rrtype == 15 && isset($rr['target'])) $rrdata = dns_encode_uint16be(isset($rr['priority']) ? $rr['priority'] : 0).dns_encode_name($rr['target'], $labels, strlen($data) + 8); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
316 elseif ($rrclass == 1) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
317 if (($rrtype == 1 || $rrtype == 28) && isset($rr['address'])) $rrdata = inet_pton($rr['address']); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
318 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
319 $rrdata = (string)$rrdata; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
320 $data .= dns_encode_uint32be(isset($rr['ttl']) ? $rr['ttl'] : 0); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
321 $data .= dns_encode_uint16be(strlen($rrdata)); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
322 $data .= $rrdata; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
323 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
324 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
325 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
326 function dns_encode_name($name, &$labels, $offset = FALSE) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
327 $name = rtrim($name, '.'); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
328 $data = ''; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
329 while (strlen($name)) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
330 $lname = strtolower($name); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
331 if (isset($labels[$lname])) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
332 $ptr = $labels[$lname]; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
333 return $data.chr(0xC0 | (($ptr >> 8) & 0x3F)).chr($ptr & 0xFF); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
334 } else { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
335 $dot = strpos($name, '.'); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
336 if ($dot === FALSE) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
337 $part = $name; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
338 $name = ''; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
339 } else { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
340 $part = substr($name, 0, $dot); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
341 $name = substr($name, $dot + 1); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
342 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
343 if (strlen($part) > 63) $part = substr($part, 0, 63); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
344 if ($offset !== FALSE && $offset + strlen($data) <= 0x3FFF) $labels[$lname] = strlen($data) + $offset; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
345 $data .= chr(strlen($part)).$part; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
346 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
347 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
348 return $data.chr(0); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
349 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
350 function dns_encode_txt($parts) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
351 $data = ''; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
352 if (!is_array($parts)) $parts = array($parts); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
353 foreach ($parts as $part) { |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
354 $part = (string)$part; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
355 if (strlen($part) > 63) $part = substr($part, 0, 63); |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
356 $data .= chr(strlen($part)).$part; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
357 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
358 return $data; |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
359 } |
caa68b502313
Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff
changeset
|
360 |