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$
47 * @link      http://pear.php.net/package/Net_DNS2
48 * @since     File available since Release 0.6.0
49 *
50 */
51
52/*
53 * check to see if the socket defines exist; if they don't, then define them
54 */
55if (defined('SOCK_STREAM') == false) {
56    define('SOCK_STREAM', 1);
57}
58if (defined('SOCK_DGRAM') == false) {
59    define('SOCK_DGRAM', 2);
60}
61
62/**
63 * This is the abstract base class for the two sockets classes; this simply
64 * provides the class definition for the two sockets classes.
65 *
66 * @category Networking
67 * @package  Net_DNS2
68 * @author   Mike Pultz <mike@mikepultz.com>
69 * @license  http://www.opensource.org/licenses/bsd-license.php  BSD License
70 * @link     http://pear.php.net/package/Net_DNS2
71 * @see      Net_DNS2_Socket_Sockets, Net_DNS2_Socket_Streams
72 *
73 */
74abstract class Net_DNS2_Socket
75{
76    protected $sock;
77    protected $type;
78    protected $host;
79    protected $port;
80    protected $timeout;
81
82    protected $local_host;
83    protected $local_port;
84
85    public $last_error;
86
87    /*
88     * type of sockets
89     */
90    const SOCK_STREAM   = SOCK_STREAM;
91    const SOCK_DGRAM    = SOCK_DGRAM;
92
93    /**
94     * constructor - set the port details
95     *
96     * @param integer $type    the socket type
97     * @param string  $host    the IP address of the DNS server to connect to
98     * @param integer $port    the port of the DNS server to connect to
99     * @param integer $timeout the timeout value to use for socket functions
100     *
101     * @access public
102     *
103     */
104    public function __construct($type, $host, $port, $timeout)
105    {
106        $this->type     = $type;
107        $this->host     = $host;
108        $this->port     = $port;
109        $this->timeout  = $timeout;
110    }
111
112    /**
113     * destructor
114     *
115     * @access public
116     */
117    public function __destruct()
118    {
119        $this->close();
120    }
121
122    /**
123     * sets the local address/port for the socket to bind to
124     *
125     * @param string $address the local IP address to bind to
126     * @param mixed  $port    the local port to bind to, or 0 to let the socket
127     *                        function select a port
128     *
129     * @return boolean
130     * @access public
131     *
132     */
133    public function bindAddress($address, $port = 0)
134    {
135        $this->local_host = $address;
136        $this->local_port = $port;
137
138        return true;
139    }
140
141    /**
142     * opens a socket connection to the DNS server
143     *
144     * @return boolean
145     * @access public
146     *
147     */
148    abstract public function open();
149
150    /**
151     * closes a socket connection to the DNS server
152     *
153     * @return boolean
154     * @access public
155     *
156     */
157    abstract public function close();
158
159    /**
160     * writes the given string to the DNS server socket
161     *
162     * @param string $data a binary packed DNS packet
163     *
164     * @return boolean
165     * @access public
166     *
167     */
168    abstract public function write($data);
169
170    /**
171     * reads a response from a DNS server
172     *
173     * @param integer &$size    the size of the DNS packet read is passed back
174     * @param integer $max_size the max data size returned.
175     *
176     * @return mixed         returns the data on success and false on error
177     * @access public
178     *
179     */
180    abstract public function read(&$size, $max_size);
181}
182
183/*
184 * Local variables:
185 * tab-width: 4
186 * c-basic-offset: 4
187 * c-hanging-comment-ender-p: nil
188 * End:
189 */
190?>
191