This repository has been archived by the owner on Dec 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.php
executable file
·266 lines (212 loc) · 7.11 KB
/
Network.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* Copyright (c) 2013, 2014 Gunnar Guðvarðarson, Gabríel Arthúr Pétursson
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
abstract class Network
{
protected $address;
protected $bitmask;
protected $length;
protected $sprintf_padder;
protected $last_network;
protected $subnets = array();
public function __construct($address, $bitmask)
{
if ($bitmask > $this->length - 2)
throw new OutOfRangeException("Bitmask must be 2 bits less than the bitlength of the address type");
if ($bitmask <= 1)
throw new OutOfRangeException("Bitmask must be at least 1 bit");
if ( ! $this->verifyAddress($address))
throw new InvalidArgumentException();
$this->setAddress($address);
$this->setBitmask($bitmask);
$this->last_network = $this->getNetworkAddress();
}
protected abstract function setAddress($address);
public abstract function formatAddress($address);
protected abstract function verifyAddress($address);
protected function setBitmask($bitmask)
{
$this->bitmask = gmp_xor(gmp_sub(gmp_pow(2, $this->length), 1), gmp_sub(gmp_pow(2, $this->length - $bitmask), 1));
}
public function addSubnet($size)
{
$bits_needed = $this->length - ceil(log($size + 2, 2));
$class = get_called_class();
$new_network = new $class($this->last_network, $bits_needed);
$this->subnets[] = $new_network;
$this->last_network = $new_network->getNextNetwork();
}
public function address()
{
return $this->address;
}
public function networkAddress()
{
return gmp_and($this->address, $this->bitmask);
}
public function lowestAddress()
{
return gmp_add($this->networkAddress(), 1);
}
public function highestAddress()
{
return gmp_sub($this->broadcastAddress(), 1);
}
public function broadcastAddress()
{
return gmp_or($this->networkAddress(), not($this->bitmask, $this->length));
}
public function hostCount()
{
return gmp_sub(gmp_sub($this->broadcastAddress(), $this->networkAddress()), 1);
}
// Public getters
public function getAddress()
{
return $this->formatAddress($this->address);
}
public function getNetworkAddress()
{
return $this->formatAddress($this->networkAddress());
}
public function getLowestAddress()
{
return $this->formatAddress($this->lowestAddress());
}
public function getHighestAddress()
{
return $this->formatAddress($this->highestAddress());
}
public function getBroadcastAddress()
{
return $this->formatAddress($this->broadcastAddress());
}
public function getBitmask()
{
return $this->formatAddress($this->bitmask);
}
public function getBitCount()
{
$count = 0;
foreach (str_split(gmp_strval($this->bitmask, 2)) as $bit)
{
if ($bit == "1")
{
$count++;
}
}
return $count;
}
public function getHostCount()
{
return gmp_strval($this->hostCount());
}
public function getNextNetwork()
{
return $this->formatAddress(gmp_add($this->broadcastAddress(), 1));
}
// //
protected function displayAddressBinary($address)
{
return sprintf("%0{$this->length}s", gmp_strval($address, 2));
}
public function summary()
{
$ret = "";
$ret .= "<table border='1'>\n";
$ret .= "\t<tr>\n";
$ret .= "\t\t<th>Network Type</th>\n";
$ret .= "\t\t<th>Provided address</th>\n";
$ret .= "\t\t<th>Network address</th>\n";
$ret .= "\t\t<th>First address</th>\n";
$ret .= "\t\t<th>Last address</th>\n";
$ret .= "\t\t<th>Broadcast address</th>\n";
$ret .= "\t\t<th>Subnet mask</th>\n";
$ret .= "\t\t<th>Subnet bitlength</th>\n";
$ret .= "\t\t<th>Number of hosts</th>\n";
$ret .= "\t</tr>\n";
$ret .= "\t<tr>\n";
$ret .= sprintf("\t\t<td>%s</td>\n", get_called_class());
$ret .= sprintf("\t\t<td>%s</td>\n", $this->getAddress());
$ret .= sprintf("\t\t<td>%s</td>\n", $this->getNetworkAddress());
$ret .= sprintf("\t\t<td>%s</td>\n", $this->getLowestAddress());
$ret .= sprintf("\t\t<td>%s</td>\n", $this->getHighestAddress());
$ret .= sprintf("\t\t<td>%s</td>\n", $this->getBroadcastAddress());
$ret .= sprintf("\t\t<td>%s</td>\n", $this->getBitmask());
$ret .= sprintf("\t\t<td>%s</td>\n", $this->getBitCount());
$ret .= sprintf("\t\t<td>%s</td>\n", $this->getHostCount());
$ret .= "\t</tr>\n";
$ret .= "\t<tr>\n";
if (count($this->subnets) > 0)
{
$ret .= "\t<th colspan='9'>SUBNETS</th>\n";
foreach ($this->subnets as $subnet)
{
$ret .= $subnet->summary_subnet();
}
}
$ret .= "</table>";
return $ret;
}
public function summary_subnet()
{
$ret = "\t<tr>\n";
$ret .= sprintf("\t\t<td>%s</td>\n", get_called_class());
$ret .= sprintf("\t\t<td>%s</td>\n", $this->getAddress());
$ret .= sprintf("\t\t<td>%s</td>\n", $this->getNetworkAddress());
$ret .= sprintf("\t\t<td>%s</td>\n", $this->getLowestAddress());
$ret .= sprintf("\t\t<td>%s</td>\n", $this->getHighestAddress());
$ret .= sprintf("\t\t<td>%s</td>\n", $this->getBroadcastAddress());
$ret .= sprintf("\t\t<td>%s</td>\n", $this->getBitmask());
$ret .= sprintf("\t\t<td>%s</td>\n", $this->getBitCount());
$ret .= sprintf("\t\t<td>%s</td>\n", $this->getHostCount());
$ret .= "\t</tr>\n";
return $ret;
}
}
// Various helpers
function ip2long6($ipv6)
{
if (filter_var($ipv6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false)
return false;
$ip_n = inet_pton($ipv6);
$ipv6long = implode('', array_map(function($bytes) use ($ip_n)
{
return sprintf('%08b', ord($ip_n[$bytes]));
}, range(0, 15)));
return gmp_strval(gmp_init($ipv6long, 2));
}
function long2ip6($ipv6long, $compress = true)
{
$bin = sprintf('%0128s', gmp_strval($ipv6long, 2));
$ipv6 = implode(':', array_map(function($bytes) use ($bin)
{
$bin_part = substr($bin, $bytes * 16, 16);
return str_pad(dechex(bindec($bin_part)), 4, '0', STR_PAD_LEFT);
}, range(0, 7)));
return $compress ? inet_ntop(inet_pton($ipv6)) : $ipv6;
}
function not($number, $length)
{
return gmp_xor($number, gmp_sub(gmp_pow(2, $length), 1));
}