Re: Free IP Calculator

2015-01-15 Thread Bob Sneidar
OK I took some time today to really go through this IPCalc function. Note to 
self: Don’t try to write publicly distributable code quickly. 

That being said, I’ve been asked to include this function in the library of 
commands and functions everyone’s been talking about. So I am going to post the 
code here, and anyone wanting a shot at breaking it (as if you could) give it a 
go. NOTE: There is no error checking as of yet so you *could* break it by 
passing an invalid value like 256.256.256.0 for a subnet address. 

Meanwhile I am going to write some pretty thorough error checking before I 
submit it for public consumption, so that there are no values you can pass to 
it that would break it or otherwise return bad values. 

function IPCalc theIPAddress, theSubnetMask
   -- pass either CIDR notation in theIPAddress or else a standard IP and 
subnet mask
   -- returns an array of the following values:
   -- bcastaddr
   -- cidraddr
   -- cidrdepth
   -- firstaddr
   -- ipaddress
   -- lastaddr
   -- subnetaddr
   -- subnetmask
   -- usablecountset the itemdelimiter to .

   set the itemdelimiter to .
   
   -- initial setup
   set the numberFormat to 
   
   -- detemine format
   if theIPAddress contains / then
  put offset(/, theIPAddress) into theCIDRDelim
  put char theCIDRDelim +1 to -1 of theIPAddress into theCIDRDepth
  put charx(1, theCIDRDepth)  charx(0, 32-theCIDRDepth) into 
theBinSubnetMask
  put baseconvert(char 1 to 8 of theBinSubnetMask, 2, 10) into item 1 of 
theSubnetMask
  put baseconvert(char 9 to 16 of theBinSubnetMask, 2, 10) into item 2 of 
theSubnetMask
  put baseconvert(char 17 to 24 of theBinSubnetMask, 2, 10) into item 3 of 
theSubnetMask
  put baseconvert(char 25 to 32 of theBinSubnetMask, 2, 10) into item 4 of 
theSubnetMask
  put char 1 to theCIDRDelim -1 of theIPAddress into theIPAddress
   else
  -- convert the subnet mask to binary
  put 0 into whichOctet
  repeat for each item theOctet in theSubnetMask
 add 1 to whichOctet
 put value(baseConvert(theOctet, 10, 2)) after theBinSubnetMask
  end repeat
  put offset(0, theBinSubnetMask) -1 into theCIDRDepth
   end if
   
   -- 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 theBinValue
  add 0 to theBinValue
  put theBinValue after theBinIPAddress
   end repeat
   
   -- calculate the binary subnet address
   put char 1 to theCIDRDepth of theBinIPAddress into theBinNetworkAddr
   put char theCIDRDepth +1 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 baseconvert(char 1 to 8 of theBinSubnetAddr, 2, 10)  into item 1 of 
theSubnetAddr
   put baseconvert(char 9 to 16 of theBinSubnetAddr, 2, 10)  into item 2 of 
theSubnetAddr
   put baseconvert(char 17 to 24 of theBinSubnetAddr, 2, 10)  into item 3 of 
theSubnetAddr
   put baseconvert(char 25 to 32 of theBinSubnetAddr, 2, 10)  into item 4 of 
theSubnetAddr
   
   -- 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 baseconvert(char 1 to 8 of theBinBcastAddr, 2, 10) into item 1 of 
theBcastAddr
   put baseconvert(char 9 to 16 of theBinBcastAddr, 2, 10) into item 2 of 
theBcastAddr
   put baseconvert(char 17 to 24 of theBinBcastAddr, 2, 10) into item 3 of 
theBcastAddr
   put baseconvert(char 25 to 32 of theBinBcastAddr, 2, 10) into item 4 of 
theBcastAddr
   
   -- 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
   put baseconvert(theBinBcastNodeAddr, 2, 10) -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
___
use-livecode mailing list
use-livecode@lists.runrev.com

Re: Free IP Calculator

2015-01-15 Thread Bob Sneidar
Change this line to:

  — usablecount
  set the itemdelimiter to “.”

won’t work without it. Another note to self: Don’t try to modify code posted in 
an email.

Bob S



On Jan 15, 2015, at 15:18 , Bob Sneidar 
bobsnei...@iotecdigital.commailto:bobsnei...@iotecdigital.com wrote:

  -- usablecountset the itemdelimiter to .

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Re: Free IP Calculator

2015-01-15 Thread Skip Kimpel
That's a lot of notes to yourself :)

Thanks for sharing!

SKIP

 On Jan 15, 2015, at 6:17 PM, Bob Sneidar bobsnei...@iotecdigital.com wrote:
 
 Change this line to:
 
  — usablecount
  set the itemdelimiter to “.”
 
 won’t work without it. Another note to self: Don’t try to modify code posted 
 in an email.
 
 Bob S
 
 
 
 On Jan 15, 2015, at 15:18 , Bob Sneidar 
 bobsnei...@iotecdigital.commailto:bobsnei...@iotecdigital.com wrote:
 
  -- usablecountset the itemdelimiter to .
 
 ___
 use-livecode mailing list
 use-livecode@lists.runrev.com
 Please visit this url to subscribe, unsubscribe and manage your subscription 
 preferences:
 http://lists.runrev.com/mailman/listinfo/use-livecode

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Re: Free IP Calculator

2015-01-13 Thread Bob Sneidar
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 
   
   -- 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 bobsnei...@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
 
 

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Re: Free IP Calculator

2015-01-13 Thread Bob Sneidar
Ok belay that last. Seems I found another bug that only appears with glassful 
subnet masks. I am going to have to convert everything to actual binary (not 
delimited octets) do my math then convert back to delimited octets.

Stay tuned…

Bob S


On Jan 13, 2015, at 16:39 , Bob Sneidar 
bobsnei...@iotecdigital.commailto:bobsnei...@iotecdigital.com wrote:

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

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Re: Free IP Calculator

2015-01-09 Thread Alex Tweedly

Thanks Bob - that looks really useful.

You do need one tiny tweak - the CIDR calculations are slightly off, 
because of the .s in  theBinSubnetMask.

I added the line
  replace . with empty in theBinSubnetMask
just before
   put offset(0, theBinSubnetMask) into theFirstNodeChar

to get the right answer (e.g. 24 instead of 27 in   192.168.1.1 
255.255.255.0)


Thanks again
-- Alex.


On 09/01/2015 00:44, Bob Sneidar wrote:

Hi all.

It’s not often I get a chance to give something back to the LC community, so 
here is my contribution. I put together an IP calculator function which when 
passed any IP address and subnet mask returns an array of pretty much every 
value you want to know about your network. It may also help others who don’t 
know about how subnets are calculated to understand it a bit better. I didn’t 
do any error checking, because frankly there are a lot of bad values you can 
pass, including for example passing 0 for the first octet of the ip address or 
subnet, or passing the broadcast address as the IP address etc.

I will add error checking later. so without further ado, here you go!
function IPCalc theIPAddress, theSubnetMask
set the itemdelimiter to .
-- initial setup
set the numberFormat to 

-- 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 offset(0, theBinSubnetMask) into theFirstNodeChar
put theFirstNodeChar -1 into 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 theCIDRAddr into ipdata [cidrdepth]
put theAddrCount into ipdata [usablecount]
put theCIDRAddr into ipdata [cidraddr]
return ipdata
end IPCalc

Bob S


___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode



___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode