Improve the resiliency of the packet deserialization code.
Packet deserializers now check for malformed input while reading the byte
stream. Deserializers are re-implemented as functions that take a byte array
and return a packet object. The old IPacket.deserialize(...) methods have been
deprecated with the goal of eventually moving to immutable packet objects.
Unit tests have been implemented for all Deserializer functions.
ONOS-1589
Change-Id: I9073d5e6e7991e15d43830cfd810989256b71c56
diff --git a/utils/misc/src/main/java/org/onlab/packet/ndp/Redirect.java b/utils/misc/src/main/java/org/onlab/packet/ndp/Redirect.java
index 0515869..c3f46dd 100644
--- a/utils/misc/src/main/java/org/onlab/packet/ndp/Redirect.java
+++ b/utils/misc/src/main/java/org/onlab/packet/ndp/Redirect.java
@@ -16,6 +16,7 @@
package org.onlab.packet.ndp;
import org.onlab.packet.BasePacket;
+import org.onlab.packet.Deserializer;
import org.onlab.packet.IPacket;
import org.onlab.packet.Ip6Address;
@@ -23,6 +24,8 @@
import java.util.Arrays;
import java.util.List;
+import static org.onlab.packet.PacketUtils.checkInput;
+
/**
* Implements ICMPv6 Redirect packet format. (RFC 4861)
*/
@@ -188,4 +191,33 @@
}
return true;
}
+
+ /**
+ * Deserializer function for redirect packets.
+ *
+ * @return deserializer function
+ */
+ public static Deserializer<Redirect> deserializer() {
+ return (data, offset, length) -> {
+ checkInput(data, offset, length, HEADER_LENGTH);
+
+ Redirect redirect = new Redirect();
+
+ ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
+
+ bb.getInt();
+
+ bb.get(redirect.targetAddress, 0, Ip6Address.BYTE_LENGTH);
+ bb.get(redirect.destinationAddress, 0, Ip6Address.BYTE_LENGTH);
+
+ NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
+ .deserialize(data, bb.position(), bb.limit() - bb.position());
+
+ for (NeighborDiscoveryOptions.Option option : options.options()) {
+ redirect.addOption(option.type(), option.data());
+ }
+
+ return redirect;
+ };
+ }
}