|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2015 François Kooman <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace fkooman\VPN\Server; |
|
19
|
|
|
|
|
20
|
|
|
use InvalidArgumentException; |
|
21
|
|
|
|
|
22
|
|
|
class IPv4 |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
private $ip; |
|
26
|
|
|
|
|
27
|
|
|
/** @var int */ |
|
28
|
|
|
private $prefix; |
|
29
|
|
|
|
|
30
|
|
View Code Duplication |
public function __construct($cidrIp) |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
// must be of form IP/PREFIX |
|
33
|
|
|
if (1 !== substr_count($cidrIp, '/')) { |
|
34
|
|
|
throw new InvalidArgumentException('not in CIDR format'); |
|
35
|
|
|
} |
|
36
|
|
|
list($ip, $prefix) = explode('/', $cidrIp); |
|
37
|
|
|
|
|
38
|
|
|
// check IP |
|
39
|
|
|
self::validateIP($ip); |
|
40
|
|
|
$this->ip = $ip; |
|
41
|
|
|
|
|
42
|
|
|
// check prefix |
|
43
|
|
|
if (!is_numeric($prefix) || 0 > $prefix || 32 < $prefix) { |
|
44
|
|
|
throw new InvalidArgumentException('invalid prefix, must be >0 and <32'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$this->prefix = intval($prefix); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getRange() |
|
51
|
|
|
{ |
|
52
|
|
|
return sprintf('%s/%d', $this->ip, $this->prefix); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getPrefix() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->prefix; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getNetmask() |
|
61
|
|
|
{ |
|
62
|
|
|
return long2ip(-1 << (32 - $this->prefix)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getNetwork() |
|
66
|
|
|
{ |
|
67
|
|
|
return long2ip(ip2long($this->ip) & ip2long($this->getNetmask())); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function getFirstHost() |
|
71
|
|
|
{ |
|
72
|
|
|
return long2ip(ip2long($this->getNetwork()) + 1); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getLastHost() |
|
76
|
|
|
{ |
|
77
|
|
|
return long2ip(ip2long($this->getBroadcast()) + -1); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getBroadcast() |
|
81
|
|
|
{ |
|
82
|
|
|
return long2ip( |
|
83
|
|
|
ip2long($this->getNetwork()) | ~ip2long($this->getNetmask()) |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getNumberOfHosts() |
|
88
|
|
|
{ |
|
89
|
|
|
return pow(2, 32 - $this->prefix) - 2; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Check if a given IP address is in the range of the network. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $ip the IP address to check |
|
96
|
|
|
* @param bool $includeNetworkBroadcast whether or not to consider the |
|
97
|
|
|
* network and broadcast address of the network also part of the range |
|
98
|
|
|
*/ |
|
99
|
|
|
public function inRange($ip, $includeNetworkBroadcast = false) |
|
100
|
|
|
{ |
|
101
|
|
|
self::validateIP($ip); |
|
102
|
|
|
|
|
103
|
|
|
$longIp = ip2long($ip); |
|
104
|
|
|
$startIp = ip2long($this->getNetwork()); |
|
105
|
|
|
$endIp = ip2long($this->getBroadcast()); |
|
106
|
|
|
|
|
107
|
|
|
if ($includeNetworkBroadcast) { |
|
108
|
|
|
return $longIp >= $startIp && $longIp <= $endIp; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $longIp > $startIp && $longIp < $endIp; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Split the provided range in $no equal sized CIDRs. |
|
116
|
|
|
*/ |
|
117
|
|
|
public function splitRange($no) |
|
118
|
|
|
{ |
|
119
|
|
|
if (1 === $no) { |
|
120
|
|
|
$prefixNo = 1; |
|
121
|
|
|
} elseif (2 === $no) { |
|
122
|
|
|
$prefixNo = 2; |
|
123
|
|
|
} elseif (3 === $no || 4 === $no) { |
|
124
|
|
|
$prefixNo = 4; |
|
125
|
|
|
} else { |
|
126
|
|
|
throw new InvalidArgumentException('too many instances, only 1,2,3 or 4 allowed'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$prefix = $this->prefix + floor($prefixNo / 2); |
|
130
|
|
|
$ranges = []; |
|
131
|
|
|
for ($i = 0; $i < $no; ++$i) { |
|
132
|
|
|
$noHosts = pow(2, 32 - $prefix); |
|
133
|
|
|
$networkAddress = long2ip($i * $noHosts + ip2long($this->ip)); |
|
134
|
|
|
$ip = new self($networkAddress.'/'.$prefix); |
|
135
|
|
|
$ranges[] = $ip->getRange(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $ranges; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
private static function validateIP($ip) |
|
142
|
|
|
{ |
|
143
|
|
|
if (false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
144
|
|
|
throw new InvalidArgumentException('invalid IP address'); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.