List of usage examples for io.netty.buffer ByteBuf readUnsignedByte
public abstract short readUnsignedByte();
From source file:io.advantageous.conekt.dns.impl.netty.DnsResponseDecoder.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// w ww.j a v a 2s.c o m */ public 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); }
From source file:io.airlift.drift.transport.netty.HeaderMessageEncoding.java
License:Apache License
@Override @SuppressFBWarnings("DLS_DEAD_LOCAL_STORE") public Object readResponse(ByteBuf buffer, int sequenceId, MethodMetadata method) throws Exception { short magic = buffer.readShort(); verify(magic == HEADER_MAGIC, "Unexpected response header magic"); short flags = buffer.readShort(); verify(flags == 1, "Unexpected response header flags"); int frameSequenceId = buffer.readInt(); if (frameSequenceId != sequenceId) { throw new TApplicationException(BAD_SEQUENCE_ID, method.getName() + " failed: out of sequence response"); }// ww w . j a v a 2s . c o m int headerSize = buffer.readShort() << 2; ByteBuf messageHeader = buffer.readBytes(headerSize); int protocolId = messageHeader.readUnsignedByte(); verify(protocolId == this.protocolId, "response protocol is different than request protocol"); int numberOfTransforms = messageHeader.readUnsignedByte(); verify(numberOfTransforms < 128, "Too many transforms for response"); boolean gzipCompressed = false; for (int i = 0; i < numberOfTransforms; i++) { int transform = messageHeader.readUnsignedByte(); verify(transform == 0x01, "Unsupported response transform"); gzipCompressed = true; } // Currently we ignore response headers from the server because there is no API to fetch the headers Map<String, String> normalHeaders = decodeHeaders(NORMAL_HEADERS, messageHeader); Map<String, String> persistentHeaders = decodeHeaders(PERSISTENT_HEADERS, messageHeader); ByteBuf message = buffer.readBytes(buffer.readableBytes()); if (gzipCompressed) { // todo decompress throw new TTransportException("gzip compression not implemented"); } return MessageEncoding.decodeResponse(protocolFactory, message, sequenceId, method); }
From source file:io.hydramq.core.type.converters.PartitionStateConverter.java
License:Open Source License
@Override public PartitionFlags.Flag read(ConversionContext context, ByteBuf buffer) { return PartitionFlags.Flag.valueOf(buffer.readUnsignedByte()); }
From source file:io.netlibs.bgp.netty.codec.OpenPacketDecoder.java
License:Apache License
/** * decode the OPEN network packet. The passed channel buffer MUST point to the first packet octet AFTER the packet type and the buffer * must be at least 9 octets large at this point. * * @param buffer// ww w .jav a 2 s . com * the buffer containing the data. * @return */ public OpenPacket decodeOpenPacket(final ByteBuf buffer) { OpenPacketBuilder b = OpenPacket.builder(); ProtocolPacketUtils.verifyPacketSize(buffer, BGPv4Constants.BGP_PACKET_MIN_SIZE_OPEN, -1); short version = buffer.readUnsignedByte(); if (version != BGPv4Constants.BGP_VERSION) { throw new UnsupportedVersionNumberException(BGPv4Constants.BGP_VERSION); } b.protocolVersion(version); b.autonomousSystem(buffer.readUnsignedShort()); b.holdTime(buffer.readUnsignedShort()); long identifier = buffer.readUnsignedInt(); if ((identifier & IPV4_MULTICAST_MASK) == IPV4_MULTICAST_MASK) { throw new BadBgpIdentifierException(); } b.bgpIdentifier(identifier); final int parameterLength = buffer.readUnsignedByte(); if (parameterLength > 0) { while (buffer.isReadable()) { final int parameterType = buffer.readUnsignedByte(); final int paramLength = buffer.readUnsignedByte(); final ByteBuf valueBuffer = Unpooled.buffer(paramLength); buffer.readBytes(valueBuffer); switch (parameterType) { case BGPv4Constants.BGP_OPEN_PARAMETER_TYPE_AUTH: log.warn("Ignoring auth parameter"); // RFC 4271 says auth is deprecated. break; case BGPv4Constants.BGP_OPEN_PARAMETER_TYPE_CAPABILITY: b.capabilities(CapabilityCodec.decodeCapabilities(valueBuffer)); break; default: throw new UnsupportedOptionalParameterException(); } } } return b.build(); }
From source file:io.netlibs.bgp.netty.codec.RouteRefreshPacketDecoder.java
License:Apache License
/** * decode the REFRESH network packet. The passed channel buffer MUST point to the first packet octet AFTER the type octet. * /* w ww .ja v a2s . com*/ * @param buffer * the buffer containing the data. * * @return the decoded packet or null on decoding problems. Neither RFC2918 nor RFC5291 nor RFC4271 describe an error handling procedure, * so best advise is to ignore invalid packets for now. */ public BGPv4Packet decodeRouteRefreshPacket(ByteBuf buffer) { RouteRefreshPacket packet = null; try { AddressFamily af = AddressFamily.fromCode(buffer.readUnsignedShort()); buffer.readByte(); // swallow reserved octet SubsequentAddressFamily saf = SubsequentAddressFamily.fromCode(buffer.readUnsignedByte()); packet = new RouteRefreshPacket(af, saf); if (buffer.isReadable()) { // we have outbound router filter rules here OutboundRouteFilter orf = new OutboundRouteFilter(af, saf); orf.setRefreshType(ORFRefreshType.fromCode(buffer.readUnsignedByte())); while (buffer.isReadable()) { ORFType orfType = ORFType.fromCode(buffer.readUnsignedByte()); ByteBuf entriesBuffer = Unpooled.buffer(buffer.readUnsignedShort()); buffer.readBytes(entriesBuffer); orf.addAllORFEntries(decodeORFEntries(entriesBuffer, orfType)); } packet.setOutboundRouteFilter(orf); } } catch (Exception e) { log.error("cannot decode ROUTE_REFRESH packet, suppressing it from further processing", e); packet = null; } return packet; }
From source file:io.netlibs.bgp.netty.codec.RouteRefreshPacketDecoder.java
License:Apache License
private List<ORFEntry> decodeORFEntries(ByteBuf buffer, ORFType orfType) { List<ORFEntry> entries = new LinkedList<ORFEntry>(); while (buffer.isReadable()) { int actionMatch = buffer.readUnsignedByte(); ORFAction action = ORFAction.fromCode((actionMatch >> 6) & 0x03); ORFMatch match = ORFMatch.fromCode((actionMatch >> 5) & 0x01); switch (orfType) { case ADDRESS_PREFIX_BASED: entries.add(decodeAddressPrefixBasedORFEntry(buffer, action, match)); break; default:/*ww w . j a v a2 s. c o m*/ throw new IllegalArgumentException("cannot decode OutboudRouteFilter entries of type " + orfType); } } return entries; }
From source file:io.netlibs.bgp.netty.codec.RouteRefreshPacketDecoder.java
License:Apache License
private ORFEntry decodeAddressPrefixBasedORFEntry(ByteBuf buffer, ORFAction action, ORFMatch match) { AddressPrefixBasedORFEntry entry = new AddressPrefixBasedORFEntry(action, match); if (action != ORFAction.REMOVE_ALL) { entry.setSequence((int) buffer.readUnsignedInt()); entry.setMinLength(buffer.readUnsignedByte()); entry.setMaxLength(buffer.readUnsignedByte()); entry.setPrefix(NLRICodec.decodeNLRI(buffer)); }//from w w w .j av a 2 s . co m return entry; }
From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java
License:Apache License
private ASPathAttribute decodeASPathAttribute(final ByteBuf buffer, final ASType asType) { final ASPathAttribute attr = new ASPathAttribute(asType); while (buffer.isReadable()) { if (buffer.readableBytes() < 2) { throw new MalformedASPathAttributeException(); }/* www.j av a 2 s . c o m*/ final int segmentType = buffer.readUnsignedByte(); final int pathLength = buffer.readUnsignedByte(); final int pathOctetLength = (pathLength * (asType == ASType.AS_NUMBER_4OCTETS ? 4 : 2)); if (buffer.readableBytes() < pathOctetLength) { throw new MalformedASPathAttributeException(); } final PathSegment segment = new PathSegment(asType); try { segment.setPathSegmentType(PathSegmentTypeCodec.fromCode(segmentType)); } catch (final IllegalArgumentException e) { throw new MalformedASPathAttributeException(); } for (int i = 0; i < pathLength; i++) { if (asType == ASType.AS_NUMBER_4OCTETS) { segment.getAses().add((int) buffer.readUnsignedInt()); } else { segment.getAses().add(buffer.readUnsignedShort()); } } attr.getPathSegments().add(segment); } return attr; }
From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java
License:Apache License
private OriginPathAttribute decodeOriginPathAttribute(final ByteBuf buffer) { final OriginPathAttribute attr = new OriginPathAttribute(); if (buffer.readableBytes() != 1) { throw new AttributeLengthException(); }//from w w w. j a va2 s .c om try { attr.setOrigin(OriginCodec.fromCode(buffer.readUnsignedByte())); } catch (final IllegalArgumentException e) { throw new InvalidOriginException(); } return attr; }
From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java
License:Apache License
private MultiProtocolReachableNLRI decodeMpReachNlriPathAttribute(final ByteBuf buffer) { final MultiProtocolReachableNLRI attr = new MultiProtocolReachableNLRI(); try {// w w w . j a v a 2 s . c o m attr.setAddressFamily(AddressFamily.fromCode(buffer.readUnsignedShort())); attr.setSubsequentAddressFamily(SubsequentAddressFamily.fromCode(buffer.readUnsignedByte())); final int nextHopLength = buffer.readUnsignedByte(); if (nextHopLength > 0) { final byte[] nextHop = new byte[nextHopLength]; buffer.readBytes(nextHop); attr.setNextHopAddress(nextHop); } buffer.readByte(); // reserved while (buffer.isReadable()) { attr.getNlris().add(NLRICodec.decodeNLRI(buffer)); } } catch (final RuntimeException e) { throw new OptionalAttributeErrorException(); } return attr; }