This is a class that exposes the SubnetUtils class from apache commons net.
http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/util/SubnetUtils.html
You will need the jNet and JavaObject library.
Example:
Output:
http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/util/SubnetUtils.html
You will need the jNet and JavaObject library.
B4X:
Sub Class_Globals
Private joSubnetInfo As JavaObject
End Sub
'You must call one of the SetSubnet methods after this and then all other methods and properties will work.
Public Sub Initialize()
End Sub
'Takes a CIDR-notation string, e.g. "192.168.0.1/16".
'If you want to include the network and broadcast addresses SetInclusiveHostCount needs to be True.
Public Sub SetSubnet(CIDR As String, SetInclusiveHostCount As Boolean)
Dim joSubnetUtils As JavaObject
joSubnetUtils.InitializeNewInstance("org.apache.commons.net.util.SubnetUtils", Array(CIDR)).RunMethodJO("setInclusiveHostCount", Array(SetInclusiveHostCount))
joSubnetInfo = joSubnetUtils.RunMethodJO("getInfo", Null)
End Sub
'Takes a dotted decimal address (e.g. "192.168.0.1") and a dotted decimal mask (e.g. "255.255.0.0").
'If you want to include the network and broadcast addresses SetInclusiveHostCount needs to be True.
Public Sub SetSubnet2(Address As String, Mask As String, SetInclusiveHostCount As Boolean)
Dim joSubnetUtils As JavaObject
joSubnetUtils.InitializeNewInstance("org.apache.commons.net.util.SubnetUtils", Array(Address, Mask)).RunMethodJO("setInclusiveHostCount", Array(SetInclusiveHostCount))
joSubnetInfo = joSubnetUtils.RunMethodJO("getInfo", Null)
End Sub
'Returns true if the parameter Address is in the range of usable endpoint addresses for this subnet.
'This excludes the network And broadcast adresses.
'Takes A dot-delimited IPv4 Address, e.g. "192.168.0.1"
Public Sub IsInRange(Address As String) As Boolean
Return joSubnetInfo.RunMethod("isInRange", Array(Address))
End Sub
Public Sub getBroadcastAddress() As String
Return joSubnetInfo.RunMethod("getBroadcastAddress", Null)
End Sub
Public Sub getNetworkAddress() As String
Return joSubnetInfo.RunMethod("getNetworkAddress", Null)
End Sub
Public Sub getNetmask() As String
Return joSubnetInfo.RunMethod("getNetmask", Null)
End Sub
Public Sub getAddress() As String
Return joSubnetInfo.RunMethod("getAddress", Null)
End Sub
'Returns the low address as a dotted IP address.
'Will be zero for CIDR/31 And CIDR/32 if the inclusive flag is False.
Public Sub getLowAddress() As String
Return joSubnetInfo.RunMethod("getLowAddress", Null)
End Sub
'Returns the high address as a dotted IP address.
'Will be zero for CIDR/31 And CIDR/32 if the inclusive flag is False.
Public Sub getHighAddress() As String
Return joSubnetInfo.RunMethod("getHighAddress", Null)
End Sub
'Get the count of available addresses.
'Will be zero for CIDR/31 And CIDR/32 if the inclusive flag is False.
Public Sub getAddressCount() As Long
Dim result As Long
Try
result = joSubnetInfo.RunMethod("getAddressCountLong", Null)
Catch
result = joSubnetInfo.RunMethod("getAddressCount", Null)
End Try
Return result
End Sub
Public Sub getCidrSignature() As String
Return joSubnetInfo.RunMethod("getCidrSignature", Null)
End Sub
Public Sub getAllAddresses() As String()
Return joSubnetInfo.RunMethod("getAllAddresses", Null)
End Sub
Public Sub ToString() As String
Return joSubnetInfo.RunMethod("toString", Null)
End Sub
Example:
B4X:
Dim x As SubnetUtils
x.Initialize
x.SetSubnet("192.168.0.1/24", True)
Log("address " & x.Address)
Log("count " & x.AddressCount)
Log("broadcast " & x.BroadcastAddress)
Log("cidr " & x.CidrSignature)
Log("mask " & x.Netmask)
Log("network " & x.NetworkAddress)
Log(x.ToString)
B4X:
address 192.168.0.1
count 256
broadcast 192.168.0.255
cidr 192.168.0.1/24
mask 255.255.255.0
network 192.168.0.0
CIDR Signature: [192.168.0.1/24] Netmask: [255.255.255.0]
Network: [192.168.0.0]
Broadcast: [192.168.0.255]
First Address: [192.168.0.0]
Last Address: [192.168.0.255]
# Addresses: [256]
Last edited: