Version 0 of Handling internet addresses

Updated 2004-06-07 10:17:14 by PT

I think it would be useful to have a module in tcllib to do some manipulations of internet addresses. If we can think up a sensible API then we can provide something to help cope with both IPv4 and IPv6 addresses. For instance, how do I check that an address is within a certain range? Is 192.168.0.4 within 192.168.0.0./24 or within 192.16./16 or even within 192.168.0.0./255.255.255.0?

Here are a few helper functions I've used elsewhere.

ip4_to_x Convert an IPv4 address in dotted quad notation into a hexadecimal representation. This will extend truncated ip4 addresses with zeros. eg: ip2x 192.168.0.4 -> 0xc0a80004 or ip2x 127 -> 0x7f000000

 proc ::ipaddr::ip2x {ip} {
    set octets [lrange [concat [split $ip .] 0 0 0] 0 3]
    foreach oct $octets {
        if {$oct < 0 || $oct > 255} {
            return -code error "invalid ip address"
        }
    }
    eval [linsert $octets 0 format 0x%02x%02x%02x%02x]
 }

[Category Internet]