-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathIP.php
More file actions
289 lines (246 loc) · 5.49 KB
/
Copy pathIP.php
File metadata and controls
289 lines (246 loc) · 5.49 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
namespace IPTools;
use IPTools\Exception\IpException;
use LogicException;
/**
* @author Safarov Alisher <alisher.safarov@outlook.com>
* @link https://github.com/S1lentium/IPTools
*/
class IP
{
use PropertyTrait;
const IP_V4 = 'IPv4';
const IP_V6 = 'IPv6';
const IP_V4_MAX_PREFIX_LENGTH = 32;
const IP_V6_MAX_PREFIX_LENGTH = 128;
const IP_V4_OCTETS = 4;
const IP_V6_OCTETS = 16;
/**
* @var string
*/
private $in_addr;
/**
* @param string ip
* @throws IpException
*/
public function __construct($ip)
{
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
throw new IpException("Invalid IP address format");
}
$this->in_addr = inet_pton($ip);
}
/**
* @return string
*/
public function __toString()
{
return inet_ntop($this->in_addr);
}
/**
* @param string $ip
* @return IP
*/
public static function parse($ip)
{
if (strpos($ip, '0x') === 0) {
$ip = substr($ip, 2);
return self::parseHex($ip);
}
if (strpos($ip, '0b') === 0) {
$ip = substr($ip, 2);
return self::parseBin($ip);
}
if (is_numeric($ip)) {
return self::parseLong($ip);
}
return new self($ip);
}
/**
* @param string $binIP
* @throws IpException
* @return IP
*/
public static function parseBin($binIP)
{
if (!preg_match('/^([0-1]{32}|[0-1]{128})$/', $binIP)) {
throw new IpException("Invalid binary IP address format");
}
$in_addr = '';
foreach (array_map('bindec', str_split($binIP, 8)) as $char) {
$in_addr .= pack('C*', $char);
}
return new self(inet_ntop($in_addr));
}
/**
* @param string $hexIP
* @throws IpException
* @return IP
*/
public static function parseHex($hexIP)
{
if (!preg_match('/^([0-9a-fA-F]{8}|[0-9a-fA-F]{32})$/', $hexIP)) {
throw new IpException("Invalid hexadecimal IP address format");
}
return new self(inet_ntop(pack('H*', $hexIP)));
}
/**
* @return IP
*/
public static function parseLong(int|string $longIP, $version=self::IP_V4)
{
if ($version === self::IP_V4) {
$ip = new self(long2ip($longIP));
} else {
$binary = array();
for ($i = 0; $i < self::IP_V6_OCTETS; $i++) {
$binary[] = bcmod($longIP, 256);
$longIP = bcdiv($longIP, 256, 0);
}
$ip = new self(inet_ntop(call_user_func_array('pack', array_merge(array('C*'), array_reverse($binary)))));
}
return $ip;
}
/**
* @param string $inAddr
* @return IP
*/
public static function parseInAddr($inAddr)
{
return new self(inet_ntop($inAddr));
}
/**
* @phpstan-return IP::IP_V4|IP::IP_V6
* @return string
*/
public function getVersion()
{
if (filter_var(inet_ntop($this->in_addr), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$version = self::IP_V4;
} elseif (filter_var(inet_ntop($this->in_addr), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$version = self::IP_V6;
} else {
throw new LogicException("Invalid IP address format");
}
return $version;
}
/**
* @return int
*/
public function getMaxPrefixLength()
{
return $this->getVersion() === self::IP_V4
? self::IP_V4_MAX_PREFIX_LENGTH
: self::IP_V6_MAX_PREFIX_LENGTH;
}
/**
* @return int
*/
public function getOctetsCount()
{
return $this->getVersion() === self::IP_V4
? self::IP_V4_OCTETS
: self::IP_V6_OCTETS;
}
/**
* @return string
*/
public function getReversePointer()
{
if ($this->getVersion() === self::IP_V4) {
$reverseOctets = array_reverse(explode('.', $this->__toString()));
$reversePointer = implode('.', $reverseOctets) . '.in-addr.arpa';
} else {
$unpacked = unpack('H*hex', $this->in_addr);
$reverseOctets = array_reverse(str_split($unpacked['hex']));
$reversePointer = implode('.', $reverseOctets) . '.ip6.arpa';
}
return $reversePointer;
}
/**
* @return string
*/
public function inAddr()
{
return $this->in_addr;
}
/**
* @return string
*/
public function toBin()
{
$binary = array();
foreach (unpack('C*', $this->in_addr) as $char) {
$binary[] = str_pad(decbin($char), 8, '0', STR_PAD_LEFT);
}
return implode($binary);
}
/**
* @return string
*/
public function toHex()
{
return bin2hex($this->in_addr);
}
/**
* @return string
*/
public function toLong()
{
$long = 0;
if ($this->getVersion() === self::IP_V4) {
$long = sprintf('%u', ip2long(inet_ntop($this->in_addr)));
} else {
$octet = self::IP_V6_OCTETS - 1;
foreach ($chars = unpack('C*', $this->in_addr) as $char) {
$long = bcadd($long, bcmul($char, bcpow(256, $octet--)));
}
}
return $long;
}
/**
* @param int $to
* @return IP
* @throws IpException
*/
public function next($to=1)
{
if ($to < 0) {
throw new IpException("Number must be greater than 0");
}
$unpacked = unpack('C*', $this->in_addr);
for ($i = 0; $i < $to; $i++) {
for ($byte = count($unpacked); $byte >= 0; --$byte) {
if ($unpacked[$byte] < 255) {
$unpacked[$byte]++;
break;
}
$unpacked[$byte] = 0;
}
}
return new self(inet_ntop(call_user_func_array('pack', array_merge(array('C*'), $unpacked))));
}
/**
* @param int $to
* @return IP
* @throws IpException
*/
public function prev($to=1)
{
if ($to < 0) {
throw new IpException("Number must be greater than 0");
}
$unpacked = unpack('C*', $this->in_addr);
for ($i = 0; $i < $to; $i++) {
for ($byte = count($unpacked); $byte >= 0; --$byte) {
if ($unpacked[$byte] === 0) {
$unpacked[$byte] = 255;
} else {
$unpacked[$byte]--;
break;
}
}
}
return new self(inet_ntop(call_user_func_array('pack', array_merge(array('C*'), $unpacked))));
}
}