1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
The MIT License (MIT) |
5
|
|
|
|
6
|
|
|
Copyright (c) 2015 Vectorface, Inc. |
7
|
|
|
|
8
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
in the Software without restriction, including without limitation the rights |
11
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
furnished to do so, subject to the following conditions: |
14
|
|
|
|
15
|
|
|
The above copyright notice and this permission notice shall be included in |
16
|
|
|
all copies or substantial portions of the Software. |
17
|
|
|
|
18
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Vectorface\Whip\IpRange; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* A class representing an IPv4 address range. |
31
|
|
|
* @copyright Vectorface, Inc 2015 |
32
|
|
|
* @author Daniel Bruce <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class Ipv4Range implements IpRange |
35
|
|
|
{ |
36
|
|
|
/** the lower value of the range (as a long integer) */ |
37
|
|
|
private int $lowerInt; |
38
|
|
|
|
39
|
|
|
/** the upper value of the range (as a long integer) */ |
40
|
|
|
private int $upperInt; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor for the class. |
44
|
|
|
* |
45
|
|
|
* @param string $range A valid IPv4 range as a string. Supported range styles: |
46
|
|
|
* - CIDR notation (127.0.0.1/24) |
47
|
|
|
* - hyphen notation (127.0.0.1-127.0.0.255) |
48
|
|
|
* - wildcard notation (127.0.0.*) |
49
|
|
|
* - a single specific IP address (127.0.0.1) |
50
|
9 |
|
*/ |
51
|
|
|
public function __construct(string $range) |
52
|
9 |
|
{ |
53
|
9 |
|
$this->computeLowerAndUpperBounds($range); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the lower value of the IPv4 range as a long integer. |
58
|
|
|
* |
59
|
7 |
|
* @return int The lower value of the IPv4 range. |
60
|
|
|
*/ |
61
|
7 |
|
public function getLowerInt() : int |
62
|
|
|
{ |
63
|
|
|
return $this->lowerInt; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the upper value of the IPv4 range as a long integer. |
68
|
6 |
|
* |
69
|
|
|
* @return int The upper value of the IPv4 range. |
70
|
6 |
|
*/ |
71
|
|
|
public function getUpperInt() : int |
72
|
|
|
{ |
73
|
|
|
return $this->upperInt; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns whether a given IP address falls within this range. |
78
|
|
|
* |
79
|
7 |
|
* @param string $ipAddress The given IP address. |
80
|
|
|
* @return bool Returns true if the IP address falls within the range |
81
|
7 |
|
* and false otherwise. |
82
|
7 |
|
*/ |
83
|
|
|
public function containsIp(string $ipAddress) : bool |
84
|
|
|
{ |
85
|
|
|
$ipLong = ip2long($ipAddress); |
86
|
|
|
return ($this->getLowerInt() <= $ipLong) && ($this->getUpperInt() >= $ipLong); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
9 |
|
* Computes the lower and upper bounds of the IPv4 range by parsing the |
91
|
|
|
* range string. |
92
|
9 |
|
* |
93
|
|
|
* @param string $range The IPv4 range as a string. |
94
|
3 |
|
*/ |
95
|
6 |
|
private function computeLowerAndUpperBounds(string $range) : void |
96
|
|
|
{ |
97
|
1 |
|
// support CIDR notation |
98
|
5 |
|
if (str_contains($range, '/')) { |
99
|
|
|
[$this->lowerInt, $this->upperInt] = $this->parseCidrRange($range); |
100
|
1 |
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
4 |
|
// support for IP ranges like '10.0.0.0-10.0.0.255' |
104
|
4 |
|
if (str_contains($range, '-')) { |
105
|
|
|
[$this->lowerInt, $this->upperInt] = $this->parseHyphenRange($range); |
106
|
9 |
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// support for IP ranges like '10.0.*' |
110
|
|
|
if (str_contains($range, '*')) { |
111
|
|
|
[$this->lowerInt, $this->upperInt] = $this->parseWildcardRange($range, strpos($range, '*')); |
112
|
|
|
return; |
113
|
|
|
} |
114
|
3 |
|
|
115
|
|
|
// assume we have a single address |
116
|
3 |
|
$this->lowerInt = ip2long($range); |
117
|
3 |
|
$this->upperInt = $this->lowerInt; |
118
|
|
|
} |
119
|
3 |
|
|
120
|
3 |
|
/** |
121
|
|
|
* Parses a CIDR notation range. |
122
|
|
|
* |
123
|
|
|
* @param string $range The CIDR range. |
124
|
|
|
* @return array Returns an array with the first element being the lower |
125
|
|
|
* bound of the range and second element being the upper bound. |
126
|
|
|
*/ |
127
|
|
|
private function parseCidrRange(string $range) : array |
128
|
|
|
{ |
129
|
|
|
list($address, $mask) = explode('/', $range); |
130
|
1 |
|
$longAddress = ip2long($address); |
131
|
|
|
return [ |
132
|
1 |
|
$longAddress & (((1 << $mask) - 1) << (32 - $mask)), |
133
|
|
|
$longAddress | ((1 << (32 - $mask)) - 1) |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Parses a hyphen notation range. |
139
|
|
|
* |
140
|
|
|
* @param string $range The hyphen notation range. |
141
|
|
|
* @return array Returns an array with the first element being the lower |
142
|
1 |
|
* bound of the range and second element being the upper bound. |
143
|
|
|
*/ |
144
|
1 |
|
private function parseHyphenRange(string $range) : array |
145
|
1 |
|
{ |
146
|
1 |
|
return array_map('ip2long', explode('-', $range)); |
147
|
|
|
} |
148
|
1 |
|
|
149
|
1 |
|
/** |
150
|
|
|
* Parses a wildcard notation range. |
151
|
|
|
* |
152
|
|
|
* @param string $range The wildcard notation range. |
153
|
|
|
* @param int $pos The integer position of the wildcard within the range string. |
154
|
|
|
* @return array Returns an array with the first element being the lower |
155
|
|
|
* bound of the range and second element being the upper bound. |
156
|
|
|
*/ |
157
|
|
|
private function parseWildcardRange(string $range, int $pos) : array |
158
|
|
|
{ |
159
|
|
|
$prefix = substr($range, 0, $pos - 1); |
160
|
|
|
$parts = explode('.', $prefix); |
161
|
|
|
$partsCount = 4 - count($parts); |
162
|
|
|
return [ |
163
|
|
|
ip2long(implode('.', array_merge($parts, array_fill(0, $partsCount, 0)))), |
164
|
|
|
ip2long(implode('.', array_merge($parts, array_fill(0, $partsCount, 255)))) |
165
|
|
|
]; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|