1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2016-present Ganbaro Digital Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package OperatingSystem/IpRoute/Classifiers |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2016-present Ganbaro Digital Ltd www.ganbarodigital.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://code.ganbarodigital.com/php-operating-system |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace GanbaroDigital\OperatingSystem\IpRoute\Classifiers; |
45
|
|
|
|
46
|
|
|
use GanbaroDigital\OperatingSystem\Exceptions\E4xx_CannotClassifyIpAddrLine; |
47
|
|
|
use GanbaroDigital\OperatingSystem\Exceptions\E4xx_UnsupportedType; |
48
|
|
|
use GanbaroDigital\Reflection\Requirements\RequireStringy; |
49
|
|
|
|
50
|
|
|
class ClassifyIpAddrLine |
51
|
|
|
{ |
52
|
|
|
const LINK_START = 1; |
53
|
|
|
const LINK_ETHER = 2; |
54
|
|
|
const LINK_LOOPBACK = 3; |
55
|
|
|
const LINK_NONE = 4; |
56
|
|
|
const INET_START = 50; |
57
|
|
|
const INET6_START = 51; |
58
|
|
|
const INET_OPTIONS = 52; |
59
|
|
|
const TEST_NEVER_SUPPORTED = 100; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* a list of regexes to use to classify an address line |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
private static $typeMap = [ |
66
|
|
|
'|^\d+:|' => self::LINK_START, |
67
|
|
|
'|link/ether|' => self::LINK_ETHER, |
68
|
|
|
'|link/loopback|' => self::LINK_LOOPBACK, |
69
|
|
|
'|link/none|' => self::LINK_NONE, |
70
|
|
|
'|^\s{0,}inet [0-9{1,3}\.]+/|' => self::INET_START, |
71
|
|
|
'|^\s{0,}inet6 [0-9a-f:]+/|' => self::INET6_START, |
72
|
|
|
'|^\s{0,}valid_lft |' => self::INET_OPTIONS, |
73
|
|
|
'|TEST LINE, NEVER SUPPORTED|' => self::TEST_NEVER_SUPPORTED, |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* given a single line from the output of the 'ip addr show' or 'ip link show' |
78
|
|
|
* commands, tell us what kind of line we are looking at |
79
|
|
|
* |
80
|
|
|
* @param string $line |
81
|
|
|
* the line to classify |
82
|
|
|
* @return int |
83
|
|
|
* one of this class's constants |
84
|
|
|
*/ |
85
|
|
|
public function __invoke($line) |
86
|
|
|
{ |
87
|
|
|
return self::from($line); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* given a single line from the output of the 'ip addr show' or 'ip link show' |
92
|
|
|
* commands, tell us what kind of line we are looking at |
93
|
|
|
* |
94
|
|
|
* @param string $line |
95
|
|
|
* the line to classify |
96
|
|
|
* @return int |
97
|
|
|
* one of this class's constants |
98
|
|
|
*/ |
99
|
|
|
public static function from($line) |
100
|
|
|
{ |
101
|
|
|
// robustness! |
102
|
|
|
RequireStringy::check($line, E4xx_UnsupportedType::class); |
103
|
|
|
$line = (string)$line; |
104
|
|
|
|
105
|
|
|
// what do we have? |
106
|
|
|
foreach (self::$typeMap as $regex => $lineType) { |
107
|
|
|
if (preg_match($regex, $line)) { |
108
|
|
|
return $lineType; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// if we get here, we do not understand what we are looking at |
113
|
|
|
throw new E4xx_CannotClassifyIpAddrLine($line); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|