A little 6to4 calculator

A little 6to4 calculator

rmax 2005-04:

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

$ 6to4 132.235.201.144 
132.235.201.144 -> 2610:a8:4831:0538::

Code

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 tcllib_ip 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