Network Subnet Calculator
Network Subnet Calculator
A subnet is a division of an IP network (internet protocol suite), where an IP network is a set of communications protocols used on the Internet and other similar networks.
It is commonly known as TCP/IP (Transmission Control Protocol/Internet Protocol).
The act of dividing a network into at least two separate networks is called subnetting, and routers are devices that allow traffic exchange between subnetworks, serving as a physical boundary.
IPv4 is the most common network addressing architecture used, though the use of IPv6 has been growing since 2006.
How to calculator subnet CIDR Block in java
Below simple algorithm calculates required cidr block Using master CIDR Block.
CidrBlockBuilderTest.java
package cidr;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Iterator;
/**
* @author tvajjala
*/
public class CidrBlockBuilderTest {
@Test
void subnetBlockWithMask24Test() {
//given:
NetworkSubnet networkSubnet = NetworkSubnet.builder(NetworkSubnet.Protocol.IPV4)
.fromMasterBlock("10.0.128.0/17")//<-- vcnSubnet
.withSubnetMask(NetworkSubnet.Mask._24)
.build();
//when:
Iterator<String> iterator = networkSubnet.getSubnetCidrBlocks();
//then: with 256 IP blocks
Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals("10.0.128.0/24", iterator.next());
Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals("10.0.129.0/24", iterator.next());
Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals("10.0.130.0/24", iterator.next());
}
@Test
void subnetBlockWithMask28Test() {
//given: scenario
NetworkSubnet networkSubnet = NetworkSubnet.builder(NetworkSubnet.Protocol.IPV4)
.fromMasterBlock("10.0.128.0/17")//<-- vcnSubnet
.withSubnetMask(NetworkSubnet.Mask._28)
.build();
//when: invoke build cidr block
Iterator<String> iterator = networkSubnet.getSubnetCidrBlocks();
//then: expect 128 IP slots
Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals("10.0.128.0/28", iterator.next());
Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals("10.0.128.16/28", iterator.next());
Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals("10.0.128.32/28", iterator.next());
}
@Test
void foundOverlappingAddressTest() {
NetworkSubnet networkSubnet = NetworkSubnet.builder(NetworkSubnet.Protocol.IPV4)
.fromMasterBlock("10.0.128.0/17")//<-- vcnSubnet
.withSubnetMask(NetworkSubnet.Mask._28)
.build();
//given: overlapping address [10.0.128.30, 10.0.128.31]
boolean flag = networkSubnet.hasAnyOverlappingAddress("10.0.128.30/27", "10.0.128.0/27");
Assertions.assertTrue(flag);
}
}
Refer full source code at subnet-calculator repository.
Comments
Post a Comment