Jian Li
Committed by Charles Chan

Add abstract IP header class to unify IPv4 and IPv6 header classes

Change-Id: Ia932dad67f64595b52b6fbc7dc43a13f64d53796
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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 java.nio.ByteBuffer;
/**
* Implements IP packet format.
*/
public abstract class IP extends BasePacket {
/**
* Gets IP version number.
*
* @return IP version number
*/
public abstract byte getVersion();
/**
* Sets IP version number.
*
* @param version the version to set
* @return IP class
*/
public abstract IP setVersion(final byte version);
/**
* Deserializer function for IP packets.
*
* @return deserializer function
*/
public static Deserializer<? extends IP> deserializer() {
return (data, offset, length) -> {
final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
byte version = (byte) (bb.get() >> 4 & 0xf);
switch (version) {
case 4:
return IPv4.deserializer().deserialize(data, offset, length);
case 6:
return IPv6.deserializer().deserialize(data, offset, length);
default:
throw new DeserializationException("Invalid IP version");
}
};
}
}
......@@ -14,9 +14,6 @@
* limitations under the License.
*/
/**
*
*/
package org.onlab.packet;
import java.nio.ByteBuffer;
......@@ -31,7 +28,7 @@ import static org.onlab.packet.PacketUtils.*;
/**
* Implements IPv4 packet format.
*/
public class IPv4 extends BasePacket {
public class IPv4 extends IP {
public static final byte PROTOCOL_ICMP = 0x1;
public static final byte PROTOCOL_IGMP = 0x2;
public static final byte PROTOCOL_TCP = 0x6;
......@@ -79,18 +76,12 @@ public class IPv4 extends BasePacket {
this.isTruncated = false;
}
/**
* @return the version
*/
@Override
public byte getVersion() {
return this.version;
}
/**
* @param version
* the version to set
* @return this
*/
@Override
public IPv4 setVersion(final byte version) {
this.version = version;
return this;
......
......@@ -14,8 +14,6 @@
* limitations under the License.
*/
package org.onlab.packet;
import org.onlab.packet.ipv6.Authentication;
......@@ -37,7 +35,7 @@ import static org.onlab.packet.PacketUtils.checkInput;
/**
* Implements IPv6 packet format. (RFC 2460)
*/
public class IPv6 extends BasePacket implements IExtensionHeader {
public class IPv6 extends IP implements IExtensionHeader {
public static final byte FIXED_HEADER_LENGTH = 40; // bytes
public static final byte PROTOCOL_TCP = 0x6;
......@@ -83,21 +81,12 @@ public class IPv6 extends BasePacket implements IExtensionHeader {
this.version = 6;
}
/**
* Gets IP version.
*
* @return the IP version
*/
@Override
public byte getVersion() {
return this.version;
}
/**
* Sets IP version.
*
* @param version the IP version to set
* @return this
*/
@Override
public IPv6 setVersion(final byte version) {
this.version = version;
return this;
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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.junit.Before;
import org.junit.Test;
import java.nio.ByteBuffer;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
/**
* Unit tests for IP class.
*/
public class IpTest {
private byte v4Version = 4;
private byte v4HeaderLength = 6;
private byte badVersion = 5;
private byte badHeaderLength = 6;
private static Data data;
private static UDP udp;
private byte[] v4HeaderBytes;
private byte[] v6HeaderBytes;
private byte[] badHeaderBytes;
@Before
public void setUp() throws Exception {
ByteBuffer v4bb = ByteBuffer.allocate(v4HeaderLength * 4);
v4bb.put((byte) ((v4Version & 0xf) << 4));
v4HeaderBytes = v4bb.array();
ByteBuffer badBb = ByteBuffer.allocate(badHeaderLength * 4);
badBb.put((byte) ((badVersion & 0xf) << 4));
badHeaderBytes = badBb.array();
data = new Data();
data.setData("testSerialize".getBytes());
udp = new UDP();
udp.setPayload(data);
byte[] bytePayload = udp.serialize();
byte[] byteHeader = {
(byte) 0x69, (byte) 0x31, (byte) 0x35, (byte) 0x79,
(byte) (bytePayload.length >> 8 & 0xff), (byte) (bytePayload.length & 0xff),
(byte) 0x11, (byte) 0x20,
(byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
(byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce,
(byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
(byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8,
};
v6HeaderBytes = new byte[byteHeader.length + bytePayload.length];
System.arraycopy(byteHeader, 0, v6HeaderBytes, 0, byteHeader.length);
System.arraycopy(bytePayload, 0, v6HeaderBytes, byteHeader.length, bytePayload.length);
}
@Test
public void testDeserialize() throws Exception {
Deserializer ipDeserializer = IP.deserializer();
IPacket v4Packet = ipDeserializer.deserialize(v4HeaderBytes, 0, v4HeaderLength * 4);
IPacket v6Packet = ipDeserializer.deserialize(v6HeaderBytes, 0, v6HeaderBytes.length);
assertThat(v4Packet, is(instanceOf(IPv4.class)));
assertThat(v6Packet, is(instanceOf(IPv6.class)));
IPv6 ipv6 = (IPv6) v6Packet;
assertThat(ipv6.getVersion(), is((byte) 6));
assertThat(ipv6.getTrafficClass(), is((byte) 0x93));
assertThat(ipv6.getFlowLabel(), is(0x13579));
assertThat(ipv6.getNextHeader(), is(IPv6.PROTOCOL_UDP));
assertThat(ipv6.getHopLimit(), is((byte) 32));
}
@Test(expected = DeserializationException.class)
public void testBadIpVersion() throws Exception {
Deserializer ipDeserializer = IP.deserializer();
ipDeserializer.deserialize(badHeaderBytes, 0, badHeaderLength * 4);
}
}
\ No newline at end of file