Free IP Calculator
Bob Sneidar
bobsneidar at iotecdigital.com
Tue Jan 13 19:39:50 EST 2015
OK Sorry all this seems to return all the correct values. If anyone finds anymore bugs let me know.
Bob S
function IPCalc theIPAddress, theSubnetMask
set the itemdelimiter to "."
-- initial setup
set the numberFormat to "00000000"
-- convert the ip address to binary
put 0 into whichOctet
repeat for each item theOctet in theIPAddress
add 1 to whichOctet
put baseConvert(theOctet, 10, 2) into item whichOctet of theBinIPAddress
add 0 to item whichOctet of theBinIPAddress
end repeat
-- convert the subnet mask to binary
put 0 into whichOctet
repeat for each item theOctet in theSubnetMask
add 1 to whichOctet
put baseConvert(theOctet, 10, 2) into item whichOctet of theBinSubnetMask
add 0 to item whichOctet of theBinSubnetMask
end repeat
-- calculate the binary subnet address
put theBinSubnetMask into theBinSubnetAddr
put offset("0", theBinSubnetAddr) into theFirstNodeChar
put theFirstNodeChar -1 into theCIDRDepth
if theCIDRDepth >9 then subtract 1 from theCIDRDepth
if theCIDRDepth >18 then subtract 1 from theCIDRDepth
if theCIDRDepth >27 then subtract 1 from theCIDRDepth
put char 1 to theFirstNodeChar -1 of theBinIPAddress into theBinNetworkAddr
put char theFirstNodeChar to -1 of theBinIPAddress into theBinNodeAddr
put theBinNodeAddr into theBinSubnetNodeAddr
set the numberFormat to "0"
replace "1" with "0" in theBinSubnetNodeAddr
put theBinNetworkAddr & theBinSubnetNodeAddr into theBinSubnetAddr
-- convert the binary subnet address to decimal
put 0 into whichOctet
repeat for each item theOctet in theBinSubnetAddr
add 1 to whichOctet
put baseconvert(theOctet, 2, 10) into item whichOctet of theSubnetAddr
end repeat
-- calculate the first usable IP address
put theSubnetAddr into theFirstAddr
add 1 to item 4 of theFirstAddr
-- calculate the binary broadcast address
put theBinNodeAddr into theBinBcastNodeAddr
replace "0" with "1" in theBinBcastNodeAddr
put theBinNetworkAddr & theBinBcastNodeAddr into theBinBcastAddr
-- convert the binary broadcast address to decimal
put 0 into whichOctet
repeat for each item theOctet in theBinBcastAddr
add 1 to whichOctet
put baseConvert(theOctet, 2 ,10) into item WhichOctet of theBcastAddr
end repeat
-- calculate the last usable IP address
put theBcastAddr into theLastAddr
subtract 1 from item 4 of theLastAddr
-- calculate the number of usable addresses
put item 4 of theLastAddr - item 4 of theFirstAddr +1 into theAddrCount
-- calculate the CIDR notation
put theSubnetAddr & "/" & theCIDRDepth into theCIDRAddr
-- create array
put theIPAddress into ipdata ["ipaddress"]
put theSubnetMask into ipdata ["subnetmask"]
put theSubnetAddr into ipdata ["subnetaddr"]
put theFirstAddr into ipdata ["firstaddr"]
put theBcastAddr into ipdata["bcastaddr"]
put theLastAddr into ipdata ["lastaddr"]
put theCIDRDepth into ipdata ["cidrdepth"]
put theAddrCount into ipdata ["usablecount"]
put theCIDRAddr into ipdata ["cidraddr"]
return ipdata
end IPCalc
> On Jan 9, 2015, at 16:42 , Bob Sneidar <bobsneidar at iotecdigital.com> wrote:
>
> Looks like that whacks some other things. I’ll look at this on the weekend and verify all the returned values are correct. I can’t believe I saw 25 when it was supposed to be 23 and I didn’t catch it.
>
> Bob S
>
>
More information about the use-livecode
mailing list