| 1 |
<?php |
| 2 |
/* Copyright (c) 2011, Sam Clarke <www.samclarke.com> |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
* of this software and associated documentation files (the "Software"), to |
| 6 |
* deal in the Software without restriction, including without limitation the |
| 7 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
* sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
* furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
* SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
// Include only if necessary |
| 24 |
if(!class_exists('Math_BigInteger')){ |
| 25 |
require_once 'BigInteger.php'; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Converts human readable representation to a 128 bit int |
| 30 |
* which can be stored in MySQL using DECIMAL(39,0). |
| 31 |
* |
| 32 |
* Requires PHP to be compiled with IPv6 support. |
| 33 |
* This could be made to work without IPv6 support but |
| 34 |
* I don't think there would be much use for it if PHP |
| 35 |
* doesn't support IPv6. |
| 36 |
* |
| 37 |
* @param string $ip IPv4 or IPv6 address to convert |
| 38 |
* @return string 128 bit string that can be used with DECIMNAL(39,0) or false |
| 39 |
*/ |
| 40 |
if(!function_exists('inet_ptoi')) |
| 41 |
{ |
| 42 |
function inet_ptoi($ip) |
| 43 |
{ |
| 44 |
// make sure it is an ip |
| 45 |
if (filter_var($ip, FILTER_VALIDATE_IP) === false) |
| 46 |
return false; |
| 47 |
|
| 48 |
$parts = unpack('N*', inet_pton($ip)); |
| 49 |
|
| 50 |
// fix IPv4 |
| 51 |
if (strpos($ip, '.') !== false) |
| 52 |
$parts = array(1=>0, 2=>0, 3=>0, 4=>$parts[1]); |
| 53 |
|
| 54 |
foreach ($parts as &$part) |
| 55 |
{ |
| 56 |
// convert any unsigned ints to signed from unpack. |
| 57 |
// this should be OK as it will be a PHP float not an int |
| 58 |
if ($part < 0) |
| 59 |
$part += 4294967296; |
| 60 |
} |
| 61 |
|
| 62 |
// use BCMath if the extension exists |
| 63 |
if (function_exists('bcadd')) |
| 64 |
{ |
| 65 |
$decimal = $parts[4]; |
| 66 |
$decimal = bcadd($decimal, bcmul($parts[3], '4294967296')); |
| 67 |
$decimal = bcadd($decimal, bcmul($parts[2], '18446744073709551616')); |
| 68 |
$decimal = bcadd($decimal, bcmul($parts[1], '79228162514264337593543950336')); |
| 69 |
} |
| 70 |
// otherwise use the pure PHP BigInteger |
| 71 |
else |
| 72 |
{ |
| 73 |
$decimal = new Math_BigInteger($parts[4]); |
| 74 |
$part3 = new Math_BigInteger($parts[3]); |
| 75 |
$part2 = new Math_BigInteger($parts[2]); |
| 76 |
$part1 = new Math_BigInteger($parts[1]); |
| 77 |
|
| 78 |
$decimal = $decimal->add($part3->multiply(new Math_BigInteger('4294967296'))); |
| 79 |
$decimal = $decimal->add($part2->multiply(new Math_BigInteger('18446744073709551616'))); |
| 80 |
$decimal = $decimal->add($part1->multiply(new Math_BigInteger('79228162514264337593543950336'))); |
| 81 |
|
| 82 |
$decimal = $decimal->toString(); |
| 83 |
} |
| 84 |
|
| 85 |
return $decimal; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Converts a 128 bit int to a human readable representation. |
| 91 |
* |
| 92 |
* Requires PHP to be compiled with IPv6 support. |
| 93 |
* This could be made to work without IPv6 support but |
| 94 |
* I don't think there would be much use for it if PHP |
| 95 |
* doesn't support IPv6. |
| 96 |
* |
| 97 |
* @param string $decimal 128 bit int |
| 98 |
* @return string IPv4 or IPv6 |
| 99 |
*/ |
| 100 |
if(!function_exists('inet_itop')) |
| 101 |
{ |
| 102 |
function inet_itop($decimal) |
| 103 |
{ |
| 104 |
$parts = array(); |
| 105 |
|
| 106 |
// use BCMath if the extension exists |
| 107 |
if (function_exists('bcadd')) |
| 108 |
{ |
| 109 |
$parts[1] = bcdiv($decimal, '79228162514264337593543950336', 0); |
| 110 |
$decimal = bcsub($decimal, bcmul($parts[1], '79228162514264337593543950336')); |
| 111 |
$parts[2] = bcdiv($decimal, '18446744073709551616', 0); |
| 112 |
$decimal = bcsub($decimal, bcmul($parts[2], '18446744073709551616')); |
| 113 |
$parts[3] = bcdiv($decimal, '4294967296', 0); |
| 114 |
$decimal = bcsub($decimal, bcmul($parts[3], '4294967296')); |
| 115 |
$parts[4] = $decimal; |
| 116 |
} |
| 117 |
// otherwise use the pure PHP BigInteger |
| 118 |
else |
| 119 |
{ |
| 120 |
$decimal = new Math_BigInteger($decimal); |
| 121 |
list($parts[1],) = $decimal->divide(new Math_BigInteger('79228162514264337593543950336')); |
| 122 |
$decimal = $decimal->subtract($parts[1]->multiply(new Math_BigInteger('79228162514264337593543950336'))); |
| 123 |
list($parts[2],) = $decimal->divide(new Math_BigInteger('18446744073709551616')); |
| 124 |
$decimal = $decimal->subtract($parts[2]->multiply(new Math_BigInteger('18446744073709551616'))); |
| 125 |
list($parts[3],) = $decimal->divide(new Math_BigInteger('4294967296')); |
| 126 |
$decimal = $decimal->subtract($parts[3]->multiply(new Math_BigInteger('4294967296'))); |
| 127 |
$parts[4] = $decimal; |
| 128 |
|
| 129 |
$parts[1] = $parts[1]->toString(); |
| 130 |
$parts[2] = $parts[2]->toString(); |
| 131 |
$parts[3] = $parts[3]->toString(); |
| 132 |
$parts[4] = $parts[4]->toString(); |
| 133 |
} |
| 134 |
|
| 135 |
foreach ($parts as &$part) |
| 136 |
{ |
| 137 |
// convert any signed ints to unsigned for pack |
| 138 |
// this should be fine as it will be treated as a float |
| 139 |
if ($part > 2147483647) |
| 140 |
$part -= 4294967296; |
| 141 |
} |
| 142 |
|
| 143 |
$ip = inet_ntop(pack('N4', $parts[1], $parts[2], $parts[3], $parts[4])); |
| 144 |
|
| 145 |
// fix IPv4 by removing :: from the beginning |
| 146 |
if (strpos($ip, '.') !== false) |
| 147 |
return substr($ip, 2); |
| 148 |
|
| 149 |
return $ip; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
// Example |
| 154 |
// |
| 155 |
// echo inet_ptoi('::FFFF:FFFF:FFFF:FFFF') . "\n"; |
| 156 |
// echo inet_ptoi('::FFFF:FFFF') . "\n"; |
| 157 |
// echo inet_ptoi('255.255.255.255') . "\n"; |
| 158 |
// echo inet_itop(inet_ptoi('::FFFF:FFFF:FFFF:FFFF')) . "\n"; |
| 159 |
// echo inet_itop(inet_ptoi('::FFFF:FFFF')) . "\n"; |
| 160 |
|
| 161 |
/* Output: |
| 162 |
|
| 163 |
18446744073709551615 |
| 164 |
4294967295 |
| 165 |
4294967295 |
| 166 |
::ffff:ffff:ffff:ffff |
| 167 |
255.255.255.255 |
| 168 |
*/ |
| 169 |
|