Committed by
Charles Chan
Add abstract IP header class to unify IPv4 and IPv6 header classes
Change-Id: Ia932dad67f64595b52b6fbc7dc43a13f64d53796
Showing
4 changed files
with
159 additions
and
26 deletions
1 | +/* | ||
2 | + * Copyright 2016-present Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onlab.packet; | ||
17 | + | ||
18 | +import java.nio.ByteBuffer; | ||
19 | + | ||
20 | +/** | ||
21 | + * Implements IP packet format. | ||
22 | + */ | ||
23 | +public abstract class IP extends BasePacket { | ||
24 | + | ||
25 | + /** | ||
26 | + * Gets IP version number. | ||
27 | + * | ||
28 | + * @return IP version number | ||
29 | + */ | ||
30 | + public abstract byte getVersion(); | ||
31 | + | ||
32 | + /** | ||
33 | + * Sets IP version number. | ||
34 | + * | ||
35 | + * @param version the version to set | ||
36 | + * @return IP class | ||
37 | + */ | ||
38 | + public abstract IP setVersion(final byte version); | ||
39 | + | ||
40 | + /** | ||
41 | + * Deserializer function for IP packets. | ||
42 | + * | ||
43 | + * @return deserializer function | ||
44 | + */ | ||
45 | + public static Deserializer<? extends IP> deserializer() { | ||
46 | + return (data, offset, length) -> { | ||
47 | + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); | ||
48 | + byte version = (byte) (bb.get() >> 4 & 0xf); | ||
49 | + | ||
50 | + switch (version) { | ||
51 | + case 4: | ||
52 | + return IPv4.deserializer().deserialize(data, offset, length); | ||
53 | + case 6: | ||
54 | + return IPv6.deserializer().deserialize(data, offset, length); | ||
55 | + default: | ||
56 | + throw new DeserializationException("Invalid IP version"); | ||
57 | + } | ||
58 | + }; | ||
59 | + } | ||
60 | +} |
... | @@ -14,9 +14,6 @@ | ... | @@ -14,9 +14,6 @@ |
14 | * limitations under the License. | 14 | * limitations under the License. |
15 | */ | 15 | */ |
16 | 16 | ||
17 | -/** | ||
18 | - * | ||
19 | - */ | ||
20 | package org.onlab.packet; | 17 | package org.onlab.packet; |
21 | 18 | ||
22 | import java.nio.ByteBuffer; | 19 | import java.nio.ByteBuffer; |
... | @@ -31,7 +28,7 @@ import static org.onlab.packet.PacketUtils.*; | ... | @@ -31,7 +28,7 @@ import static org.onlab.packet.PacketUtils.*; |
31 | /** | 28 | /** |
32 | * Implements IPv4 packet format. | 29 | * Implements IPv4 packet format. |
33 | */ | 30 | */ |
34 | -public class IPv4 extends BasePacket { | 31 | +public class IPv4 extends IP { |
35 | public static final byte PROTOCOL_ICMP = 0x1; | 32 | public static final byte PROTOCOL_ICMP = 0x1; |
36 | public static final byte PROTOCOL_IGMP = 0x2; | 33 | public static final byte PROTOCOL_IGMP = 0x2; |
37 | public static final byte PROTOCOL_TCP = 0x6; | 34 | public static final byte PROTOCOL_TCP = 0x6; |
... | @@ -79,18 +76,12 @@ public class IPv4 extends BasePacket { | ... | @@ -79,18 +76,12 @@ public class IPv4 extends BasePacket { |
79 | this.isTruncated = false; | 76 | this.isTruncated = false; |
80 | } | 77 | } |
81 | 78 | ||
82 | - /** | 79 | + @Override |
83 | - * @return the version | ||
84 | - */ | ||
85 | public byte getVersion() { | 80 | public byte getVersion() { |
86 | return this.version; | 81 | return this.version; |
87 | } | 82 | } |
88 | 83 | ||
89 | - /** | 84 | + @Override |
90 | - * @param version | ||
91 | - * the version to set | ||
92 | - * @return this | ||
93 | - */ | ||
94 | public IPv4 setVersion(final byte version) { | 85 | public IPv4 setVersion(final byte version) { |
95 | this.version = version; | 86 | this.version = version; |
96 | return this; | 87 | return this; | ... | ... |
... | @@ -14,8 +14,6 @@ | ... | @@ -14,8 +14,6 @@ |
14 | * limitations under the License. | 14 | * limitations under the License. |
15 | */ | 15 | */ |
16 | 16 | ||
17 | - | ||
18 | - | ||
19 | package org.onlab.packet; | 17 | package org.onlab.packet; |
20 | 18 | ||
21 | import org.onlab.packet.ipv6.Authentication; | 19 | import org.onlab.packet.ipv6.Authentication; |
... | @@ -37,7 +35,7 @@ import static org.onlab.packet.PacketUtils.checkInput; | ... | @@ -37,7 +35,7 @@ import static org.onlab.packet.PacketUtils.checkInput; |
37 | /** | 35 | /** |
38 | * Implements IPv6 packet format. (RFC 2460) | 36 | * Implements IPv6 packet format. (RFC 2460) |
39 | */ | 37 | */ |
40 | -public class IPv6 extends BasePacket implements IExtensionHeader { | 38 | +public class IPv6 extends IP implements IExtensionHeader { |
41 | public static final byte FIXED_HEADER_LENGTH = 40; // bytes | 39 | public static final byte FIXED_HEADER_LENGTH = 40; // bytes |
42 | 40 | ||
43 | public static final byte PROTOCOL_TCP = 0x6; | 41 | public static final byte PROTOCOL_TCP = 0x6; |
... | @@ -83,21 +81,12 @@ public class IPv6 extends BasePacket implements IExtensionHeader { | ... | @@ -83,21 +81,12 @@ public class IPv6 extends BasePacket implements IExtensionHeader { |
83 | this.version = 6; | 81 | this.version = 6; |
84 | } | 82 | } |
85 | 83 | ||
86 | - /** | 84 | + @Override |
87 | - * Gets IP version. | ||
88 | - * | ||
89 | - * @return the IP version | ||
90 | - */ | ||
91 | public byte getVersion() { | 85 | public byte getVersion() { |
92 | return this.version; | 86 | return this.version; |
93 | } | 87 | } |
94 | 88 | ||
95 | - /** | 89 | + @Override |
96 | - * Sets IP version. | ||
97 | - * | ||
98 | - * @param version the IP version to set | ||
99 | - * @return this | ||
100 | - */ | ||
101 | public IPv6 setVersion(final byte version) { | 90 | public IPv6 setVersion(final byte version) { |
102 | this.version = version; | 91 | this.version = version; |
103 | return this; | 92 | return this; | ... | ... |
1 | +/* | ||
2 | + * Copyright 2016-present Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onlab.packet; | ||
17 | + | ||
18 | +import org.junit.Before; | ||
19 | +import org.junit.Test; | ||
20 | + | ||
21 | +import java.nio.ByteBuffer; | ||
22 | + | ||
23 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
24 | +import static org.hamcrest.Matchers.instanceOf; | ||
25 | +import static org.hamcrest.Matchers.is; | ||
26 | + | ||
27 | +/** | ||
28 | + * Unit tests for IP class. | ||
29 | + */ | ||
30 | +public class IpTest { | ||
31 | + private byte v4Version = 4; | ||
32 | + private byte v4HeaderLength = 6; | ||
33 | + private byte badVersion = 5; | ||
34 | + private byte badHeaderLength = 6; | ||
35 | + private static Data data; | ||
36 | + private static UDP udp; | ||
37 | + private byte[] v4HeaderBytes; | ||
38 | + private byte[] v6HeaderBytes; | ||
39 | + private byte[] badHeaderBytes; | ||
40 | + | ||
41 | + @Before | ||
42 | + public void setUp() throws Exception { | ||
43 | + | ||
44 | + ByteBuffer v4bb = ByteBuffer.allocate(v4HeaderLength * 4); | ||
45 | + v4bb.put((byte) ((v4Version & 0xf) << 4)); | ||
46 | + v4HeaderBytes = v4bb.array(); | ||
47 | + | ||
48 | + ByteBuffer badBb = ByteBuffer.allocate(badHeaderLength * 4); | ||
49 | + badBb.put((byte) ((badVersion & 0xf) << 4)); | ||
50 | + badHeaderBytes = badBb.array(); | ||
51 | + | ||
52 | + data = new Data(); | ||
53 | + data.setData("testSerialize".getBytes()); | ||
54 | + udp = new UDP(); | ||
55 | + udp.setPayload(data); | ||
56 | + | ||
57 | + byte[] bytePayload = udp.serialize(); | ||
58 | + byte[] byteHeader = { | ||
59 | + (byte) 0x69, (byte) 0x31, (byte) 0x35, (byte) 0x79, | ||
60 | + (byte) (bytePayload.length >> 8 & 0xff), (byte) (bytePayload.length & 0xff), | ||
61 | + (byte) 0x11, (byte) 0x20, | ||
62 | + (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, | ||
63 | + (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce, | ||
64 | + (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, | ||
65 | + (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8, | ||
66 | + }; | ||
67 | + v6HeaderBytes = new byte[byteHeader.length + bytePayload.length]; | ||
68 | + System.arraycopy(byteHeader, 0, v6HeaderBytes, 0, byteHeader.length); | ||
69 | + System.arraycopy(bytePayload, 0, v6HeaderBytes, byteHeader.length, bytePayload.length); | ||
70 | + } | ||
71 | + | ||
72 | + @Test | ||
73 | + public void testDeserialize() throws Exception { | ||
74 | + Deserializer ipDeserializer = IP.deserializer(); | ||
75 | + IPacket v4Packet = ipDeserializer.deserialize(v4HeaderBytes, 0, v4HeaderLength * 4); | ||
76 | + IPacket v6Packet = ipDeserializer.deserialize(v6HeaderBytes, 0, v6HeaderBytes.length); | ||
77 | + assertThat(v4Packet, is(instanceOf(IPv4.class))); | ||
78 | + assertThat(v6Packet, is(instanceOf(IPv6.class))); | ||
79 | + | ||
80 | + IPv6 ipv6 = (IPv6) v6Packet; | ||
81 | + assertThat(ipv6.getVersion(), is((byte) 6)); | ||
82 | + assertThat(ipv6.getTrafficClass(), is((byte) 0x93)); | ||
83 | + assertThat(ipv6.getFlowLabel(), is(0x13579)); | ||
84 | + assertThat(ipv6.getNextHeader(), is(IPv6.PROTOCOL_UDP)); | ||
85 | + assertThat(ipv6.getHopLimit(), is((byte) 32)); | ||
86 | + } | ||
87 | + | ||
88 | + @Test(expected = DeserializationException.class) | ||
89 | + public void testBadIpVersion() throws Exception { | ||
90 | + Deserializer ipDeserializer = IP.deserializer(); | ||
91 | + ipDeserializer.deserialize(badHeaderBytes, 0, badHeaderLength * 4); | ||
92 | + } | ||
93 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment