* 0010  REM program template
0020  LET appSpec={goto:'endProgram,title:"IP Calculator"}
0030  window app,appSpec,"APP"
0040  show app
0050  widgetdef Layout_0:={lb1:{widgetType:"labelPicker",order:14,Â
checkCurrentItem:TRUE,indent:15,viewBounds:{top:150,bottom:Â
175,left:79,right:122},labelCommands:["1"],text:"",viewValuÂ
e:0,gosub:'recalc},txtSNMask:{widgetType:"title",order:11,vÂ
iewBounds:{left:168,top:66,right:313,bottom:83},viewJustifyÂ
:0,viewFont:{family:'espy,face:0,size:10},viewFlags:1,text:Â
"255.0.0.0",viewFormat:0},lbSubnet:{widgetType:"labelPickerÂ
",order:4,checkCurrentItem:TRUE,indent:85,viewBounds:{top:6Â
7,bottom:87,left:8,right:116},labelCommands:["0","1","2","3Â
","4","5","6"],text:"Subnet Bits:",viewValue:0,gosub:'recalÂ
c},lb3:{widgetType:"labelPicker",order:17,checkCurrentItem:Â
TRUE,indent:15,viewBounds:{top:150,bottom:175,left:160,righÂ
t:203},labelCommands:["1"],text:"",viewValue:0,gosub:'recalÂ
c},Widget_1:{widgetType:"closeBox",order:1,declareSelf:'basÂ
e,viewFlags:514,viewJustify:9},txtPorts:{widgetType:"title"Â
,order:28,viewBounds:{left:13,top:332,right:288,bottom:410}Â
,viewJustify:0,viewFont:{family:'Monaco,face:0,size:9},viewÂ
Flags:1,text:"ftp-data 20    ftp  21      telnet  23     smÂ
tp  25       time  37     bootps  67     bootpc  68     tftÂ
p  69     gopher  70       finger  79     www-http 80  kerbÂ
eros 88      pop3 110       nntp 119     snmp 161     snmptÂ
rap 162   irc  194     imap3  220",viewFormat:336},lblAddrRÂ
ng:{widgetType:"title",order:13,viewBounds:{top:39,bottom:6Â
0,left:35,right:130},viewJustify:0,viewFont:{family:'espy,fÂ
ace:1,size:10},viewFlags:1,text:"Address Range:",viewFormatÂ
:0},lblsubnets:{widgetType:"title",order:19,viewBounds:{lefÂ
t:11,top:221,right:75,bottom:238},viewJustify:0,viewFont:{fÂ
amily:'espy,face:1,size:10},viewFlags:1,text:"Subnets",viewÂ
Format:0},lblHst:{widgetType:"title",order:22,viewBounds:{lÂ
eft:212,top:183,right:268,bottom:198},viewJustify:0,viewFonÂ
t:{family:'espy,face:1,size:10},viewFlags:1,text:"Host",vieÂ
wFormat:0},lblNet:{widgetType:"title",order:21,viewBounds:{Â
top:184,bottom:199,left:10,right:67},viewJustify:0,viewFontÂ
:{family:'espy,face:1,size:10},viewFlags:1,text:"Network",vÂ
iewFormat:0},lblSubn:{widgetType:"title",order:23,viewBoundÂ
s:{left:114,top:184,right:173,bottom:199},viewJustify:0,vieÂ
wFont:{family:'espy,face:1,size:10},viewFlags:1,text:"SubneÂ
t",viewFormat:0},lblHosts:{widgetType:"title",order:7,viewBÂ
ounds:{top:83,bottom:105,left:35,right:261},viewJustify:0,vÂ
iewFont:{family:'espy,face:0,size:12},viewFlags:1,text:"No Â
subnets, 16,777,214 hosts",viewFormat:0},txtBitUsage:{widgeÂ
tType:"title",order:6,viewBounds:{top:125,bottom:146,left:3Â
5,right:291},viewJustify:0,viewFont:{family:'espy,face:0,siÂ
ze:12},viewFlags:1,text:"0nnnnnnn.hhhhhhhh.hhhhhhhh.hhhhhhhÂ
h",viewFormat:0},lb1ClassMask:{widgetType:"title",order:8,vÂ
iewBounds:{left:131,top:20,right:167,bottom:44},viewJustifyÂ
:0,viewFont:{family:'espy,face:1,size:10},viewFlags:1,text:Â
"Mask:",viewFormat:0},txtSubnet:{widgetType:"title",order:2Â
6,viewBounds:{left:113,top:200,right:208,bottom:222},viewJuÂ
stify:0,viewFont:{family:'espy,face:0,size:10},viewFlags:1,Â
text:"",viewFormat:0},lb1IP:{widgetType:"title",order:15,viÂ
ewBounds:{top:150,bottom:171,left:10,right:84},viewJustify:Â
0,viewFont:{family:'espy,face:1,size:10},viewFlags:1,text:"Â
IP Address:",viewFormat:0},txtNetwork:{widgetType:"title",oÂ
rder:24,viewBounds:{left:10,top:199,right:115,bottom:223},vÂ
iewJustify:0,viewFont:{family:'espy,face:0,size:10},viewFlaÂ
gs:1,text:"1.0.0.0",viewFormat:0},lb2:{widgetType:"labelPicÂ
ker",order:16,checkCurrentItem:TRUE,indent:15,viewBounds:{tÂ
op:150,bottom:175,left:120,right:163},labelCommands:["1"],tÂ
ext:"",viewValue:0,gosub:'recalc},txtbSubnetList:{widgetTypÂ
e:"textList",order:0,viewBounds:{top:237,bottom:305,left:11Â
,right:286},ListItems:["No Subnets"],useScrollers:TRUE,useMÂ
ultipleSelections:NIL,ScrollAmounts:[1,3,20],viewFormat:337Â
},txtHost:{widgetType:"title",order:25,viewBounds:{left:209Â
,top:197,right:314,bottom:224},viewJustify:0,viewFont:{famiÂ
ly:'espy,face:0,size:10},viewFlags:1,text:"0.1.1.1",viewForÂ
mat:0},txtClassMask:{widgetType:"title",order:10,viewBoundsÂ
:{left:168,top:19,right:310,bottom:33},viewJustify:0,viewFoÂ
nt:{family:'espy,face:0,size:10},viewFlags:1,text:"255.0.0.Â
0",viewFormat:0},txtAddrRng:{widgetType:"title",order:12,viÂ
ewBounds:{left:129,top:40,right:294,bottom:65},viewJustify:Â
0,viewFont:{family:'espy,face:0,size:10},viewFlags:1,text:"Â
1.0.0.0 to 126.255.255.255",viewFormat:0},lblPorts:{widgetTÂ
ype:"title",order:27,viewBounds:{left:13,top:319,right:253,Â
bottom:336},viewJustify:0,viewFont:{family:'espy,face:1,sizÂ
e:10},viewFlags:1,text:"Some Well-known Ports",viewFormat:0Â
},BitUsageLabel:{widgetType:"title",order:5,viewBounds:{topÂ
:109,bottom:129,left:35,right:244},viewJustify:0,viewFont:{Â
family:'espy,face:1,size:10},viewFlags:1,text:"Bit Usage   Â
 network/subnet/host",viewFormat:0},lb4:{widgetType:"labelPÂ
icker",order:18,checkCurrentItem:TRUE,indent:15,viewBounds:Â
{top:150,bottom:175,left:200,right:243},labelCommands:["1"]Â
,text:"",viewValue:0,gosub:'recalc},lbClass:{widgetType:"laÂ
belPicker",order:3,checkCurrentItem:TRUE,indent:40,viewBounÂ
ds:{top:20,bottom:45,left:8,right:66},labelCommands:["A","BÂ
","C"],text:"Class:",viewValue:0,gosub:'recalc},lb1SNMask:{Â
widgetType:"title",order:9,viewBounds:{left:130,top:65,righÂ
t:168,bottom:88},viewJustify:0,viewFont:{family:'espy,face:Â
1,size:10},viewFlags:1,text:"Mask:",viewFormat:0},Widget_2:Â
{widgetType:"closeBox",order:2,declareSelf:'base,viewFlags:Â
514,viewJustify:9},txterrmsg:{widgetType:"title",order:20,vÂ
iewBounds:{left:38,top:163,right:278,bottom:184},viewJustifÂ
y:0,viewFont:{family:'espy,face:0,size:10},viewFlags:1,text:"Enter IP Address",v
iewFormat:0}}
0060  window wlist,Layout_0
0070  for i=2 to 254
0080    if i < 224 then addarrayslot(Layout_0.lb1.labelCommands,numberstr(i))
0090    addarrayslot(Layout_0.lb2.labelCommands,numberstr(i))
0100    addarrayslot(Layout_0.lb3.labelCommands,numberstr(i))
0110    addarrayslot(Layout_0.lb4.labelCommands,numberstr(i))
0120  next i
0130  show wlist
0140  wait -1 // indefinitely
0150  goto endProgram //
0160 recalc: REM
0170  let subnet=Layout_0.lbSubnet.viewValue
0180  let class=Layout_0.lbClass.viewValue
0190  let netbits=8*(class+1)
0200  let numnets=round(pow(2,subnet))-2
0210  let hosts=round(pow(2,32-netbits-subnet))-2
0220  if subnet=0 then
0230    let s="No"&&"subnets,"&&numberstr(hosts)&&"hosts each"
0240  else
0250    let s=numberstr(numnets)&&"subnets,"&&numberstr(hosts)&&"hosts each"
0260  end if
0270  LET b=[1,128,192]
0280  LET e=[126,191,223]
0290  SETVALUE(Layout_0.txtAddrRng,'text,numberstr(b[class])&".0.0.0 to "&number
str(e[class])&".255.255.255")
0300  SETVALUE(Layout_0.lblHosts,'text,clone(s))
0310  let s=["0nnnnnnn","10nnnnnnnnnnnnnn","11nnnnnnnnnnnnnnnnnnnnnn"]
0320  let bits=s[class]
0330  for i=1 to subnet
0340    LET bits=bits&"s"
0350  next i
0360  for i=1 to 32-strlen(bits)
0370    LET bits=bits&"h"
0380  next i
0390  LET unused=SETVALUE(Layout_0.txtBitUsage,'text,substr(bits,0,8)&"."&substr
(bits,8,8)&"."&substr(bits,16,8)&"."&substr(bits,24,8))
0400  let s=substr("255.255.255.",0,4*(class+1))&&substr("0.0.0.",0,2*(3-class)-
1)
0410  SETVALUE(Layout_0.txtClassMask,'text,clone(s))
0420  let s=substr("255.255.255.",0,4*(class+1))&&numberstr(round(256-pow(2,8-su
bnet)))&substr(".0.0.0",0,2*(2-class))
0430  SETVALUE(Layout_0.txtSNMask,'text,clone(s))
0440  SETVALUE(Layout_0.txterrmsg,'text,clone(""))
0450  dim o[5]
0460  let o[1]=layout_0.lb1.viewValue+1
0470  let o[2]=layout_0.lb2.viewValue+1
0480  let o[3]=layout_0.lb3.viewValue+1
0490  let o[4]=layout_0.lb4.viewValue+1
0500  if o[1]<b[class] or o[1]>e[class] then
0510    SETVALUE(Layout_0.txterrmsg,'text,clone("Invalid Class "&&chr(65+class)&
&"Address"))
0520  end if
0530  Let myHost=""
0540  Let myNetwork=""
0550  Let mySubnet=""
0560  for i=0 to class
0570    LET myHost=myHost&"0."
0580    let myNetwork=myNetwork&Numberstr(o[i+1])&"."
0590    let mySubnet=mysubnet&"0."
0600  next i
0610  let b1 = round(floor(o[class+2]/pow(2,8-subnet)) *pow(2,8-subnet))
0620  let b2 = o[class+2]-b1
0625  let myHost=myHost&numberstr(b2)&"."
0630  let mySubnet = mySubnet&numberstr(b1)&"."
0640  let myNetwork=myNetwork&numberstr(b1)&"."
0650  for i=class + 2 to 3
0660    LET myNetwork=myNetwork&"0."
0670    LET myHost=myHost&Numberstr(o[i+1])&"."
0680    LET mySubnet=mySubnet&"0."
0690  next i
0700  if subnet=0 then mysubnet=""
0710  SETVALUE(Layout_0.txtNetwork,'text,substr(myNetwork,0,strlen(myNetwork) -1
))
0720  SETVALUE(Layout_0.txtHost,'text,substr(myHost,0,strlen(myHost) - 1))
0730  SETVALUE(Layout_0.txtSubnet,'text,substr(mySubnet,0,strlen(mySubnet) - 1))
0740  if subnet = 0 then
0750    LET slist:=["No Subnets"]
0760  else
0770    LET slist:=[]
0780    for netno=1 to numnets
0790      let ostart = numberstr(o[1])
0800      let oend = numberstr(o[1])
0810      for octet = 1 to class
0820        LET ostart = ostart&&"."&&o[octet+1]
0830        LET oend = oend&&"."&&o[octet+1]
0840      next octet
0850      let obit=netno*(pow(2,8-subnet))
0860      if class=2 then obit=obit+1
0870      let ostart=ostart&&"."&&numberstr(obit)
0880      let obit=obit+pow(2,8-subnet)-1
0890      if class=2 then obit=obit-2
0900      let oend=oend&&"."&&numberstr(obit)
0910      if class=0 then
0920        let ostart = ostart&&".0.1"
0930        let oend=oend&&".255.254"
0940      end if
0950      if class=1 then
0960        let ostart = ostart&&".1"
0970        let oend=oend&&".254"
0980      endif
0990      let sline=numberstr(netno)&"   "&ostart&&"to"&&oend
1000      addarrayslot(slist,sline)
1010    next netno
1020  endif
1030  SETVALUE( Layout_0.txtbSubnetList,'listItems,clone(slist))
1040  u.Layout_0.txtbSubnetList:SETUPLIST()
1050  u.Layout_0.txtbSubnetList:REDOCHILDREN()
1060  return
1070 endProgram: rem
1080  hide
1090  bye
