Guest User

Untitled

a guest
Apr 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. 'place this code in a separate module
  2.  
  3. Const shBits As Boolean = True 'True = show debug with bits using showBits function
  4. Public Function ipRange(ByVal ipAddr As String, _
  5. ByVal ipMask As String) As List(Of String)
  6. 'returns a list of all ip addresse on a given network
  7. '
  8. 'ipRange("192.168.3.133", "255.255.254.0") '512 hosts - octet crossing
  9. '
  10. '255.255.254.0 is /23
  11. 'List(Of String) - partial output
  12. '192.168.2.0
  13. '192.168.2.1
  14. '192.168.2.2
  15. '...
  16. '192.168.2.254
  17. '192.168.2.255
  18. '192.168.3.0
  19. '192.168.3.1
  20. '...
  21. '192.168.3.253
  22. '192.168.3.254
  23. '192.168.3.255
  24. 'note
  25. 'first entry in list is THE network number
  26. 'last entry in list is the directed broadcast
  27. '
  28. nMSK = IPasNum(ipMask) 'get the Mask as number
  29. CIDR = nCIDR 'get CIDR
  30. nIP = IPasNum(ipAddr) 'get the IP address as number
  31. nNET = IPnetAsNum(nIP, nMSK) 'get THE network number as number
  32. addrs = nNET 'addrs = THE network number as number
  33. nHOST = IPnumOfHosts(nMSK) 'get the number of hosts
  34.  
  35. If shBits Then
  36. Debug.WriteLine("")
  37. Debug.WriteLine("IP".PadRight(10, " "c) & showBits(nIP) & " " & ipAddr)
  38. Debug.WriteLine("Mask".PadRight(10, " "c) & showBits(nMSK) & " " & ipMask)
  39. Debug.WriteLine("Net".PadRight(10, " "c) & showBits(nNET) & " " & NumToIp(nNET))
  40. Debug.WriteLine("Hosts".PadRight(10, " "c) & showBits(nHOST) & " " & nHOST.ToString)
  41. Debug.WriteLine("CIDR".PadRight(10, " "c) & CIDR.ToString)
  42. 'Stop
  43. End If
  44.  
  45. If nIP < 0 OrElse nMSK < 0 OrElse nNET = 0 OrElse nHOST = 0 Then
  46. Return New List(Of String) 'error
  47. End If
  48. ipRange = New List(Of String)
  49. For x As Long = 0 To nHOST
  50. ipRange.Add(NumToIp(addrs)) 'add host to list
  51. addrs += 1 'increment it
  52. Next
  53. End Function
  54.  
  55. Public Function IPnets(ByVal ipAddr As String, _
  56. ByVal curCIDR As Integer, _
  57. ByVal newCIDR As Integer) As List(Of String)
  58. 'returns a list of sub-nets using any IP in the current network,
  59. 'the current mask as CIDR and new mask as CIDR.
  60. '192.168.3.? /23 to /25 - octet crossing
  61. '
  62. 'IPnets("192.168.3.128", 23, 25)
  63. '
  64. 'List(Of String)
  65. '255.255.255.128
  66. '192.168.2.0
  67. '192.168.2.128
  68. '192.168.3.0
  69. '192.168.3.128
  70. '
  71. Dim curMASK, newMASK, netPlus1 As Long, numOfNets As Integer
  72. nIP = IPasNum(ipAddr) 'get the IP address as number
  73. If nIP < 0 OrElse curCIDR < 2 OrElse curCIDR > newCIDR OrElse newCIDR > 30 Then
  74. Return New List(Of String) 'error
  75. End If
  76. IPnets = New List(Of String)
  77. curMASK = MaskFromCidr(curCIDR) 'convert CIDR's to mask as number
  78. newMASK = MaskFromCidr(newCIDR)
  79. nHOST = IPnumOfHosts(newMASK)
  80. netPlus1 = nHOST + 1 'used to add 1 to the network portion of the ip address
  81. nNET = IPnetAsNum(nIP, curMASK) 'get THE network number as number
  82. numOfNets = CInt(2 ^ (newCIDR - curCIDR)) 'number of networks
  83. IPnets.Add(NumToIp(newMASK)) '<<<<<<<<<<<<<<<<<<<<< first entry in list is the new mask
  84.  
  85. If shBits Then
  86. Debug.WriteLine("")
  87. Debug.WriteLine("IP".PadRight(10, " "c) & showBits(nIP) & " " & ipAddr)
  88. Debug.WriteLine("Network".PadRight(10, " "c) & showBits(nNET) & " " & NumToIp(nNET))
  89. Debug.WriteLine("Cur. Mask".PadRight(10, " "c) & showBits(curMASK) & " " & NumToIp(curMASK))
  90. Debug.WriteLine("New Mask".PadRight(10, " "c) & showBits(newMASK) & " " & NumToIp(newMASK))
  91. Debug.WriteLine("Plus 1".PadRight(10, " "c) & showBits(netPlus1) & " " & NumToIp(netPlus1))
  92. Debug.WriteLine("Num. Nets".PadRight(10, " "c) & numOfNets.ToString)
  93. 'Stop
  94. End If
  95.  
  96. For x As Integer = 0 To numOfNets - 1
  97. IPnets.Add(NumToIp(nNET))
  98. 'to get last address(broadcast) Dim BrdCst as long = nNET Or nHOST
  99. nNET += netPlus1 'add one network number
  100. Next
  101. End Function
  102.  
  103. Private Function MaskFromCidr(ByVal CIDR As Integer) As Long
  104. 'x = 32 - CIDR
  105. 'z = (2^x)-1
  106. 'return z xor 255.255.255.255
  107. MaskFromCidr = CLng(2 ^ ((32 - CIDR)) - 1) Xor IPbrdcst
  108. End Function
  109.  
  110. Private Function IPnumOfHosts(ByVal IPmsk As Long) As Long 'a mask for the host portion
  111. '255.255.255.0 XOR 255.255.255.255 = 255 so 0 to 255 is 256 hosts
  112. IPnumOfHosts = IPmsk Xor IPbrdcst 'cacluate the number of hosts
  113. End Function
  114.  
  115. Private Function IPnetAsNum(ByVal IPnum As Long, ByVal IPmsk As Long) As Long 'extract network from ip address
  116. '192.168.2.113 AND 255.255.255.0 = 192.168.2.0
  117. IPnetAsNum = IPnum And IPmsk 'calculate THE network number
  118. End Function
  119.  
  120. Private Function IPasNum(ByVal AipAddr As String) As Long 'return -1 on error
  121. If Not IPAddress.TryParse(AipAddr, aIP) Then 'convert string to IP
  122. Return -1 'error
  123. Else
  124. IPasNum = IPtoNum(aIP) 'convert IP to number
  125. If IPasNum = 0 Then
  126. Return -1 'error
  127. End If
  128. End If
  129. End Function
  130. Private Function IPtoNum(ByVal theIP As IPAddress) As Long 'convert IP to number
  131. Dim IPb() As Byte = theIP.GetAddressBytes 'get the octets
  132. Dim theBit As Integer = 31 'work MSb to LSb
  133. Dim addr As Long 'accumulator for address
  134. nCIDR = 0
  135. For x = 0 To 3 'four octets
  136. For y = 7 To 0 Step -1 'with 8 bits each - duh
  137. If (IPb(x) And CByte(2 ^ y)) = CByte(2 ^ y) Then 'if the bit is on
  138. addr += CLng(2 ^ theBit) 'accumulate
  139. nCIDR += 1 'count bits
  140. End If
  141. theBit -= 1 'decrement bit pos
  142. Next
  143. Next
  144. Return addr
  145. End Function
  146. Private Function NumToIp(ByVal theIP As Long) As String 'convert number back to IP
  147. Dim IPb(3) As Byte '4 octets
  148. Dim theBit As Integer = 31 'work MSb to LSb
  149. Dim addr As String 'accumulator for address
  150. For x = 0 To 3 'four octets
  151. For y = 7 To 0 Step -1 '8 bits
  152. If (theIP And CLng(2 ^ theBit)) = CLng(2 ^ theBit) Then 'if the bit is on
  153. IPb(x) += CByte(2 ^ y) 'accumulate
  154. End If
  155. theBit -= 1
  156. Next
  157. addr &= IPb(x).ToString & "." 'add current octet to string
  158. Next
  159. Return addr.TrimEnd("."c)
  160. End Function
  161. Private Function showBits(ByVal aNum As Long) As String
  162. Dim strObits As New StringBuilder
  163. strObits.Append(Convert.ToString(aNum, 2).PadLeft(32, "0"c))
  164. For ix As Integer = 24 To 0 Step -8
  165. strObits.Insert(ix, " ")
  166. Next
  167. showBits = strObits.ToString.Trim
  168. End Function
Add Comment
Please, Sign In to add comment