|
Joined: Apr 2010
Posts: 964
Hoopy frood
|
OP
Hoopy frood
Joined: Apr 2010
Posts: 964 |
As stated in the subject: $longip() for ipv6 addresses
|
|
|
|
Joined: Feb 2006
Posts: 523
Fjord artisan
|
Fjord artisan
Joined: Feb 2006
Posts: 523 |
i'm not sure of the practicality of dealing with such large numbers in mIRC, but i went ahead and made a snippet that attempts to re-create $longip() for ipv6 addresses:
alias longipv6 {
if ($1-. isnum) {
if ($1 >= 0) {
var %num = $1, %i = 8, %out = :
while (%i) {
bunset "
bset " 50 0
while ($left(%num, 15) > 65535) {
var %div = $int($calc($v1 / 65536)), %rem = $v1 % 65536
bset -t " $calc(51 - $mid(%num, 16, 0) - $len(%div)) %div
%num = %rem $+ $mid(%num, 16)
}
%out = $+(:, $base(0 $+ $v1, 10, 16), %out)
!breplace " 0 48
%num = $regsubex($bvar(", 1-).text, /^0*/, )
dec %i
}
return $lower($regsubex(%out, /.$|((?:(:)0)++)(:$|(?(?!\1).)+(?=.$))|(((^)))./g, \2\3))
}
}
elseif ($regex($1-, /^(?(?=.*::)(?!(?>.*?:(?=:)){2})(::|(?>(?<=^|::)|:)[a-f\d]{1,4}+){1,8}+|(?1){8})$/)) {
var %n = $replace($1, ::, $str(:0:, $calc(8 - $numtok($1, 58)))), %i = 8, %out, $&
%3 = 281474976710656, $&
%4 = 18446744073709551616, $&
%5 = 1208925819614629174706176, $&
%6 = 79228162514264337593543950336, $&
%7 = 5192296858534827628530496329220096
while (%i) {
var %num = $base($gettok(%n, %i, 58), 16, 10), %pow = 8 - %i
if (%pow < 3) {
inc %out $calc(%num * 65536 ^ %pow)
}
elseif (%num) {
var %pow = $eval(% $+ %pow, 2), %carry, %result
while ($right(%pow, 10)) {
var %mul = $v1 * %num, %result = $base($calc($right(%mul, 10) + %carry), 10, 10, 10) $&
$+ %result, %carry = $left(%mul, -10), %pow = $left(%pow, -10)
}
var %result = %carry $+ %result, %carry, %num
while (%out $+ %result) {
var %add = $calc($right(%result, 15) + $right(%out, 15)), $&
%num = $base($calc($right(%add, 15) + %carry), 10, 10, 15) $+ %num, $&
%carry = $left(%add, -15), %out = $left(%out, -15), %result = $left(%result, -15)
}
%out = %carry $+ %num
}
dec %i
}
return $regsubex(%out, /^0*+\B/, )
}
}
i tried to make it quick as opposed to short :P you can also use this to validate ipv6 addresses, it supports compressed address and will even output in that format whenever possible. you can even use it to convert from base 10 to hex and back for ridiculously large integers!
"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
|
|
|
|
MeStinkBAD
|
MeStinkBAD
|
Er IPv6 IPs are already base 16. I don't believe that longip addresses are necessary w/ IPv6.
|
|
|
|
Joined: Oct 2004
Posts: 8,061
Hoopy frood
|
Hoopy frood
Joined: Oct 2004
Posts: 8,061 |
I believe the point of the converting sentence is that you can convert a $longip() number, which would be base 10 to base 16. If you already have the ipv6 number, then you don't need to do that, but if you're providing the $longip number and don't have the ipv6 number, you probably want to get it into ipv6.
Personally, I don't see any reason why anyone would need $longip for ipv6 either, but that's at least what I think was meant by jaytea and it is a nice feature to include for anyone who really has a need of using $longip with ipv6.
|
|
|
|
Joined: Feb 2006
Posts: 523
Fjord artisan
|
Fjord artisan
Joined: Feb 2006
Posts: 523 |
i realized that i overlooked the new $iptype() and its useful properties. here's a version of the above snippet that should be marginally quicker in light of these changes:
alias longipv6 {
if ($1-. isnum) {
if ($1 >= 0) {
var %num = $1, %i = 8, %out
while (%i) {
bunset "
bset " 50 0
while ($left(%num, 15) > 65535) {
var %div = $int($calc($v1 / 65536)), %rem = $v1 % 65536
bset -t " $calc(51 - $mid(%num, 16, 0) - $len(%div)) %div
%num = %rem $+ $mid(%num, 16)
}
%out = $instok(%out, $base(0 $+ $v1, 10, 16), 1, 58)
!breplace " 0 48
%num = $regsubex($bvar(", 1-).text, /^0*/, )
dec %i
}
return $lower($iptype(%out).compress)
}
}
elseif ($iptype($1-) == ipv6) {
var %n = $iptype($1).expand, %i = 8, %out, $&
%3 = 281474976710656, $&
%4 = 18446744073709551616, $&
%5 = 1208925819614629174706176, $&
%6 = 79228162514264337593543950336, $&
%7 = 5192296858534827628530496329220096
while (%i) {
var %num = $base($gettok(%n, %i, 58), 16, 10), %pow = 8 - %i
if (%pow < 3) {
inc %out $calc(%num * 65536 ^ %pow)
}
elseif (%num) {
var %pow = $eval(% $+ %pow, 2), %carry, %result
while ($right(%pow, 10)) {
var %mul = $v1 * %num, %result = $base($calc($right(%mul, 10) + %carry), 10, 10, 10) $&
$+ %result, %carry = $left(%mul, -10), %pow = $left(%pow, -10)
}
var %result = %carry $+ %result, %carry, %num
while (%out $+ %result) {
var %add = $calc($right(%result, 15) + $right(%out, 15)), $&
%num = $base($calc($right(%add, 15) + %carry), 10, 10, 15) $+ %num, $&
%carry = $left(%add, -15), %out = $left(%out, -15), %result = $left(%result, -15)
}
%out = %carry $+ %num
}
dec %i
}
return $regsubex(%out, /^0*+\B/, )
}
}
"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
|
|
|
|
wshs
|
wshs
|
Quite some complex code there, but does it handle IPs like 2001:570:: or 2001:570::f, etc?
Last edited by wshs; 04/10/10 07:36 PM.
|
|
|
|
Joined: Dec 2002
Posts: 294
Pan-dimensional mouse
|
Pan-dimensional mouse
Joined: Dec 2002
Posts: 294 |
Quite some complex code there, but does it handle IPs like 2001:570:: or 2001:570::f, etc? Yes, that's what $iptype().expand does. The complexity is necessary because mIRC doesn't handle numbers as large as this accurately and will round them. Which is part of the reason why it's probably not a good idea to use longipv6 in the first place.
|
|
|
|
wshs
|
wshs
|
Completely missed that. Guess that's what I get for being lazy.
|
|
|
|
|