~edb/quam-plures/cronjob_stuff

« back to all changes in this revision

Viewing changes to qp_plugins/basic_antispam_plugin/Net/DNS/Header.php

  • Committer: EdB
  • Date: 2011-03-08 04:39:47 UTC
  • mfrom: (7592.1.1 quam-plures)
  • Revision ID: 1912webworks@gmail.com-20110308043947-apiklf384cgnwb7s
new stuff in core now here - woohoo!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
*  License Information:
 
4
*
 
5
*  Net_DNS:  A resolver library for PHP
 
6
*  Copyright (c) 2002-2003 Eric Kilfoil eric@ypass.net
 
7
*  Maintainers:
 
8
*  Marco Kaiser <bate@php.net>
 
9
*  Florian Anderiasch <fa@php.net>
 
10
*
 
11
* PHP versions 4 and 5
 
12
*
 
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.
 
18
*/
 
19
 
 
20
/*  Net_DNS_Header object definition {{{ */
 
21
/**
 
22
 * Object representation of the HEADER section of a DNS packet
 
23
 *
 
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
 
27
 * RFC1035.
 
28
 *
 
29
 * @package Net_DNS
 
30
 */
 
31
class Net_DNS_Header
 
32
{
 
33
    /* class variable definitions {{{ */
 
34
    /**
 
35
     * The packet's request id
 
36
     *
 
37
     * The request id of the packet represented as  a 16 bit integer.
 
38
     */
 
39
    var $id;
 
40
    /**
 
41
     * The QR bit in a DNS packet header
 
42
     *
 
43
     * The QR bit as described in RFC1035.  QR is set to 0 for queries, and
 
44
     * 1 for repsones.
 
45
     */
 
46
    var $qr;
 
47
    /**
 
48
     * The OPCODE name of this packet.
 
49
     *
 
50
     * The string value (name) of the opcode for the DNS packet.
 
51
     */
 
52
    var $opcode;
 
53
    /**
 
54
     * The AA (authoritative answer) bit in a DNS packet header
 
55
     *
 
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.
 
58
     */
 
59
    var $aa;
 
60
    /**
 
61
     * The TC (truncated) bit in a DNS packet header
 
62
     *
 
63
     * This flag is set to 1 if the response was truncated.  This flag has
 
64
     * no meaning in a query packet.
 
65
     */
 
66
    var $tc;
 
67
    /**
 
68
     * The RD (recursion desired) bit in a DNS packet header
 
69
     *
 
70
     * This bit should be set to 1 in a query if recursion  is desired by
 
71
     * the DNS server.
 
72
     */
 
73
    var $rd;
 
74
    /**
 
75
     * The RA (recursion available) bit in a DNS packet header
 
76
     *
 
77
     * This bit is set to 1 by the DNS server if the server is willing to
 
78
     * perform recursion.
 
79
     */
 
80
    var $ra;
 
81
    /**
 
82
     * The RCODE name for this packet.
 
83
     *
 
84
     * The string value (name) of the rcode for the DNS packet.
 
85
     */
 
86
    var $rcode;
 
87
    /**
 
88
     * Number of questions contained within the packet
 
89
     *
 
90
     * 16bit integer representing the number of questions in the question
 
91
     * section of the DNS packet.
 
92
     *
 
93
     * @var integer $qdcount
 
94
     * @see     Net_DNS_Question class
 
95
     */
 
96
    var $qdcount;
 
97
    /**
 
98
     * Number of answer RRs contained within the packet
 
99
     *
 
100
     * 16bit integer representing the number of answer resource records
 
101
     * contained in the answer section of the DNS packet.
 
102
     *
 
103
     * @var integer $ancount
 
104
     * @see     Net_DNS_RR class
 
105
     */
 
106
    var $ancount;
 
107
    /**
 
108
     * Number of authority RRs within the packet
 
109
     *
 
110
     * 16bit integer representing the number of authority (NS) resource
 
111
     * records  contained in the authority section of the DNS packet.
 
112
     *
 
113
     * @var integer $nscount
 
114
     * @see     Net_DNS_RR class
 
115
     */
 
116
    var $nscount;
 
117
    /**
 
118
     * Number of additional RRs within the packet
 
119
     *
 
120
     * 16bit integer representing the number of additional resource records
 
121
     * contained in the additional section of the DNS packet.
 
122
     *
 
123
     * @var integer $arcount
 
124
     * @see     Net_DNS_RR class
 
125
     */
 
126
    var $arcount;
 
127
 
 
128
    /* }}} */
 
129
    /* class constructor - Net_DNS_Header($data = "") {{{ */
 
130
    /**
 
131
     * Initializes the default values for the Header object.
 
132
     *
 
133
     * Builds a header object from either default values, or from a DNS
 
134
     * packet passed into the constructor as $data
 
135
     *
 
136
     * @param string $data  A DNS packet of which the header will be parsed.
 
137
     * @return  object  Net_DNS_Header
 
138
     * @access public
 
139
     */
 
140
    function Net_DNS_Header($data = '')
 
141
    {
 
142
        if (empty($data)) {
 
143
            $this->id      = Net_DNS_Resolver::nextid();
 
144
            $this->qr      = 0;
 
145
            $this->opcode  = 0;
 
146
            $this->aa      = 0;
 
147
            $this->tc      = 0;
 
148
            $this->rd      = 1;
 
149
            $this->ra      = 0;
 
150
            $this->rcode   = 0;
 
151
            $this->qdcount = 1;
 
152
            $this->ancount = 0;
 
153
            $this->nscount = 0;
 
154
            $this->arcount = 0;
 
155
        } else {
 
156
            /*
 
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
 
160
             */
 
161
            if (strlen($data) < 12) {
 
162
                return false;
 
163
            }
 
164
 
 
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'];
 
178
        }
 
179
 
 
180
 
 
181
        $dns = new Net_DNS();
 
182
        if ($dns->opcodesbyval($this->opcode)) {
 
183
            $this->opcode = $dns->opcodesbyval($this->opcode);
 
184
        }
 
185
        if ($dns->rcodesbyval($this->rcode)) {
 
186
            $this->rcode = $dns->rcodesbyval($this->rcode);
 
187
        }
 
188
    }
 
189
 
 
190
    /* }}} */
 
191
    /* Net_DNS_Header::display() {{{ */
 
192
    /**
 
193
     * Displays the properties of the header.
 
194
     *
 
195
     * Displays the properties of the header.
 
196
     *
 
197
     * @access public
 
198
     */
 
199
    function display()
 
200
    {
 
201
        echo $this->string();
 
202
    }
 
203
 
 
204
    /* }}} */
 
205
    /* Net_DNS_Header::string() {{{ */
 
206
    /**
 
207
     * Returns a formatted string containing the properties of the header.
 
208
     *
 
209
     * @return string   a formatted string containing the properties of the header.
 
210
     * @access public
 
211
     */
 
212
    function string()
 
213
    {
 
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";
 
223
        } else {
 
224
            $retval .= ';; qr = ' . $this->qr . '    ' .
 
225
                'opcode = ' . $this->opcode . '    '   .
 
226
                'aa = ' . $this->aa . '    '           .
 
227
                'tc = ' . $this->tc . '    '           .
 
228
                'rd = ' . $this->rd . "\n";
 
229
 
 
230
            $retval .= ';; ra = ' . $this->ra . '    ' .
 
231
                'rcode  = ' . $this->rcode . "\n";
 
232
 
 
233
            $retval .= ';; qdcount = ' . $this->qdcount . '  ' .
 
234
                'ancount = ' . $this->ancount . '  '    .
 
235
                'nscount = ' . $this->nscount . '  '    .
 
236
                'arcount = ' . $this->arcount . "\n";
 
237
        }
 
238
        return $retval;
 
239
    }
 
240
 
 
241
    /* }}} */
 
242
    /* Net_DNS_Header::data() {{{ */
 
243
    /**
 
244
     * Returns the binary data containing the properties of the header
 
245
     *
 
246
     * Packs the properties of the Header object into a binary string
 
247
     * suitable for using as the Header section of a DNS packet.
 
248
     *
 
249
     * @return string   binary representation of the header object
 
250
     * @access public
 
251
     */
 
252
    function data()
 
253
    {
 
254
        $dns = new Net_DNS();
 
255
        $opcode = $dns->opcodesbyname($this->opcode);
 
256
        $rcode  = $dns->rcodesbyname($this->rcode);
 
257
 
 
258
        $byte2 = ($this->qr << 7)
 
259
            | ($opcode << 3)
 
260
            | ($this->aa << 2)
 
261
            | ($this->tc << 1)
 
262
            | ($this->rd);
 
263
 
 
264
        $byte3 = ($this->ra << 7) | $rcode;
 
265
 
 
266
        return pack('nC2n4', $this->id,
 
267
                $byte2,
 
268
                $byte3,
 
269
                $this->qdcount,
 
270
                $this->ancount,
 
271
                $this->nscount,
 
272
                $this->arcount);
 
273
    }
 
274
 
 
275
    /* }}} */
 
276
}
 
277
/* }}} */
 
278
/* VIM settings {{{
 
279
 * Local variables:
 
280
 * tab-width: 4
 
281
 * c-basic-offset: 4
 
282
 * soft-stop-width: 4
 
283
 * c indent on
 
284
 * expandtab on
 
285
 * End:
 
286
 * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
 
287
 * vim<600: sw=4 ts=4
 
288
 * }}} */
 
289
?>
 
 
b'\\ No newline at end of file'