WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

TCPIP and IP Address

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Subnet Calculations


; Given an ip address and its subnet, thie example
; provides the network address, the range of usable addresses
; in the subnet and the broadcast address

;============================================================
#DefineSubRoutine DoWork()

;ipaddr="207.195.193.145"
;subnet="255.255.255.192"

;Code begins...
statustext="Enter IP Address"
networkaddress=""
broadcastaddress=""
lowrange=""
highrange=""
adused=""
adavail=""
;validate input ip
If ItemCount(ipaddr,".") != 4
  statustext="Enter IP Address"
  ;Message(ipaddr,statustext)
  Return
EndIf
If ItemExtract(4,ipaddr,".")==""
  statustext="Enter IP Address"
  ;Message(ipaddr,statustext)
  Return
EndIf

;validate subnet
If ItemCount(subnet,".") != 4
  statustext="Enter Subnet"
  ;Message(subnet,statustext)
  Return
EndIf
If ItemExtract(4,subnet,".")==""
  statustext="Enter subnet"
  ;Message(ipaddr,statustext)
  Return
EndIf

;;more validate ip
For xx=1 To 4
   ipx=ItemExtract(xx,ipaddr,".")
   If IsInt(ipx)==@FALSE
        statustext="Dotted quad ip address must have 4 integers"
       ; Message(ipaddr,statustext)
        Return
   EndIf
   If (ipx<0) || (ipx>255)
        statustext="Each number in IP Address dotted quad must be between 0 and 255"
        ;Message(ipaddr,statustext)
        Return
   EndIf
Next
   
;;more validate subnet
mustbezero=@FALSE
validsubnets="255 254 252 248 249 224 192 128 0"
For xx=1 To 4
   sub=ItemExtract(xx,subnet,".")
   If IsInt(sub)==@FALSE
        statustext="Dotted quad subnet must have 4 integers"
        ;Message(subnet,statustext)
        Return
   EndIf
   If (sub<0) || (sub>255)
        statustext="Each number subnet dotted quad must be between 0 and 255"
        ;Message(subnet,statustext)
        Return
   EndIf
   If sub!=0 && mustbezero==@TRUE
        statustext="Any number after a non-255 number must be zero"
        ;Message(subnet,statustext)
        Return
   EndIf

   If sub!=255 Then mustbezero=@TRUE

   sub=sub+0  ; remove leading zeros if any
   If ItemLocate(sub,validsubnets," ")==0
        statustext=StrCat("Valid nums are: ",validsubnets)
        ;Message(subnet,statustext)
        Return
   EndIf

   If xx>=254
        statustext="Last octet of subnet must not be 254 or 255"
        ;Message(subnet,statustext)
        Exit
   EndIf

Next





networkaddress=""
broadcastaddress=""
For xx=1 To 4
   ipx=ItemExtract(xx,ipaddr,".")
   subx=ItemExtract(xx,subnet,".")

   low = ipx & subx

   high = low | (~subx & 255)

   If xx==1 
      networkaddress=low
      broadcastaddress=high
   Else
      networkaddress=StrCat(networkaddress,".",low)
      broadcastaddress=StrCat(broadcastaddress,".",high)
  EndIf
Next


;get usable address range

L4=ItemExtract(4,networkaddress,".")
L3=ItemExtract(3,networkaddress,".")
L2=ItemExtract(2,networkaddress,".")
L1=ItemExtract(1,networkaddress,".")

H4=ItemExtract(4,broadcastaddress,".")
H3=ItemExtract(3,broadcastaddress,".")
H2=ItemExtract(2,broadcastaddress,".")
H1=ItemExtract(1,broadcastaddress,".")

L4=L4+1
If L4==256
   L4=0
   L3=L3+1
EndIf
If L3==256
   L3=0
   L2=L2+1
EndIf
If L2==256
   L2=0
   L1=L1+1
EndIf
; If L1==256 well its an error  t0o bad

H4=H4-1
If H4 == -1
   H4 = 255
   H3 = H3 -1
EndIf
If H3 == -1
   H3 = 255
   H2 = H2 -1
EndIf
If H2 == -1
   H2 = 255
   H1=H1-1
EndIf
;if H1 == -1 its an error  too bad

lowrange=StrCat(L1,".",L2,".",L3,".",L4)
highrange=StrCat(H1,".",H2,".",H3,".",H4)

;calc number of addreses used
S4=ItemExtract(4,subnet,".")
S3=ItemExtract(3,subnet,".")
S2=ItemExtract(2,subnet,".")
S1=ItemExtract(1,subnet,".")

S1=S1 << 24
S2=S2 << 16
S3=S3 << 8
subnetX=S1 | S2 | S3 | S4

adused = (~subnetx) +1
adavail=adused-2
If adused < 0
   adused="Lots"
   adavail="Lots-2"
EndIf





T1=StrCat("Ip Address: ",ipaddr)
T2=StrCat("Subnet: ",subnet)
T3=StrCat("Net Address: ",networkaddress)
T4=StrCat("Broadcast: ",broadcastaddress)
T5=StrCat("Low usable:",lowrange)
T6=StrCat("Hi Usable ",highrange)
T7=StrCat("Addresses used: ",adused)
T8=StrCat("Addresses available: ",adused-2)

pastetext=StrCat(T1,@CRLF,T2,@CRLF,T3,@CRLF,T4,@CRLF,T5,@CRLF,T6,@CRLF,T7,@CRLF,T8)
;Message("Info",text)

Return
#EndSubRoutine
;============================================================
;============================================================


#DefineSubRoutine MyDialogCallbackProc(MyDialog_Handle,MyDialog_Message,MyDialog_ID,rsvd1,rsvd2)
   ;DialogprocOptions Constants
   MSG_INIT=0                ; The one-time initialization
   MSG_TIMER=1               ; Timer event
   MSG_BUTTONPUSHED=2        ; Pushbutton or Picturebutton
   MSG_RADIOPUSHED=3         ; Radiobutton clicked
   MSG_CHECKBOX=4            ; Checkbox clicked
   MSG_EDITBOX=5             ; Editbox or Multilinebox
   MSG_FILESELECT=6          ; Filelistbox
   MSG_ITEMSELECT=7          ; Itembox
   MSG_COMBOCHANGE=8         ; Combobox/Droplistbox
   MSG_CALENDAR=9            ; Calendar date change
   MSG_SPINNER=10            ; Spinner number change
   MSG_CLOSEVIA49=11         ; Close clicked (Enabled via Intcontrol 49)
   MSG_FILEBOXDOUBLECLICK=12 ; Get double-click message on a FileListBox
   MSG_ITEMBOXDOUBLECLICK=13 ; Get double-click message on an ItemBox
   DPO_DISABLESTATE=1000     ; codes -1=GetSetting 0=EnableDialog 1=DisableDialog
   DPO_CHANGEBACKGROUND=1001 ; -1=GetSetting otherise bitmap or color string
   ;DialogControlState Constants
   DCSTATE_SETFOCUS=1        ; Give Control Focus
   DCSTATE_QUERYSTYLE=2      ; Query control's style
   DCSTATE_ADDSTYLE=3        ; Add control style
   DCSTATE_REMOVESTYLE=4     ; Remove control style
   DCSTATE_GETFOCUS=5        ; Get control that has focus
   DCSTYLE_INVISIBLE=1       ; Set Control Invisible
   DCSTYLE_DISABLED=2        ; Set Control Disabled
   DCSTYLE_NOUSERDATA=4      ; Note: Setable via DialogControlState function ONLY SPINNER control only
   DCSTYLE_READONLY=8        ; Sets control to read-only (user cannot type in data) EDITBOX MULTILINEBOX SPINNER
   DCSTYLE_PASSWORD=16       ; Sets 'password mode' where only *'s are displayed EDITBOX
   DCSTYLE_DEFAULTBUTTON=32  ; Sets a button as the default button PUSHBUTTON PICTUREBUTTON
   DCSTYLE_DIGITSONLY=64     ; Set edit box to accept digits only EDITMOX MULTILINEBOX
   DCSTYLE_FLAT=128          ; Makes a 'flat' hyperlink-looking button PUSHBUTTON PICTUREBUTTON
   DCSTYLE_NOADJUST=256      ; Turns off auto-height adjustment  ITEMBOX FILELISTBOX
   DCSTYLE_TEXTCENTER=512    ; Center text in control VARYTEXT STATICTEXT
   DCSTYLE_TEXTRIGHT=1024    ; Flush-Right text in control VARYTEXT STATICTEXT
   ;DialogControlSet / DialogControlGet Constants
   DC_CHECKBOX=1             ; CHECKBOX
   DC_RADIOBUTTON=2          ; RADIOBUTTON
   DC_EDITBOX=3              ; EDITBOX MULTILINEBOX
   DC_TITLE=4                ; PICTURE RADIOBUTTON CHECKBOX PICTUREBUTTON VARYTEXT STATICTEXT GROUPBOX PUSHBUTTON
   DC_ITEMBOXCONTENTS=5      ; ITEMBOX FILELISTBOX DROPLISTBOX
   DC_ITEMBOXSELECT=6        ; ITEMBOX FILELISTBOX DROPLISTBOX
   DC_CALENDAR=7             ; CALENDAR
   DC_SPINNER=8              ; SPINNER
   DC_MULTITABSTOPS=9        ; MULTILINEBOX
   DC_ITEMSCROLLPOS=10       ; ITEMBOX FILELISTBOX
   DC_BACKGROUNDCOLOR=11     ; RADIOBUTTON CHECKBOX VARYTEXT STATICTEXT GROUPBOX PUSHBUTTON ITEMBOX FILELISTBOX DROPLISTBOX SPINNER EDITBOX MULTILINEBOX
   DC_PICTUREBITMAP=12       ; PICTURE PICTUREBUTTON
   DC_TEXTCOLOR=13           ; RADIOBUTTON CHECKBOX VARYTEXT STATICTEXT GROUPBOX PUSHBUTTON ITEMBOX FIELLISTBOX DROPLISTBOX SPINNER EDITBOX MULTILINEBOX
   DC_ITEMBOXADD=14          ; ITEMBOX FILELISTBOX DROPLISTBOX
   DC_ITEMBOXREMOVE=15       ; ITEMBOX FILELISTBOX DROPLISTBOX


   Switch MyDialog_Message
      Case MSG_INIT
;         DialogProcOptions(MyDialog_Handle,MSG_TIMER,1000)
         DialogProcOptions(MyDialog_Handle,MSG_BUTTONPUSHED,@TRUE)
         DialogProcOptions(MyDialog_Handle,MSG_EDITBOX,@TRUE)
         DialogControlSet(MyDialog_Handle,003,DC_EDITBOX,""  )   ;ip
         DialogControlSet(MyDialog_Handle,004,DC_EDITBOX,""  )   ;Sub
         DialogControlSet(MyDialog_Handle,008,DC_TITLE,"Enter IP Address"  )   ;status
         DialogControlSet(MyDialog_Handle,014,DC_TITLE,""  )   ;NetAddr
         DialogControlSet(MyDialog_Handle,015,DC_TITLE,""  )   ;lowrange
         DialogControlSet(MyDialog_Handle,016,DC_TITLE,""  )   ;hirange
         DialogControlSet(MyDialog_Handle,017,DC_TITLE,""  )   ;broadcast
         DialogControlSet(MyDialog_Handle,019,DC_TITLE,""  )   ;
         DialogControlSet(MyDialog_Handle,021,DC_TITLE,""  )   ;

         Return(-1)

     Case MSG_BUTTONPUSHED
        Switch MyDialog_ID
           Case 001         ; OK/Quit
              Return(1)
           Case 002         ; Cancel
              Return(-1)
           Case 009           ; Paste
              ClipPut(pastetext)
              Return(-2)

        EndSwitch    ;MyDialog_ID
        Return(-1)      ;  Do default processing

     Case MSG_EDITBOX
        Switch MyDialog_ID
           Case 003
           Case 004
              ipaddr=DialogControlGet(MyDialog_Handle,003,DC_EDITBOX)
              subnet=DialogControlGet(MyDialog_Handle,004,DC_EDITBOX)
              DoWork()
              DialogControlSet(MyDialog_Handle,008,DC_TITLE,statustext  )   ;status
              DialogControlSet(MyDialog_Handle,014,DC_TITLE,networkaddress  )   ;NetAddr
              DialogControlSet(MyDialog_Handle,015,DC_TITLE,lowrange  )   ;lowrange
              DialogControlSet(MyDialog_Handle,016,DC_TITLE,highrange  )   ;hirange
              DialogControlSet(MyDialog_Handle,017,DC_TITLE,broadcastaddress  )   ;broadcast
              DialogControlSet(MyDialog_Handle,019,DC_TITLE,adused  )   ;
              DialogControlSet(MyDialog_Handle,021,DC_TITLE,adavail  )   ;

              Return(-1)

        Return(-1)      ;  Do default processing

   EndSwitch       ; MyDialog_Message
   Return(-1)      ;  Do default processing
#EndSubRoutine       ;End of Dialog Callback MyDialogCallbackProc

;============================================================
;============================================================
;============================================================



 pastetext="No data entered"



MyDialogFormat=`WWWDLGED,6.1`

MyDialogCaption=`SubNet Info`
MyDialogX=-69
MyDialogY=104
MyDialogWidth=350
MyDialogHeight=104
MyDialogNumControls=021
MyDialogProcedure=`MyDialogCallbackProc`
MyDialogFont=`Microsoft Sans Serif|5632|70|34`
MyDialogTextColor=`0|0|0`
MyDialogBackground=`DEFAULT,DEFAULT`
MyDialogConfig=0

MyDialog001=`059,079,036,012,PUSHBUTTON,DEFAULT,"Quit",1,1,32,DEFAULT,DEFAULT,DEFAULT`
MyDialog002=`103,079,036,012,PUSHBUTTON,DEFAULT,"Cancel",0,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog003=`051,011,096,012,EDITBOX,MyVariable1,"Edit 1",DEFAULT,3,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog004=`051,029,094,012,EDITBOX,MyVariable2,"Edit 2",DEFAULT,4,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog005=`001,011,044,012,STATICTEXT,DEFAULT,"IP Address:",DEFAULT,5,1024,DEFAULT,DEFAULT,DEFAULT`
MyDialog006=`001,029,044,012,STATICTEXT,DEFAULT,"SubNet:",DEFAULT,6,1024,DEFAULT,DEFAULT,DEFAULT`
MyDialog007=`001,049,044,012,STATICTEXT,DEFAULT,"Status Info:",DEFAULT,7,1024,DEFAULT,DEFAULT,DEFAULT`
MyDialog008=`053,049,090,024,STATICTEXT,DEFAULT,"Enter IP Address",DEFAULT,8,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog009=`015,079,036,012,PUSHBUTTON,DEFAULT,"Paste",2,9,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog010=`159,011,064,012,STATICTEXT,DEFAULT,"Netword Addr:",DEFAULT,10,1024,DEFAULT,DEFAULT,DEFAULT`
MyDialog011=`163,031,060,012,STATICTEXT,DEFAULT,"Beginning IP Addr:",DEFAULT,11,1024,DEFAULT,DEFAULT,DEFAULT`
MyDialog012=`159,049,064,012,STATICTEXT,DEFAULT,"Ending IP Addr:",DEFAULT,12,1024,DEFAULT,DEFAULT,DEFAULT`
MyDialog013=`161,067,062,012,STATICTEXT,DEFAULT,"Broadcast Addr:",DEFAULT,13,1024,DEFAULT,DEFAULT,DEFAULT`
MyDialog014=`235,011,094,012,STATICTEXT,DEFAULT,"Static 9",DEFAULT,14,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog015=`235,029,094,012,STATICTEXT,DEFAULT,"Static 10",DEFAULT,15,DEFAULT,"Microsoft Sans Serif|5632|70|34","255|0|0",DEFAULT`
MyDialog016=`235,047,088,012,STATICTEXT,DEFAULT,"Static 11",DEFAULT,16,DEFAULT,"Microsoft Sans Serif|5632|70|34","255|0|0",DEFAULT`
MyDialog017=`235,065,094,012,STATICTEXT,DEFAULT,"Static 12",DEFAULT,17,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog018=`163,083,022,012,STATICTEXT,DEFAULT,"Used:",DEFAULT,18,1024,DEFAULT,DEFAULT,DEFAULT`
MyDialog019=`193,083,034,012,STATICTEXT,DEFAULT,"Static 2",DEFAULT,19,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog020=`235,083,034,012,STATICTEXT,DEFAULT,"Available:",DEFAULT,20,1024,DEFAULT,DEFAULT,DEFAULT`
MyDialog021=`275,083,038,012,STATICTEXT,DEFAULT,"Static 4",DEFAULT,21,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("MyDialog")




Exit




Article ID:   W16726
File Created: 2005:02:18:12:22:02
Last Updated: 2005:02:18:12:22:02