blob: a25ad540416a5a83be321a19ba564a58a6723afc [file] [log] [blame]
Jian Li1270aea2016-09-15 13:32:24 +09001/*
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 */
16package org.onlab.packet;
17
18import java.nio.ByteBuffer;
19
20/**
21 * Implements IP packet format.
22 */
23public 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}