mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Apr 2010
Posts: 969
F
Hoopy frood
OP Offline
Hoopy frood
F
Joined: Apr 2010
Posts: 969
As stated in the subject: $longip() for ipv6 addresses


I am SReject
My Stuff
Joined: Feb 2006
Posts: 546
J
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
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:

Code:
alias longipv6 {
  if ($1-. isnum) {
    if ($1 >= 0) {
      var %num = $1, %i = 8, %out = :
      while (%i) {
        bunset &quot
        bset &quot 50 0
        while ($left(%num, 15) > 65535) {
          var %div = $int($calc($v1 / 65536)), %rem = $v1 % 65536
          bset -t &quot $calc(51 - $mid(%num, 16, 0) - $len(%div)) %div
          %num = %rem $+ $mid(%num, 16)
        }
        %out = $+(:, $base(0 $+ $v1, 10, 16), %out)
        !breplace &quot 0 48
        %num = $regsubex($bvar(&quot, 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
Joined: Apr 2003
Posts: 342
M
Fjord artisan
Offline
Fjord artisan
M
Joined: Apr 2003
Posts: 342
Er IPv6 IPs are already base 16. I don't believe that longip addresses are necessary w/ IPv6.


Beware of MeStinkBAD! He knows more than he actually does!
Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
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.


Invision Support
#Invision on irc.irchighway.net
Joined: Feb 2006
Posts: 546
J
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
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:

Code:

alias longipv6 {
  if ($1-. isnum) {
    if ($1 >= 0) {
      var %num = $1, %i = 8, %out
      while (%i) {
        bunset &quot
        bset &quot 50 0
        while ($left(%num, 15) > 65535) {
          var %div = $int($calc($v1 / 65536)), %rem = $v1 % 65536
          bset -t &quot $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 &quot 0 48
        %num = $regsubex($bvar(&quot, 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
Joined: Dec 2002
Posts: 56
W
Babel fish
Offline
Babel fish
W
Joined: Dec 2002
Posts: 56
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.

Acquire. Analyze. Adapt.
Joined: Dec 2002
Posts: 344
D
Pan-dimensional mouse
Offline
Pan-dimensional mouse
D
Joined: Dec 2002
Posts: 344
Originally Posted By: wshs
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.

Joined: Dec 2002
Posts: 56
W
Babel fish
Offline
Babel fish
W
Joined: Dec 2002
Posts: 56
Completely missed that. Guess that's what I get for being lazy.


Acquire. Analyze. Adapt.

Link Copied to Clipboard