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/RouterSolicitation.java b/utils/misc/src/main/java/org/onlab/packet/ndp/RouterSolicitation.java
index 3fddc5e..07729df 100644
--- a/utils/misc/src/main/java/org/onlab/packet/ndp/RouterSolicitation.java
+++ b/utils/misc/src/main/java/org/onlab/packet/ndp/RouterSolicitation.java
@@ -16,19 +16,21 @@
package org.onlab.packet.ndp;
import org.onlab.packet.BasePacket;
+import org.onlab.packet.Deserializer;
import org.onlab.packet.IPacket;
import java.nio.ByteBuffer;
import java.util.List;
+import static org.onlab.packet.PacketUtils.checkInput;
+
/**
* Implements ICMPv6 Router Solicitation packet format. (RFC 4861)
*/
public class RouterSolicitation extends BasePacket {
public static final byte HEADER_LENGTH = 4; // bytes
- private final NeighborDiscoveryOptions options =
- new NeighborDiscoveryOptions();
+ private final NeighborDiscoveryOptions options = new NeighborDiscoveryOptions();
/**
* Gets the Neighbor Discovery Protocol packet options.
@@ -122,4 +124,30 @@
}
return true;
}
+
+ /**
+ * Deserializer function for router solicitation packets.
+ *
+ * @return deserializer function
+ */
+ public static Deserializer<RouterSolicitation> deserializer() {
+ return (data, offset, length) -> {
+ checkInput(data, offset, length, HEADER_LENGTH);
+
+ RouterSolicitation routerSolicitation = new RouterSolicitation();
+
+ ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
+
+ bb.getInt();
+
+ NeighborDiscoveryOptions options = NeighborDiscoveryOptions.deserializer()
+ .deserialize(data, bb.position(), bb.limit() - bb.position());
+
+ for (NeighborDiscoveryOptions.Option option : options.options()) {
+ routerSolicitation.addOption(option.type(), option.data());
+ }
+
+ return routerSolicitation;
+ };
+ }
}