1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4/**
5 * Host object
6 *
7 * PHP version 5
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA
22 *
23 * @category  Net
24 * @package   Net_Nmap
25 * @author    Luca Corbo <lucor@ortro.net>
26 * @copyright 2008 Luca Corbo
27 * @license   GNU/LGPL v2.1
28 * @link      http://pear.php.net/packages/Net_Nmap
29 */
30
31/**
32 * Host object
33 *
34 * @category  Net
35 * @package   Net_Nmap
36 * @author    Luca Corbo <lucor@ortro.net>
37 * @copyright 2008 Luca Corbo
38 * @license   GNU/LGPL v2.1
39 * @link      http://pear.php.net/packages/Net_Nmap
40 */
41class Net_Nmap_Host
42{
43
44    /**
45     * Status of the Host
46     * @var string
47     */
48    private $_status;
49
50    /**
51     * Contains all discovered addresses grouped by type
52     * Example:
53     * Array ([mac]  => Array ([0] => 00:19:E3:07:D5:37)
54     *        [ipv4] => Array ([0] => 192.168.1.100
55     *                         [1] => 192.168.1.112))
56     * @var array
57     */
58    private $_addresses = array();
59
60    /**
61     * Contains all discovered hostnames
62     * @var array
63     */
64    private $_hostnames = array();
65
66    /**
67     * Contains the name of the discovered Operating System
68     * and the relative accuracy.
69     * Elements are sorted by decreasing accuracy
70     * @var array
71     */
72    private $_os = array();
73
74    /**
75     * Contains all discovered Service objects
76     * @var ArrayIterator
77     */
78    private $_services;
79
80    /**
81     * Constructor
82     *
83     * @return void
84     */
85    public function __construct()
86    {
87        $this->_services = new ArrayIterator();
88    }
89
90    /**
91     * Returns the value of the discovered address
92     * By default the first ipv4 address is returned
93     *
94     * @param string $type  Type of address (i.e. mac, ipv4, ipv6)
95     * @param int    $index The index number of the parameter to obtain
96     *
97     * @return string
98     */
99    public function getAddress($type = 'ipv4', $index = 0)
100    {
101        return $this->_addresses[$type][$index];
102    }
103
104    /**
105     * Add an address to the addresses container
106     *
107     * @param string $type  Type of address (i.e. mac, ipv4, ipv6)
108     * @param string $value Address value
109     *
110     * @return void
111     */
112    public function addAddress($type, $value)
113    {
114        $this->_addresses = array_merge_recursive(
115            $this->_addresses,
116            array($type => array($value))
117        );
118    }
119
120    /**
121     * Returns the value of the discovered hostname
122     * By default the first hostname is returned
123     *
124     * @param int $index The index number of the parameter to obtain
125     *
126     * @return string
127     */
128    public function getHostname($index = 0)
129    {
130        if (count($this->_hostnames) == 0) {
131            return 'unknown';
132        }
133        return $this->_hostnames[$index];
134    }
135
136    /**
137     * Add a hostname to the hostnames container
138     *
139     * @param string $value hostaname value
140     *
141     * @return void
142     */
143    public function addHostname($value)
144    {
145          $this->_hostnames[] = $value;
146    }
147
148    /**
149     * Add a service object to the services container
150     *
151     * @param object $service Service object
152     *
153     * @return void
154     */
155    public function addService($service)
156    {
157        $this->_services->append($service);
158    }
159
160    /**
161     * Returns the discovered services
162     *
163     * @return ArrayIterator
164     */
165    public function getServices()
166    {
167        return $this->_services;
168    }
169
170    /**
171     * Add an the accuracy ant the OS name to the OS container
172     *
173     * @param string $accuracy accuracy value
174     * @param string $name     OS name value
175     *
176     * @return void
177     */
178    public function addOS($accuracy, $name)
179    {
180        $this->_os[] = array('accuracy'=> $accuracy, 'name' => $name);
181    }
182
183    /**
184     * Returns the name of discovered OS with the highest accuracy value or
185     * the "Too many fingerprint" message if no OS is matched.
186     *
187     * @return string
188     */
189    public function getOS()
190    {
191        if (count($this->_os) == 0) {
192            return 'Too many fingerprints match this host to give specific OS details';
193        }
194        return $this->_os[0]['name'];
195    }
196
197    /**
198     * Returns the OS container the discovered OS
199     * All informations are sorted by decreasing accuracy
200     *
201     * @return array
202     */
203    public function getAllOS()
204    {
205        return $this->_os;
206    }
207
208    /**
209     * Set the status of the Host
210     *
211     * @param string $status Host status
212     *
213     * @return void
214     */
215    public function setStatus($status)
216    {
217        $this->_status = $status;
218    }
219
220    /**
221     * Returns the status of the Host
222     *
223     * @return string
224     */
225    public function getStatus()
226    {
227        return $this->_status;
228    }
229}
230