mIRC Homepage
Posted By: FroggieDaFrog $longip() for ipv6 addresses - 20/09/10 11:58 AM
As stated in the subject: $longip() for ipv6 addresses
Posted By: jaytea Re: $longip() for ipv6 addresses - 22/09/10 07:00 AM
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!

Posted By: MeStinkBAD Re: $longip() for ipv6 addresses - 23/09/10 04:07 PM
Er IPv6 IPs are already base 16. I don't believe that longip addresses are necessary w/ IPv6.
Posted By: Riamus2 Re: $longip() for ipv6 addresses - 23/09/10 04:55 PM
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.
Posted By: jaytea Re: $longip() for ipv6 addresses - 25/09/10 04:28 PM
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/, )
  }
}


Posted By: wshs Re: $longip() for ipv6 addresses - 04/10/10 07:35 PM
Quite some complex code there, but does it handle IPs like 2001:570:: or 2001:570::f, etc?
Posted By: drum Re: $longip() for ipv6 addresses - 04/10/10 09:40 PM
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.
Posted By: wshs Re: $longip() for ipv6 addresses - 04/10/10 09:44 PM
Completely missed that. Guess that's what I get for being lazy.
© mIRC Discussion Forums