annotate marns.php @ 2:f6954b464d2f draft

Small fixes and improvements in MARC NameServer
author Ivo Smits <Ivo@UCIS.nl>
date Fri, 14 Nov 2014 00:08:24 +0100
parents caa68b502313
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
2
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
50 $res = array('id' => $req['id'], 'isresponse' => true, 'opcode' => $req['opcode'], 'responsecode' => 'NOERROR', 'recursiondesired' => $req['recursiondesired'], 'question' => $req['question'], 'answer' => array(), 'authority' => array(), 'additional' => array());
1
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') {
2
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
56 if (dns_get_response($res, $q)) {
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
57 $res['authoritative'] = true;
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
58 } else {
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
59 $res['responsecode'] = 'NXDOMAIN';
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
60 }
1
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
61 } 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
62 switch ($q['name']) {
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
63 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
64 case 'reload.marns':
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
65 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
66 dbreload();
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
67 $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
68 } else {
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
69 $res['responsecode'] = 'REFUSED';
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
70 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
71 break;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
72 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
73 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
74 } else {
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
75 $res['responsecode'] = 'NOTIMPL';
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 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
79 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
80 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
81
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
82 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
83 global $database;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
84 $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
85 $name = '';
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
86 while (strlen($zone)) {
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
87 $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
88 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
89 $i = strpos($zone, '.');
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
90 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
91 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
92 $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
93 $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
94 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
95 return NULL;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
96 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
97 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
98 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
99 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
100 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
101 $realname .= $zonename;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
102 $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
103 $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
104 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
105 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
106 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
107 else return FALSE;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
108 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
109 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
110 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
111 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
112 $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
113 return $rr;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
114 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
115 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
116 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
117 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
118 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
119 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
120 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
121 $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
122 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
123 $response[] = $record;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
124 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
125 return TRUE;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
126 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
127 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
128 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
129 $foundns = FALSE;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
130 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
131 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
132 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
133 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
134 $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
135 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
136 $res['authority'][] = $record;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
137 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
138 $foundns = TRUE;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
139 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
140 return $foundns;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
141 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
142 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
143 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
144 $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
145 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
146 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
147 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
148 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
149 return FALSE;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
150 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
151 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
152 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
153 $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
154 $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
155 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
156 $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
157 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
158 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
159 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
160 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
161 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
162 function dns_find_authority(&$res, $dbzone, $zone, $nsquery = FALSE) {
2
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
163 if (!isset($dbzone->value['ns']) || !is_array($dbzone->value['ns'])) return FALSE;
1
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
164 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
165 $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
166 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
167 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
168 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
169 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
170 $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
171 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
172 $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
173 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
174 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
175 }
2
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
176 return TRUE;
1
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
177 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
178
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
179 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
180 $name = $q['name'];
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
181 $zone = '';
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
182 $dbzone = dns_find_zone($name, $zone);
2
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
183 if ($dbzone == NULL) return FALSE;
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
184 return dns_find_records($res, $dbzone, $name, $zone, $q['type']);
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
185 //return dns_find_authority($res, $dbzone, $zone, $q['type'] == 'NS');
1
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
186 }
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_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
189 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
190 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
191 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
192
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
193 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
194 global $dns_opcode, $dns_responsecode;
2
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
195 $flags1 = ord($packet[2]);
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
196 $flags2 = ord($packet[3]);
1
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
197 $dp = array(
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
198 'id' => dns_decode_uint16be($packet, 0),
2
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
199 'isresponse' => ($flags1 & 128) != 0,
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
200 'opcode' => ($flags1 >> 3) & 0x0F,
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
201 'authoritative' => ($flags1 & 4) != 0,
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
202 'truncation' => ($flags1 & 2) != 0,
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
203 'recursiondesired' => ($flags1 & 1) != 0,
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
204 'recursionavailable' => ($flags2 & 128) != 0,
f6954b464d2f Small fixes and improvements in MARC NameServer
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
205 'responsecode' => ($flags2 & 0x0F),
1
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
206 '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
207 '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
208 '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
209 '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
210 );
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
211 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
212 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
213 $ptr = 12;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
214 $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
215 $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
216 $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
217 $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
218 return $dp;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
219 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
220 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
221 $ret = array();
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
222 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
223 return $ret;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
224 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
225 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
226 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
227 $rr = array();
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
228 $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
229 $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
230 $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
231 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
232 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
233 if ($isanswer) {
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
234 $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
235 $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
236 $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
237 $dataptr = $ptr;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
238 $ptr += $rrdatalength;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
239 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
240 switch ($rr['type']) {
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
241 case 'A':
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
242 case 'AAAA':
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
243 $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
244 break;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
245 case 'CNAME':
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
246 case 'NS':
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
247 case 'PTR':
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
248 $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
249 break;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
250 case 'MX':
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
251 $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
252 $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
253 break;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
254 case 'TXT':
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
255 $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
256 break;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
257 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
258 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
259 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
260 return $rr;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
261 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
262 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
263 $name = '';
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
264 $canrecurse = 64;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
265 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
266 $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
267 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
268 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
269 $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
270 $ptr += $l;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
271 } 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
272 $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
273 $ptr = &$newptr;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
274 $canrecurse--;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
275 } else {
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
276 return FALSE;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
277 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
278 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
279 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
280 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
281
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
282 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
283 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
284 $data = '';
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
285 $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
286 $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
287 $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
288 $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
289 $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
290 $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
291 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
292 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
293 $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
294 $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
295 $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
296 $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
297 $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
298 $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
299 $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
300 $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
301 $labels = array();
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
302 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
303 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
304 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
305 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
306 return $data;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
307 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
308 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
309 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
310 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
311 $rrtype = $rr['type'];
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
312 $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
313 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
314 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
315 $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
316 $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
317 $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
318 if ($isanswer) {
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
319 $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
320 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
321 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
322 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
323 elseif ($rrclass == 1) {
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
324 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
325 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
326 $rrdata = (string)$rrdata;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
327 $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
328 $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
329 $data .= $rrdata;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
330 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
331 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
332 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
333 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
334 $name = rtrim($name, '.');
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
335 $data = '';
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
336 while (strlen($name)) {
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
337 $lname = strtolower($name);
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
338 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
339 $ptr = $labels[$lname];
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
340 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
341 } else {
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
342 $dot = strpos($name, '.');
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
343 if ($dot === FALSE) {
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
344 $part = $name;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
345 $name = '';
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
346 } else {
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
347 $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
348 $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
349 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
350 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
351 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
352 $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
353 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
354 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
355 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
356 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
357 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
358 $data = '';
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
359 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
360 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
361 $part = (string)$part;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
362 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
363 $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
364 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
365 return $data;
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
366 }
caa68b502313 Added the MARC DNS server (and small fixes in marcus and anoclaims)
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
367