[CORD-2182] Implement packet serializer/deserializer for Routing Information Protocol (RIP)
Change-Id: If80af0b04a86972b91e6a9d9731864ff35c833e4
diff --git a/utils/misc/src/main/java/org/onlab/packet/RIP.java b/utils/misc/src/main/java/org/onlab/packet/RIP.java
new file mode 100644
index 0000000..7fefe40
--- /dev/null
+++ b/utils/misc/src/main/java/org/onlab/packet/RIP.java
@@ -0,0 +1,267 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.packet;
+
+import org.slf4j.Logger;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+import static org.onlab.packet.PacketUtils.checkInput;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implements RIP Packet format, according to RFC 2453.
+ */
+
+public class RIP extends BasePacket {
+ /**
+ * Routing Information Protocol packet.
+ * ------------------------------------------ |cmdType (1) | version(1) |
+ * ------------------------------------------ |reserved (2) |
+ * ------------------------------------------ | route entries (n*20) |
+ * ------------------------------------------
+ *
+ */
+ // the case of no RIP entry
+ public static final int MIN_HEADER_LENGTH = 4;
+
+
+ private final Logger log = getLogger(getClass());
+
+ public enum CmdType {
+ RIPREQUEST(1),
+ RIPRESPONSE(2);
+
+ protected int value;
+
+ CmdType(final int value) {
+ this.value = value;
+ }
+
+ public int getValue() {
+ return this.value;
+ }
+
+ public static CmdType getType(final int value) {
+ switch (value) {
+ case 1:
+ return RIPREQUEST;
+ case 2:
+ return RIPRESPONSE;
+ default:
+ return null;
+ }
+ }
+ }
+
+ protected byte cmdType;
+ protected byte version;
+ protected short reserved;
+ protected List<RIPV2Entry> rtEntries = new ArrayList<RIPV2Entry>();
+ protected RIPV2AuthEntry authEntry = null;
+ protected byte[] rawData = null;
+
+ /**
+ * @return the cmdType
+ */
+ public byte getCmdType() {
+ return this.cmdType;
+ }
+
+ /**
+ * @param cmdType
+ * the cmdType to set
+ * @return this
+ */
+ public RIP setCmdType(final byte cmdType) {
+ this.cmdType = cmdType;
+ return this;
+ }
+
+ /**
+ * @return the version
+ */
+ public byte getVersion() {
+ return this.version;
+ }
+
+ /**
+ * @param version
+ * the version to set
+ * @return this
+ */
+ public RIP setVersion(final byte version) {
+ this.version = version;
+ return this;
+ }
+
+ /**
+ * @return the reserved short
+ */
+ public short getReserved() {
+ return this.reserved;
+ }
+
+ /**
+ * @param reserved
+ * the reserved short to set
+ * @return this
+ */
+ public RIP setReserved(final short reserved) {
+ this.reserved = reserved;
+ return this;
+ }
+
+ /**
+ * @return the route entries
+ */
+ public List<RIPV2Entry> getRtEntries() {
+ return this.rtEntries;
+ }
+
+ /**
+ * @param entries
+ * the route entries to set
+ * @return this
+ */
+ public RIP setRtEntries(final List<RIPV2Entry> entries) {
+ this.rtEntries = entries;
+ return this;
+ }
+ /**
+ * @return the authentication entry
+ */
+ public RIPV2AuthEntry getAuthEntry() {
+ return this.authEntry;
+ }
+
+ /**
+ * @return the raw data of whole RIP packet (after RIP header)
+ */
+ public byte[] getRawData() {
+ return this.rawData;
+ }
+
+ @Override
+ public byte[] serialize() {
+ // not guaranteed to retain length/exact format
+ this.resetChecksum();
+ int dataLength = MIN_HEADER_LENGTH + RIPV2Entry.ENTRY_LEN * rtEntries.size();
+ if (authEntry != null) {
+ dataLength += RIPV2Entry.ENTRY_LEN;
+ }
+ final byte[] data = new byte[dataLength];
+ final ByteBuffer bb = ByteBuffer.wrap(data);
+ bb.put(this.cmdType);
+ bb.put(this.version);
+ bb.putShort(this.reserved);
+ if (authEntry != null) {
+ bb.put(authEntry.serialize());
+ }
+ for (final RIPV2Entry entry : this.rtEntries) {
+ bb.put(entry.serialize());
+ }
+ // assume the rest is padded out with zeroes
+ return data;
+ }
+
+ /**
+ * Deserializer function for RIP packets.
+ *
+ * @return deserializer function
+ */
+ public static Deserializer<RIP> deserializer() {
+ return (data, offset, length) -> {
+ RIP rip = new RIP();
+
+ checkInput(data, offset, length, MIN_HEADER_LENGTH);
+
+ ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
+ rip.rawData = Arrays.copyOfRange(data, offset, offset + length);
+
+
+ rip.cmdType = bb.get();
+ rip.version = bb.get();
+ rip.reserved = bb.getShort();
+
+ // read route entries
+ while (bb.hasRemaining() && (bb.remaining() >= RIPV2Entry.ENTRY_LEN)) {
+ byte[] rtData = new byte[RIPV2Entry.ENTRY_LEN];
+ bb.get(rtData);
+
+ if (rtData[0] == -1 && rtData[1] == -1) {
+ // second time reaching here is the signature at the end of the packet, don't process it
+ if (rip.authEntry == null) {
+ rip.authEntry = RIPV2AuthEntry.deserializer().deserialize(rtData, 0, rtData.length);
+ }
+ } else {
+ RIPV2Entry rtEntry = RIPV2Entry.deserializer().deserialize(rtData, 0, rtData.length);
+ rip.rtEntries.add(rtEntry);
+ }
+ }
+
+ return rip;
+ };
+ }
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ return Objects.hash(super.hashCode(), cmdType, reserved, version, authEntry, rtEntries);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof RIP)) {
+ return false;
+ }
+ final RIP that = (RIP) obj;
+
+
+ return super.equals(that) &&
+ Objects.equals(version, that.version) &&
+ Objects.equals(reserved, that.reserved) &&
+ Objects.equals(cmdType, that.cmdType) &&
+ Objects.equals(authEntry, that.authEntry) &&
+ Objects.equals(rtEntries, that.rtEntries);
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(getClass())
+ .add("cmdType", Byte.toString(cmdType))
+ .add("version", Byte.toString(version))
+ .add("reserved", Short.toString(reserved))
+ .toString();
+ // TODO: need to handle route entries
+ }
+}
diff --git a/utils/misc/src/main/java/org/onlab/packet/RIPV2AuthEntry.java b/utils/misc/src/main/java/org/onlab/packet/RIPV2AuthEntry.java
new file mode 100644
index 0000000..f77f6dd
--- /dev/null
+++ b/utils/misc/src/main/java/org/onlab/packet/RIPV2AuthEntry.java
@@ -0,0 +1,231 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet;
+
+import org.slf4j.Logger;
+
+import java.nio.ByteBuffer;
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/*
+ * Authentication Entry for RIP version 2 - RFC 2082
+ */
+public class RIPV2AuthEntry extends BasePacket {
+ private static final int ENTRY_LEN = 20;
+ private final Logger log = getLogger(getClass());
+ protected short addressFamilyId;
+ protected short type;
+ protected short offset;
+ protected byte keyId;
+ protected byte authLen;
+ protected int sequence;
+
+
+ @Override
+ public byte[] serialize() {
+ ByteBuffer byteBuffer;
+ byteBuffer = ByteBuffer.allocate(ENTRY_LEN);
+ byteBuffer.putShort(addressFamilyId);
+ byteBuffer.putShort(type);
+ byteBuffer.putShort(offset);
+ byteBuffer.put(keyId);
+ byteBuffer.put(authLen);
+ byteBuffer.putInt(sequence);
+ byteBuffer.putInt(0);
+ byteBuffer.putInt(0);
+ return byteBuffer.array();
+ }
+
+ /**
+ * Deserializer function for RIPv2 entry.
+ *
+ * @return deserializer function
+ */
+ public static Deserializer<RIPV2AuthEntry> deserializer() {
+ return (data, offset, length) -> {
+ RIPV2AuthEntry authEntry = new RIPV2AuthEntry();
+
+ checkNotNull(data);
+
+ if (offset < 0 || length < 0 ||
+ length > data.length || offset >= data.length ||
+ offset + length > data.length) {
+ throw new DeserializationException("Illegal offset or length");
+ }
+ ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
+ if (bb.remaining() < ENTRY_LEN) {
+ throw new DeserializationException(
+ "Buffer underflow while reading RIP authentication entry");
+ }
+ authEntry.addressFamilyId = bb.getShort();
+ authEntry.type = bb.getShort();
+ authEntry.offset = bb.getShort();
+ authEntry.keyId = bb.get();
+ authEntry.authLen = bb.get();
+ authEntry.sequence = bb.getInt();
+ return authEntry;
+ };
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ return Objects.hash(super.hashCode(), sequence, authLen, keyId, offset, addressFamilyId, type);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof RIPV2AuthEntry)) {
+ return false;
+ }
+ final RIPV2AuthEntry that = (RIPV2AuthEntry) obj;
+
+ return super.equals(that) &&
+ Objects.equals(type, that.type) &&
+ Objects.equals(addressFamilyId, that.addressFamilyId) &&
+ Objects.equals(offset, that.offset) &&
+ Objects.equals(keyId, that.keyId) &&
+ Objects.equals(authLen, that.authLen) &&
+ Objects.equals(sequence, that.sequence);
+ }
+
+ /**
+ * @return the Address Family Identifier
+ */
+ public short getAddressFamilyId() {
+ return this.addressFamilyId;
+ }
+
+ /**
+ * @param addressFamilyIdentifier the address family identifier to set
+ * @return this
+ */
+ public RIPV2AuthEntry setAddressFamilyId(final short addressFamilyIdentifier) {
+ this.addressFamilyId = addressFamilyIdentifier;
+ return this;
+ }
+
+ /**
+ * @return the authentication type
+ */
+ public short getType() {
+ return this.type;
+ }
+
+ /**
+ * @param type the authentication type to set
+ * @return this
+ */
+ public RIPV2AuthEntry setType(final short type) {
+ this.type = type;
+ return this;
+ }
+
+ /**
+ * @return the offset of authentication data
+ */
+ public short getOffset() {
+ return this.offset;
+ }
+
+ /**
+ * @param offset the offset of authentication data to set
+ * @return this
+ */
+ public RIPV2AuthEntry setOffset(final short offset) {
+ this.offset = offset;
+ return this;
+ }
+ /**
+ * @return the subnet mask
+ */
+ public byte getKeyId() {
+ return this.keyId;
+ }
+
+ /**
+ * @param keyId The key id to set
+ * @return this
+ */
+ public RIPV2AuthEntry setKeyId(final byte keyId) {
+ this.keyId = keyId;
+ return this;
+ }
+
+ /**
+ * @return the authentication data length
+ */
+ public byte getAuthLen() {
+ return this.authLen;
+ }
+
+ /**
+ * @param authlen the length of the authentication data to set
+ * @return this
+ */
+ public RIPV2AuthEntry setAuthLen(final byte authlen) {
+ this.authLen = authlen;
+ return this;
+ }
+
+
+ /**
+ * @return the sequence number
+ */
+ public int getSequence() {
+ return this.sequence;
+ }
+
+ /**
+ * @param sequencenumber sequence number to set
+ * @return this
+ */
+ public RIPV2AuthEntry setSequenceNumber(final int sequencenumber) {
+ this.sequence = sequencenumber;
+ return this;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return "RIPV2AuthEntry [address family Id=" + this.addressFamilyId + ", type=" + this.type
+ + ", offset=" + this.offset
+ + ", key ID=" + this.keyId
+ + ", authentication length = " + this.authLen
+ + ", sequence number=" + this.sequence + "]";
+ }
+}
diff --git a/utils/misc/src/main/java/org/onlab/packet/RIPV2Entry.java b/utils/misc/src/main/java/org/onlab/packet/RIPV2Entry.java
new file mode 100644
index 0000000..20d54e5
--- /dev/null
+++ b/utils/misc/src/main/java/org/onlab/packet/RIPV2Entry.java
@@ -0,0 +1,238 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet;
+
+import org.slf4j.Logger;
+
+import java.nio.ByteBuffer;
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/*
+ * Entry for RIP version 2 - RFC 2453
+ */
+public class RIPV2Entry extends BasePacket {
+ public static final int ENTRY_LEN = 20;
+ public static final short AFI_IP = 2;
+ public static final byte INFINITY_METRIC = 16;
+ public static final byte NEXTHOP_METRIC = -128; // actually it is 0xFF
+
+ private final Logger log = getLogger(getClass());
+ protected short addressFamilyId;
+ protected short routeTag;
+ protected Ip4Address ipAddress;
+ protected Ip4Address subnetMask;
+ protected Ip4Address nextHop;
+ protected int metric;
+
+ @Override
+ public byte[] serialize() {
+ ByteBuffer byteBuffer;
+ byteBuffer = ByteBuffer.allocate(ENTRY_LEN);
+ byteBuffer.putShort(addressFamilyId);
+ byteBuffer.putShort(routeTag);
+ byteBuffer.putInt(ipAddress.toInt());
+ byteBuffer.putInt(subnetMask.toInt());
+ byteBuffer.putInt(nextHop.toInt());
+ byteBuffer.putInt(metric);
+ return byteBuffer.array();
+ }
+
+ /**
+ * Deserializer function for RIPv2 entry.
+ *
+ * @return deserializer function
+ */
+ public static Deserializer<RIPV2Entry> deserializer() {
+ return (data, offset, length) -> {
+ RIPV2Entry ripEntry = new RIPV2Entry();
+
+ checkNotNull(data);
+
+ if (offset < 0 || length < 0 ||
+ length > data.length || offset >= data.length ||
+ offset + length > data.length) {
+ throw new DeserializationException("Illegal offset or length");
+ }
+ ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
+ if (bb.remaining() < ENTRY_LEN) {
+ throw new DeserializationException(
+ "Buffer underflow while reading RIP entry");
+ }
+ ripEntry.addressFamilyId = bb.getShort();
+ // skip the authentication entry
+ if (ripEntry.addressFamilyId == 0xffff) {
+ return ripEntry;
+ }
+ ripEntry.routeTag = bb.getShort();
+ ripEntry.ipAddress = Ip4Address.valueOf(bb.getInt());
+ ripEntry.subnetMask = Ip4Address.valueOf(bb.getInt());
+ ripEntry.nextHop = Ip4Address.valueOf(bb.getInt());
+ ripEntry.metric = bb.getInt();
+ return ripEntry;
+ };
+ }
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ return Objects.hash(super.hashCode(), nextHop.toInt(), subnetMask.toInt(),
+ ipAddress.toInt(), addressFamilyId, metric, routeTag);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof RIPV2Entry)) {
+ return false;
+ }
+ final RIPV2Entry that = (RIPV2Entry) obj;
+
+
+ return super.equals(that) &&
+ Objects.equals(routeTag, that.routeTag) &&
+ Objects.equals(metric, that.metric) &&
+ Objects.equals(addressFamilyId, that.addressFamilyId) &&
+ Objects.equals(ipAddress, that.ipAddress) &&
+ Objects.equals(nextHop, that.nextHop) &&
+ Objects.equals(subnetMask, that.subnetMask);
+ }
+
+
+ /**
+ * @return the Address Family Identifier
+ */
+ public short getAddressFamilyId() {
+ return this.addressFamilyId;
+ }
+
+ /**
+ * @param addressFamilyIdentifier the address family identifier to set
+ * @return this
+ */
+ public RIPV2Entry setAddressFamilyId(final short addressFamilyIdentifier) {
+ this.addressFamilyId = addressFamilyIdentifier;
+ return this;
+ }
+
+ /**
+ * @return the route tag
+ */
+ public short getRouteTag() {
+ return this.routeTag;
+ }
+
+ /**
+ * @param routetag the route tag to set
+ * @return this
+ */
+ public RIPV2Entry setRouteTag(final short routetag) {
+ this.routeTag = routetag;
+ return this;
+ }
+
+ /**
+ * @return the ip address
+ */
+ public Ip4Address getipAddress() {
+ return this.ipAddress;
+ }
+
+ /**
+ * @param ipaddress the Ip Address to set
+ * @return this
+ */
+ public RIPV2Entry setIpAddress(final Ip4Address ipaddress) {
+ this.ipAddress = ipaddress;
+ return this;
+ }
+ /**
+ * @return the subnet mask
+ */
+ public Ip4Address getSubnetMask() {
+ return this.subnetMask;
+ }
+
+ /**
+ * @param subnetmask the subnet mask to set
+ * @return this
+ */
+ public RIPV2Entry setSubnetMask(final Ip4Address subnetmask) {
+ this.subnetMask = subnetmask;
+ return this;
+ }
+
+ /**
+ * @return the next hop
+ */
+ public Ip4Address getNextHop() {
+ return this.nextHop;
+ }
+
+ /**
+ * @param nexthop the ip address if the next hop to set
+ * @return this
+ */
+ public RIPV2Entry setNextHop(final Ip4Address nexthop) {
+ this.nextHop = nexthop;
+ return this;
+ }
+
+
+ /**
+ * @return the metric
+ */
+ public int getMetric() {
+ return this.metric;
+ }
+
+ /**
+ * @param metric the route metric to set
+ * @return this
+ */
+ public RIPV2Entry setMetric(final int metric) {
+ this.metric = metric;
+ return this;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return "RIPV2Entry [address family Id=" + this.addressFamilyId + ", route tag=" + this.routeTag
+ + ", Address=" + this.ipAddress
+ + ", Subnet mask=" + this.subnetMask
+ + ", Mext hop=" + this.nextHop
+ + ", metric = " + this.metric + "]";
+ }
+}
diff --git a/utils/misc/src/main/java/org/onlab/packet/RIPng.java b/utils/misc/src/main/java/org/onlab/packet/RIPng.java
new file mode 100644
index 0000000..144302f
--- /dev/null
+++ b/utils/misc/src/main/java/org/onlab/packet/RIPng.java
@@ -0,0 +1,238 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Implemented according to RFC 2080
+ */
+
+package org.onlab.packet;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+
+import static org.onlab.packet.PacketUtils.checkInput;
+import static com.google.common.base.MoreObjects.toStringHelper;
+import org.slf4j.Logger;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Representation of an RIPng Packet.
+ */
+public class RIPng extends BasePacket {
+ /**
+ * Routing Information Protocol Next Generation packet.
+ * ------------------------------------------ |cmdType (1) | version(1) |
+ * ------------------------------------------ |reserved (2) |
+ * ------------------------------------------ | route entries (n*20) |
+ * ------------------------------------------
+ *
+ */
+ // the case of no route entry
+ public static final int MIN_HEADER_LENGTH = 4;
+
+
+ private final Logger log = getLogger(getClass());
+
+ public enum CmdType {
+ RIPngREQUEST(1),
+ RIPngRESPONSE(2);
+
+ protected int value;
+
+ CmdType(final int value) {
+ this.value = value;
+ }
+
+ public int getValue() {
+ return this.value;
+ }
+
+ public static CmdType getType(final int value) {
+ switch (value) {
+ case 1:
+ return RIPngREQUEST;
+ case 2:
+ return RIPngRESPONSE;
+ default:
+ return null;
+ }
+ }
+ }
+
+ protected byte cmdType;
+ protected byte version;
+ protected short reserved;
+ protected List<RIPngEntry> rtEntries = new ArrayList<RIPngEntry>();
+
+ /**
+ * @return the cmdType
+ */
+ public byte getCmdType() {
+ return this.cmdType;
+ }
+
+ /**
+ * @param cmdType
+ * the cmdType to set
+ * @return this
+ */
+ public RIPng setCmdType(final byte cmdType) {
+ this.cmdType = cmdType;
+ return this;
+ }
+
+ /**
+ * @return the version
+ */
+ public byte getVersion() {
+ return this.version;
+ }
+
+ /**
+ * @param version
+ * the version to set
+ * @return this
+ */
+ public RIPng setVersion(final byte version) {
+ this.version = version;
+ return this;
+ }
+
+ /**
+ * @return the reserved short
+ */
+ public short getReserved() {
+ return this.reserved;
+ }
+
+ /**
+ * @param reserved
+ * the reserved short to set
+ * @return this
+ */
+ public RIPng setReserved(final short reserved) {
+ this.reserved = reserved;
+ return this;
+ }
+
+ /**
+ * @return the route entries
+ */
+ public List<RIPngEntry> getRtEntries() {
+ return this.rtEntries;
+ }
+
+ /**
+ * @param entries
+ * the route entries to set
+ * @return this
+ */
+ public RIPng setRtEntries(final List<RIPngEntry> entries) {
+ this.rtEntries = entries;
+ return this;
+ }
+
+ @Override
+ public byte[] serialize() {
+ // not guaranteed to retain length/exact format
+ this.resetChecksum();
+
+ final byte[] data = new byte[MIN_HEADER_LENGTH + RIPngEntry.ENTRY_LEN * rtEntries.size()];
+ final ByteBuffer bb = ByteBuffer.wrap(data);
+ bb.put(this.cmdType);
+ bb.put(this.version);
+ bb.putShort(this.reserved);
+ for (final RIPngEntry entry : this.rtEntries) {
+ bb.put(entry.serialize());
+ }
+ // assume the rest is padded out with zeroes
+ return data;
+ }
+
+ /**
+ * Deserializer function for RIPng packets.
+ *
+ * @return deserializer function
+ */
+ public static Deserializer<RIPng> deserializer() {
+ return (data, offset, length) -> {
+ RIPng ripng = new RIPng();
+
+ checkInput(data, offset, length, MIN_HEADER_LENGTH);
+
+ ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
+
+ ripng.cmdType = bb.get();
+ ripng.version = bb.get();
+ ripng.reserved = bb.getShort();
+
+ // read route entries
+ while (bb.hasRemaining() && (bb.remaining() >= RIPngEntry.ENTRY_LEN)) {
+ RIPngEntry rtEntry;
+ byte[] rtData = new byte[RIPngEntry.ENTRY_LEN];
+ bb.get(rtData);
+
+ rtEntry = RIPngEntry.deserializer().deserialize(rtData, 0, rtData.length);
+ ripng.rtEntries.add(rtEntry);
+ }
+
+ return ripng;
+ };
+ }
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ return Objects.hash(super.hashCode(), cmdType, reserved, version, rtEntries);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof RIPng)) {
+ return false;
+ }
+ final RIPng that = (RIPng) obj;
+
+ return super.equals(that) &&
+ Objects.equals(version, that.version) &&
+ Objects.equals(reserved, that.reserved) &&
+ Objects.equals(cmdType, that.cmdType) &&
+ Objects.equals(rtEntries, that.rtEntries);
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(getClass())
+ .add("cmdType", Byte.toString(cmdType))
+ .add("version", Byte.toString(version))
+ .add("reserved", Short.toString(reserved))
+ .toString();
+ // TODO: need to handle route entries
+ }
+}
diff --git a/utils/misc/src/main/java/org/onlab/packet/RIPngEntry.java b/utils/misc/src/main/java/org/onlab/packet/RIPngEntry.java
new file mode 100644
index 0000000..0e4f5c8
--- /dev/null
+++ b/utils/misc/src/main/java/org/onlab/packet/RIPngEntry.java
@@ -0,0 +1,191 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.packet;
+
+import org.slf4j.Logger;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Default DHCP option.
+ */
+public class RIPngEntry extends BasePacket {
+ public static final int OPT_CODE_LEN = 1;
+ public static final int ENTRY_LEN = 20;
+ public static final byte INFINITY_METRIC = 16;
+ public static final byte NEXTHOP_METRIC = -128; // actually it is 0xFF
+
+ private final Logger log = getLogger(getClass());
+ protected byte[] prefix; // 16 bytes
+ protected short routeTag;
+ protected byte prefixLen;
+ protected byte metric;
+
+ @Override
+ public byte[] serialize() {
+ ByteBuffer byteBuffer;
+ byteBuffer = ByteBuffer.allocate(ENTRY_LEN);
+ byteBuffer.put(prefix);
+ byteBuffer.putShort(routeTag);
+ byteBuffer.put(prefixLen);
+ byteBuffer.put(metric);
+ return byteBuffer.array();
+ }
+
+ /**
+ * Deserializer function for RIPng entry.
+ *
+ * @return deserializer function
+ */
+ public static Deserializer<RIPngEntry> deserializer() {
+ return (data, offset, length) -> {
+ RIPngEntry ripngEntry = new RIPngEntry();
+
+ checkNotNull(data);
+
+ if (offset < 0 || length < 0 ||
+ length > data.length || offset >= data.length ||
+ offset + length > data.length) {
+ throw new DeserializationException("Illegal offset or length");
+ }
+ ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
+ if (bb.remaining() < ENTRY_LEN) {
+ throw new DeserializationException(
+ "Buffer underflow while reading RIPng entry");
+ }
+ ripngEntry.prefix = new byte[IpAddress.INET6_BYTE_LENGTH];
+ bb.get(ripngEntry.prefix);
+ ripngEntry.routeTag = bb.getShort();
+ ripngEntry.prefixLen = bb.get();
+ ripngEntry.metric = bb.get();
+ return ripngEntry;
+ };
+ }
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ return Objects.hash(super.hashCode(), metric, prefixLen, Arrays.hashCode(prefix), routeTag);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof RIPngEntry)) {
+ return false;
+ }
+ final RIPngEntry that = (RIPngEntry) obj;
+
+ return super.equals(that) &&
+ Objects.equals(metric, metric) &&
+ Objects.equals(routeTag, that.routeTag) &&
+ Objects.equals(prefixLen, that.prefixLen) &&
+ Arrays.equals(prefix, that.prefix) &&
+ Objects.equals(routeTag, that.routeTag);
+ }
+ /**
+ * @return the IPv6 prefix
+ */
+ public byte[] getPrefix() {
+ return this.prefix;
+ }
+
+ /**
+ * @param prefix the IPv6 prefix to set
+ * @return this
+ */
+ public RIPngEntry setPrefix(final byte[] prefix) {
+ this.prefix = prefix;
+ return this;
+ }
+
+ /**
+ * @return the route tag
+ */
+ public short getRouteTag() {
+ return this.routeTag;
+ }
+
+ /**
+ * @param routetag the route tag to set
+ * @return this
+ */
+ public RIPngEntry setRouteTag(final short routetag) {
+ this.routeTag = routetag;
+ return this;
+ }
+
+ /**
+ * @return the prefix length
+ */
+ public byte getPrefixLen() {
+ return this.prefixLen;
+ }
+
+ /**
+ * @param prefixlen the prefix length to set
+ * @return this
+ */
+ public RIPngEntry setPrefixLen(final byte prefixlen) {
+ this.prefixLen = prefixlen;
+ return this;
+ }
+
+ /**
+ * @return the metric
+ */
+ public byte getMetric() {
+ return this.metric;
+ }
+
+ /**
+ * @param metric the route metric to set
+ * @return this
+ */
+ public RIPngEntry setMetric(final byte metric) {
+ this.metric = metric;
+ return this;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return "RIPngEntry [prefix=" + this.prefix + ", route tag=" + this.routeTag
+ + ", prefix length=" + this.prefixLen
+ + ", metric = " + this.metric + "]";
+ }
+}
diff --git a/utils/misc/src/main/java/org/onlab/packet/UDP.java b/utils/misc/src/main/java/org/onlab/packet/UDP.java
index 9f51713..a5711e7 100644
--- a/utils/misc/src/main/java/org/onlab/packet/UDP.java
+++ b/utils/misc/src/main/java/org/onlab/packet/UDP.java
@@ -35,6 +35,8 @@
.put(UDP.DHCP_V6_SERVER_PORT, DHCP6.deserializer())
.put(UDP.DHCP_V6_CLIENT_PORT, DHCP6.deserializer())
.put(UDP.VXLAN_UDP_PORT, VXLAN.deserializer())
+ .put(UDP.RIP_PORT, RIP.deserializer())
+ .put(UDP.RIPNG_PORT, RIPng.deserializer())
.build();
public static final int DHCP_SERVER_PORT = 67;
@@ -42,6 +44,8 @@
public static final int DHCP_V6_SERVER_PORT = 547;
public static final int DHCP_V6_CLIENT_PORT = 546;
public static final int VXLAN_UDP_PORT = 4789;
+ public static final int RIP_PORT = 520;
+ public static final int RIPNG_PORT = 521;
private static final short UDP_HEADER_LENGTH = 8;