Completed
Push — master ( 9b9ca0...f3dbfd )
by Lukas
13:11
created

IpAddress::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Lukas Reschke <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\Security\Normalizer;
23
24
/**
25
 * Class IpAddress is used for normalizing IPv4 and IPv6 addresses in security
26
 * relevant contexts in Nextcloud.
27
 *
28
 * @package OC\Security\Normalizer
29
 */
30
class IpAddress {
31
	/** @var string */
32
	private $ip;
33
34
	/**
35
	 * @param string $ip IP to normalized
36
	 */
37
	public function __construct($ip) {
38
		$this->ip = $ip;
39
	}
40
41
	/**
42
	 * Return the given subnet for an IPv4 address and mask bits
43
	 *
44
	 * @param string $ip
45
	 * @param int $maskBits
46
	 * @return string
47
	 */
48 View Code Duplication
	private function getIPv4Subnet($ip,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
								   $maskBits = 32) {
50
		$binary = \inet_pton($ip);
51
		for ($i = 32; $i > $maskBits; $i -= 8) {
52
			$j = \intdiv($i, 8) - 1;
53
			$k = (int) \min(8, $i - $maskBits);
54
			$mask = (0xff - ((pow(2, $k)) - 1));
55
			$int = \unpack('C', $binary[$j]);
56
			$binary[$j] = \pack('C', $int[1] & $mask);
57
		}
58
		return \inet_ntop($binary).'/'.$maskBits;
59
	}
60
61
	/**
62
	 * Return the given subnet for an IPv6 address and mask bits
63
	 *
64
	 * @param string $ip
65
	 * @param int $maskBits
66
	 * @return string
67
	 */
68 View Code Duplication
	private function getIPv6Subnet($ip, $maskBits = 48) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
69
		$binary = \inet_pton($ip);
70
		for ($i = 128; $i > $maskBits; $i -= 8) {
71
			$j = \intdiv($i, 8) - 1;
72
			$k = (int) \min(8, $i - $maskBits);
73
			$mask = (0xff - ((pow(2, $k)) - 1));
74
			$int = \unpack('C', $binary[$j]);
75
			$binary[$j] = \pack('C', $int[1] & $mask);
76
		}
77
		return \inet_ntop($binary).'/'.$maskBits;
78
	}
79
80
	/**
81
	 * Gets either the /32 (IPv4) or the /128 (IPv6) subnet of an IP address
82
	 *
83
	 * @return string
84
	 */
85
	public function getSubnet() {
86
		if (\preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $this->ip)) {
87
			return $this->getIPv4Subnet(
88
				$this->ip,
89
				32
90
			);
91
		}
92
		return $this->getIPv6Subnet(
93
			$this->ip,
94
			128
95
		);
96
	}
97
98
	/**
99
	 * Returns the specified IP address
100
	 *
101
	 * @return string
102
	 */
103
	public function __toString() {
104
		return $this->ip;
105
	}
106
}
107