Forum OpenACS Q&A: IP Address/netmask utilities

Collapse
Posted by Vlad Seryakov on
May be someone will find these routines useful, i needed to check IP/netmask validity from TCL, so these procs were born.

# Converts string IP address into binary representation
proc util_inet_addr { ipaddr } {

set addr [binary format c4 [split $ipaddr .]]
binary scan $addr i bin
return $bin
}

# Converts binary IP address into string
proc util_inet_ntoa { ipaddr } {

set bin [binary format i $ipaddr]
binary scan $bin cccc a b c d
set a [expr { ($a + 0x100) % 0x100 }]
set b [expr { ($b + 0x100) % 0x100 }]
set c [expr { ($c + 0x100) % 0x100 }]
set d [expr { ($d + 0x100) % 0x100 }]
return "$a.$b.$c.$d"
}