2 * Copyright (c) 2013, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.liblldp;
11 import java.util.Arrays;
13 import java.util.Map.Entry;
15 import org.apache.commons.lang3.tuple.Pair;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * Abstract class which represents the generic network packet object It provides
21 * the basic methods which are common for all the packets, like serialize and
25 public abstract class Packet {
26 protected static final Logger LOG = LoggerFactory.getLogger(Packet.class);
27 // Access level granted to this packet
28 protected boolean writeAccess;
29 // When deserialized from wire, packet could result corrupted
30 protected boolean corrupted;
31 // The packet that encapsulate this packet
32 protected Packet parent;
33 // The packet encapsulated by this packet
34 protected Packet payload;
35 // The unparsed raw payload carried by this packet
36 protected byte[] rawPayload;
37 // Bit coordinates of packet header fields
38 protected Map<String, Pair<Integer, Integer>> hdrFieldCoordMap;
39 // Header fields values: Map<FieldName,Value>
40 protected Map<String, byte[]> hdrFieldsMap;
41 // The class of the encapsulated packet object
42 protected Class<? extends Packet> payloadClass;
49 public Packet(final boolean writeAccess) {
50 this.writeAccess = writeAccess;
54 public Packet getParent() {
58 public Packet getPayload() {
62 public void setParent(final Packet parent) {
66 public void setPayload(final Packet payload) {
67 this.payload = payload;
70 public void setHeaderField(final String headerField, final byte[] readValue) {
71 hdrFieldsMap.put(headerField, readValue);
75 * This method deserializes the data bits obtained from the wire into the
76 * respective header and payload which are of type Packet
78 * @param data - data from wire to deserialize
79 * @param bitOffset bit position where packet header starts in data
81 * @param size size of packet in bits
83 * @throws PacketException
85 public Packet deserialize(final byte[] data, final int bitOffset, final int size)
86 throws PacketException {
88 // Deserialize the header fields one by one
89 int startOffset = 0, numBits = 0;
90 for (Entry<String, Pair<Integer, Integer>> pairs : hdrFieldCoordMap
92 String hdrField = pairs.getKey();
93 startOffset = bitOffset + this.getfieldOffset(hdrField);
94 numBits = this.getfieldnumBits(hdrField);
96 byte[] hdrFieldBytes = null;
98 hdrFieldBytes = BitBufferHelper.getBits(data, startOffset,
100 } catch (final BufferException e) {
101 throw new PacketException(e.getMessage());
105 * Store the raw read value, checks the payload type and set the
106 * payloadClass accordingly
108 this.setHeaderField(hdrField, hdrFieldBytes);
110 if (LOG.isTraceEnabled()) {
111 LOG.trace("{}: {}: {} (offset {} bitsize {})",
112 new Object[] { this.getClass().getSimpleName(), hdrField,
113 HexEncode.bytesToHexString(hdrFieldBytes),
114 startOffset, numBits });
118 // Deserialize the payload now
119 int payloadStart = startOffset + numBits;
120 int payloadSize = data.length * NetUtils.NumBitsInAByte - payloadStart;
122 if (payloadClass != null) {
124 payload = payloadClass.newInstance();
125 } catch (final Exception e) {
126 throw new RuntimeException(
127 "Error parsing payload for Ethernet packet", e);
129 payload.deserialize(data, payloadStart, payloadSize);
130 payload.setParent(this);
133 * The payload class was not set, it means no class for parsing
134 * this payload is present. Let's store the raw payload if any.
136 int start = payloadStart / NetUtils.NumBitsInAByte;
137 int stop = start + payloadSize / NetUtils.NumBitsInAByte;
138 rawPayload = Arrays.copyOfRange(data, start, stop);
142 // Take care of computation that can be done only after deserialization
143 postDeserializeCustomOperation(data, payloadStart - getHeaderSize());
149 * This method serializes the header and payload from the respective
150 * packet class, into a single stream of bytes to be sent on the wire
152 * @return The byte array representing the serialized Packet
153 * @throws PacketException
155 public byte[] serialize() throws PacketException {
157 // Acquire or compute the serialized payload
158 byte[] payloadBytes = null;
159 if (payload != null) {
160 payloadBytes = payload.serialize();
161 } else if (rawPayload != null) {
162 payloadBytes = rawPayload;
164 int payloadSize = payloadBytes == null ? 0 : payloadBytes.length;
166 // Allocate the buffer to contain the full (header + payload) packet
167 int headerSize = this.getHeaderSize() / NetUtils.NumBitsInAByte;
168 byte packetBytes[] = new byte[headerSize + payloadSize];
169 if (payloadBytes != null) {
170 System.arraycopy(payloadBytes, 0, packetBytes, headerSize, payloadSize);
173 // Serialize this packet header, field by field
174 for (Map.Entry<String, Pair<Integer, Integer>> pairs : hdrFieldCoordMap
176 String field = pairs.getKey();
177 byte[] fieldBytes = hdrFieldsMap.get(field);
178 // Let's skip optional fields when not set
179 if (fieldBytes != null) {
181 BitBufferHelper.setBytes(packetBytes, fieldBytes,
182 getfieldOffset(field), getfieldnumBits(field));
183 } catch (final BufferException e) {
184 throw new PacketException(e.getMessage());
189 // Perform post serialize operations (like checksum computation)
190 postSerializeCustomOperation(packetBytes);
192 if (LOG.isTraceEnabled()) {
193 LOG.trace("{}: {}", this.getClass().getSimpleName(),
194 HexEncode.bytesToHexString(packetBytes));
201 * This method gets called at the end of the serialization process It is
202 * intended for the child packets to insert some custom data into the output
203 * byte stream which cannot be done or cannot be done efficiently during the
204 * normal Packet.serialize() path. An example is the checksum computation
207 * @param myBytes serialized bytes
208 * @throws PacketException
210 protected void postSerializeCustomOperation(byte[] myBytes)
211 throws PacketException {
216 * This method re-computes the checksum of the bits received on the wire and
217 * validates it with the checksum in the bits received Since the computation
218 * of checksum varies based on the protocol, this method is overridden.
219 * Currently only IPv4 and ICMP do checksum computation and validation. TCP
220 * and UDP need to implement these if required.
222 * @param data The byte stream representing the Ethernet frame
223 * @param startBitOffset The bit offset from where the byte array corresponding to this Packet starts in the frame
224 * @throws PacketException
226 protected void postDeserializeCustomOperation(byte[] data, int startBitOffset)
227 throws PacketException {
232 * Gets the header length in bits
234 * @return int the header length in bits
236 public int getHeaderSize() {
239 * We need to iterate over the fields that were read in the frame
240 * (hdrFieldsMap) not all the possible ones described in
241 * hdrFieldCoordMap. For ex, 802.1Q may or may not be there
243 for (Map.Entry<String, byte[]> fieldEntry : hdrFieldsMap.entrySet()) {
244 if (fieldEntry.getValue() != null) {
245 String field = fieldEntry.getKey();
246 size += getfieldnumBits(field);
253 * This method fetches the start bit offset for header field specified by
254 * 'fieldname'. The offset is present in the hdrFieldCoordMap of the
255 * respective packet class
257 * @return Integer - startOffset of the requested field
259 public int getfieldOffset(final String fieldName) {
260 return hdrFieldCoordMap.get(fieldName).getLeft();
264 * This method fetches the number of bits for header field specified by
265 * 'fieldname'. The numBits are present in the hdrFieldCoordMap of the
266 * respective packet class
268 * @return Integer - number of bits of the requested field
270 public int getfieldnumBits(final String fieldName) {
271 return hdrFieldCoordMap.get(fieldName).getRight();
275 public String toString() {
276 StringBuilder ret = new StringBuilder();
277 ret.append(this.getClass().getSimpleName());
279 for (String field : hdrFieldCoordMap.keySet()) {
280 byte[] value = hdrFieldsMap.get(field);
283 ret.append(HexEncode.bytesToHexString(value));
286 ret.replace(ret.length()-2, ret.length()-1, "]");
287 return ret.toString();
291 * Returns the raw payload carried by this packet in case payload was not
292 * parsed. Caller can call this function in case the getPaylod() returns null.
294 * @return The raw payload if not parsable as an array of bytes, null otherwise
296 public byte[] getRawPayload() {
301 * Set a raw payload in the packet class
303 * @param payload The raw payload as byte array
305 public void setRawPayload(final byte[] payload) {
306 this.rawPayload = Arrays.copyOf(payload, payload.length);
310 * Return whether the deserialized packet is to be considered corrupted.
311 * This is the case when the checksum computed after reconstructing the
312 * packet received from wire is not equal to the checksum read from the
313 * stream. For the Packet class which do not have a checksum field, this
314 * function will always return false.
317 * @return true if the deserialized packet's recomputed checksum is not
318 * equal to the packet carried checksum
320 public boolean isCorrupted() {
325 public int hashCode() {
326 final int prime = 31;
327 int result = super.hashCode();
328 result = prime * result
329 + (this.hdrFieldsMap == null ? 0 : hdrFieldsMap.hashCode());
334 public boolean equals(final Object obj) {
338 if (getClass() != obj.getClass()) {
341 Packet other = (Packet) obj;
342 if (hdrFieldsMap == other.hdrFieldsMap) {
345 if (hdrFieldsMap == null || other.hdrFieldsMap == null) {
348 if (hdrFieldsMap != null && other.hdrFieldsMap != null) {
349 for (String field : hdrFieldsMap.keySet()) {
350 if (!Arrays.equals(hdrFieldsMap.get(field), other.hdrFieldsMap.get(field))) {