Public Class clsTEST Private Sub Test Dim testIP As String Dim testCIDR As String Dim c As New clsCIDR ' Test parameters testIP = "192.168.1.2" testCIDR = "192.168.0.0/16" ' Test cases MsgBox("Is " & testIP & " an ip adress?" & vbCrLf & c.IsIPAddress(testIP)) MsgBox("Is " & testCIDR & " an CIDR mask ?" & vbCrLf & c.IsCIDRMask(testCIDR)) MsgBox("Max adress of " & testCIDR & " is: " & vbCrLf & c.CIDRMaxAddress(testCIDR)) MsgBox("Is " & testIP & " valid within " & testCIDR & "?" & vbCrLf & c.IPValidateCIDR(testCIDR, testIP)) c = Nothing End End Sub End Class Public Class clsCIDR ' CIDR class (C) 2005-2009 Glenn Larsson ' ------------------------------------------------ ' Free for non-commercial use. Commercial use prohibited! ' -------------------------------------------------------------------------------- ' IsIPAddress : Test if the provided variable an IP Adress ' -------------------------------------------------------------------------------- Public Function IsIPAddress(ByVal IPAddress As String) As Boolean IsIPAddress = False Const VALIDCHARS = "0123456789." ' Set Valid chars for IP Address Dim t As String Dim n As Integer Dim x() As String For n = 1 To Len(IPAddress) ' Check for invalid characters t = Mid(IPAddress, n, 1) 'If InStr(1, VALIDCHARS, t) = 0 Then Exit Function '(VB6 syntax) If Microsoft.VisualBasic.InStr(VALIDCHARS, t) = 0 Then Exit Function Next n If InStr(1, IPAddress, "..") > 0 Then Exit Function ' Skip ".." If InStr(1, IPAddress, ".00") > 0 Then Exit Function ' Skip ".00" and ".000" x = Split(IPAddress, ".") If UBound(x) <> 3 Then Exit Function ' IP V4 adresses only (4 digits exactly) For n = 0 To UBound(x) ' Values between 0-255 and not "" If Trim(x(n)) = "" Then Exit Function If CLng(x(n)) < 0 Then Exit Function If CLng(x(n)) > 255 Then Exit Function Next n Return True End Function ' -------------------------------------------------------------------------------- ' IsCIDRMask = Test if the provided variable a CIDR mask ' -------------------------------------------------------------------------------- Public Function IsCIDRMask(ByVal CIDRMask As String) As Boolean IsCIDRMask = False Const VALIDCHARS = "0123456789./" ' Set valid chars for CIDR mask Dim n As Integer Dim T As String Dim X() As String For n = 1 To Len(CIDRMask) ' Check valid chars T = Mid(CIDRMask, n, 1) If InStr(1, VALIDCHARS, T) = 0 Then Exit Function Next n If InStr(1, CIDRMask, "/") = 0 Then Exit Function ' No "/" ? If InStr(1, CIDRMask, "//") > 0 Then Exit Function ' No "//" ? If Left(CIDRMask, 1) = "/" Then Exit Function ' no Left "/" If Right(CIDRMask, 1) = "/" Then Exit Function ' no Right "/" If InstrCnt(CIDRMask, "/") <> 1 Then Exit Function ' Only ONE slash ("/") please X = Split(CIDRMask, "/") If UBound(X) <> 1 Then Exit Function ' Must be 2 fields (0-1) If IsIPAddress(CStr(X(0))) = False Then Exit Function ' Left part must be IP Address format If Trim(X(1)) = "" Then Exit Function ' no "" If CLng(X(1)) < 1 Then Exit Function ' Valid range = 31-1 (no 0.0.0.0 or 255.255.255.255) If CLng(X(1)) > 31 Then Exit Function Return True End Function ' -------------------------------------------------------------------------------- ' IPValidateCIDR = Validate if a given IP Adress is allowed within a CIDR mask ' -------------------------------------------------------------------------------- Public Function IPValidateCIDR(ByVal CIDRMask As String, ByVal IPAddress As String) As Boolean IPValidateCIDR = False Dim n As Integer Dim T() As String Dim CIP() As String Dim z As String Dim BaseIP() As String Dim Mask As String ' Test the obvious first If IsCIDRMask(CIDRMask) = False Then Exit Function ' Make sure CIDRMask is a CIDR Mask If IsIPAddress(IPAddress) = False Then Exit Function 'Make sure IPAddress is an IP Address T = Split(CIDRMask, "/") BaseIP = Split(T(0), ".") ' Split into IP address Mask = T(1) ' Set up Max IP Dim MaxIP(3) As Long For n = 0 To 3 MaxIP(n) = 255 Next n Mask = Mask + 1 ' Bit 128 in Left Octet For n = 0 To 3 ' Build up CIDR Mask If Mask > 1 Then MaxIP(n) = MaxIP(n) - 128 Mask = Mask - 1 End If If Mask > 1 Then MaxIP(n) = MaxIP(n) - 64 Mask = Mask - 1 End If If Mask > 1 Then MaxIP(n) = MaxIP(n) - 32 Mask = Mask - 1 End If If Mask > 1 Then MaxIP(n) = MaxIP(n) - 16 Mask = Mask - 1 End If If Mask > 1 Then MaxIP(n) = MaxIP(n) - 8 Mask = Mask - 1 End If If Mask > 1 Then MaxIP(n) = MaxIP(n) - 4 Mask = Mask - 1 End If If Mask > 1 Then MaxIP(n) = MaxIP(n) - 2 Mask = Mask - 1 End If If Mask > 1 Then MaxIP(n) = MaxIP(n) - 1 Mask = Mask - 1 End If If Mask < 1 Then Exit For Next n For n = 0 To 3 ' Calculate Max IP Address z = BaseIP(n) + MaxIP(n) If CInt(z) > 255 Then z = 255 MaxIP(n) = z Next n CIP = Split(IPAddress, ".") Dim cmpBaseIp As Long Dim cmpMaxIp As Long Dim cmpCurrentIp As Long ' Build Current IP cmpCurrentIp = (CLng(CIP(0) * 16777216) + CLng(CIP(1) * 65536) + CLng(CIP(2) * 256) + CLng(CIP(3))) ' Build Base IP cmpBaseIp = (CLng(BaseIP(0) * 16777216) + CLng(BaseIP(1) * 65536) + CLng(BaseIP(2) * 256) + CLng(BaseIP(3))) ' Build Max IP cmpMaxIp = (CLng(MaxIP(0) * 16777216) + CLng(MaxIP(1) * 65536) + CLng(MaxIP(2) * 256) + CLng(MaxIP(3))) ' Compare if within range If cmpCurrentIp < cmpBaseIp Then Exit Function If cmpCurrentIp > cmpMaxIp Then Exit Function Return True ' If we got this far... End Function ' -------------------------------------------------------------------------------- ' CIDRMaxAddress = Return the maximum (Top adress) for a CIDR mask) ' -------------------------------------------------------------------------------- Public Function CIDRMaxAddress(ByVal CIDRMask As String) As String CIDRMaxAddress = "255.255.255.255" ' Default CIDRMaxAddress - if fail. Dim n As Integer Dim z As String Dim T() As String Dim BaseIP() As String Dim Mask As String If IsCIDRMask(CIDRMask) = False Then Exit Function ' Make sure CIDRMask is a CIDR mask T = Split(CIDRMask, "/") BaseIP = Split(T(0), ".") Mask = T(1) Dim MaxIP(3) As Long For n = 0 To 3 ' Get Max IP MaxIP(n) = 255 Next n Mask = Mask + 1 For n = 0 To 3 If Mask > 1 Then MaxIP(n) = MaxIP(n) - 128 Mask = Mask - 1 End If If Mask > 1 Then MaxIP(n) = MaxIP(n) - 64 Mask = Mask - 1 End If If Mask > 1 Then MaxIP(n) = MaxIP(n) - 32 Mask = Mask - 1 End If If Mask > 1 Then MaxIP(n) = MaxIP(n) - 16 Mask = Mask - 1 End If If Mask > 1 Then MaxIP(n) = MaxIP(n) - 8 Mask = Mask - 1 End If If Mask > 1 Then MaxIP(n) = MaxIP(n) - 4 Mask = Mask - 1 End If If Mask > 1 Then MaxIP(n) = MaxIP(n) - 2 Mask = Mask - 1 End If If Mask > 1 Then MaxIP(n) = MaxIP(n) - 1 Mask = Mask - 1 End If If Mask < 1 Then Exit For Next n CIDRMaxAddress = "" For n = 0 To 3 ' reassemble into an IP Address z = BaseIP(n) + MaxIP(n) If CInt(z) > 255 Then z = 255 CIDRMaxAddress = CIDRMaxAddress & z & "." Next n Return Left(CIDRMaxAddress, Len(CIDRMaxAddress) - 1) End Function ' -------------------------------------------------------------------------------- ' InstrCnt = Calculate nr of matches within a string ' -------------------------------------------------------------------------------- Private Function InstrCnt(ByVal Data As String, ByVal Match As String) As Long If Data = "" Or Match = "" Then Exit Function Dim N As Integer For N = 1 To (Data.Length - (Match.Length - 1)) If Mid(Data, N, Match.Length) = Match Then InstrCnt = InstrCnt + 1 End If Next N Return InstrCnt End Function End Class