Version 12 of A little 6to4 calculator

Updated 2010-04-30 09:08:48 by rmax

rmax - April 2005

This little tool takes a list of IPv4 addresses on the command line, and calculates the respective IPv6 address prefixes for the 6to4 network. (The IPv4 addresses need to be in dotted decimal notation; other dotted quad formats where each octet is in octal or hexadecimal notation will fail.)

Example usage:

 $ 6to4 10.0.0.1 192.168.22.33 
 10.0.0.1 -> 2002:a00:1::
 192.168.22.33 -> 2002:c0a8:1621::

 proc 6to4 {addr} {
    set octets [split [string trim $addr] .]
    if {[llength $octets] != 4} {
        return -code error \
            "\"$addr\" does not consist of 4 octets separated by dots"
    }
    foreach octet $octets {
        if {![string is integer $octet] || $octet < 0 || $octet > 255} {
            return -code error "\"$octet\" is not a valid octet"
        }
    }
    foreach {a b} $octets {
        lappend words [expr {($a<<8) | $b}]
    }
    eval [linsert $words 0 format "2002:%x:%x::"]
 }

 foreach v4addr $argv {
    catch {6to4 $v4addr} v6addr
    puts "$v4addr -> $v6addr"
 }

WHD: Not to be pedantic, but since it's converting IPV4 to IPV6, shouldn't it be called 4to6? Or am I missing something?

PT: you are missing something - RFC 3056 is all about something called 6to4 as opposed to 6over4 which is something else.

Note also that the tcllib ip package (from the dns module) can accept and manipulate ipv4 and ipv6 addresses and includes the ability to take 6to4 addresses of the type 2002:192.168.0.4::/48