5
* Net_DNS: A resolver library for PHP
6
* Copyright (c) 2002-2003 Eric Kilfoil eric@ypass.net
8
* Marco Kaiser <bate@php.net>
9
* Florian Anderiasch <fa@php.net>
11
* PHP versions 4 and 5
13
* LICENSE: This source file is subject to version 3.01 of the PHP license
14
* that is available through the world-wide-web at the following URI:
15
* http://www.php.net/license/3_01.txt. If you did not receive a copy of
16
* the PHP License and are unable to obtain it through the web, please
17
* send a note to license@php.net so we can mail you a copy immediately.
20
/* Net_DNS_Header object definition {{{ */
22
* Object representation of the HEADER section of a DNS packet
24
* The Net_DNS::Header class contains the values of a DNS packet. It parses
25
* the header of a DNS packet or can generate the binary data
26
* representation of the packet. The format of the header is described in
33
/* class variable definitions {{{ */
35
* The packet's request id
37
* The request id of the packet represented as a 16 bit integer.
41
* The QR bit in a DNS packet header
43
* The QR bit as described in RFC1035. QR is set to 0 for queries, and
48
* The OPCODE name of this packet.
50
* The string value (name) of the opcode for the DNS packet.
54
* The AA (authoritative answer) bit in a DNS packet header
56
* The AA bit as described in RFC1035. AA is set to 1 if the answer
57
* is authoritative. It has no meaning if QR is set to 0.
61
* The TC (truncated) bit in a DNS packet header
63
* This flag is set to 1 if the response was truncated. This flag has
64
* no meaning in a query packet.
68
* The RD (recursion desired) bit in a DNS packet header
70
* This bit should be set to 1 in a query if recursion is desired by
75
* The RA (recursion available) bit in a DNS packet header
77
* This bit is set to 1 by the DNS server if the server is willing to
82
* The RCODE name for this packet.
84
* The string value (name) of the rcode for the DNS packet.
88
* Number of questions contained within the packet
90
* 16bit integer representing the number of questions in the question
91
* section of the DNS packet.
93
* @var integer $qdcount
94
* @see Net_DNS_Question class
98
* Number of answer RRs contained within the packet
100
* 16bit integer representing the number of answer resource records
101
* contained in the answer section of the DNS packet.
103
* @var integer $ancount
104
* @see Net_DNS_RR class
108
* Number of authority RRs within the packet
110
* 16bit integer representing the number of authority (NS) resource
111
* records contained in the authority section of the DNS packet.
113
* @var integer $nscount
114
* @see Net_DNS_RR class
118
* Number of additional RRs within the packet
120
* 16bit integer representing the number of additional resource records
121
* contained in the additional section of the DNS packet.
123
* @var integer $arcount
124
* @see Net_DNS_RR class
129
/* class constructor - Net_DNS_Header($data = "") {{{ */
131
* Initializes the default values for the Header object.
133
* Builds a header object from either default values, or from a DNS
134
* packet passed into the constructor as $data
136
* @param string $data A DNS packet of which the header will be parsed.
137
* @return object Net_DNS_Header
140
function Net_DNS_Header($data = '')
143
$this->id = Net_DNS_Resolver::nextid();
157
* The header MUST be at least 12 bytes.
158
* Passing the full datagram to this constructor
159
* will examine only the header section of the DNS packet
161
if (strlen($data) < 12) {
165
$a = unpack('nid/C2flags/n4counts', $data);
166
$this->id = $a['id'];
167
$this->qr = ($a['flags1'] >> 7) & 0x1;
168
$this->opcode = ($a['flags1'] >> 3) & 0xf;
169
$this->aa = ($a['flags1'] >> 2) & 0x1;
170
$this->tc = ($a['flags1'] >> 1) & 0x1;
171
$this->rd = $a['flags1'] & 0x1;
172
$this->ra = ($a['flags2'] >> 7) & 0x1;
173
$this->rcode = $a['flags2'] & 0xf;
174
$this->qdcount = $a['counts1'];
175
$this->ancount = $a['counts2'];
176
$this->nscount = $a['counts3'];
177
$this->arcount = $a['counts4'];
181
$dns = new Net_DNS();
182
if ($dns->opcodesbyval($this->opcode)) {
183
$this->opcode = $dns->opcodesbyval($this->opcode);
185
if ($dns->rcodesbyval($this->rcode)) {
186
$this->rcode = $dns->rcodesbyval($this->rcode);
191
/* Net_DNS_Header::display() {{{ */
193
* Displays the properties of the header.
195
* Displays the properties of the header.
201
echo $this->string();
205
/* Net_DNS_Header::string() {{{ */
207
* Returns a formatted string containing the properties of the header.
209
* @return string a formatted string containing the properties of the header.
214
$retval = ';; id = ' . $this->id . "\n";
215
if ($this->opcode == 'UPDATE') {
216
$retval .= ';; qr = ' . $this->qr . ' ' .
217
'opcode = ' . $this->opcode . ' ' .
218
'rcode = ' . $this->rcode . "\n";
219
$retval .= ';; zocount = ' . $this->qdcount . ' ' .
220
'prcount = ' . $this->ancount . ' ' .
221
'upcount = ' . $this->nscount . ' ' .
222
'adcount = ' . $this->arcount . "\n";
224
$retval .= ';; qr = ' . $this->qr . ' ' .
225
'opcode = ' . $this->opcode . ' ' .
226
'aa = ' . $this->aa . ' ' .
227
'tc = ' . $this->tc . ' ' .
228
'rd = ' . $this->rd . "\n";
230
$retval .= ';; ra = ' . $this->ra . ' ' .
231
'rcode = ' . $this->rcode . "\n";
233
$retval .= ';; qdcount = ' . $this->qdcount . ' ' .
234
'ancount = ' . $this->ancount . ' ' .
235
'nscount = ' . $this->nscount . ' ' .
236
'arcount = ' . $this->arcount . "\n";
242
/* Net_DNS_Header::data() {{{ */
244
* Returns the binary data containing the properties of the header
246
* Packs the properties of the Header object into a binary string
247
* suitable for using as the Header section of a DNS packet.
249
* @return string binary representation of the header object
254
$dns = new Net_DNS();
255
$opcode = $dns->opcodesbyname($this->opcode);
256
$rcode = $dns->rcodesbyname($this->rcode);
258
$byte2 = ($this->qr << 7)
264
$byte3 = ($this->ra << 7) | $rcode;
266
return pack('nC2n4', $this->id,
286
* vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
b'\\ No newline at end of file'