~ubuntu-branches/ubuntu/utopic/php-net-dns2/utopic-proposed

« back to all changes in this revision

Viewing changes to Net_DNS2-1.3.2/Net/DNS2/RR/WKS.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2014-02-21 16:59:58 UTC
  • Revision ID: package-import@ubuntu.com-20140221165958-tyc92gchfhho9660
Tags: upstream-1.3.2
ImportĀ upstreamĀ versionĀ 1.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
 
3
 
 
4
/**
 
5
 * DNS Library for handling lookups and updates. 
 
6
 *
 
7
 * PHP Version 5
 
8
 *
 
9
 * Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
 
10
 * All rights reserved.
 
11
 *
 
12
 * Redistribution and use in source and binary forms, with or without
 
13
 * modification, are permitted provided that the following conditions
 
14
 * are met:
 
15
 *
 
16
 *   * Redistributions of source code must retain the above copyright
 
17
 *     notice, this list of conditions and the following disclaimer.
 
18
 *
 
19
 *   * Redistributions in binary form must reproduce the above copyright
 
20
 *     notice, this list of conditions and the following disclaimer in
 
21
 *     the documentation and/or other materials provided with the
 
22
 *     distribution.
 
23
 *
 
24
 *   * Neither the name of Mike Pultz nor the names of his contributors 
 
25
 *     may be used to endorse or promote products derived from this 
 
26
 *     software without specific prior written permission.
 
27
 *
 
28
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
29
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
30
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 
31
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 
32
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 
33
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 
34
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
35
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
36
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
 
37
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 
38
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
39
 * POSSIBILITY OF SUCH DAMAGE.
 
40
 *
 
41
 * @category  Networking
 
42
 * @package   Net_DNS2
 
43
 * @author    Mike Pultz <mike@mikepultz.com>
 
44
 * @copyright 2010 Mike Pultz <mike@mikepultz.com>
 
45
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
 
46
 * @version   SVN: $Id: WKS.php 179 2012-11-23 05:49:01Z mike.pultz $
 
47
 * @link      http://pear.php.net/package/Net_DNS2
 
48
 * @since     File available since Release 1.0.1
 
49
 *
 
50
 */
 
51
 
 
52
/**
 
53
 * WKS Resource Record - RFC1035 section 3.4.2
 
54
 *
 
55
 *   +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 
56
 *   |                    ADDRESS                    |
 
57
 *   +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 
58
 *   |       PROTOCOL        |                       |
 
59
 *   +--+--+--+--+--+--+--+--+                       |
 
60
 *   |                                               |
 
61
 *   /                   <BIT MAP>                   /
 
62
 *   /                                               /
 
63
 *   +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
 
64
 *
 
65
 * @category Networking
 
66
 * @package  Net_DNS2
 
67
 * @author   Mike Pultz <mike@mikepultz.com>
 
68
 * @license  http://www.opensource.org/licenses/bsd-license.php  BSD License
 
69
 * @link     http://pear.php.net/package/Net_DNS2
 
70
 * @see      Net_DNS2_RR
 
71
 *
 
72
 */
 
73
class Net_DNS2_RR_WKS extends Net_DNS2_RR
 
74
{
 
75
    /*
 
76
     * The IP address of the service
 
77
     */
 
78
    public $address;
 
79
 
 
80
    /*
 
81
     * The protocol of the service
 
82
     */
 
83
    public $protocol;
 
84
 
 
85
    /*
 
86
     * bitmap
 
87
     */
 
88
    public $bitmap = array();
 
89
 
 
90
    /**
 
91
     * method to return the rdata portion of the packet as a string
 
92
     *
 
93
     * @return  string
 
94
     * @access  protected
 
95
     *
 
96
     */
 
97
    protected function rrToString()
 
98
    {
 
99
        $data = $this->address . ' ' . $this->protocol;
 
100
 
 
101
        foreach ($this->bitmap as $port) {
 
102
            $data .= ' ' . $port;
 
103
        }
 
104
 
 
105
        return $data;
 
106
    }
 
107
 
 
108
    /**
 
109
     * parses the rdata portion from a standard DNS config line
 
110
     *
 
111
     * @param array $rdata a string split line of values for the rdata
 
112
     *
 
113
     * @return boolean
 
114
     * @access protected
 
115
     *
 
116
     */
 
117
    protected function rrFromString(array $rdata)
 
118
    {
 
119
        $this->address  = strtolower(trim(array_shift($rdata), '.'));
 
120
        $this->protocol = array_shift($rdata);
 
121
        $this->bitmap   = $rdata;
 
122
 
 
123
        return true;
 
124
    }
 
125
 
 
126
    /**
 
127
     * parses the rdata of the Net_DNS2_Packet object
 
128
     *
 
129
     * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
 
130
     *
 
131
     * @return boolean
 
132
     * @access protected
 
133
     *
 
134
     */
 
135
    protected function rrSet(Net_DNS2_Packet &$packet)
 
136
    {
 
137
        if ($this->rdlength > 0) {
 
138
 
 
139
            //
 
140
            // get the address and protocol value
 
141
            //
 
142
            $x = unpack('Naddress/Cprotocol', $this->rdata);
 
143
 
 
144
            $this->address  = long2ip($x['address']);
 
145
            $this->protocol = $x['protocol'];
 
146
 
 
147
            //
 
148
            // unpack the port list bitmap
 
149
            //
 
150
            $port = 0;
 
151
            foreach (unpack('@5/C*', $this->rdata) as $set) {
 
152
 
 
153
                $s = sprintf('%08b', $set);
 
154
 
 
155
                for ($i=0; $i<8; $i++, $port++) {
 
156
                    if ($s[$i] == '1') {
 
157
                        $this->bitmap[] = $port;
 
158
                    }
 
159
                }
 
160
            }
 
161
 
 
162
            return true;
 
163
        }
 
164
 
 
165
        return false;
 
166
    }
 
167
 
 
168
    /**
 
169
     * returns the rdata portion of the DNS packet
 
170
     *
 
171
     * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for
 
172
     *                                 compressed names
 
173
     *
 
174
     * @return mixed                   either returns a binary packed
 
175
     *                                 string or null on failure
 
176
     * @access protected
 
177
     *
 
178
     */
 
179
    protected function rrGet(Net_DNS2_Packet &$packet)
 
180
    {
 
181
        if (strlen($this->address) > 0) {
 
182
 
 
183
            $data = pack('NC', ip2long($this->address), $this->protocol);
 
184
 
 
185
            $ports = array();
 
186
 
 
187
            $n = 0;
 
188
            foreach ($this->bitmap as $port) {
 
189
                $ports[$port] = 1;
 
190
 
 
191
                if ($port > $n) {
 
192
                    $n = $port;
 
193
                }
 
194
            }
 
195
            for ($i=0; $i<ceil($n/8)*8; $i++) {
 
196
                if (!isset($ports[$i])) {
 
197
                    $ports[$i] = 0;
 
198
                }
 
199
            }
 
200
 
 
201
            ksort($ports);
 
202
 
 
203
            $string = '';
 
204
            $n = 0;
 
205
 
 
206
            foreach ($ports as $s) {
 
207
 
 
208
                $string .= $s;
 
209
                $n++;
 
210
 
 
211
                if ($n == 8) {
 
212
 
 
213
                    $data .= chr(bindec($string));
 
214
                    $string = '';
 
215
                    $n = 0;
 
216
                }
 
217
            }
 
218
 
 
219
            $packet->offset += strlen($data);
 
220
 
 
221
            return $data;
 
222
        }
 
223
 
 
224
        return null;
 
225
    }
 
226
}
 
227
 
 
228
/*
 
229
 * Local variables:
 
230
 * tab-width: 4
 
231
 * c-basic-offset: 4
 
232
 * c-hanging-comment-ender-p: nil
 
233
 * End:
 
234
 */
 
235
?>