Example usage for io.netty.buffer ByteBuf readUnsignedInt

List of usage examples for io.netty.buffer ByteBuf readUnsignedInt

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readUnsignedInt.

Prototype

public abstract long readUnsignedInt();

Source Link

Document

Gets an unsigned 32-bit integer at the current readerIndex and increases the readerIndex by 4 in this buffer.

Usage

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//from   w  ww.  j  a va2  s .  c  om
 *          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

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));
    }// w  w  w  .  j  a  v  a  2  s .  c  o  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();
        }/*from w  ww  .  ja v a  2  s .c om*/

        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 MultiExitDiscPathAttribute decodeMultiExitDiscPathAttribute(final ByteBuf buffer) {
    final MultiExitDiscPathAttribute attr = new MultiExitDiscPathAttribute();

    if (buffer.readableBytes() != 4) {
        throw new AttributeLengthException();
    }//from w w  w  .j a va  2  s  . c o m

    attr.setDiscriminator((int) buffer.readUnsignedInt());

    return attr;
}

From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java

License:Apache License

private LocalPrefPathAttribute decodeLocalPrefPathAttribute(final ByteBuf buffer) {
    final LocalPrefPathAttribute attr = new LocalPrefPathAttribute();

    if (buffer.readableBytes() != 4) {
        throw new AttributeLengthException();
    }/*ww  w  . ja va 2 s  .c  o  m*/

    attr.setLocalPreference((int) buffer.readUnsignedInt());

    return attr;
}

From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java

License:Apache License

private AggregatorPathAttribute decodeAggregatorPathAttribute(final ByteBuf buffer, final ASType asType) {
    final AggregatorPathAttribute attr = new AggregatorPathAttribute(asType);

    if (buffer.readableBytes() != PathAttributeCodec.valueLength(attr)) {
        throw new AttributeLengthException();
    }//from   w  ww. jav  a  2 s  . c o m

    if (asType == ASType.AS_NUMBER_4OCTETS) {
        attr.setAsNumber((int) buffer.readUnsignedInt());
    } else {
        attr.setAsNumber(buffer.readUnsignedShort());
    }

    try {
        final byte[] addr = new byte[4];

        buffer.readBytes(addr);
        attr.setAggregator((Inet4Address) InetAddress.getByAddress(addr));
    } catch (final UnknownHostException e) {
        throw new OptionalAttributeErrorException();
    }

    return attr;
}

From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java

License:Apache License

private OriginatorIDPathAttribute decodeOriginatorIDPathAttribute(final ByteBuf buffer) {
    final OriginatorIDPathAttribute attr = new OriginatorIDPathAttribute();

    try {//from  w  w  w  . j  ava2  s. com
        attr.setOriginatorID((int) buffer.readUnsignedInt());
    } catch (final RuntimeException e) {
        throw new OptionalAttributeErrorException();
    }

    return attr;
}

From source file:io.netlibs.bgp.netty.codec.UpdatePacketDecoder.java

License:Apache License

private ClusterListPathAttribute decodeClusterListPathAttribute(final ByteBuf buffer) {
    final ClusterListPathAttribute attr = new ClusterListPathAttribute();

    try {/*from  w w  w  .  j  a v  a  2  s. c om*/
        while (buffer.isReadable()) {
            attr.getClusterIds().add((int) buffer.readUnsignedInt());
        }
    } catch (final RuntimeException e) {
        throw new OptionalAttributeErrorException();
    }
    return attr;
}

From source file:io.netlibs.bgp.netty.protocol.open.CapabilityCodec.java

License:Apache License

private static Capability decodeAutonomousSystem4Capability(final ByteBuf buffer) {
    final AutonomousSystem4Capability cap = new AutonomousSystem4Capability();

    assertFixedLength(buffer, BGPv4Constants.BGP_CAPABILITY_LENGTH_AS4_NUMBERS);
    cap.setAutonomousSystem((int) buffer.readUnsignedInt());

    return cap;// www .j  a v a  2 s.c om
}

From source file:me.melchor9000.net.resolver.DNSResourceRecord.java

License:Open Source License

@Override
public void fromByteBuf(@NotNull ByteBuf buffer) throws DataNotRepresentsObject {
    name = readName(buffer);//  w w  w  .j  a v a  2s  . c o  m
    type = rs(buffer);
    nclass = rs(buffer);
    if (buffer.readableBytes() >= 4)
        ttl = buffer.readUnsignedInt();
    else
        throw new DataNotRepresentsObject("Incomplete", buffer);
    data = DNSResourceData.forData(type, buffer);
}