From 3291888a54321f69e0ff7a050257b5506181712f Mon Sep 17 00:00:00 2001 From: Dany Maillard Date: Wed, 22 Feb 2017 10:04:24 +0100 Subject: [PATCH] Improve addresses (#15) * 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 --- src/Nmap/Address.php | 55 +++++++++++++++++++++++++ src/Nmap/Host.php | 46 +++++++++++++++++++-- src/Nmap/Nmap.php | 16 ++++++- tests/Nmap/Tests/Fixtures/test_scan.xml | 1 + tests/Nmap/Tests/NmapTest.php | 21 +++++++++- 5 files changed, 132 insertions(+), 7 deletions(-) create mode 100644 src/Nmap/Address.php diff --git a/src/Nmap/Address.php b/src/Nmap/Address.php new file mode 100644 index 0000000..f96a94c --- /dev/null +++ b/src/Nmap/Address.php @@ -0,0 +1,55 @@ + + */ +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; + } +} diff --git a/src/Nmap/Host.php b/src/Nmap/Host.php index b757009..8fc2352 100644 --- a/src/Nmap/Host.php +++ b/src/Nmap/Host.php @@ -19,7 +19,7 @@ class Host const STATE_DOWN = 'down'; - private $address; + private $addresses; private $state; @@ -27,9 +27,9 @@ class Host 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; @@ -37,10 +37,48 @@ public function __construct($address, $state, array $hostnames = array(), array /** * @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); } /** diff --git a/src/Nmap/Nmap.php b/src/Nmap/Nmap.php index d5eec58..7310c9c 100644 --- a/src/Nmap/Nmap.php +++ b/src/Nmap/Nmap.php @@ -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() @@ -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; + } } diff --git a/tests/Nmap/Tests/Fixtures/test_scan.xml b/tests/Nmap/Tests/Fixtures/test_scan.xml index 8a8cd88..1470e27 100644 --- a/tests/Nmap/Tests/Fixtures/test_scan.xml +++ b/tests/Nmap/Tests/Fixtures/test_scan.xml @@ -5,6 +5,7 @@
+
diff --git a/tests/Nmap/Tests/NmapTest.php b/tests/Nmap/Tests/NmapTest.php index ee24b3f..2c4c362 100644 --- a/tests/Nmap/Tests/NmapTest.php +++ b/tests/Nmap/Tests/NmapTest.php @@ -2,6 +2,7 @@ namespace Nmap\Tests; +use Nmap\Address; use Nmap\Host; use Nmap\Nmap; use Nmap\Port; @@ -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(); @@ -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();