|
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
|
|
|
class AddressPool |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Find the first free IPv4 address. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $startIp the IPv4 address to start from |
|
25
|
|
|
* @param string $endIp the IPv4 address to end at, i.e. limiting the range |
|
26
|
|
|
* @param array $usedIp array of IPv4 addresses already in use |
|
27
|
|
|
* |
|
28
|
|
|
* @return string|false the assigned IPv4 address or false when no address |
|
29
|
|
|
* is available |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function getIp4($startIp, $endIp, $usedIp = array()) |
|
32
|
|
|
{ |
|
33
|
|
|
$s = ip2long($startIp); |
|
34
|
|
|
$e = ip2long($endIp); |
|
35
|
|
|
|
|
36
|
|
|
$search = $s; |
|
37
|
|
|
while (in_array(long2ip($search), $usedIp)) { |
|
38
|
|
|
++$search; |
|
39
|
|
|
if ($search > $e) { |
|
40
|
|
|
return false; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return long2ip($search); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Give a matching IPv6 address for the obtained IPv4 address. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $v6 the expanded first 64 bits of an IPv6 address |
|
51
|
|
|
* (network), e.g. "fd00:4242:4242:4242" |
|
52
|
|
|
* @param string $v4 the IPv4 address to use in the IPv6 address |
|
53
|
|
|
* |
|
54
|
|
|
* @return string the IPv6 address containing the IPv4 address |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function getIp6($v6, $v4) |
|
57
|
|
|
{ |
|
58
|
|
|
if (3 !== substr_count($v6, ':')) { |
|
59
|
|
|
throw new InvalidArgumentException('specify the expanded network part of a /64 IPv6 address'); |
|
60
|
|
|
} |
|
61
|
|
|
if (3 !== substr_count($v4, '.')) { |
|
62
|
|
|
throw new InvalidArgumentException('invalid IPv4 address'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$v6e = explode(':', $v6); |
|
66
|
|
|
$v4e = explode('.', $v4); |
|
67
|
|
|
|
|
68
|
|
|
$v4v6 = sprintf( |
|
69
|
|
|
'%s:%s:%s:%s:%s:%s:%s:%s', $v6e[0], $v6e[1], $v6e[2], $v6e[3], $v4e[0], $v4e[1], $v4e[2], $v4e[3] |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
return $v4v6; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
#try { |
|
|
|
|
|
|
77
|
|
|
# $ip4 = AddressPool::getIp4( |
|
78
|
|
|
# '10.42.42.128', |
|
79
|
|
|
# '10.42.42.132', |
|
80
|
|
|
# array( |
|
81
|
|
|
# '10.42.42.128', |
|
82
|
|
|
# '10.42.42.129', |
|
83
|
|
|
# '10.42.42.130', |
|
84
|
|
|
# '10.42.42.131', |
|
85
|
|
|
# //'10.42.42.132', |
|
86
|
|
|
# ) |
|
87
|
|
|
# ); |
|
88
|
|
|
|
|
89
|
|
|
# $ip6 = AddressPool::getIp6( |
|
|
|
|
|
|
90
|
|
|
# 'fd00:4242:4242:4242', |
|
91
|
|
|
# $ip4 |
|
92
|
|
|
# ); |
|
93
|
|
|
|
|
94
|
|
|
# echo sprintf('IPv4: %s', $ip4).PHP_EOL; |
|
|
|
|
|
|
95
|
|
|
# echo sprintf('IPv6: %s', $ip6).PHP_EOL; |
|
96
|
|
|
#} catch (Exception $e) { |
|
97
|
|
|
# echo $e->getMessage().PHP_EOL; |
|
98
|
|
|
# exit(1); |
|
99
|
|
|
#} |
|
100
|
|
|
|
|
101
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.