|
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
|
|
|
namespace fkooman\VPN\Server; |
|
18
|
|
|
|
|
19
|
|
|
use InvalidArgumentException; |
|
20
|
|
|
|
|
21
|
|
|
class IP |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
private $ip; |
|
25
|
|
|
|
|
26
|
|
|
/** @var int */ |
|
27
|
|
|
private $prefix; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct($cidrIp) |
|
30
|
|
|
{ |
|
31
|
|
|
// must be of form IP/PREFIX |
|
32
|
|
|
if (1 !== substr_count($cidrIp, '/')) { |
|
33
|
|
|
throw new InvalidArgumentException('not in CIDR format'); |
|
34
|
|
|
} |
|
35
|
|
|
list($ip, $prefix) = explode('/', $cidrIp); |
|
36
|
|
|
|
|
37
|
|
|
// check IP |
|
38
|
|
|
self::validateIP($ip); |
|
39
|
|
|
$this->ip = $ip; |
|
40
|
|
|
|
|
41
|
|
|
// check prefix |
|
42
|
|
|
if (!is_numeric($prefix) || 0 > $prefix || 32 < $prefix) { |
|
43
|
|
|
throw new InvalidArgumentException('invalid prefix'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->prefix = intval($prefix); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getNetmask() |
|
50
|
|
|
{ |
|
51
|
|
|
return long2ip(-1 << (32 - $this->prefix)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getNetwork() |
|
55
|
|
|
{ |
|
56
|
|
|
return long2ip(ip2long($this->ip) & ip2long($this->getNetmask())); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getFirstHost() |
|
60
|
|
|
{ |
|
61
|
|
|
return long2ip(ip2long($this->getNetwork()) + 1); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getLastHost() |
|
65
|
|
|
{ |
|
66
|
|
|
return long2ip(ip2long($this->getBroadcast()) + -1); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getBroadcast() |
|
70
|
|
|
{ |
|
71
|
|
|
return long2ip( |
|
72
|
|
|
ip2long($this->getNetwork()) | ~ip2long($this->getNetmask()) |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* network and broadcast addresses are not allowed. |
|
78
|
|
|
*/ |
|
79
|
|
|
public function inRange($ip) |
|
80
|
|
|
{ |
|
81
|
|
|
self::validateIP($ip); |
|
82
|
|
|
|
|
83
|
|
|
$longIp = ip2long($ip); |
|
84
|
|
|
$startIp = ip2long($this->getNetwork()); |
|
85
|
|
|
$endIp = ip2long($this->getBroadcast()); |
|
86
|
|
|
|
|
87
|
|
|
return $longIp > $startIp && $longIp < $endIp; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private static function validateIP($ip) |
|
91
|
|
|
{ |
|
92
|
|
|
if (false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
93
|
|
|
throw new InvalidArgumentException('invalid IP address'); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|