|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2018 Mattheu Racine |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace mracine\IPTools\IPv4; |
|
10
|
|
|
|
|
11
|
|
|
use OutOfBoundsException; |
|
12
|
|
|
use RangeException; |
|
13
|
|
|
use BadMethodCallException; |
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
|
|
16
|
|
|
use mracine\IPTools\Iterators\SubnetIterator; |
|
17
|
|
|
use mracine\IPTools\IPv4\Address; |
|
18
|
|
|
use mracine\IPTools\IPv4\Netmask; |
|
19
|
|
|
use mracine\IPTools\IPVersion; |
|
20
|
|
|
/** |
|
21
|
|
|
* Class Subnet represents an IPv4 IP address with a netmask to specify a subnet |
|
22
|
|
|
* |
|
23
|
|
|
* Subnet implements : |
|
24
|
|
|
* * Countable : number of addresses |
|
25
|
|
|
* * ArrayAccess : Address can be acceesed by offset (ex : $range[10]) |
|
26
|
|
|
* * IteratorAggregate : range can be itrated (foreach) |
|
27
|
|
|
|
|
28
|
|
|
* @package IPTools |
|
29
|
|
|
*/ |
|
30
|
|
|
class Subnet extends Range |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var Netmask $netmask |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $netmask; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Creates an IP Subnet from a an IP network addresses and a netamsk |
|
39
|
|
|
* |
|
40
|
|
|
* Lower and upper bound are automaticly choosen, no need to choose wich one to pass first. |
|
41
|
|
|
* Only |
|
42
|
|
|
* |
|
43
|
|
|
* @param Address $baseAddress the first address of the range |
|
44
|
|
|
* @param int $count the number other bound |
|
45
|
|
|
*/ |
|
46
|
4 |
|
public function __construct(Address $network, Address $netmask) |
|
47
|
|
|
{ |
|
48
|
4 |
|
if (!($netmask instanceof Netmask)) |
|
49
|
|
|
{ |
|
50
|
|
|
/** |
|
51
|
|
|
* @todo implement |
|
52
|
|
|
*/ |
|
53
|
1 |
|
throw new InvalidArgumentException("Not implemented"); |
|
54
|
|
|
} |
|
55
|
|
|
// Verify parameters validity |
|
56
|
3 |
|
if( ($network->int() & $netmask->int()) != $network->int()) |
|
57
|
|
|
{ |
|
58
|
1 |
|
throw new RangeException(sprintf("Invalid network adress %s, this address is not usable with the netmask %s", (string)$network, (string)$netmask)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
2 |
|
parent::__construct($network, $network->shift(count($netmask)-1)); |
|
64
|
2 |
|
$this->netmask = $netmask; |
|
65
|
2 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Construct an IPv4 Subnet from a CIDR notation (#.#.#.#/#) |
|
69
|
|
|
* |
|
70
|
|
|
* @param Address $network the network base address of the subnet |
|
71
|
|
|
* @param int $cidr the CIDR notation of the netmask |
|
72
|
|
|
* @throws OutOfBoundsException when $cidr is negatve or greater than 32 |
|
73
|
|
|
* @throws RangeException when network address and netmask does not form a valid subnet |
|
74
|
|
|
* @return Subnet |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public static function fromCidr(Address $network, int $cidr) |
|
77
|
|
|
{ |
|
78
|
|
|
// Ensure CIDR is valid, use Address::fromCidr to have validation logic in only one place |
|
79
|
2 |
|
$netmask = Netmask::fromCidr($cidr); |
|
80
|
2 |
|
return new static($network, $netmask); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns an IPv4 Subnet from network address and netmask given as an address |
|
85
|
|
|
* |
|
86
|
|
|
* @param Address $network the network base address of the subnet |
|
87
|
|
|
* @param Address $netmask the netmask address |
|
88
|
|
|
* @throws DomainException when the netmask address is not a valid netmask |
|
89
|
|
|
* @throws RangeException when network and netmask addresses does not form a valid subnet |
|
90
|
|
|
* @return Subnet |
|
91
|
|
|
*/ |
|
92
|
|
|
// public static function fromAddresses(Address $network, Address $netmask) |
|
93
|
|
|
// { |
|
94
|
|
|
// return new static($network, $netmask->asCidr()); |
|
95
|
|
|
// } |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Return a subnet from an addresss and a CIDR form netmask, the addresscan be within the subnet, not only the network address |
|
99
|
|
|
* |
|
100
|
|
|
* @param Address $address an address within the desired subnet |
|
101
|
|
|
* @param int $cidr the CIDR notation of the netmask |
|
102
|
|
|
* @return Subnet |
|
103
|
|
|
*/ |
|
104
|
5 |
|
public static function fromContainedAddressCidr(Address $address, int $cidr) |
|
105
|
|
|
{ |
|
106
|
5 |
|
$netmask = Netmask::fromCidr($cidr)->int(); |
|
107
|
5 |
|
$network = Address::fromInteger($address->int() & $netmask); |
|
108
|
5 |
|
return static::fromCidr($network, $cidr); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get the network address |
|
113
|
|
|
* |
|
114
|
|
|
* NOICE : return a fresh instance of address |
|
115
|
|
|
* |
|
116
|
|
|
* @return Address |
|
117
|
|
|
*/ |
|
118
|
4 |
|
public function getNetworkAddress() |
|
119
|
|
|
{ |
|
120
|
4 |
|
return $this->getLowerBound(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get the netmask address |
|
125
|
|
|
* |
|
126
|
|
|
* @return Netmask |
|
127
|
|
|
*/ |
|
128
|
4 |
|
public function getNetmaskAddress() |
|
129
|
|
|
{ |
|
130
|
4 |
|
return $this->netmask; |
|
131
|
|
|
} |
|
132
|
|
|
/** |
|
133
|
|
|
* Get the broadcast address |
|
134
|
|
|
* |
|
135
|
|
|
* @return Address |
|
136
|
|
|
*/ |
|
137
|
6 |
|
public function getBroadcastAddress() |
|
138
|
|
|
{ |
|
139
|
6 |
|
return $this->getUpperBound(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns a Subnet shifted by an amount of units (count) |
|
144
|
|
|
* |
|
145
|
|
|
* Need explain... |
|
146
|
|
|
* |
|
147
|
|
|
* @param int $offset |
|
148
|
|
|
* @throws OutOfBounsException when the resulting subet is invalid |
|
149
|
|
|
* @return Subnet |
|
150
|
|
|
*/ |
|
151
|
2 |
|
public function shift(int $offset) |
|
152
|
|
|
{ |
|
153
|
2 |
|
return static::fromCidr( |
|
154
|
2 |
|
$this->getNetworkAddress()->shift($offset*count($this)), |
|
155
|
2 |
|
$this->getNetmaskAddress()->asCidr() |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/* |
|
160
|
|
|
* interface IteratorAggregate |
|
161
|
|
|
*/ |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Obtain an iterator to traverse the Subnet |
|
165
|
|
|
* |
|
166
|
|
|
* Allows to iterate in the subnet with foreach ($subnet as $address) {...} ) |
|
167
|
|
|
* |
|
168
|
|
|
* @return SubnetIterator |
|
169
|
|
|
*/ |
|
170
|
1 |
|
public function getIterator() |
|
171
|
|
|
{ |
|
172
|
1 |
|
return new SubnetIterator($this); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|