fortypoundhead.com

Calculate Subnet Mask Function

Posted On 2012-09-16 by dwirch
Keywords:
Tags: VB6 Miscellaneous VB6 String Handling VB6 Network Windows
Views: 988


Most developers don't have much of a clue about network fundamentals. What may be a simple thing for a network engineer or system administrator may leave a developer simply scratching his or her head.

This function can help those developers out there that need to figure out what to put into the subnet mask field of their network configuration.

Insert the first and last IP address of your range and it will calculate the subnet mask that fits these values with the starting and ending address for this subnet.

Usage:

Dim MySubnet as String
MySubnet = CalculateSubnetMask("192.168.91.254","192.168.90.0")

And here is the code for the function. Simply paste this into a module, and you should be good to go.

Function CalculateSubnetMask(HighIP As String, LowIP As String) As String
  'Insert the first and last IP address of your range and it will calculate the subnet mask
  'that fits these values with the starting and ending address for this subnet.

  'get the high numbers

  If UBound(Split(HighIP, ".")) = 3 Then

    PrevPos = 0
    For x = 1 To 4

      Pos = InStr(PrevPos + 1, HighIP, ".", 1)
      If x = 4 Then Pos = Len(HighIP) + 1
        Num = Int(Mid(HighIP, PrevPos + 1, Pos - PrevPos - 1))
        PrevPos = Pos
        
        Select Case x
          Case 1
            highnumber4 = Num
          Case 2
            highnumber3 = Num
          Case 3
            highnumber2 = Num
          Case 4
            highnumber1 = Num
        End Select

    Next

  End If

  'get the Low numbers

  If UBound(Split(LowIP, ".")) = 3 Then
    PrevPos = 0
    For x = 1 To 4
      Pos = InStr(PrevPos + 1, LowIP, ".", 1)
      If x = 4 Then Pos = Len(LowIP) + 1
      Num = Int(Mid(LowIP, PrevPos + 1, Pos - PrevPos - 1))
      PrevPos = Pos
      Select Case x
        Case 1
          lownumber4 = Num
        Case 2
          lownumber3 = Num
        Case 3
          lownumber2 = Num
        Case 4
          lownumber1 = Num
      End Select
    Next
  End If

  'Set up result variables.
  Dim subnetmask4 As Integer
  Dim subnetmask3 As Integer
  Dim subnetmask2 As Integer
  Dim subnetmask1 As Integer

  'Figure out the higest bit that changes.
  'We do this by first doing a binary xor of the low and high numbers.
  subnetmask4 = highnumber4 Xor lownumber4
  subnetmask3 = highnumber3 Xor lownumber3
  subnetmask2 = highnumber2 Xor lownumber2
  subnetmask1 = highnumber1 Xor lownumber1

  'Then we round it up to the next most significant digit.
  subnetmask4 = roundup(subnetmask4)
  subnetmask3 = roundup(subnetmask3)
  subnetmask2 = roundup(subnetmask2)
  subnetmask1 = roundup(subnetmask1)

  'Figure out which set of numbers changes, then set the lower numbers from it to 0.
  If (subnetmask4 < 255) Then
    subnetmask3 = 0
    subnetmask2 = 0
    subnetmask1 = 0
  End If

  If (subnetmask3 < 255) Then
    subnetmask2 = 0
    subnetmask1 = 0
  End If

  If (subnetmask2 < 255) Then subnetmask1 = 0

  CalculateSubnetMask = subnetmask4 & "." & subnetmask3 & "." & subnetmask2 & "." & subnetmask1

End Function

Function roundup(subnetmaskx As Integer) As Integer 'We need to figure out the most significant bit, then set the subnetmaskx to that number.

  Select Case subnetmaskx
    Case Is = 0
      roundup = 255
    Case Is < 2
      roundup = 254
    Case Is < 4
      roundup = 252
    Case Is < 8
      roundup = 248
    Case Is < 16
      roundup = 240
    Case Is < 32
      roundup = 224
    Case Is < 64
      roundup = 192
    Case Is < 128
      roundup = 128
    Case Is < 256
      roundup = 0
    Case Else
      roundup = 0
  End Select
  
End Function


About the Author

dwirch has posted a total of 181 articles.

You can find more information from dwirch by visiting http://www.derekwirch.com.


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.81.45.122

Before you can post, you need to prove you are human. If you log in, this test goes away.




Recent Forum Posts

Advanced search added
dwirch posted on September 23, 2017 at about 13:44 in Site News

Job Spammer: Gaurav Mehta - AgreeYa Solutions
dwirch posted on September 22, 2017 at about 10:35 in Spammers

Job Spammer: Prutha Siri - Javelin Systems
dwirch posted on September 10, 2017 at about 6:15 in Spammers

New security implemented
dwirch posted on September 7, 2017 at about 7:16 in Site News

Malicious IP Checker Companion Tool
dwirch posted on August 12, 2017 at about 20:24 in Site News

Job Spammer: Steve Adams
dwirch posted on August 8, 2017 at about 7:44 in Spammers