Added support to parse and serialize "default route" prefix in Ipv4Util and Ipv6Util.
Change-Id: Ibd84cad5864beb71112f72ac727ff7a65079efcf
Signed-off-by: Ladislav Borak <lborak@cisco.com>
*/
public static byte[] bytesForPrefixBegin(final Ipv4Prefix prefix) {
final String p = prefix.getValue();
+ final int length = getPrefixLength(p);
+ if (length == 0) {
+ return new byte[] { 0 };
+ }
final int sep = p.indexOf('/');
final InetAddress a = InetAddresses.forString(p.substring(0, sep));
Preconditions.checkArgument(a instanceof Inet4Address);
final byte[] bytes = a.getAddress();
- final int length = getPrefixLength(p);
return Bytes.concat(new byte[] { UnsignedBytes.checkedCast(length) }, ByteArray.subByte(bytes, 0 , getPrefixLengthBytes(p)));
}
while (byteOffset < bytes.length) {
final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]);
byteOffset += 1;
+ // if length == 0, default route will be added
+ if (bitLength == 0) {
+ list.add(new Ipv4Prefix("0.0.0.0/0"));
+ continue;
+ }
final int byteCount = (bitLength % Byte.SIZE != 0) ? (bitLength / Byte.SIZE) + 1 : bitLength / Byte.SIZE;
list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength));
byteOffset += byteCount;
+
}
return list;
}
*/
public static byte[] bytesForPrefixBegin(final Ipv6Prefix prefix) {
final String p = prefix.getValue();
+ final int length = Ipv4Util.getPrefixLength(p);
+ if (length == 0) {
+ return new byte[] { 0 };
+ }
final int sep = p.indexOf('/');
final InetAddress a = InetAddresses.forString(p.substring(0, sep));
Preconditions.checkArgument(a instanceof Inet6Address);
final byte[] bytes = a.getAddress();
- final int length = Ipv4Util.getPrefixLength(p);
return Bytes.concat(new byte[] { UnsignedBytes.checkedCast(length) }, ByteArray.subByte(bytes, 0 , Ipv4Util.getPrefixLengthBytes(p)));
}
while (byteOffset < bytes.length) {
final int bitLength = UnsignedBytes.toInt(ByteArray.subByte(bytes, byteOffset, 1)[0]);
byteOffset += 1;
+ // if length == 0, default route will be added
+ if (bitLength == 0) {
+ list.add(new Ipv6Prefix("::/0"));
+ continue;
+ }
final int byteCount = (bitLength % Byte.SIZE != 0) ? (bitLength / Byte.SIZE) + 1 : bitLength / Byte.SIZE;
list.add(prefixForBytes(ByteArray.subByte(bytes, byteOffset, byteCount), bitLength));
byteOffset += byteCount;
+
}
return list;
}
assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16));
assertArrayEquals(new byte[] { 16, (byte) 255, (byte) 255 }, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("255.255.0.0/16")));
+
+ bytes = new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
+ assertEquals(new Ipv4Prefix("0.0.0.0/0"), Ipv4Util.prefixForBytes(bytes, 0));
+
+ bytes = new byte[] { (byte) 0 };
+ assertArrayEquals(bytes, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("0.0.0.0/0")));
}
@Test
@Test
public void testBytesForPrefix6Begin() {
- final byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 };
+ byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 };
assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64));
assertArrayEquals(new byte[] { 0x40, 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 }, Ipv6Util.bytesForPrefixBegin(new Ipv6Prefix("2001:db8:1:2::/64")));
+
+ bytes = new byte[] { (byte) 0 };
+ assertEquals(new Ipv6Prefix("::/0"), Ipv6Util.prefixForBytes(bytes, 0));
+ assertArrayEquals(bytes, Ipv6Util.bytesForPrefixBegin(new Ipv6Prefix("::/0")));
}
@Test