List of usage examples for io.netty.buffer ByteBuf readUnsignedByte
public abstract short readUnsignedByte();
From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java
License:Apache License
private MultiProtocolUnreachableNLRI decodeMpUnreachNlriPathAttribute(final ByteBuf buffer) { final MultiProtocolUnreachableNLRI attr = new MultiProtocolUnreachableNLRI(); try {//from w w w. jav a 2s. c o m attr.setAddressFamily(AddressFamily.fromCode(buffer.readUnsignedShort())); attr.setSubsequentAddressFamily(SubsequentAddressFamily.fromCode(buffer.readUnsignedByte())); while (buffer.isReadable()) { attr.getNlris().add(NLRICodec.decodeNLRI(buffer)); } } catch (final RuntimeException e) { throw new OptionalAttributeErrorException(); } return attr; }
From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java
License:Apache License
private List<PathAttribute> decodePathAttributes(final ByteBuf buffer) { final List<PathAttribute> attributes = new LinkedList<PathAttribute>(); while (buffer.isReadable()) { buffer.markReaderIndex();/*from w w w . j ava 2 s. c om*/ try { final int flagsType = buffer.readUnsignedShort(); final boolean optional = ((flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_OPTIONAL_BIT) != 0); final boolean transitive = ((flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_TRANSITIVE_BIT) != 0); final boolean partial = ((flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_PARTIAL_BIT) != 0); final int typeCode = (flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_MASK); int valueLength = 0; if ((flagsType & BGPv4Constants.BGP_PATH_ATTRIBUTE_EXTENDED_LENGTH_BIT) != 0) { valueLength = buffer.readUnsignedShort(); } else { valueLength = buffer.readUnsignedByte(); } // final ByteBuf valueBuffer = Unpooled.buffer(valueLength); // buffer.readBytes(valueBuffer); final ByteBuf valueBuffer = buffer.readBytes(valueLength); PathAttribute attr = null; switch (typeCode) { case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_AGGREGATOR: attr = this.decodeAggregatorPathAttribute(valueBuffer, ASType.AS_NUMBER_2OCTETS); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_AS4_AGGREGATOR: attr = this.decodeAggregatorPathAttribute(valueBuffer, ASType.AS_NUMBER_4OCTETS); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_AS4_PATH: attr = this.decodeASPathAttribute(valueBuffer, ASType.AS_NUMBER_4OCTETS); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_AS_PATH: attr = this.decodeASPathAttribute(valueBuffer, ASType.AS_NUMBER_2OCTETS); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_ATOMIC_AGGREGATE: attr = this.decodeAtomicAggregatePathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_COMMUNITIES: attr = this.decodeCommunityPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_LOCAL_PREF: attr = this.decodeLocalPrefPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_MULTI_EXIT_DISC: attr = this.decodeMultiExitDiscPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_NEXT_HOP: attr = this.decodeNextHopPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_ORIGIN: attr = this.decodeOriginPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_MP_REACH_NLRI: attr = this.decodeMpReachNlriPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_MP_UNREACH_NLRI: attr = this.decodeMpUnreachNlriPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_ORIGINATOR_ID: attr = this.decodeOriginatorIDPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_CLUSTER_LIST: attr = this.decodeClusterListPathAttribute(valueBuffer); break; case BGPv4Constants.BGP_PATH_ATTRIBUTE_TYPE_EXTENDED_COMMUNITIES: attr = this.decodeExtendedCommunityPathAttribute(valueBuffer); break; default: { final byte[] value = new byte[valueBuffer.readableBytes()]; valueBuffer.readBytes(value); attr = new UnknownPathAttribute(typeCode, value); } break; } attr.setOptional(optional); attr.setTransitive(transitive); attr.setPartial(partial); attributes.add(attr); } catch (final AttributeException ex) { final int endReadIndex = buffer.readerIndex(); buffer.resetReaderIndex(); final int attributeLength = endReadIndex - buffer.readerIndex(); final byte[] packet = new byte[attributeLength]; buffer.readBytes(packet); ex.setOffendingAttribute(packet); throw ex; } catch (final IndexOutOfBoundsException ex) { // this is almost certinally an internal error with parsing .... final int endReadIndex = buffer.readerIndex(); buffer.resetReaderIndex(); final int attributeLength = endReadIndex - buffer.readerIndex(); final byte[] packet = new byte[attributeLength]; buffer.readBytes(packet); throw new AttributeLengthException(ex, packet); } } return attributes; }
From source file:io.netlibs.bgp.netty.protocol.open.CapabilityCodec.java
License:Apache License
public static Capability decodeCapability(final ByteBuf buffer) { Capability cap = null;/* ww w.ja v a 2 s .c o m*/ try { buffer.markReaderIndex(); final int type = buffer.readUnsignedByte(); switch (type) { case BGPv4Constants.BGP_CAPABILITY_TYPE_MULTIPROTOCOL: cap = decodeMultiProtocolCapability(buffer); break; case BGPv4Constants.BGP_CAPABILITY_TYPE_ROUTE_REFRESH: cap = decodeRouteRefreshCapability(buffer); break; case BGPv4Constants.BGP_CAPABILITY_TYPE_AS4_NUMBERS: cap = decodeAutonomousSystem4Capability(buffer); break; case BGPv4Constants.BGP_CAPABILITY_TYPE_OUTBOUND_ROUTE_FILTERING: cap = decodeOutboundRouteFilteringCapability(buffer); break; default: cap = decodeUnknownCapability(type, buffer); break; } } catch (final CapabilityException e) { buffer.resetReaderIndex(); final int type = buffer.readUnsignedByte(); final int capLength = buffer.readUnsignedByte(); final byte[] capPacket = new byte[capLength + 2]; buffer.readBytes(capPacket, 2, capLength); capPacket[0] = (byte) type; capPacket[1] = (byte) capLength; e.setCapability(capPacket); throw e; } return cap; }
From source file:io.netlibs.bgp.netty.protocol.open.CapabilityCodec.java
License:Apache License
private static Capability decodeUnknownCapability(final int type, final ByteBuf buffer) { final UnknownCapability cap = new UnknownCapability(); cap.setCapabilityType(type);/*from ww w . j ava 2 s . co m*/ final int parameterLength = buffer.readUnsignedByte(); if (parameterLength > 0) { final byte[] value = new byte[parameterLength]; buffer.readBytes(value); cap.setValue(value); } return cap; }
From source file:io.netlibs.bgp.netty.protocol.open.CapabilityCodec.java
License:Apache License
private static Capability decodeOutboundRouteFilteringCapability(final ByteBuf buffer) { final OutboundRouteFilteringCapability cap = new OutboundRouteFilteringCapability(); assertMinimalLength(buffer, 5); // 2 octest AFI + 1 octet reserved + 1 octet SAFI + 1 octet number of (ORF type, Send/Receive) tuples cap.setAddressFamily(AddressFamily.fromCode(buffer.readUnsignedShort())); buffer.readByte();/*from w w w. j a v a 2 s . c om*/ cap.setSubsequentAddressFamily(SubsequentAddressFamily.fromCode(buffer.readUnsignedByte())); final int orfs = buffer.readUnsignedByte(); if (buffer.readableBytes() != (2 * orfs)) { throw new UnspecificOpenPacketException( "Expected " + (2 * orfs) + " octets parameter, got " + buffer.readableBytes() + " octets"); } try { cap.getFilters().put(ORFType.fromCode(buffer.readUnsignedByte()), ORFSendReceive.fromCode(buffer.readUnsignedByte())); } catch (final IllegalArgumentException e) { throw new UnspecificOpenPacketException(e); } return cap; }
From source file:io.netlibs.bgp.netty.protocol.open.CapabilityCodec.java
License:Apache License
private static final void assertEmptyParameter(final ByteBuf buffer) { final int parameterLength = buffer.readUnsignedByte(); if (parameterLength != 0) { throw new UnspecificOpenPacketException( "Expected zero-length parameter, got " + parameterLength + " octets"); }/* www. jav a2s . co m*/ }
From source file:io.netlibs.bgp.netty.protocol.open.CapabilityCodec.java
License:Apache License
private static final void assertFixedLength(final ByteBuf buffer, final int length) { final int parameterLength = buffer.readUnsignedByte(); if (parameterLength != length) { throw new UnspecificOpenPacketException( "Expected " + length + " octets parameter, got " + parameterLength + " octets"); }// w w w. j a va 2s . c om }
From source file:io.netlibs.bgp.netty.protocol.open.CapabilityCodec.java
License:Apache License
private static final void assertMinimalLength(final ByteBuf buffer, final int length) { final int parameterLength = buffer.readUnsignedByte(); if (parameterLength < length) { throw new UnspecificOpenPacketException( "Expected " + length + " octets parameter, got " + parameterLength + " octets"); }/* w w w . ja v a 2 s . c om*/ }
From source file:io.netlibs.bgp.protocol.NLRICodec.java
License:Apache License
/** * *//*from w ww .j ava 2 s . c o m*/ public static NetworkLayerReachabilityInformation decodeNLRI(final ByteBuf buffer) { final NetworkLayerReachabilityInformation nlri = new NetworkLayerReachabilityInformation(); final int prefixLength = buffer.readUnsignedByte(); byte[] prefixBytes = null; if (prefixLength > 0) { prefixBytes = new byte[NetworkLayerReachabilityInformation .calculateOctetsForPrefixLength(prefixLength)]; buffer.readBytes(prefixBytes); } nlri.setPrefix(prefixLength, prefixBytes); return nlri; }
From source file:io.vertx.core.dns.impl.decoder.RecordDecoder.java
License:Open Source License
/** * Retrieves a domain name given a buffer containing a DNS packet. If the * name contains a pointer, the position of the buffer will be set to * directly after the pointer's index after the name has been read. * * @param buf the byte buffer containing the DNS packet * @return the domain name for an entry//from ww w . j a v a2s.com */ static String readName(ByteBuf buf) { int position = -1; StringBuilder name = new StringBuilder(); for (int len = buf.readUnsignedByte(); buf.isReadable() && len != 0; len = buf.readUnsignedByte()) { boolean pointer = (len & 0xc0) == 0xc0; if (pointer) { if (position == -1) { position = buf.readerIndex() + 1; } buf.readerIndex((len & 0x3f) << 8 | buf.readUnsignedByte()); } else { name.append(buf.toString(buf.readerIndex(), len, CharsetUtil.UTF_8)).append("."); buf.skipBytes(len); } } if (position != -1) { buf.readerIndex(position); } if (name.length() == 0) { return null; } return name.substring(0, name.length() - 1); }