Java Code Examples for org.jboss.netty.buffer.ChannelBuffer#readShort()

The following examples show how to use org.jboss.netty.buffer.ChannelBuffer#readShort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BgpAttrNodeIsIsAreaId.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the IS-IS Area Identifier.
 *
 * @param cb ChannelBuffer
 * @return object of BgpAttrNodeIsIsAreaId
 * @throws BgpParseException while parsing BgpAttrNodeIsIsAreaId
 */
public static BgpAttrNodeIsIsAreaId read(ChannelBuffer cb)
        throws BgpParseException {
    byte[] isisAreaId;

    short lsAttrLength = cb.readShort();

    if (cb.readableBytes() < lsAttrLength) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
                               BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
                               lsAttrLength);
    }

    isisAreaId = new byte[lsAttrLength];
    cb.readBytes(isisAreaId);

    return BgpAttrNodeIsIsAreaId.of(isisAreaId);
}
 
Example 2
Source File: BgpLinkAttrTeDefaultMetric.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the BGP link attributes of TE default metric.
 *
 * @param cb Channel buffer
 * @return object of type BgpLinkAttrTeDefaultMetric
 * @throws BgpParseException while parsing BgpLinkAttrTeDefaultMetric
 */
public static BgpLinkAttrTeDefaultMetric read(ChannelBuffer cb)
        throws BgpParseException {
    int linkTeMetric;

    short lsAttrLength = cb.readShort();

    if ((lsAttrLength != TE_DATA_LEN)
            || (cb.readableBytes() < lsAttrLength)) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
                               BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
                               lsAttrLength);
    }

    linkTeMetric = cb.readInt();

    return new BgpLinkAttrTeDefaultMetric(linkTeMetric);
}
 
Example 3
Source File: BgpPrefixAttrRouteTag.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the Route Tag.
 *
 * @param cb ChannelBuffer
 * @return object of BgpPrefixAttrRouteTag
 * @throws BgpParseException while parsing BgpPrefixAttrRouteTag
 */
public static BgpPrefixAttrRouteTag read(ChannelBuffer cb)
        throws BgpParseException {
    int tmp;
    ArrayList<Integer> pfxRouteTag = new ArrayList<Integer>();

    short lsAttrLength = cb.readShort();
    int len = lsAttrLength / SIZE;

    if (cb.readableBytes() < lsAttrLength) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
                               BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
                               lsAttrLength);
    }

    for (int i = 0; i < len; i++) {
        tmp = cb.readInt();
        pfxRouteTag.add(new Integer(tmp));
    }

    return BgpPrefixAttrRouteTag.of(pfxRouteTag);
}
 
Example 4
Source File: OFMatch.java    From floodlight_with_topoguard with Apache License 2.0 6 votes vote down vote up
/**
 * Read this message off the wire from the specified ByteBuffer
 *
 * @param data
 */
public void readFrom(ChannelBuffer data) {
    this.wildcards = data.readInt();
    this.inputPort = data.readShort();
    this.dataLayerSource = new byte[6];
    data.readBytes(this.dataLayerSource);
    this.dataLayerDestination = new byte[6];
    data.readBytes(this.dataLayerDestination);
    this.dataLayerVirtualLan = data.readShort();
    this.dataLayerVirtualLanPriorityCodePoint = data.readByte();
    data.readByte(); // pad
    this.dataLayerType = data.readShort();
    this.networkTypeOfService = data.readByte();
    this.networkProtocol = data.readByte();
    data.readByte(); // pad
    data.readByte(); // pad
    this.networkSource = data.readInt();
    this.networkDestination = data.readInt();
    this.transportSource = data.readShort();
    this.transportDestination = data.readShort();
}
 
Example 5
Source File: BgpLinkAttrMplsProtocolMask.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the BGP link attributes MPLS protocol mask.
 *
 * @param cb Channel buffer
 * @return object of type BgpLinkAttrMPLSProtocolMask
 * @throws BgpParseException while parsing BgpLinkAttrMplsProtocolMask
 */
public static BgpLinkAttrMplsProtocolMask read(ChannelBuffer cb)
        throws BgpParseException {
    boolean bLdp = false;
    boolean bRsvpTe = false;

    short lsAttrLength = cb.readShort();

    if ((lsAttrLength != MASK_BYTE_LEN)
            || (cb.readableBytes() < lsAttrLength)) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
                               BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
                               lsAttrLength);
    }

    byte flags = cb.readByte();

    bLdp = ((flags & (byte) FIRST_BIT) == FIRST_BIT);
    bRsvpTe = ((flags & (byte) SECOND_BIT) == SECOND_BIT);

    return BgpLinkAttrMplsProtocolMask.of(bLdp, bRsvpTe);
}
 
Example 6
Source File: PcepRsvpIpv4ErrorSpec.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Reads PCPE RSVP error spec from channel buffer and returns PCEP rsvp IPv4 error spec object.
 *
 * @param cb channel buffer
 * @return PCEP rsvp IPv4 error spec object
 */
public static PcepRsvpErrorSpec read(ChannelBuffer cb) {
    PcepRsvpSpecObjHeader objHeader;
    int ipv4Addr;
    byte flags;
    byte errCode;
    short errValue;

    objHeader = PcepRsvpSpecObjHeader.read(cb);
    ipv4Addr = cb.readInt();
    flags = cb.readByte();
    errCode = cb.readByte();
    errValue = cb.readShort();
    return new PcepRsvpIpv4ErrorSpec(objHeader, ipv4Addr, flags, errCode, errValue);
}
 
Example 7
Source File: OFActionEnqueue.java    From floodlight_with_topoguard with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(ChannelBuffer data) {
    super.readFrom(data);
    this.port = data.readShort();
    data.readShort();
    data.readInt();
    this.queueId = data.readInt();
}
 
Example 8
Source File: MessageFrame.java    From TrendrrNSQClient with MIT License 5 votes vote down vote up
@Override
public void setData(byte[] bytes) {
    //parse the bytes
    super.setData(bytes);

    ChannelBuffer buf = ChannelBuffers.wrappedBuffer(bytes);
    this.timestamp = buf.readLong();
    this.attempts = buf.readShort();
    this.messageId = new byte[16];

    buf.readBytes(this.messageId);
    this.messageBody = buf.readBytes(buf.readableBytes()).array();
}
 
Example 9
Source File: BgpPeerFrameDecoderTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Processes BGP open message.
 *
 * @param ctx Channel handler context
 * @param message open message
 */
private void processBgpOpen(ChannelHandlerContext ctx,
                            ChannelBuffer message) {
    int minLength =
        MINIMUM_OPEN_MSG_LENGTH - MINIMUM_COMMON_HEADER_LENGTH;
    if (message.readableBytes() < minLength) {
        log.debug("Error: Bad message length");
        ctx.getChannel().close();
        return;
    }

    message.readByte(); // read version
    message.readShort(); // read AS number
    message.readShort(); // read Hold timer
    message.readInt(); // read BGP Identifier
    // Optional Parameters
    int optParamLen = message.readUnsignedByte();
    if (message.readableBytes() < optParamLen) {
        log.debug("Error: Bad message length");
        ctx.getChannel().close();
        return;
    }
    message.readBytes(optParamLen);

    // Open message received
    receivedOpenMessageLatch.countDown();
}
 
Example 10
Source File: OFError.java    From floodlight_with_topoguard with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(ChannelBuffer data) {
    super.readFrom(data);
    this.errorType = data.readShort();
    this.errorCode = data.readShort();
    int dataLength = this.getLengthU() - MINIMUM_LENGTH;
    if (dataLength > 0) {
        this.error = new byte[dataLength];
        data.readBytes(this.error);
        if (this.errorType == OFErrorType.OFPET_HELLO_FAILED.getValue())
            this.errorIsAscii = true;
    }
}
 
Example 11
Source File: OspfMessageReader.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the OSPF packet Header.
 *
 * @param channelBuffer channel buffer instance.
 * @return Ospf Header instance.
 */
private OspfPacketHeader getOspfHeader(ChannelBuffer channelBuffer) {
    OspfPacketHeader ospfPacketHeader = new OspfPacketHeader();

    // Determine OSPF version & Packet Type
    int version = channelBuffer.readByte(); //byte 1 is ospf version
    int packetType = channelBuffer.readByte(); //byte 2 is ospf packet type

    // byte 3 & 4 combine is packet length.
    int packetLength = channelBuffer.readShort();

    byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
    channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
    Ip4Address routerId = Ip4Address.valueOf(tempByteArray);

    tempByteArray = new byte[OspfUtil.FOUR_BYTES];
    channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
    Ip4Address areaId = Ip4Address.valueOf(tempByteArray);

    int checkSum = channelBuffer.readUnsignedShort();
    int auType = channelBuffer.readUnsignedShort();
    int authentication = (int) channelBuffer.readLong();

    ospfPacketHeader.setOspfVer(version);
    ospfPacketHeader.setOspftype(packetType);
    ospfPacketHeader.setOspfPacLength(packetLength);
    ospfPacketHeader.setRouterId(routerId);
    ospfPacketHeader.setAreaId(areaId);
    ospfPacketHeader.setChecksum(checkSum);
    ospfPacketHeader.setAuthType(auType);
    ospfPacketHeader.setAuthentication(authentication);

    return ospfPacketHeader;
}
 
Example 12
Source File: PcepRsvpSpecObjHeader.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the PcepRsvpObjectHeader.
 *
 * @param cb of type channel buffer
 * @return PcepRsvpObjectHeader
 */
public static PcepRsvpSpecObjHeader read(ChannelBuffer cb) {
    byte objClassNum;
    byte objClassType;
    short objLen;
    objLen = cb.readShort();
    objClassNum = cb.readByte();
    objClassType = cb.readByte();

    return new PcepRsvpSpecObjHeader(objLen, objClassNum, objClassType);
}
 
Example 13
Source File: PcepOpenMsgVer1.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public PcepOpenMsg readFrom(ChannelBuffer cb) throws PcepParseException {

    if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
        throw new PcepParseException("Packet size is less than the minimum length.");
    }

    byte version = cb.readByte();
    version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG);
    if (version != PACKET_VERSION) {
        log.error("[readFrom] Invalid version: " + version);
        throw new PcepParseException(PcepErrorDetailInfo.ERROR_TYPE_1, PcepErrorDetailInfo.ERROR_VALUE_1);
    }
    // fixed value property type == 1
    byte type = cb.readByte();

    if (type != MSG_TYPE.getType()) {
        log.error("[readFrom] Unexpected type: " + type);
        throw new PcepParseException(PcepErrorDetailInfo.ERROR_TYPE_1, PcepErrorDetailInfo.ERROR_VALUE_1);
    }
    int length = cb.readShort();
    if (length < PACKET_MINIMUM_LENGTH) {
        throw new PcepParseException(
                "Wrong length: Expected to be >= " + PACKET_MINIMUM_LENGTH + ", was: " + length);
    }
    return new PcepOpenMsgVer1(PcepOpenObjectVer1.read(cb));
}
 
Example 14
Source File: PcepLabelRangeObjectVer1.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Returns list of optional tlvs.
 *
 * @param cb of type channle buffer
 * @return list of optional tlvs
 * @throws PcepParseException whne fails to parse list of optional tlvs
 */
public static LinkedList<PcepValueType> parseOptionalTlv(ChannelBuffer cb) throws PcepParseException {

    LinkedList<PcepValueType> llOutOptionalTlv = new LinkedList<>();

    while (MINIMUM_COMMON_HEADER_LENGTH <= cb.readableBytes()) {

        PcepValueType tlv;
        int iValue;
        short hType = cb.readShort();
        short hLength = cb.readShort();

        switch (hType) {

        case PathSetupTypeTlv.TYPE:
            iValue = cb.readInt();
            tlv = new PathSetupTypeTlv(iValue);
            break;

        default:
            throw new PcepParseException("Unsupported TLV in LabelRange Object.");
        }

        // Check for the padding
        int pad = hLength % 4;
        if (0 < pad) {
            pad = 4 - pad;
            if (pad <= cb.readableBytes()) {
                cb.skipBytes(pad);
            }
        }
        llOutOptionalTlv.add(tlv);
    }
    return llOutOptionalTlv;
}
 
Example 15
Source File: OFQueueGetConfigRequest.java    From floodlight_with_topoguard with Apache License 2.0 4 votes vote down vote up
@Override
public void readFrom(ChannelBuffer data) {
    super.readFrom(data);
    this.portNumber = data.readShort();
    data.readShort(); // pad
}
 
Example 16
Source File: OFPortStatisticsRequest.java    From floodlight_with_topoguard with Apache License 2.0 4 votes vote down vote up
@Override
public void readFrom(ChannelBuffer data) {
    this.portNumber = data.readShort();
    data.readShort(); // pad
    data.readInt(); // pad
}
 
Example 17
Source File: BgpPrefixLSIdentifier.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Parse list of prefix descriptors.
 *
 * @param cb ChannelBuffer
 * @return list of prefix descriptors
 * @throws BgpParseException while parsing list of prefix descriptors
 */
public static List<BgpValueType> parsePrefixDescriptors(ChannelBuffer cb) throws BgpParseException {
    LinkedList<BgpValueType> prefixDescriptor = new LinkedList<>();
    BgpValueType tlv = null;
    boolean isIpReachInfo = false;
    ChannelBuffer tempCb;
    int count = 0;

    while (cb.readableBytes() > 0) {
        ChannelBuffer tempBuf = cb.copy();
        short type = cb.readShort();
        short length = cb.readShort();
        if (cb.readableBytes() < length) {
            //length + 4 implies data contains type, length and value
            throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR,
                    tempBuf.readBytes(cb.readableBytes() + TYPE_AND_LEN));
        }
        tempCb = cb.readBytes(length);
        switch (type) {
        case OspfRouteTypeTlv.TYPE:
            tlv = OspfRouteTypeTlv.read(tempCb);
            break;
        case IPReachabilityInformationTlv.TYPE:
            tlv = IPReachabilityInformationTlv.read(tempCb, length);
            isIpReachInfo = true;
            break;
        case BgpAttrNodeMultiTopologyId.ATTRNODE_MULTITOPOLOGY:
            tlv = BgpAttrNodeMultiTopologyId.read(tempCb, length);
            count = count + 1;
            if (count > 1) {
                //length + 4 implies data contains type, length and value
                throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR,
                       BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR, tempBuf.readBytes(length + TYPE_AND_LEN));
            }
            break;
        default:
            UnSupportedAttribute.skipBytes(tempCb, length);
        }
        prefixDescriptor.add(tlv);
    }

    if (!isIpReachInfo) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR,
                null);
    }
    return prefixDescriptor;
}
 
Example 18
Source File: OFAction.java    From floodlight_with_topoguard with Apache License 2.0 4 votes vote down vote up
public void readFrom(ChannelBuffer data) {
    this.type = OFActionType.valueOf(data.readShort());
    this.length = data.readShort();
    // Note missing PAD, see MINIMUM_LENGTH comment for details
}
 
Example 19
Source File: ServerPacketDecoder.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
    if (buffer.readableBytes() < 2) {
        return null;
    }
    buffer.markReaderIndex();
    final short packetType = buffer.readShort();
    switch (packetType) {
        case PacketType.APPLICATION_SEND:
            return readSend(packetType, buffer);
        case PacketType.APPLICATION_REQUEST:
            return readRequest(packetType, buffer);
        case PacketType.APPLICATION_RESPONSE:
            return readResponse(packetType, buffer);
        case PacketType.APPLICATION_STREAM_CREATE:
            return readStreamCreate(packetType, buffer);
        case PacketType.APPLICATION_STREAM_CLOSE:
            return readStreamClose(packetType, buffer);
        case PacketType.APPLICATION_STREAM_CREATE_SUCCESS:
            return readStreamCreateSuccess(packetType, buffer);
        case PacketType.APPLICATION_STREAM_CREATE_FAIL:
            return readStreamCreateFail(packetType, buffer);
        case PacketType.APPLICATION_STREAM_RESPONSE:
            return readStreamData(packetType, buffer);
        case PacketType.APPLICATION_STREAM_PING:
            return readStreamPing(packetType, buffer);
        case PacketType.APPLICATION_STREAM_PONG:
            return readStreamPong(packetType, buffer);
        case PacketType.CONTROL_CLIENT_CLOSE:
            return readControlClientClose(packetType, buffer);
        case PacketType.CONTROL_SERVER_CLOSE:
            return readControlServerClose(packetType, buffer);
        case PacketType.CONTROL_PING_SIMPLE:
            return readPing(packetType, buffer);
        case PacketType.CONTROL_PING_PAYLOAD:
            return readPayloadPing(packetType, buffer);
        case PacketType.CONTROL_PING:
            return readLegacyPing(packetType, buffer);
        case PacketType.CONTROL_PONG:
            logger.debug("receive pong. {}", channel);
            readPong(packetType, buffer);
            // just also drop pong.
            return null;
        case PacketType.CONTROL_HANDSHAKE:
            return readEnableWorker(packetType, buffer);
        case PacketType.CONTROL_HANDSHAKE_RESPONSE:
            return readEnableWorkerConfirm(packetType, buffer);
    }
    logger.error("invalid packetType received. packetType:{}, channel:{}", packetType, channel);
    channel.close();
    return null;
}
 
Example 20
Source File: OFActionTransportLayer.java    From floodlight_with_topoguard with Apache License 2.0 4 votes vote down vote up
@Override
public void readFrom(ChannelBuffer data) {
    super.readFrom(data);
    this.transportPort = data.readShort();
    data.readShort();
}