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

The following examples show how to use org.jboss.netty.buffer.ChannelBuffer#copy() . 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: BgpPrefixLSIdentifier.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Parse local node descriptors.
 *
 * @param cb ChannelBuffer
 * @param protocolId protocol identifier
 * @return LocalNodeDescriptors
 * @throws BgpParseException while parsing local node descriptors
 */
public static NodeDescriptors parseLocalNodeDescriptors(ChannelBuffer cb, byte protocolId)
                                                             throws BgpParseException {
    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));
    }
    NodeDescriptors localNodeDescriptors = new NodeDescriptors();
    ChannelBuffer tempCb = cb.readBytes(length);

    if (type == NodeDescriptors.LOCAL_NODE_DES_TYPE) {
        localNodeDescriptors = NodeDescriptors.read(tempCb, length, type, protocolId);
    } else {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR,
                                       BgpErrorType.MALFORMED_ATTRIBUTE_LIST, null);
    }
    return localNodeDescriptors;
}
 
Example 2
Source File: Origin.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Reads from ChannelBuffer and parses Origin.
 *
 * @param cb ChannelBuffer
 * @return object of Origin
 * @throws BgpParseException while parsing Origin path attribute
 */
public static Origin read(ChannelBuffer cb) throws BgpParseException {
    ChannelBuffer tempCb = cb.copy();
    Validation parseFlags = Validation.parseAttributeHeader(cb);

    int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : parseFlags
            .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
    ChannelBuffer data = tempCb.readBytes(len);
    if ((parseFlags.getLength() > ORIGIN_VALUE_LEN) || (cb.readableBytes() < parseFlags.getLength())) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
                parseFlags.getLength());
    }
    if (parseFlags.getFirstBit() && !parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
    }

    byte originValue;
    originValue = cb.readByte();
    if ((originValue != OriginType.INCOMPLETE.value) && (originValue != OriginType.IGP.value) &&
          (originValue != OriginType.EGP.value)) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.INVALID_ORIGIN_ATTRIBUTE, data);
    }
    return new Origin(originValue);
}
 
Example 3
Source File: NextHop.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Reads from ChannelBuffer and parses NextHop.
 *
 * @param cb ChannelBuffer
 * @return object of NextHop
 * @throws BgpParseException while parsing nexthop attribute
 */
public static NextHop read(ChannelBuffer cb) throws BgpParseException {
    Ip4Address nextHop;
    ChannelBuffer tempCb = cb.copy();
    Validation parseFlags = Validation.parseAttributeHeader(cb);

    if (cb.readableBytes() < parseFlags.getLength()) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
                parseFlags.getLength());
    }
    int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : parseFlags
            .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
    ChannelBuffer data = tempCb.readBytes(len);
    if (parseFlags.getFirstBit() && !parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
    }

     InetAddress ipAddress = Validation.toInetAddress(parseFlags.getLength(), cb);
    if (ipAddress.isMulticastAddress()) {
        throw new BgpParseException("Multicast address is not supported");
    }

    nextHop = Ip4Address.valueOf(ipAddress);
    return new NextHop(nextHop);
}
 
Example 4
Source File: LocalPref.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the channel buffer and returns object of LocalPref.
 *
 * @param cb channelBuffer
 * @return object of LocalPref
 * @throws BgpParseException while parsing localPref attribute
 */
public static LocalPref read(ChannelBuffer cb) throws BgpParseException {
    int localPref;
    ChannelBuffer tempCb = cb.copy();
    Validation parseFlags = Validation.parseAttributeHeader(cb);
    if ((parseFlags.getLength() > LOCAL_PREF_MAX_LEN) || cb.readableBytes() < parseFlags.getLength()) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
                parseFlags.getLength());
    }

    int len = parseFlags.isShort() ? parseFlags.getLength() +
              Constants.TYPE_AND_LEN_AS_SHORT : parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
    ChannelBuffer data = tempCb.readBytes(len);
    if (parseFlags.getFirstBit()) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
    }

    localPref = cb.readInt();
    return new LocalPref(localPref);
}
 
Example 5
Source File: Med.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the channel buffer and returns object of Med.
 *
 * @param cb ChannelBuffer
 * @return object of Med
 * @throws BgpParseException while parsing Med path attribute
 */
public static Med read(ChannelBuffer cb) throws BgpParseException {
    int med;
    ChannelBuffer tempCb = cb.copy();
    Validation parseFlags = Validation.parseAttributeHeader(cb);

    if ((parseFlags.getLength() > MED_MAX_LEN) || cb.readableBytes() < parseFlags.getLength()) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
                parseFlags.getLength());
    }
    int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : parseFlags
            .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
    ChannelBuffer data = tempCb.readBytes(len);
    if (!parseFlags.getFirstBit() && parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
    }

    med = cb.readInt();
    return new Med(med);
}
 
Example 6
Source File: BgpNodeLSIdentifier.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Parse local node descriptors.
 *
 * @param cb ChannelBuffer
 * @param protocolId protocol identifier
 * @return object of this BGPNodeLSIdentifier
 * @throws BgpParseException while parsing local node descriptors
 */
public static BgpNodeLSIdentifier parseLocalNodeDescriptors(ChannelBuffer cb, byte protocolId)
        throws BgpParseException {
    log.debug("parse Local node descriptor");
    ChannelBuffer tempBuf = cb.copy();
    short type = cb.readShort();
    short length = cb.readShort();
    if (cb.readableBytes() < length) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR,
                                    tempBuf.readBytes(cb.readableBytes() + Constants.TYPE_AND_LEN));
    }
    NodeDescriptors nodeDescriptors = new NodeDescriptors();
    ChannelBuffer tempCb = cb.readBytes(length);

    if (type == NodeDescriptors.LOCAL_NODE_DES_TYPE) {
        nodeDescriptors = NodeDescriptors.read(tempCb, length, type, protocolId);
    } else {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.MALFORMED_ATTRIBUTE_LIST, null);
    }
    return new BgpNodeLSIdentifier(nodeDescriptors);
}
 
Example 7
Source File: BgpLinkLSIdentifier.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Parses Local/Remote node descriptors.
 *
 * @param cb ChannelBuffer
 * @param desType descriptor type
 * @param protocolId protocol identifier
 * @return object of NodeDescriptors
 * @throws BgpParseException while parsing Local/Remote node descriptors
 */
public static NodeDescriptors parseNodeDescriptors(ChannelBuffer cb, short desType, byte protocolId)
        throws BgpParseException {
    log.debug("Parse node descriptors");
    ChannelBuffer tempBuf = cb.copy();
    short type = cb.readShort();
    short length = cb.readShort();
    if (cb.readableBytes() < length) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR,
                tempBuf.readBytes(cb.readableBytes() + Constants.TYPE_AND_LEN_AS_SHORT));
    }
    NodeDescriptors nodeIdentifier = new NodeDescriptors();
    ChannelBuffer tempCb = cb.readBytes(length);

    if (type == desType) {
        nodeIdentifier = NodeDescriptors.read(tempCb, length, desType, protocolId);
    } else {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.MALFORMED_ATTRIBUTE_LIST, null);
    }
    return nodeIdentifier;
}
 
Example 8
Source File: OFActionTunnelDstIPTest.java    From floodlight_with_topoguard with Apache License 2.0 6 votes vote down vote up
private void testAll(OFActionTunnelDstIP tip) {
    assertEquals(OFActionType.VENDOR, tip.getType());
    assertEquals(2, tip.getSubtype());
    assertEquals(16, tip.getLength());
    assertEquals(0x005c16c7, tip.getVendor());

    tip.setTunnelDstIP(24);
    assertEquals(24, tip.getTunnelDstIP());
    
    // Test wire format
    int ip = IPv4.toIPv4Address("17.33.49.65");
    tip.setTunnelDstIP(ip);
    ChannelBuffer buf = ChannelBuffers.buffer(32);
    tip.writeTo(buf);
    ChannelBuffer buf2 = buf.copy();
    assertEquals(16, buf.readableBytes());
    byte fromBuffer[] = new byte[16]; 
    buf.readBytes(fromBuffer);
    assertArrayEquals(expectedWireFormat1, fromBuffer);
    
    OFActionTunnelDstIP act2 = new OFActionTunnelDstIP();
    act2.readFrom(buf2);
    assertEquals(tip, act2);
    
    
}
 
Example 9
Source File: ChannelBuffers.java    From simple-netty-source with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new buffer whose content is a copy of the specified
 * {@code buffer}'s readable bytes.  The new buffer's {@code readerIndex}
 * and {@code writerIndex} are {@code 0} and {@code buffer.readableBytes}
 * respectively.
 */
public static ChannelBuffer copiedBuffer(ChannelBuffer buffer) {
    if (buffer.readable()) {
        return buffer.copy();
    } else {
        return EMPTY_BUFFER;
    }
}
 
Example 10
Source File: BgpEvpnNlriImpl.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Reads from channelBuffer and parses Evpn Nlri.
 *
 * @param cb ChannelBuffer
 * @return object of BgpEvpnNlriVer4
 * @throws BgpParseException while parsing Bgp Evpn Nlri
 */
public static BgpEvpnNlriImpl read(ChannelBuffer cb)
        throws BgpParseException {

    BgpEvpnNlriData routeNlri = null;

    if (cb.readableBytes() > 0) {
        ChannelBuffer tempBuf = cb.copy();
        byte type = cb.readByte();
        byte length = cb.readByte();
        if (cb.readableBytes() < length) {
            throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR,
                                        BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR,
                                        tempBuf.readBytes(cb.readableBytes()
                                                                  + TYPE_AND_LEN));
        }
        ChannelBuffer tempCb = cb.readBytes(length);
        switch (type) {
            case BgpEvpnRouteType2Nlri.TYPE:
                routeNlri = BgpEvpnRouteType2Nlri.read(tempCb);
                break;
            default:
                log.info("Discarding, EVPN route type {}", type);
                throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR,
                                            BgpErrorType.MISSING_WELLKNOWN_ATTRIBUTE, null);
                //        break;
        }
        return new BgpEvpnNlriImpl(type, routeNlri);
    } else {
        return new BgpEvpnNlriImpl();
    }

}
 
Example 11
Source File: OFActionNiciraTtlDecrementTest.java    From floodlight_with_topoguard with Apache License 2.0 5 votes vote down vote up
@Test
public void testAction() {
    ChannelBuffer buf = ChannelBuffers.buffer(32);
    
    OFActionNiciraTtlDecrement act = new OFActionNiciraTtlDecrement();
    
    assertEquals(true, act instanceof OFActionNiciraVendor);
    assertEquals(true, act instanceof OFActionVendor);
    assertEquals(true, act instanceof OFAction);
    
    act.writeTo(buf);
    
    ChannelBuffer buf2 = buf.copy();
    
    assertEquals(16, buf.readableBytes());
    byte fromBuffer[] = new byte[16]; 
    buf.readBytes(fromBuffer);
    assertArrayEquals(expectedWireFormat, fromBuffer);
    
    // Test parsing. TODO: we don't really have the proper parsing
    // infrastructure....
    OFActionNiciraVendor act2 = new OFActionNiciraTtlDecrement();
    act2.readFrom(buf2);
    assertEquals(act, act2);
    assertNotSame(act, act2);
    
    assertEquals(OFActionType.VENDOR, act2.getType());
    assertEquals(16, act2.getLength());
    assertEquals(OFActionNiciraVendor.NICIRA_VENDOR_ID, act2.getVendor());
    assertEquals((short)18, act2.getSubtype());
}
 
Example 12
Source File: NodeDescriptors.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Reads node descriptors Sub-TLVs.
 *
 * @param cb ChannelBuffer
 * @param desLength node descriptor length
 * @param desType local node descriptor or remote node descriptor type
 * @param protocolId protocol ID
 * @return object of NodeDescriptors
 * @throws BgpParseException while parsing node descriptors
 */
public static NodeDescriptors read(ChannelBuffer cb, short desLength, short desType, byte protocolId)
        throws BgpParseException {
    log.debug("Read NodeDescriptor");
    List<BgpValueType> subTlvs = new LinkedList<>();
    BgpValueType tlv = null;

    while (cb.readableBytes() > 0) {
        ChannelBuffer tempBuf = cb.copy();
        short type = cb.readShort();
        short length = cb.readShort();
        if (cb.readableBytes() < length) {
            throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR,
                    tempBuf.readBytes(cb.readableBytes() + TYPE_AND_LEN));
        }
        ChannelBuffer tempCb = cb.readBytes(length);
        switch (type) {
        case AutonomousSystemTlv.TYPE:
            tlv = AutonomousSystemTlv.read(tempCb);
            break;
        case BgpLSIdentifierTlv.TYPE:
            tlv = BgpLSIdentifierTlv.read(tempCb);
            break;
        case AreaIDTlv.TYPE:
            tlv = AreaIDTlv.read(tempCb);
            break;
        case IGP_ROUTERID_TYPE:
            if (protocolId == IS_IS_LEVEL_1_PROTOCOL_ID || protocolId == IS_IS_LEVEL_2_PROTOCOL_ID) {
                boolean isNonPseudoNode = true;
                if ((length == ISISPSEUDONODE_LEN) && (tempCb.getByte(ISISPSEUDONODE_LEN - 1) != 0)) {
                    isNonPseudoNode = false;
                }
                if (isNonPseudoNode) {
                    tlv = IsIsNonPseudonode.read(tempCb);
                } else {
                    tlv = IsIsPseudonode.read(tempCb);
                }
            } else if (protocolId == OSPF_V2_PROTOCOL_ID || protocolId == OSPF_V3_PROTOCOL_ID) {
                if (length == OSPFNONPSEUDONODE_LEN) {
                    tlv = OspfNonPseudonode.read(tempCb);
                } else if (length == OSPFPSEUDONODE_LEN) {
                    tlv = OspfPseudonode.read(tempCb);
                }
            }
            break;
        default:
            UnSupportedAttribute.skipBytes(tempCb, length);
        }
        subTlvs.add(tlv);
    }
    return new NodeDescriptors(subTlvs, desLength, desType);
}
 
Example 13
Source File: BgpLinkLSIdentifier.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Parses link descriptors.
 *
 * @param cb ChannelBuffer
 * @return list of link descriptors
 * @throws BgpParseException while parsing link descriptors
 */
public static LinkedList<BgpValueType> parseLinkDescriptors(ChannelBuffer cb) throws BgpParseException {
    LinkedList<BgpValueType> linkDescriptor = new LinkedList<>();
    BgpValueType tlv = null;
    int count = 0;

    while (cb.readableBytes() > 0) {
        ChannelBuffer tempBuf = cb.copy();
        short type = cb.readShort();
        short length = cb.readShort();
        if (cb.readableBytes() < length) {
            throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR,
                    tempBuf.readBytes(cb.readableBytes() + Constants.TYPE_AND_LEN_AS_SHORT));
        }
        ChannelBuffer tempCb = cb.readBytes(length);
        switch (type) {
        case LinkLocalRemoteIdentifiersTlv.TYPE:
            tlv = LinkLocalRemoteIdentifiersTlv.read(tempCb);
            break;
        case IPV4_INTERFACE_ADDRESS_TYPE:
            tlv = IPv4AddressTlv.read(tempCb, IPV4_INTERFACE_ADDRESS_TYPE);
            break;
        case IPV4_NEIGHBOR_ADDRESS_TYPE:
            tlv = IPv4AddressTlv.read(tempCb, IPV4_NEIGHBOR_ADDRESS_TYPE);
            break;
        case IPV6_INTERFACE_ADDRESS_TYPE:
            tlv = IPv6AddressTlv.read(tempCb, IPV6_INTERFACE_ADDRESS_TYPE);
            break;
        case IPV6_NEIGHBOR_ADDRESS_TYPE:
            tlv = IPv6AddressTlv.read(tempCb, IPV6_NEIGHBOR_ADDRESS_TYPE);
            break;
        case BgpAttrNodeMultiTopologyId.ATTRNODE_MULTITOPOLOGY:
            tlv = BgpAttrNodeMultiTopologyId.read(tempCb, length);
            count++;
            log.debug("MultiTopologyId TLV cannot repeat more than once");
            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
                                + Constants.TYPE_AND_LEN_AS_SHORT));
            }
            break;
        default:
            UnSupportedAttribute.skipBytes(tempCb, length);
        }
        linkDescriptor.add(tlv);
    }
    return linkDescriptor;
}
 
Example 14
Source File: MemcachedCommandDecoder.java    From fqueue with Apache License 2.0 4 votes vote down vote up
/**
 * Process an inbound string from the pipeline's downstream, and depending
 * on the state (waiting for data or processing commands), turn them into
 * the correct type of command.
 * 
 * @param channelHandlerContext
 * @param messageEvent
 * @throws Exception
 */
@Override
public void messageReceived(ChannelHandlerContext channelHandlerContext, MessageEvent messageEvent)
		throws Exception {
	ChannelBuffer in = (ChannelBuffer) messageEvent.getMessage();

	try {
		// Because of the frame handler, we are assured that we are
		// receiving only complete lines or payloads.
		// Verify that we are in 'processing()' mode
		if (status.state == SessionStatus.State.PROCESSING) {
			// split into pieces
			List<String> pieces = new ArrayList<String>(6);
			int pos = in.bytesBefore(space);
			do {
				if (pos != -1) {
					pieces.add(in.toString(in.readerIndex(), pos, USASCII));
					in.skipBytes(pos + 1);
				}
			} while ((pos = in.bytesBefore(space)) != -1);
			pieces.add(in.toString(USASCII));

			processLine(pieces, messageEvent.getChannel(), channelHandlerContext);
		} else if (status.state == SessionStatus.State.PROCESSING_MULTILINE) {
			ChannelBuffer slice = in.copy();
			byte[] payload = slice.array();
			in.skipBytes(in.readableBytes());
			continueSet(messageEvent.getChannel(), status, payload, channelHandlerContext);
		} else {
			throw new InvalidProtocolStateException("invalid protocol state");
		}

	} finally {
		// Now indicate that we need more for this command by changing the
		// session status's state.
		// This instructs the frame decoder to start collecting data for us.
		// Note, we don't do this if we're waiting for data.
		if (status.state != SessionStatus.State.WAITING_FOR_DATA)
			status.ready();
	}
}
 
Example 15
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 16
Source File: As4Path.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Reads from the channel buffer and parses As4Path.
 *
 * @param cb ChannelBuffer
 * @return object of As4Path
 * @throws BgpParseException while parsing As4Path
 */
public static As4Path read(ChannelBuffer cb) throws BgpParseException {
    List<Integer> as4pathSet = new ArrayList<>();
    List<Integer> as4pathSeq = new ArrayList<>();
    ChannelBuffer tempCb = cb.copy();
    Validation validation = Validation.parseAttributeHeader(cb);

    if (cb.readableBytes() < validation.getLength()) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
                validation.getLength());
    }
    //if fourth bit is set length is read as short otherwise as byte , len includes type, length and value
    int len = validation.isShort() ? validation.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : validation
            .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
    ChannelBuffer data = tempCb.readBytes(len);
    if (validation.getFirstBit() && !validation.getSecondBit() && validation.getThirdBit()) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
    }

    ChannelBuffer tempBuf = cb.readBytes(validation.getLength());
    while (tempBuf.readableBytes() > 0) {
        byte pathSegType = tempBuf.readByte();
        //no of ASes
        byte pathSegLen = tempBuf.readByte();
        //length = no of Ases * ASnum size (4 bytes)
        int length = pathSegLen * ASNUM_SIZE;
        if (tempBuf.readableBytes() < length) {
            Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
                    BgpErrorType.ATTRIBUTE_LENGTH_ERROR, length);
        }
        ChannelBuffer aspathBuf = tempBuf.readBytes(length);
        while (aspathBuf.readableBytes() > 0) {
            int asNum;
            asNum = aspathBuf.readInt();
            switch (pathSegType) {
            case AsPath.ASPATH_SET_TYPE:
                as4pathSet.add(asNum);
                break;
            case AsPath.ASPATH_SEQ_TYPE:
                as4pathSeq.add(asNum);
                break;
            default: log.debug("Other type Not Supported:" + pathSegType);
            }
        }
    }
    return new As4Path(as4pathSet, as4pathSeq);
}
 
Example 17
Source File: BgpExtendedCommunity.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Reads from the channel buffer and parses extended community.
 *
 * @param cb ChannelBuffer
 * @return object of BgpExtendedCommunity
 * @throws BgpParseException while parsing extended community
 */
public static BgpExtendedCommunity read(ChannelBuffer cb) throws BgpParseException {

    ChannelBuffer tempCb = cb.copy();
    Validation validation = Validation.parseAttributeHeader(cb);
    List<BgpValueType> fsActionTlvs = new LinkedList<>();

    if (cb.readableBytes() < validation.getLength()) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
                validation.getLength());
    }
    //if fourth bit is set, length is read as short otherwise as byte , len includes type, length and value
    int len = validation.isShort() ? validation.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : validation
            .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
    ChannelBuffer data = tempCb.readBytes(len);
    if (validation.getFirstBit() && !validation.getSecondBit() && validation.getThirdBit()) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
    }

    ChannelBuffer tempBuf = cb.readBytes(validation.getLength());
    if (tempBuf.readableBytes() > 0) {
        BgpValueType fsActionTlv = null;
        ChannelBuffer actionBuf = tempBuf.readBytes(validation.getLength());

        while (actionBuf.readableBytes() > 0) {
            short actionType = actionBuf.readShort();
            switch (actionType) {
                case Constants.BGP_ROUTE_TARGET_AS:
                case Constants.BGP_ROUTE_TARGET_IP:
                case Constants.BGP_ROUTE_TARGET_LARGEAS:
                    fsActionTlv = RouteTarget.read(actionType, actionBuf);
                    break;
                case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_ACTION:
                    fsActionTlv = BgpFsActionTrafficAction.read(actionBuf);
                    break;
                case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_MARKING:
                    fsActionTlv = BgpFsActionTrafficMarking.read(actionBuf);
                    break;
                case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_RATE:
                    fsActionTlv = BgpFsActionTrafficRate.read(actionBuf);
                    break;
                case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_REDIRECT:
                    fsActionTlv = BgpFsActionReDirect.read(actionBuf);
                    break;
                default:
                    log.debug("Other type Not Supported:" + actionType);
                    break;
            }
            if (fsActionTlv != null) {
                fsActionTlvs.add(fsActionTlv);
            }
        }
    }
    return new BgpExtendedCommunity(fsActionTlvs);
}
 
Example 18
Source File: OFBsnPktinSupressionSetRequestVendorDataTest.java    From floodlight_with_topoguard with Apache License 2.0 4 votes vote down vote up
@Test
public void test() {
    ChannelBuffer buf = ChannelBuffers.buffer(32);

    OFBsnPktinSuppressionSetRequestVendorData vendorData =
            new OFBsnPktinSuppressionSetRequestVendorData(
                                                 true,
                                                 (short)0x5a,
                                                 (short)0xf0e0,
                                                 (short)0x1234,
                                                 0x3333666677779999L);
    assertEquals(11, vendorData.getDataType());

    assertEquals(true, vendorData instanceof OFBigSwitchVendorData);

    vendorData.writeTo(buf);

    ChannelBuffer buf2 = buf.copy();
    assertEquals(20, buf.readableBytes());
    byte fromBuffer[] = new byte[20];
    buf.readBytes(fromBuffer);
    assertArrayEquals(expectedWireFormat, fromBuffer);

    OFBsnPktinSuppressionSetRequestVendorData vendorData2 =
            new OFBsnPktinSuppressionSetRequestVendorData();

    assertEquals(11, vendorData2.getDataType());

    vendorData2.setIdleTimeout((short)1);
    assertEquals((short)1, vendorData2.getIdleTimeout());

    vendorData2.setHardTimeout((short)2);
    assertEquals((short)2, vendorData2.getHardTimeout());

    vendorData2.setPriority((short)3);
    assertEquals((short)3, vendorData2.getPriority());

    vendorData2.setCookie(12345678901234L);
    assertEquals(12345678901234L, vendorData2.getCookie());

    vendorData2.readFrom(buf2, buf2.readableBytes());
    assertEquals(vendorData, vendorData2);
}
 
Example 19
Source File: AsPath.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Reads from the channel buffer and parses AsPath.
 *
 * @param cb ChannelBuffer
 * @return object of AsPath
 * @throws BgpParseException while parsing AsPath
 */
public static AsPath read(ChannelBuffer cb) throws BgpParseException {
    List<Short> aspathSet = new ArrayList<>();
    List<Short> aspathSeq = new ArrayList<>();
    ChannelBuffer tempCb = cb.copy();
    Validation validation = Validation.parseAttributeHeader(cb);

    if (cb.readableBytes() < validation.getLength()) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
                validation.getLength());
    }
    //if fourth bit is set, length is read as short otherwise as byte , len includes type, length and value
    int len = validation.isShort() ? validation.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : validation
            .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
    ChannelBuffer data = tempCb.readBytes(len);
    if (validation.getFirstBit() && !validation.getSecondBit() && validation.getThirdBit()) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
    }

    ChannelBuffer tempBuf = cb.readBytes(validation.getLength());
    while (tempBuf.readableBytes() > 0) {
        byte pathSegType = tempBuf.readByte();
        //no of ASes
        byte pathSegLen = tempBuf.readByte();
        int length = pathSegLen * ASNUM_SIZE;
        if (tempBuf.readableBytes() < length) {
            Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
                    BgpErrorType.ATTRIBUTE_LENGTH_ERROR, length);
        }
        ChannelBuffer aspathBuf = tempBuf.readBytes(length);
        while (aspathBuf.readableBytes() > 0) {
            short asNum;
            asNum = aspathBuf.readShort();
            switch (pathSegType) {
            case ASPATH_SET_TYPE:
                aspathSet.add(asNum);
                break;
            case ASPATH_SEQ_TYPE:
                aspathSeq.add(asNum);
                break;
            default: log.debug("Other type Not Supported:" + pathSegType);
            }
        }
    }
    return new AsPath(aspathSet, aspathSeq);
}
 
Example 20
Source File: MemcachedCommandDecoder.java    From fqueue with Apache License 2.0 4 votes vote down vote up
/**
 * Process an inbound string from the pipeline's downstream, and depending
 * on the state (waiting for data or processing commands), turn them into
 * the correct type of command.
 * 
 * @param channelHandlerContext
 * @param messageEvent
 * @throws Exception
 */
@Override
public void messageReceived(ChannelHandlerContext channelHandlerContext, MessageEvent messageEvent)
		throws Exception {
	ChannelBuffer in = (ChannelBuffer) messageEvent.getMessage();

	try {
		// Because of the frame handler, we are assured that we are
		// receiving only complete lines or payloads.
		// Verify that we are in 'processing()' mode
		if (status.state == SessionStatus.State.PROCESSING) {
			// split into pieces
			List<String> pieces = new ArrayList<String>(6);
			int pos = in.bytesBefore(space);
			do {
				if (pos != -1) {
					pieces.add(in.toString(in.readerIndex(), pos, USASCII));
					in.skipBytes(pos + 1);
				}
			} while ((pos = in.bytesBefore(space)) != -1);
			pieces.add(in.toString(USASCII));

			processLine(pieces, messageEvent.getChannel(), channelHandlerContext);
		} else if (status.state == SessionStatus.State.PROCESSING_MULTILINE) {
			ChannelBuffer slice = in.copy();
			byte[] payload = slice.array();
			in.skipBytes(in.readableBytes());
			continueSet(messageEvent.getChannel(), status, payload, channelHandlerContext);
		} else {
			throw new InvalidProtocolStateException("invalid protocol state");
		}

	} finally {
		// Now indicate that we need more for this command by changing the
		// session status's state.
		// This instructs the frame decoder to start collecting data for us.
		// Note, we don't do this if we're waiting for data.
		if (status.state != SessionStatus.State.WAITING_FOR_DATA)
			status.ready();
	}
}