|
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\Config; |
|
19
|
|
|
|
|
20
|
|
|
use InvalidArgumentException; |
|
21
|
|
|
|
|
22
|
|
|
class IP |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
private $ip; |
|
26
|
|
|
|
|
27
|
|
|
/** @var int */ |
|
28
|
|
|
private $prefix; |
|
29
|
|
|
|
|
30
|
|
|
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'); |
|
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 getNetmask() |
|
56
|
|
|
{ |
|
57
|
|
|
return long2ip(-1 << (32 - $this->prefix)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getNetwork() |
|
61
|
|
|
{ |
|
62
|
|
|
return long2ip(ip2long($this->ip) & ip2long($this->getNetmask())); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getFirstHostAs6() |
|
66
|
|
|
{ |
|
67
|
|
|
$firstHost = $this->getFirstHost(); |
|
68
|
|
|
$firstHost6 = str_split(bin2hex(inet_pton($firstHost)), 4); |
|
69
|
|
|
|
|
70
|
|
|
return sprintf('::ffff:%s:%s', $firstHost6[0], $firstHost6[1]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getFirstHost() |
|
74
|
|
|
{ |
|
75
|
|
|
return long2ip(ip2long($this->getNetwork()) + 1); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getLastHost() |
|
79
|
|
|
{ |
|
80
|
|
|
return long2ip(ip2long($this->getBroadcast()) + -1); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getBroadcast() |
|
84
|
|
|
{ |
|
85
|
|
|
return long2ip( |
|
86
|
|
|
ip2long($this->getNetwork()) | ~ip2long($this->getNetmask()) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Check if a given IP address is in the range of the network. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $ip the IP address to check |
|
94
|
|
|
* @param bool $includeNetworkBroadcast whether or not to consider the |
|
95
|
|
|
* network and broadcast address of the network also part of the range |
|
96
|
|
|
*/ |
|
97
|
|
|
public function inRange($ip, $includeNetworkBroadcast = false) |
|
98
|
|
|
{ |
|
99
|
|
|
self::validateIP($ip); |
|
100
|
|
|
|
|
101
|
|
|
$longIp = ip2long($ip); |
|
102
|
|
|
$startIp = ip2long($this->getNetwork()); |
|
103
|
|
|
$endIp = ip2long($this->getBroadcast()); |
|
104
|
|
|
|
|
105
|
|
|
if ($includeNetworkBroadcast) { |
|
106
|
|
|
return $longIp >= $startIp && $longIp <= $endIp; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $longIp > $startIp && $longIp < $endIp; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Split the provided range in $no equal sized CIDRs. |
|
114
|
|
|
*/ |
|
115
|
|
|
public function splitRange($no) |
|
116
|
|
|
{ |
|
117
|
|
|
if (1 === $no) { |
|
118
|
|
|
$prefixNo = 1; |
|
119
|
|
|
} elseif (2 === $no) { |
|
120
|
|
|
$prefixNo = 2; |
|
121
|
|
|
} elseif (3 === $no || 4 === $no) { |
|
122
|
|
|
$prefixNo = 4; |
|
123
|
|
|
} else { |
|
124
|
|
|
throw new InvalidArgumentException('too many instances, only 1,2,3 or 4 allowed'); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$prefix = $this->prefix + floor($prefixNo / 2); |
|
128
|
|
|
$ranges = []; |
|
129
|
|
|
for ($i = 0; $i < $no; ++$i) { |
|
130
|
|
|
$noHosts = pow(2, 32 - $prefix); |
|
131
|
|
|
$networkAddress = long2ip($i * $noHosts + ip2long($this->ip)); |
|
132
|
|
|
$ip = new self($networkAddress.'/'.$prefix); |
|
133
|
|
|
$ranges[] = $ip->getRange(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $ranges; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
private static function validateIP($ip) |
|
140
|
|
|
{ |
|
141
|
|
|
if (false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
142
|
|
|
throw new InvalidArgumentException('invalid IP address'); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|