Skip to content

Commit

Permalink
[NF] OUI utility class to download and parse the IEEE OUI data file -…
Browse files Browse the repository at this point in the history
… for issue #87
  • Loading branch information
barryo committed Feb 17, 2014
1 parent 3766c84 commit ecfe124
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
92 changes: 92 additions & 0 deletions library/IXP/OUI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php


/*
* Copyright (C) 2009-2014 Internet Neutral Exchange Association Limited.
* All Rights Reserved.
*
* This file is part of IXP Manager.
*
* IXP Manager is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version v2.0 of the License.
*
* IXP Manager is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License v2.0
* along with IXP Manager. If not, see:
*
* http://www.gnu.org/licenses/gpl-2.0.html
*/

/**
* OUI functions
*
* @author Barry O'Donovan <[email protected]>
* @package IXP_OUI3
*/
class IXP_OUI
{
/**
* Where to get the OUI list from
* @var string Where to get the OUI list from
*/
public $file = 'http://standards.ieee.org/develop/regauth/oui/oui.txt';

/**
* Raw OUI data
*/
private $raw = null;

/**
* Processed OUIs array as [ 'oui' => 'organisation', ... ]
*/
private $ouis = null;

/**
* Constructor
*
* @param string $file Where to get the OUI list from
*/
public function __construct( $file = false )
{
if( $file )
$this->file = $file;
}

/**
* Load the raw OUI data from the specificed location
*
* @return IXP_OUI An instance of this class for fluent interfaces
*/
public function loadList()
{
$this->raw = @file_get_contents( $this->file );

if( $this->raw === false )
throw new IXP_Exception( 'IXP_OUI - could not load OUI list from ' . $this->file );

return $this;
}

public function processRawData( $data = false )
{
if( $data == false && $this->raw === null )
throw new IXP_Exception( 'IXP_OUI - cannot process when no data has been loaded or provided' );

if( $data == false )
$data = $this->raw;

$this->ouis = [];
foreach( explode( "\n", $data ) as $line )
{
if( preg_match( "/^\s*([0-9A-F]{6})\s+\(base 16\)\s+(.*)$/", $line, $matches ) )
$this->ouis[ strtolower( $matches[1] ) ] = $matches[2];
}

return $this->ouis;
}
}
77 changes: 77 additions & 0 deletions tests/IXP/OUITest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

require __DIR__ . '/../../library/Zend/Exception.php';
require __DIR__ . '/../../library/IXP/Exception.php';
require __DIR__ . '/../../library/IXP/OUI.php';

/**
* PHPUnit test class to test the IXP_OUI class
*/
class OUITest extends PHPUnit_Framework_TestCase
{
public $sampleRawData = <<<END_DATA
OUI/MA-L Organization
company_id Organization
Address
00-00-00 (hex) XEROX CORPORATION
000000 (base 16) XEROX CORPORATION
M/S 105-50C
800 PHILLIPS ROAD
WEBSTER NY 14580
UNITED STATES
00-00-01 (hex) XEROX CORPORATION
000001 (base 16) XEROX CORPORATION
ZEROX SYSTEMS INSTITUTE
M/S 105-50C 800 PHILLIPS ROAD
WEBSTER NY 14580
UNITED STATES
00-00-4C (hex) NEC CORPORATION
00004C (base 16) NEC CORPORATION
7-1 SHIBA 5-CHOME
MINATO-KU
TOKYO 108-01
JAPAN
END_DATA;

public function testParse()
{
$oui = new IXP_OUI();
$parsed = $oui->processRawData( $this->sampleRawData );
$this->assertTrue( is_array( $parsed ) );
$this->assertArrayHasKey( '000000', $parsed );
$this->assertArrayHasKey( '000001', $parsed );
$this->assertArrayHasKey( '00004c', $parsed );

$this->assertEquals( $parsed[ '000000' ], 'XEROX CORPORATION' );
$this->assertEquals( $parsed[ '000001' ], 'XEROX CORPORATION' );
$this->assertEquals( $parsed[ '00004c' ], 'NEC CORPORATION' );

$this->assertEquals( 3, count( $parsed ) );
}


/**
* @expectedException IXP_Exception
*/
public function testBadFile()
{
$oui = new IXP_OUI( '/path/that/does/not/exist/I/hope.txt' );
$oui->loadList();
}

public function testDownloadDefault()
{
$oui = new IXP_OUI();
$this->assertInstanceOf( 'IXP_OUI', $oui->loadList() );
}



}

0 comments on commit ecfe124

Please sign in to comment.