Commit b5b8f7e1 authored by Tigran Mkrtchyan's avatar Tigran Mkrtchyan
Browse files

nfs: make ip matcher IPv6 compliant

parent 879de45b
......@@ -81,31 +81,29 @@ public class IPMatcher {
byte[] ipBytes = ip.getAddress();
byte[] netBytes = subnet.getAddress();
int ipLong = 0;
int netLong = 0;
// create an integer from address bytes
ipLong |= (255 & ipBytes[0]);
ipLong <<= 8;
ipLong |= (255 & ipBytes[1]);
ipLong <<= 8;
ipLong |= (255 & ipBytes[2]);
ipLong <<= 8;
ipLong |= (255 & ipBytes[3]);
netLong |= (255 & netBytes[0]);
netLong <<= 8;
netLong |= (255 & netBytes[1]);
netLong <<= 8;
netLong |= (255 & netBytes[2]);
netLong <<= 8;
netLong |= (255 & netBytes[3]);
// first mask bits from left should be the same
ipLong = ipLong >> (32 - mask);
netLong = netLong >> (32 - mask);
return ipLong == netLong ;
int maskLen = ipBytes.length * 8;
if(mask > maskLen || mask < 0)
throw new IllegalArgumentException("Invalid mask: " + mask);
/*
* check that all full bytes matching and then compare last bits, e.g.
* for netmask /25 compare that first 3 bytes are equal and then check
* last bit.
*/
int fullBytes = (mask / 8);
for(int i = 0; i < fullBytes; i++) {
if(ipBytes[i] != netBytes[i])
return false;
}
int lastBits = mask % 8;
/*
* if there are no partial defined bytes we are done, other vise check them.
*/
return lastBits == 0 ? true :
(ipBytes[fullBytes] >> (8 - lastBits)) == (netBytes[fullBytes] >> (8 - lastBits));
}
......
......@@ -155,4 +155,12 @@ public class IPMatcherTest {
assertTrue("failed to match localhost", match);
}
@Test
public void testIpV6SuccessfulIpNetMatchingFractionedMask() throws UnknownHostException {
boolean match = IPMatcher.match( InetAddress.getByName("fe80::3FF:F00D:BAD:F00D"),
InetAddress.getByName("fe80::0:0:0:0"),70);
assertTrue("Failed to match host with netmask.", match);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment