GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

IP::split6()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 8.5806
cc 4
eloc 18
nc 4
nop 1
1
<?php
2
3
/**
4
 * eduVPN - End-user friendly VPN.
5
 *
6
 * Copyright: 2016-2017, The Commons Conservancy eduVPN Programme
7
 * SPDX-License-Identifier: AGPL-3.0+
8
 */
9
10
namespace SURFnet\VPN\Node;
11
12
use InvalidArgumentException;
13
use SURFnet\VPN\Node\Exception\IPException;
14
15
class IP
16
{
17
    /** @var string */
18
    private $ipAddress;
19
20
    /** @var int */
21
    private $ipPrefix;
22
23
    /** @var int */
24
    private $ipFamily;
25
26
    public function __construct($ipAddressPrefix)
27
    {
28
        // detect if there is a prefix
29
        $hasPrefix = false !== mb_strpos($ipAddressPrefix, '/');
30
        if ($hasPrefix) {
31
            list($ipAddress, $ipPrefix) = explode('/', $ipAddressPrefix);
32
        } else {
33
            $ipAddress = $ipAddressPrefix;
34
            $ipPrefix = null;
35
        }
36
37
        // validate the IP address
38
        if (false === filter_var($ipAddress, FILTER_VALIDATE_IP)) {
39
            throw new IPException('invalid IP address');
40
        }
41
42
        $is6 = false !== mb_strpos($ipAddress, ':');
43
        if ($is6) {
44
            if (is_null($ipPrefix)) {
45
                $ipPrefix = 128;
46
            }
47
48 View Code Duplication
            if (!is_numeric($ipPrefix) || 0 > $ipPrefix || 128 < $ipPrefix) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
49
                throw new IPException('IP prefix must be a number between 0 and 128');
50
            }
51
            // normalize the IPv6 address
52
            $ipAddress = inet_ntop(inet_pton($ipAddress));
53
        } else {
54
            if (is_null($ipPrefix)) {
55
                $ipPrefix = 32;
56
            }
57 View Code Duplication
            if (!is_numeric($ipPrefix) || 0 > $ipPrefix || 32 < $ipPrefix) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
58
                throw new IPException('IP prefix must be a number between 0 and 32');
59
            }
60
        }
61
62
        $this->ipAddress = $ipAddress;
63
        $this->ipPrefix = (int) $ipPrefix;
64
        $this->ipFamily = $is6 ? 6 : 4;
65
    }
66
67
    public function __toString()
68
    {
69
        return $this->getAddressPrefix();
70
    }
71
72
    public function getAddress()
73
    {
74
        return $this->ipAddress;
75
    }
76
77
    public function getPrefix()
78
    {
79
        return $this->ipPrefix;
80
    }
81
82
    public function getAddressPrefix()
83
    {
84
        return sprintf('%s/%d', $this->getAddress(), $this->getPrefix());
85
    }
86
87
    public function getFamily()
88
    {
89
        return $this->ipFamily;
90
    }
91
92
    /**
93
     * IPv4 only.
94
     */
95
    public function getNetmask()
96
    {
97
        $this->requireIPv4();
98
99
        return long2ip(-1 << (32 - $this->getPrefix()));
100
    }
101
102
    /**
103
     * IPv4 only.
104
     */
105
    public function getNetwork()
106
    {
107
        $this->requireIPv4();
108
109
        return long2ip(ip2long($this->getAddress()) & ip2long($this->getNetmask()));
110
    }
111
112
    /**
113
     * IPv4 only.
114
     */
115
    public function getNumberOfHosts()
116
    {
117
        $this->requireIPv4();
118
119
        return pow(2, 32 - $this->getPrefix()) - 2;
120
    }
121
122
    public function split($networkCount)
123
    {
124
        if (!is_int($networkCount)) {
125
            throw new InvalidArgumentException('parameter must be integer');
126
        }
127
128
        if (0 !== ($networkCount & ($networkCount - 1))) {
129
            throw new InvalidArgumentException('parameter must be power of 2');
130
        }
131
132
        if (4 === $this->getFamily()) {
133
            return $this->split4($networkCount);
134
        }
135
136
        return $this->split6($networkCount);
137
    }
138
139
    private function split4($networkCount)
140
    {
141
        if (pow(2, 32 - $this->getPrefix() - 2) < $networkCount) {
142
            throw new IPException('network too small to split in this many networks');
143
        }
144
145
        $prefix = $this->getPrefix() + log($networkCount, 2);
146
        $splitRanges = [];
147
        for ($i = 0; $i < $networkCount; ++$i) {
148
            $noHosts = pow(2, 32 - $prefix);
149
            $networkAddress = long2ip($i * $noHosts + ip2long($this->getAddress()));
150
            $splitRanges[] = new self($networkAddress.'/'.$prefix);
151
        }
152
153
        return $splitRanges;
154
    }
155
156
    private function split6($networkCount)
157
    {
158
        if (124 < $this->getPrefix()) {
159
            throw new IPException('network too small to split up, must be >= /124');
160
        }
161
162
        if (0 !== $this->getPrefix() % 4) {
163
            throw new IPException('network prefix length must be divisible by 4');
164
        }
165
166
        $hexAddress = bin2hex(inet_pton($this->getAddress()));
167
        // strip the last digits based on prefix size
168
        $hexAddress = substr($hexAddress, 0, 32 - ((128 - $this->getPrefix()) / 4));
169
        $splitRanges = [];
170
        for ($i = 0; $i < $networkCount; ++$i) {
171
            $tmpHexAddress = $hexAddress.dechex($i);
172
            $splitRanges[] = new self(
173
                sprintf(
174
                    '%s/%d',
175
                    inet_ntop(
176
                        hex2bin(
177
                            str_pad($tmpHexAddress, 32, '0')
178
                        )
179
                    ),
180
                    $this->getPrefix() + 4
181
                )
182
            );
183
        }
184
185
        return $splitRanges;
186
    }
187
188
    private function requireIPv4()
189
    {
190
        if (4 !== $this->getFamily()) {
191
            throw new IPException('method only for IPv4');
192
        }
193
    }
194
}
195