|
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/NetInterface/Parsers |
|
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\Parsers; |
|
45
|
|
|
|
|
46
|
|
|
use GanbaroDigital\ArrayTools\Filters\ExtractFirstItem; |
|
47
|
|
|
use GanbaroDigital\ArrayTools\Parsers\ConvertKeyValuePairsToArray; |
|
48
|
|
|
use GanbaroDigital\ArrayTools\ValueBuilders\ConvertToArray; |
|
49
|
|
|
use GanbaroDigital\OperatingSystem\Exceptions\E4xx_CannotParseInetAddressLine; |
|
50
|
|
|
use GanbaroDigital\OperatingSystem\Exceptions\E4xx_UnsupportedType; |
|
51
|
|
|
use GanbaroDigital\OperatingSystem\IpRoute\Classifiers\ClassifyIpAddrLine; |
|
52
|
|
|
use GanbaroDigital\OperatingSystem\NetInterfaces\Values\InetAddress; |
|
53
|
|
|
use GanbaroDigital\Reflection\Maps\MapTypeToMethod; |
|
54
|
|
|
use GanbaroDigital\Reflection\ValueBuilders\SimpleType; |
|
55
|
|
|
|
|
56
|
|
|
class ParseInetAddress |
|
57
|
|
|
{ |
|
58
|
|
|
/** |
|
59
|
|
|
* parse an inet (IPv4) address block from the output of 'ip addr show' |
|
60
|
|
|
* |
|
61
|
|
|
* @param mixed $inetLines |
|
62
|
|
|
* the line(s) to parse |
|
63
|
|
|
* @return InetAddress |
|
64
|
|
|
* the inet address defined in the output |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __invoke($inetLines) |
|
67
|
|
|
{ |
|
68
|
|
|
return self::from($inetLines); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* parse an inet (IPv4) address block from the output of 'ip addr show' |
|
73
|
|
|
* |
|
74
|
|
|
* @param mixed $inetLines |
|
75
|
|
|
* the line(s) to parse |
|
76
|
|
|
* @return InetAddress |
|
77
|
|
|
* the inet address defined in the output |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function from($inetLines) |
|
80
|
|
|
{ |
|
81
|
|
|
$method = MapTypeToMethod::using($inetLines, self::$dispatchMap); |
|
82
|
|
|
return self::$method($inetLines); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* a map of which types we support, and how to process them |
|
87
|
|
|
* @var array |
|
88
|
|
|
*/ |
|
89
|
|
|
private static $dispatchMap = [ |
|
90
|
|
|
"String" => "fromString", |
|
91
|
|
|
"Traversable" => "fromTraversable", |
|
92
|
|
|
]; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* called when we've been given a data type that we do not support |
|
96
|
|
|
* |
|
97
|
|
|
* @param mixed $output |
|
98
|
|
|
* the unsupported data |
|
99
|
|
|
* @return void |
|
100
|
|
|
* @throws E4xx_UnsupportedType |
|
101
|
|
|
*/ |
|
102
|
|
|
private static function nothingMatchesTheInputType($output) |
|
103
|
|
|
{ |
|
104
|
|
|
throw new E4xx_UnsupportedType(SimpleType::from($output)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* parse an inet (IPv4) address block from the output of 'ip addr show' |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $inetLines |
|
111
|
|
|
* the line(s) to parse |
|
112
|
|
|
* @return InetAddress |
|
113
|
|
|
* the inet address defined in the output |
|
114
|
|
|
*/ |
|
115
|
|
|
private static function fromString($inetLines) |
|
116
|
|
|
{ |
|
117
|
|
|
$lines = explode("\n", $inetLines); |
|
118
|
|
|
return self::fromTraversable($lines); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* parse an inet (IPv4) address block from the output of 'ip addr show' |
|
123
|
|
|
* |
|
124
|
|
|
* @param array|Traversable $inetLines |
|
125
|
|
|
* the line(s) to parse |
|
126
|
|
|
* @return InetAddress |
|
127
|
|
|
* the inet address defined in the output |
|
128
|
|
|
*/ |
|
129
|
|
|
private static function fromTraversable($inetLines) |
|
130
|
|
|
{ |
|
131
|
|
|
// we want a real PHP array for this |
|
132
|
|
|
$inetLines = ConvertToArray::from($inetLines); |
|
133
|
|
|
|
|
134
|
|
|
// line 0 is the inet line itself |
|
135
|
|
|
$inetDetails = self::parseFirstLine(array_shift($inetLines)); |
|
136
|
|
|
|
|
137
|
|
|
// if there is a line 1, it contains additional flags |
|
138
|
|
|
while (!empty($inetLines)) { |
|
139
|
|
|
$inetDetails['properties'] = array_merge(self::parseSecondLine(array_shift($inetLines)), $inetDetails['properties']); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
// now to convert this into an InetAddress value |
|
143
|
|
|
$retval = new InetAddress( |
|
144
|
|
|
$inetDetails['address'], |
|
145
|
|
|
$inetDetails['netmask'], |
|
146
|
|
|
$inetDetails['broadcast'], |
|
147
|
|
|
$inetDetails['scope'], |
|
148
|
|
|
$inetDetails['addressLabel'], |
|
149
|
|
|
$inetDetails['linkDevice'], |
|
150
|
|
|
$inetDetails['properties'] |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
// all done |
|
154
|
|
|
return $retval; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* extract data from the first line of the inet address definition |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $line |
|
161
|
|
|
* the line to parse |
|
162
|
|
|
* @return array |
|
163
|
|
|
* the extracted data |
|
164
|
|
|
*/ |
|
165
|
|
|
private static function parseFirstLine($line) |
|
166
|
|
|
{ |
|
167
|
|
|
// this regex will parse everything we know to expect from the first |
|
168
|
|
|
// line of an inet definition |
|
169
|
|
|
$regex = "~inet (?<addr>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})(?<netmask>/[0-9]{1,3})( brd (?<broadcast>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})){0,1}( scope (?<scope>[a-z]+)){0,1}( (?<addressLabel>secondary|dynamic|deprecated|tentative)){0,1}( (?<linkDevice>.*)){0,1}$~"; |
|
170
|
|
|
$matches = []; |
|
171
|
|
|
if (!preg_match_all($regex, $line, $matches)) { |
|
172
|
|
|
throw new E4xx_CannotParseInetAddressLine($line); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return [ |
|
176
|
|
|
'address' => $matches['addr'][0], |
|
177
|
|
|
'netmask' => $matches['netmask'][0], |
|
178
|
|
|
'broadcast' => ExtractFirstItem::from($matches['broadcast'], null), |
|
179
|
|
|
'scope' => ExtractFirstItem::from($matches['scope'], null), |
|
180
|
|
|
'addressLabel' => ExtractFirstItem::from($matches['addressLabel'], null), |
|
181
|
|
|
'linkDevice' => ExtractFirstItem::from($matches['linkDevice'], null), |
|
182
|
|
|
'properties' => [], |
|
183
|
|
|
]; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* extract data from the second line of the inet address definition |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $line |
|
190
|
|
|
* the line to parse |
|
191
|
|
|
* @return array |
|
192
|
|
|
* the extracted data, as key/value pairs |
|
193
|
|
|
*/ |
|
194
|
|
|
private static function parseSecondLine($line) |
|
195
|
|
|
{ |
|
196
|
|
|
return ConvertKeyValuePairsToArray::from($line, ' ', ' '); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|