Search in sources :

Example 1 with BufferException

use of org.opendaylight.openflowplugin.libraries.liblldp.BufferException in project netvirt by opendaylight.

the class DHCP method deserialize.

// public void setPadding(byte[] pad) {
// this.pad = pad;
// }
/**
 * This method deserializes the data bits obtained from the wire into the
 * respective header and payload which are of type Packet.
 *
 * @param data       byte[] data from wire to deserialize
 * @param bitOffset  int    bit position where packet header starts in data
 *        array
 * @param size       int    size of packet in bits
 * @return Packet
 * @throws PacketException the packet deserialization failed
 *
 * <p>Note: Copied from org.opendaylight.controller.sal.packet.Packet</p>
 */
@Override
// We can’t do much about PacketException (yet; see https://git.opendaylight.org/gerrit/65837)
@SuppressWarnings("checkstyle:AvoidHidingCauseException")
public Packet deserialize(byte[] data, int bitOffset, int size) throws PacketException {
    // Deserialize the header fields one by one
    int startOffset = 0;
    int numBits = 0;
    for (Entry<String, Pair<Integer, Integer>> pairs : hdrFieldCoordMap.entrySet()) {
        String hdrField = pairs.getKey();
        startOffset = bitOffset + this.getfieldOffset(hdrField);
        if (hdrField.equals(OPTIONS)) {
            numBits = (size - DHCP_NOOPT_HDR_SIZE) * 8;
        } else {
            numBits = this.getfieldnumBits(hdrField);
        }
        byte[] hdrFieldBytes = null;
        try {
            hdrFieldBytes = BitBufferHelper.getBits(data, startOffset, numBits);
        } catch (BufferException e) {
            throw new PacketException(e.getMessage());
        }
        /*
             * Store the raw read value, checks the payload type and set the
             * payloadClass accordingly
             */
        this.setHeaderField(hdrField, hdrFieldBytes);
        if (LOG.isTraceEnabled()) {
            LOG.trace("{}: {}: {} (offset {} bitsize {})", this.getClass().getSimpleName(), hdrField, HexEncode.bytesToHexString(hdrFieldBytes), startOffset, numBits);
        }
    }
    // Deserialize the payload now
    int payloadStart = startOffset + numBits;
    int payloadSize = data.length * NetUtils.NUM_BITS_IN_A_BYTE - payloadStart;
    if (payloadClass != null) {
        try {
            payload = payloadClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("Error parsing payload for Ethernet packet", e);
        }
        payload.deserialize(data, payloadStart, payloadSize);
        payload.setParent(this);
    } else {
        /*
             *  The payload class was not set, it means no class for parsing
             *  this payload is present. Let's store the raw payload if any.
             */
        int start = payloadStart / NetUtils.NUM_BITS_IN_A_BYTE;
        int stop = start + payloadSize / NetUtils.NUM_BITS_IN_A_BYTE;
        rawPayload = Arrays.copyOfRange(data, start, stop);
    }
    // Take care of computation that can be done only after deserialization
    postDeserializeCustomOperation(data, payloadStart - getHeaderSize());
    return this;
}
Also used : BufferException(org.opendaylight.openflowplugin.libraries.liblldp.BufferException) PacketException(org.opendaylight.openflowplugin.libraries.liblldp.PacketException) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

Example 2 with BufferException

use of org.opendaylight.openflowplugin.libraries.liblldp.BufferException in project genius by opendaylight.

the class IPv4 method postSerializeCustomOperation.

@Override
protected /**
 * Method to perform post serialization - like computation of checksum of serialized header
 * @param data
 * @return void
 * @Exception throws PacketException
 */
void postSerializeCustomOperation(byte[] data) throws PacketException {
    // Recompute the total length field here
    byte[] totalLength = BitBufferHelper.toByteArray((short) data.length);
    try {
        BitBufferHelper.setBytes(data, totalLength, getfieldOffset(TOTLENGTH), getfieldnumBits(TOTLENGTH));
    } catch (BufferException e) {
        throw new PacketException(e.getMessage(), e);
    }
    // Now compute the Header Checksum
    byte[] checkSum = BitBufferHelper.toByteArray(computeChecksum(data, 0));
    try {
        BitBufferHelper.setBytes(data, checkSum, getfieldOffset(CHECKSUM), getfieldnumBits(CHECKSUM));
    } catch (BufferException e) {
        throw new PacketException(e.getMessage(), e);
    }
}
Also used : BufferException(org.opendaylight.openflowplugin.libraries.liblldp.BufferException) PacketException(org.opendaylight.openflowplugin.libraries.liblldp.PacketException)

Aggregations

BufferException (org.opendaylight.openflowplugin.libraries.liblldp.BufferException)2 PacketException (org.opendaylight.openflowplugin.libraries.liblldp.PacketException)2 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 Pair (org.apache.commons.lang3.tuple.Pair)1