ARP.php 4.02 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/**
 *    Copyright (C) 2015 Deciso B.V.
 *
 *    All rights reserved.
 *
 *    Redistribution and use in source and binary forms, with or without
 *    modification, are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 *    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 *    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 *    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *    POSSIBILITY OF SUCH DAMAGE.
 *
 */
29
namespace OPNsense\CaptivePortal;
Ad Schellevis's avatar
Ad Schellevis committed
30

31
use \OPNsense\Core;
32

33 34 35 36
/**
 * Class ARP  provides access to the systems ARP table
 * @package OPNsense\CaptivePortal
 */
37 38
class ARP
{
39 40 41 42 43

    /**
     * pointer to shell object
     * @var \Core\Shell
     */
44
    private $shell;
45 46 47 48

    /**
     * construct new ARP table handlers
     */
49
    public function __construct()
50
    {
51
        $this->shell = new Core\Shell();
52 53 54 55
    }

    /**
     * set static arp entry
56 57
     * @param $ipaddress|string hosts ipaddress
     * @param $mac|string hosts physical address
58
     */
59 60
    public function setStatic($ipaddress, $mac)
    {
61
        // validate input, only set static entries for valid addresses
62 63 64
        if (preg_match('/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/', trim($mac))) {
            if (filter_var($ipaddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
                $this->shell->exec("/usr/sbin/arp -s " . trim($ipaddress) . " " . trim($mac));
65 66 67 68 69 70
            }
        }
    }

    /**
     * drop static arp entry
71
     * @param $ipaddress|string hosts ipaddress
72
     */
73 74
    public function dropStatic($ipaddress)
    {
75
        // validate input, drop arp entry
76 77
        if (filter_var($ipaddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
            $this->shell->exec("/usr/sbin/arp -d " . trim($ipaddress));
78 79 80 81 82 83
        }
    }

    /**
     * Return arp table hashed by mac address
     */
84 85
    public function getMACs()
    {
86 87 88
        $result = array();
        $shell_output = array();
        // execute arp shell command and collect (only valid) info into named array
Franco Fichtner's avatar
Franco Fichtner committed
89
        if ($this->shell->exec('arp -an', false, $shell_output) == 0) {
90 91 92 93
            foreach ($shell_output as $line) {
                $line_parts = explode(" ", $line);
                if (sizeof($line_parts) >= 4) {
                    $ipaddress = substr($line_parts[1], 1, strlen($line_parts[1]) - 2);
94 95 96
                    // reformat mac addresses, sometimes arp return segments without trailing zero's
                    $mac_raw = strtolower($line_parts[3]);
                    $mac = "";
97 98 99 100 101 102 103 104 105
                    foreach (explode(":", $mac_raw) as $segment) {
                        if ($mac != "") {
                            $mac .= ":";
                        }
                        if (strlen($segment) == 1) {
                            $mac .= "0" . $segment;
                        } else {
                            $mac .= $segment;
                        }
106
                    }
107 108
                    if (preg_match('/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/', trim($mac))) {
                        $result[$mac] = array('ip' => $ipaddress);
109 110 111 112 113 114
                    }
                }
            }
        }
        return $result;
    }
115
}