forked from binarymaster/3WiFi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipext.php
432 lines (411 loc) · 10.9 KB
/
ipext.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<?php
// Regional Internet Registries
define('RIR_RIPE', 1);
define('RIR_APNIC', 2);
define('RIR_ARIN', 3);
define('RIR_AFRINIC', 4);
define('RIR_LACNIC', 5);
/**
* Fetch data for ip object using RDAP.
*
* @param string $url Path to RDAP service.
* @param string $ip IP address to search for.
*
* @return object|null Response encoded in json as PHP object.
*/
function fetch_rdap($url, $ip)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'utf-8');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP/'.phpversion());
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_URL, $url.'ip/'.$ip);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Accept: application/json, application/rdap+json"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$res = curl_exec($ch);
//$ret_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
//if ($ret_code != 200) {
// return;
//}
return json_decode($res);
}
/**
* Fetch information from whois server.
*
* @param string $server Server address.
* @param string $request Requset string.
*
* @return string|null Response of whois server.
*/
function fetch_whois($server, $request)
{
$fp = fsockopen($server, 43, $errno, $errstr, 30);
if (!$fp)
{
return;
}
else
{
$response = "";
fputs($fp, "$request\r\n");
while (!feof($fp))
{
$response .= fread($fp,128);
}
fclose ($fp);
}
return $response;
}
/**
* Determine RIR for given IP.
*
* @param string $ip IP address.
*
* @return int|null.
*/
function get_rir($ip)
{
// use ARIN server in hope that it will redirect to right one (if neccessary)
$rdap = fetch_rdap('http://rdap.arin.net/registry/', $ip);
if (is_null($rdap) || empty($rdap->port43))
{
return;
}
$whois = $rdap->port43;
if (false !== stristr($whois, 'afrinic'))
{
return RIR_AFRINIC;
}
elseif (false !== stristr($whois, 'apnic'))
{
return RIR_APNIC;
}
elseif (false !== stristr($whois, 'arin'))
{
return RIR_ARIN;
}
elseif (false !== stristr($whois, 'lacnic'))
{
return RIR_LACNIC;
}
elseif (false !== stristr($whois, 'ripe'))
{
return RIR_RIPE;
}
return;
}
/**
* Extract value of field with given name from the response of whois server.
*
* @param string $whois_text Whois server response.
* @param string $field_name Field name to search for.
*
* @return string Value of the first field with given name.
*/
function get_whois_field($whois_text, $field_name)
{
$field_pos = strpos($whois_text, "\n".$field_name.":");
if ($field_pos === False)
{
return '';
}
$field_pos += strlen($field_name) + 2;
$field_len = strpos($whois_text, "\n", $field_pos) - $field_pos;
return trim(substr($whois_text, $field_pos, $field_len));
}
/**
* Extract values of subsequent fields with given name from the response of whois server.
*
* @param string $whois_text Whois server response.
* @param string $field_name Field name to search for.
*
* @return array Array of strings.
*/
function get_whois_field_arr($whois_text, $field_name)
{
$field_pos = strpos($whois_text, "\n".$field_name.":");
if ($field_pos === False)
{
return array('');
}
do
{
$value_pos = $field_pos + strlen($field_name) + 2;
$value_len = strpos($whois_text, "\n", $value_pos) - $value_pos;
$values[] = trim(substr($whois_text, $value_pos, $value_len));
$field_pos = strpos($whois_text, "\n".$field_name.":", $value_pos);
} while ($field_pos !== False and $field_pos == $value_pos + $value_len);
return $values;
}
/**
* Get boundaries of a range in CIDR notation.
*
* @param string $cidr IP range (x.x.x.x/z, x.x.x/z, ...).
*
* @return array Array ("startIP" => (int), "endIP" => (int)).
*/
function cidr_to_range($cidr)
{
list($ip, $mask) = explode('/', $cidr);
$i = substr_count($ip, '.');
while ($i < 3)
{
$ip .= '.0';
$i++;
}
$ip = ip2long($ip);
return array('startIP' => $ip,
// will not work on 32bit if $mask=0; but it doesn't matter here
'endIP' => $ip | ((1<<(32-$mask)) - 1));
}
/**
* Query RIR for a range to which belongs the given IP address.
*
* @param int $ip IP address as number.
*
* @return array|null Array ("startIP" => (int), "endIP" => (int), "netname" =>
* (string), "descr" => (string), "country" => (string)) or null if nothing is
* found.
*/
function query_range_from_rir($ip)
{
$rir = get_rir($ip);
if (is_null($rir))
{
return;
}
$whois_servers = array(
RIR_RIPE => 'whois.ripe.net',
RIR_APNIC => 'whois.apnic.net',
RIR_ARIN => 'whois.arin.net',
RIR_AFRINIC => 'whois.afrinic.net',
RIR_LACNIC => 'whois.lacnic.net');
$whois_req = $ip;
if ($rir == RIR_ARIN )
{
$whois_req = 'n + '.$ip;
}
$whois_res = fetch_whois($whois_servers[$rir], $whois_req);
if ($whois_res == '')
{
return;
}
switch ($rir)
{
case RIR_RIPE:
// the same as RIR_APNIC
case RIR_AFRINIC:
// the same as RIR_APNIC
case RIR_APNIC:
$inetnum = get_whois_field($whois_res, 'inetnum');
if ($inetnum == '')
{
return;
}
$inetnum = preg_split('/\s*-\s*/', $inetnum);
$netname = get_whois_field($whois_res, 'netname');
$descr = implode(" | ", get_whois_field_arr($whois_res, 'descr'));
if ($rir == RIR_RIPE)
{
$descr = iconv("ISO-8859-1", "UTF-8", $descr);
}
$country = strtoupper(get_whois_field($whois_res, 'country'));
return array('startIP' => ip2long($inetnum[0]),
'endIP' => ip2long($inetnum[1]),
'netname' => $netname,
'descr' => $descr,
'country' => $country);
break;
case RIR_ARIN:
// find the smallest range
$start_pos = -1;
$ip_count = array();
while (($start_pos = strpos($whois_res, "\n# start", $start_pos + 1)) !== False)
{
$end_pos = strpos($whois_res, "\n# end", $start_pos);
$inetnum = get_whois_field(substr($whois_res, $start_pos, $end_pos - $start_pos), 'NetRange');
if ($inetnum != '')
{
$inetnum = preg_split('/\s*-\s*/', $inetnum);
$ip_count[$start_pos] = ip2long($inetnum[1]) - ip2long($inetnum[0]);
}
}
if (!empty($ip_count))
{
$start_pos = array_search(min($ip_count), $ip_count);
$end_pos = strpos($whois_res, "\n# end", $start_pos);
$whois_res = substr($whois_res, $start_pos, $end_pos - $start_pos);
}
$inetnum = get_whois_field($whois_res, 'NetRange');
if ($inetnum == '')
{
return;
}
$inetnum = preg_split('/\s*-\s*/', $inetnum);
$netname = get_whois_field($whois_res, 'NetName');
$descr = get_whois_field($whois_res, 'OrgName');
if ($descr == '')
{
$descr = get_whois_field($whois_res, 'CustName');
}
$country = strtoupper(get_whois_field($whois_res, 'Country'));
return array('startIP' => ip2long($inetnum[0]),
'endIP' => ip2long($inetnum[1]),
'netname' => $netname,
'descr' => $descr,
'country' => $country);
break;
case RIR_LACNIC:
$inetnum = get_whois_field($whois_res, 'inetnum');
if ($inetnum == '')
{
return;
}
$inetnum = cidr_to_range($inetnum);
$netname = get_whois_field($whois_res, 'netname');
$descr = get_whois_field($whois_res, 'owner');
$descr = iconv("ISO-8859-1", "UTF-8", $descr);
$country = strtoupper(get_whois_field($whois_res, 'country'));
return array('startIP' => $inetnum["startIP"],
'endIP' => $inetnum["endIP"],
'netname' => '',
'descr' => $descr,
'country' => $country);
break;
}
}
/**
* Find a range to which belongs the given IP address.
*
* If IP doesn't belong to one of ranges stored in local database, it will try
* to fetch information from appropriate Regional Internet Registry (RIR).
*
* @param object $db Object which represents the connection to a MySQL Server.
* @param int $ip IP address as number.
*
* @return array|null Array ("startIP" => (int), "endIP" => (int), "netname" =>
* (string), "descr" => (string), "country" => (string)) or null if nothing is
* found.
*/
function get_ip_range($db, $ip)
{
// If private IP
if (($ip >= (int)0x0A000000 and $ip < (int)0x0B000000) or // 10.0.0.0-10.255.255.255
($ip >= (int)0x64400000 and $ip < (int)0x64800000) or // 100.64.0.0-100.127.255.255
($ip >= (int)0xAC100000 and $ip < (int)0xAC200000) or // 172.16.0.0-172.31.255.255
($ip >= (int)0xC0A80000 and $ip < (int)0xC0A90000)) // 192.168.0.0-192.168.255.255
{
// exact range isn't known, just use /16 mask
return array('startIP' => $ip & ~0xFFFF,
'endIP' => $ip | 0xFFFF,
'netname' => '',
'descr' => 'Local IP range',
'country' => '');
}
// If stored in local db
$uIP = sprintf('%u', $ip);
if ($res = $db->query(
"SELECT * FROM ranges
WHERE startIP <= $uIP AND endIP >= $uIP
ORDER BY endIP-startIP
LIMIT 1"))
{
if ($row = $res->fetch_row())
{
$res->close();
// TODO: convert unsigned integer (represented by string) to int
return array('startIP' => ip2long(long2ip($row[1])),
'endIP' => ip2long(long2ip($row[2])),
'netname' => $row[3],
'descr' => $row[4],
'country' => $row[5]);
}
$res->close();
}
// Query RIR
$ip_range = query_range_from_rir(long2ip($ip));
if(is_null($ip_range))
{
return;
}
$ip_range["netname"] = substr($ip_range["netname"], 0, 255);
$ip_range["descr"] = substr($ip_range["descr"], 0, 255);
$ip_range["descr"] = iconv("UTF-8", "UTF-8//IGNORE", $ip_range["descr"]);
if ($ip_range["endIP"] - $ip_range["startIP"] >= 0x00FFFFFF)
{
return $ip_range; // don't store big ranges
}
$ip_range["country"] = substr($ip_range["country"], 0, 2);
$startIP = sprintf('%u', $ip_range["startIP"]);
$endIP = sprintf('%u', $ip_range["endIP"]);
$netname = $db->real_escape_string($ip_range["netname"]);
$descr = $db->real_escape_string($ip_range["descr"]);
$country = $db->real_escape_string($ip_range["country"]);
if (!$db->query(
"INSERT into ranges
VALUES (NULL, '$startIP','$endIP','$netname','$descr','$country')"))
{
return;
}
return $ip_range;
}
/**
* Compare two IP addresses.
*
* It's a workaround for 32-bit machines, where (since int type is signed)
* IPs bigger then '127.255.255.255' are negative numbers.
*
* @param int $ip1 Numeric representation of the first IP.
* @param int $ip2 Numeric representation of the second IP.
*
* @return int Returns zero if addresses are equal, -1 if the first is smaller
* and 1 otherwise.
*/
function compare_ip($ip1, $ip2)
{
if ($ip1 == $ip2)
{
return 0;
}
elseif ($ip1 < 0 and $ip2 >=0)
{
return 1;
}
elseif ($ip1 >= 0 and $ip2 < 0)
{
return -1;
}
elseif ($ip1 < $ip2)
{
return -1;
}
else
{
return 1;
}
}
/**
* Return string representation of IP range.
*
* Convert IP range to CIDR specification (x.x.x.x/z) if it may be expressed as
* single entity. Otherwise format it to "x.x.x.x-y.y.y.y".
*
* @param int $ip1 Lower boundary of the range.
* @param int $ip2 Upper boundary of the range.
*
* @return string
*/
function pretty_range($ip1, $ip2)
{
$diff = decbin($ip1 ^ $ip2);
if (strpos($diff, '0') === false and ($ip1 & $ip2) == $ip1)
{
return long2ip($ip1).'/'.(32 - strlen($diff));
}
return long2ip($ip1).'-'.long2ip($ip2);
}