Skip to content

rtybase/ip-maths

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ip-maths

A utility library for IPv4 and IPv6 maths. It allows to ...

1. Convert IPv4 into IPv6

    SubnetUtils.convertIpv4ToIpv6(InetAddresses.forString("192.168.1.20"));

The result is 0:0:0:0:0:ffff:c0a8:114.

2. Check if an IP belongs to a subnet

For IPv4

    SubnetValidator validator = SubnetUtils.subnetValidatorWith("192.168.0.0/16");
    assertTrue(validator.isInSubnet("192.168.1.20"));

For IPv6

    SubnetValidator validator = SubnetUtils.subnetValidatorWith("2607:f818:0:0:0:0:0:0/32");
    assertTrue(validator.isInSubnet("2607:f818:200:201:616c:4b4e:f19e:cee5"));

3. Convert an IP to a subnet.

This is useful for "hiding" client IP addresses, like EDNS Client Subnet (ECS). For IPv4 and IPv6

    SubnetConverter converter = SubnetUtils.cidrConverterWith(24, 32);
		
    assertEquals(converter.toCidrNotation("192.168.1.20").toString(),
        "192.168.1.0/24");
    assertEquals(converter.toCidrNotation("2607:f818:200:201:616c:4b4e:f19e:cee5").toString(),
        "2607:f818:0:0:0:0:0:0/32");

About

A utility library for IPv4 and IPv6 maths

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages