Search code examples
c#network-programmingip-addresssubnetarcus

In the Arcus library how can a single IP address create a Subnet object?


In Arcus I can create a Subnet with a single IP Address:

var ipAddress = IPAddress.Parse("192.168.1.1");
var subnet = new Subnet(ipAddress);

Since subnets are not arbitrary ranges of IP addresses how does this work?


Solution

  • You are correct that a Subnet is not an arbitrary range of IP addresses. They are in fact a range of length 2n starting at a particular position. When provided a single IP address Arcus sets the starting position at the IP and a route prefix equivalent to a subnet of size one. From your question you've created a subnet object with the following attributes:

    var ipAddress = IPAddress.Parse("192.168.1.1");
    var subnet = new Subnet(ipAddress);
    
    Assert.Equal(1, subnet.Length);
    Assert.Equal(ipAddress, subnet.Single());
    Assert.True(subnet.IsSingleIP);
    Assert.Equal("192.168.1.1/32", subnet.ToString());