This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ipRangeCalculate.php
344 lines (292 loc) · 6.6 KB
/
ipRangeCalculate.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* @author NewEraCracker
* @version 2.0.1
* @date 2014/07/03
* @license Public Domain
*/
/** @Link : http://php.net/manual/en/function.inet-pton.php */
if ( !function_exists('inet_pton'))
{
function inet_pton($ip)
{
if(strpos($ip, '.') !== false)
{
// Pack IPv4
$ip = trim($ip, ':f');
$ip = pack('N', ip2long($ip));
return $ip;
}
elseif(strpos($ip, ':') !== false)
{
// Expand IPv6
$ip = explode(':', $ip);
$res = '';
$expand = true;
foreach($ip as $seg)
{
if($seg == '' && $expand)
{
// This will expand a compacted IPv6
$res .= str_pad('', (((8 - count($ip)) + 1) * 4), '0', STR_PAD_LEFT);
// Only expand once, otherwise it will cause troubles with ::1 or ffff::
$expand = false;
}
else
{
// This will pad to ensure each IPv6 part has 4 digits.
$res .= str_pad($seg, 4, '0', STR_PAD_LEFT);
}
}
// Pack IPv6
$ip = pack('H'.strlen($res), $res);
return $ip;
}
return false;
}
}
/** @Link : http://php.net/manual/en/function.inet-ntop.php */
if ( !function_exists('inet_ntop'))
{
function inet_ntop($ip)
{
if(strlen($ip) == 4)
{
// Unpack IPv4
list(, $ip) = unpack('N', $ip);
$ip = long2ip($ip);
return $ip;
}
elseif(strlen($ip) == 16)
{
// Unpack IPv6
$ip = bin2hex($ip);
$sz = strlen($ip);
$res = '';
while($sz >= 4)
{
$sz -= 4;
$seg = ltrim(substr($ip, $sz, 4), '0');
if($seg != '')
{
$res = $seg.($res==''?'':':').$res;
}
else
{
// Make sure 0:2:3:4:5:6:7:8 is handled
if(strpos($res, '::') === false && substr_count($res, ':') < 6)
{
// Make sure ::1 is handled
if($res != '' && $res[0] == ':' && $i > 4)
{
continue;
}
$res = ':'.$res;
continue;
}
$res = '0'.($res==''?'':':').$res;
}
}
return $res;
}
return false;
}
}
/*
==================================================
Calculates an IP range from an IP and a range code
==================================================
----
Example:
To calculate the range of:
127.0.0.1/8
You would pass 127.0.0.1 as the $ip and 8 as the $range
----
The function returns an array with the elements
'lower' and 'higher' or, in case of failure, false.
*/
function ipRangeCalculate( $ip, $range )
{
$ip_address = (string) @$ip;
$ip_range = (integer) @$range;
if( empty($ip_address) || empty($ip_range) )
{
// Something is wrong
return false;
}
else
{
// Validate IP address
$ip_address = @inet_ntop( @inet_pton( $ip_address ) );
if( !$ip_address )
{
// Not a good IP
return false;
}
}
// Pack IP, Set some vars
$ip_pack = inet_pton($ip_address);
$ip_pack_size = strlen($ip_pack);
$ip_bits_size = $ip_pack_size*8;
// IP bits (lots of 0's and 1's)
$ip_bits = '';
for($i=0; $i < $ip_pack_size; $i=$i+1)
{
$bit = decbin( ord($ip_pack[$i]) );
$bit = str_pad($bit, 8, '0', STR_PAD_LEFT);
$ip_bits .= $bit;
}
// Significative bits (from the ip range)
$ip_bits = substr($ip_bits,0,$ip_range);
// Some calculations
$ip_lower_bits = str_pad($ip_bits, $ip_bits_size, '0', STR_PAD_RIGHT);
$ip_higher_bits = str_pad($ip_bits, $ip_bits_size, '1', STR_PAD_RIGHT);
// Lower IP
$ip_lower_pack = "";
for($i=0; $i < $ip_bits_size; $i=$i+8)
{ $chr = substr($ip_lower_bits,$i,8);
$chr = chr( bindec($chr) );
$ip_lower_pack .= $chr;
}
// Higher IP
$ip_higher_pack = "";
for($i=0; $i < $ip_bits_size; $i=$i+8)
{ $chr = substr($ip_higher_bits,$i,8);
$chr = chr( bindec($chr) );
$ip_higher_pack .= $chr;
}
return array( 'lower'=>inet_ntop($ip_lower_pack), 'higher'=>inet_ntop($ip_higher_pack));
}
/*
=======================================================
Calculates if a certain IP is within a certain IP range
=======================================================
It currently supports single IPs (x.x.x.x),
ranges (x.x.x.x-y.y.y.y and x.x.x.x/n) and wildcards (x.x.x.*)
----
Example:
$ip = 8.8.8.8
$range = 8.8.4.4/16
This function would return true for that case
----
Be aware this function also returns false in case of error,
so you better validate the input ;)
*/
function isInIpRange( $ip, $range )
{
/* -------------------------
Convert IP to packed form
------------------------- */
$ip = @inet_pton( $ip );
if( !$ip )
{
// This is not right
return false;
}
/* --------------------
Calculate the range
-------------------- */
if( strpos($range,'/') !== false )
{
// Explode
$range = explode( '/', $range );
// Calculate and validate
if( $range = ipRangeCalculate( @$range[0], @$range[1] ) )
{
$ipRangeLower = $range['lower'];
$ipRangeHigher = $range['higher'];
}
else
{
// This is not right
return false;
}
}
elseif( strpos($range,'*') !== false )
{
// Calculate and validate
if( strpos($range,'.') !== false )
{
// IPv4
$ipRangeLower = str_replace('*','0',$range);
$ipRangeHigher = str_replace('*','255',$range);
}
elseif( strpos($range,':') !== false )
{
// IPv6
$ipRangeLower = str_replace('*','0',$range);
$ipRangeHigher = str_replace('*','ffff',$range);
}
else
{
// This is not right
return false;
}
}
elseif( strpos($range,'-') !== false )
{
// Calculate
$range = explode( '-', $range );
$ipRangeLower = @$range[0];
$ipRangeHigher = @$range[1];
// Validate
if( empty($ipRangeLower) || empty($ipRangeHigher) )
{
// This is not right
return false;
}
}
else
{
// Validate
$range = @inet_ntop( @inet_pton( $range ) );
if( !$range )
{
// This is not right
return false;
}
// Calculate
$ipRangeHigher = $ipRangeLower = $range;
}
/* -------------------------------------------
Convert the calculated range to packed form
------------------------------------------- */
$ipRangeHigher = inet_pton( $ipRangeHigher );
$ipRangeLower = inet_pton( $ipRangeLower );
/* --------------
Validate again
-------------- */
if( strlen($ipRangeHigher) != strlen($ipRangeLower)
|| strlen($ipRangeLower) != strlen($ip) )
{
// This is not right
return false;
}
/* -------
Compare
------- */
// Compare IP to ranges extremes
if( $ipRangeHigher == $ip || $ipRangeLower == $ip )
{
// Thats the IP we are looking for
return true;
}
for( $i=0; $i<strlen($ip); $i++)
{
if( ord($ipRangeLower[$i]) <= ord($ip[$i]) && ord($ip[$i]) <= ord($ipRangeHigher[$i]) )
{
// This IP bit is into IP Range
continue;
}
else
{
// We didn't found that IP bit into IP range
// So, the IP we are looking for is not this one
return false;
}
}
/* --------
Found it
-------- */
return true;
}