Skip to content

Commit

Permalink
Fix a bug in CIDR.contains(InetAddress) implementations
Browse files Browse the repository at this point in the history
Related issue: #2767

Motivation:

CIDR.contains(InetAddress) implementations should always return true
when the CIDR's prefix length is 0.

Modifications:

- Make CIDR.contains(InetAddress) return true if the current cidrMask is
  0
- Add tests

Result:

Fixed the issue #2767
  • Loading branch information
trustin committed Aug 26, 2014
1 parent 907d38e commit 534840a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/main/java/org/jboss/netty/handler/ipfilter/CIDR4.java
Expand Up @@ -89,6 +89,14 @@ public int compareTo(CIDR arg) {

@Override
public boolean contains(InetAddress inetAddress) {
if (inetAddress == null) {
throw new NullPointerException("inetAddress");
}

if (cidrMask == 0) {
return true;
}

int search = ipv4AddressToInt(inetAddress);
return search >= addressInt && search <= addressEndInt;
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/jboss/netty/handler/ipfilter/CIDR6.java
Expand Up @@ -93,6 +93,14 @@ public int compareTo(CIDR arg) {

@Override
public boolean contains(InetAddress inetAddress) {
if (inetAddress == null) {
throw new NullPointerException("inetAddress");
}

if (cidrMask == 0) {
return true;
}

BigInteger search = ipv6AddressToBigInteger(inetAddress);
return search.compareTo(addressBigInt) >= 0 && search.compareTo(addressEndBigInt) <= 0;
}
Expand Down
Expand Up @@ -246,6 +246,15 @@ public void testIpFilterRule() throws Exception {
addr = new InetSocketAddress(InetAddress.getByName(InetAddress.getLocalHost().getHostName()), 8080);
assertTrue(accept(h, addr));

h.clear();
h.addAll(new IpFilterRuleList("+c:0.0.0.0/0, -n:*"));
addr = new InetSocketAddress("91.114.240.43", 8080);
assertTrue(accept(h, addr));
addr = new InetSocketAddress("10.0.0.3", 8080);
assertTrue(accept(h, addr));
addr = new InetSocketAddress("192.168.93.2", 8080);
assertTrue(accept(h, addr));

h.clear();
h.addAll(new IpFilterRuleList(""));
addr = new InetSocketAddress(InetAddress.getLocalHost(), 8080);
Expand All @@ -262,7 +271,6 @@ public void testIpFilterRule() throws Exception {
assertTrue(accept(h, addr));
addr = new InetSocketAddress(InetAddress.getByName(InetAddress.getLocalHost().getHostName()), 8080);
assertTrue(accept(h, addr));

}

}

0 comments on commit 534840a

Please sign in to comment.