Skip to content
This repository has been archived by the owner on Jan 31, 2022. It is now read-only.

Commit

Permalink
Improve addresses (#15)
Browse files Browse the repository at this point in the history
* Enhance addresses

* Add some tests

* Fix bug in php 5.3

* Improve code coverage by removing setters of address

* Use ip/mac address as key of addresses array

* Add some tests
  • Loading branch information
maidmaid authored and willdurand committed Feb 22, 2017
1 parent 0ad808a commit 3291888
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 7 deletions.
55 changes: 55 additions & 0 deletions src/Nmap/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* This file is part of the nmap package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Nmap;

/**
* @author Dany Maillard <[email protected]>
*/
class Address
{
CONST TYPE_IPV4 = 'ipv4';
CONST TYPE_MAC = 'mac';

private $address;
private $type;
private $vendor;

public function __construct($address, $type = self::TYPE_IPV4, $vendor = '')
{
$this->address = $address;
$this->type = $type;
$this->vendor = $vendor;
}

/**
* @return string
*/
public function getAddress()
{
return $this->address;
}

/**
* @return string
*/
public function getType()
{
return $this->type;
}

/**
* @return string
*/
public function getVendor()
{
return $this->vendor;
}
}
46 changes: 42 additions & 4 deletions src/Nmap/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,66 @@ class Host

const STATE_DOWN = 'down';

private $address;
private $addresses;

private $state;

private $hostnames;

private $ports;

public function __construct($address, $state, array $hostnames = array(), array $ports = array())
public function __construct($addresses, $state, array $hostnames = array(), array $ports = array())
{
$this->address = $address;
$this->addresses = $addresses;
$this->state = $state;
$this->hostnames = $hostnames;
$this->ports = $ports;
}

/**
* @return string
*
* @deprecated The Host::getAddress() method is deprecated since 0.4 version. Use Host::getIpv4Addresses() instead.
*/
public function getAddress()
{
return $this->address;
return current($this->getIpv4Addresses())->getAddress();
}

/**
* @return Address[]
*/
public function getAddresses()
{
return $this->addresses;
}

/**
* @param string $type
*
* @return Address[]
*/
private function getAddressesByType($type)
{
return array_filter($this->addresses, function (Address $address) use ($type) {
return $address->getType() === $type;
});
}

/**
* @return Address[]
*/
public function getIpv4Addresses()
{
return $this->getAddressesByType(Address::TYPE_IPV4);
}

/**
* @return Address[]
*/
public function getMacAddresses()
{
return $this->getAddressesByType(Address::TYPE_MAC);
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/Nmap/Nmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private function parseOutputFile($xmlFile)
$hosts = array();
foreach ($xml->host as $host) {
$hosts[] = new Host(
(string) $host->address->attributes()->addr,
$this->parseAddresses($host),
(string) $host->status->attributes()->state,
isset($host->hostnames) ? $this->parseHostnames($host->hostnames->hostname) : array(),
isset($host->ports) ? $this->parsePorts($host->ports->port) : array()
Expand Down Expand Up @@ -240,4 +240,18 @@ private function parsePorts(\SimpleXMLElement $xmlPorts)

return $ports;
}

private function parseAddresses(\SimpleXMLElement $host)
{
$addresses = array();
foreach ($host->xpath('./address') as $address) {
$addresses[(string) $address->attributes()->addr] = new Address(
(string) $address->attributes()->addr,
(string) $address->attributes()->addrtype,
isset($address->attributes()->vendor) ? (string) $address->attributes()->vendor : ''
);
}

return $addresses;
}
}
1 change: 1 addition & 0 deletions tests/Nmap/Tests/Fixtures/test_scan.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<debugging level="0"/>
<host starttime="1379937590" endtime="1379937601"><status state="up" reason="conn-refused" reason_ttl="0"/>
<address addr="204.232.175.78" addrtype="ipv4"/>
<address addr="00:C0:49:00:11:22" addrtype="mac" vendor="U.S. Robotics"/>
<hostnames>
<hostname name="williamdurand.fr" type="user"/>
<hostname name="pages.github.com" type="PTR"/>
Expand Down
21 changes: 19 additions & 2 deletions tests/Nmap/Tests/NmapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Nmap\Tests;

use Nmap\Address;
use Nmap\Host;
use Nmap\Nmap;
use Nmap\Port;
Expand All @@ -26,7 +27,18 @@ public function testScan()

$host = current($hosts);

$this->assertEquals('204.232.175.78', $host->getAddress());
$this->assertEquals('204.232.175.78', $host->getAddress()); // deprecated
$this->assertCount(2, $host->getAddresses());
$this->assertEquals('204.232.175.78', current($host->getIpv4Addresses())->getAddress());
$this->assertArrayHasKey('204.232.175.78', $host->getIpv4Addresses());
$this->assertArrayNotHasKey('00:C0:49:00:11:22', $host->getIpv4Addresses());
$this->assertEquals(Address::TYPE_IPV4, current($host->getIpv4Addresses())->getType());
$this->assertEmpty(current($host->getIpv4Addresses())->getVendor());
$this->assertEquals('00:C0:49:00:11:22', current($host->getMacAddresses())->getAddress());
$this->assertArrayHasKey('00:C0:49:00:11:22', $host->getMacAddresses());
$this->assertArrayNotHasKey('204.232.175.78', $host->getMacAddresses());
$this->assertEquals(Address::TYPE_MAC, current($host->getMacAddresses())->getType());
$this->assertEquals('U.S. Robotics', current($host->getMacAddresses())->getVendor());
$this->assertEquals(Host::STATE_UP, $host->getState());

$hostnames = $host->getHostnames();
Expand Down Expand Up @@ -66,7 +78,12 @@ public function testScanSpecifyingPorts()

$host = current($hosts);

$this->assertEquals('204.232.175.78', $host->getAddress());
$this->assertEquals('204.232.175.78', $host->getAddress()); // deprecated
$this->assertCount(1, $host->getAddresses());
$this->assertEquals('204.232.175.78', current($host->getIpv4Addresses())->getAddress());
$this->assertArrayHasKey('204.232.175.78', $host->getIpv4Addresses());
$this->assertEquals(Address::TYPE_IPV4, current($host->getIpv4Addresses())->getType());
$this->assertEmpty(current($host->getIpv4Addresses())->getVendor());
$this->assertEquals(Host::STATE_UP, $host->getState());

$hostnames = $host->getHostnames();
Expand Down

0 comments on commit 3291888

Please sign in to comment.