Java Code Examples for org.jboss.netty.buffer.ChannelBuffer#readUnsignedByte()
The following examples show how to use
org.jboss.netty.buffer.ChannelBuffer#readUnsignedByte() .
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: BgpPeerFrameDecoderTest.java From onos with Apache License 2.0 | 5 votes |
/** * 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 2
Source File: FpmFrameDecoder.java From onos with Apache License 2.0 | 5 votes |
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { if (!channel.isConnected()) { return null; } if (buffer.readableBytes() < FpmHeader.FPM_HEADER_LENGTH) { return null; } buffer.markReaderIndex(); short version = buffer.readUnsignedByte(); short type = buffer.readUnsignedByte(); int length = buffer.readUnsignedShort(); buffer.resetReaderIndex(); if (buffer.readableBytes() < length) { // Not enough bytes to read a whole message return null; } byte[] fpmMessage = new byte[length]; buffer.readBytes(fpmMessage); return FpmHeader.decode(fpmMessage, 0, fpmMessage.length); }
Example 3
Source File: BgpUpdate.java From onos with Apache License 2.0 | 5 votes |
/** * Parses a message that contains encoded IPv4 network prefixes. * <p> * The IPv4 prefixes are encoded in the form: * <Length, Prefix> where Length is the length in bits of the IPv4 prefix, * and Prefix is the IPv4 prefix (padded with trailing bits to the end * of an octet). * * @param totalLength the total length of the data to parse * @param message the message with data to parse * @return a collection of parsed IPv4 network prefixes * @throws BgpMessage.BgpParseException */ private static Collection<Ip4Prefix> parsePackedIp4Prefixes( int totalLength, ChannelBuffer message) throws BgpMessage.BgpParseException { Collection<Ip4Prefix> result = new ArrayList<>(); if (totalLength == 0) { return result; } // Parse the data byte[] buffer = new byte[Ip4Address.BYTE_LENGTH]; int dataEnd = message.readerIndex() + totalLength; while (message.readerIndex() < dataEnd) { int prefixBitlen = message.readUnsignedByte(); int prefixBytelen = (prefixBitlen + 7) / 8; // Round-up if (message.readerIndex() + prefixBytelen > dataEnd) { String errorMsg = "Malformed Network Prefixes"; throw new BgpMessage.BgpParseException(errorMsg); } message.readBytes(buffer, 0, prefixBytelen); Ip4Prefix prefix = Ip4Prefix.valueOf(Ip4Address.valueOf(buffer), prefixBitlen); result.add(prefix); } return result; }
Example 4
Source File: BgpUpdate.java From onos with Apache License 2.0 | 5 votes |
/** * Parses a message that contains encoded IPv6 network prefixes. * <p> * The IPv6 prefixes are encoded in the form: * <Length, Prefix> where Length is the length in bits of the IPv6 prefix, * and Prefix is the IPv6 prefix (padded with trailing bits to the end * of an octet). * * @param totalLength the total length of the data to parse * @param message the message with data to parse * @return a collection of parsed IPv6 network prefixes * @throws BgpMessage.BgpParseException */ private static Collection<Ip6Prefix> parsePackedIp6Prefixes( int totalLength, ChannelBuffer message) throws BgpMessage.BgpParseException { Collection<Ip6Prefix> result = new ArrayList<>(); if (totalLength == 0) { return result; } // Parse the data byte[] buffer = new byte[Ip6Address.BYTE_LENGTH]; int dataEnd = message.readerIndex() + totalLength; while (message.readerIndex() < dataEnd) { int prefixBitlen = message.readUnsignedByte(); int prefixBytelen = (prefixBitlen + 7) / 8; // Round-up if (message.readerIndex() + prefixBytelen > dataEnd) { String errorMsg = "Malformed Network Prefixes"; throw new BgpMessage.BgpParseException(errorMsg); } message.readBytes(buffer, 0, prefixBytelen); Ip6Prefix prefix = Ip6Prefix.valueOf(Ip6Address.valueOf(buffer), prefixBitlen); result.add(prefix); } return result; }
Example 5
Source File: BgpNotification.java From onos with Apache License 2.0 | 5 votes |
/** * Processes BGP NOTIFICATION message. * * @param bgpSession the BGP Session to use * @param ctx the Channel Handler Context * @param message the message to process */ static void processBgpNotification(BgpSession bgpSession, ChannelHandlerContext ctx, ChannelBuffer message) { int minLength = BgpConstants.BGP_NOTIFICATION_MIN_LENGTH - BgpConstants.BGP_HEADER_LENGTH; if (message.readableBytes() < minLength) { log.debug("BGP RX NOTIFICATION Error from {}: " + "Message length {} too short. Must be at least {}", bgpSession.remoteInfo().address(), message.readableBytes(), minLength); // // ERROR: Bad Message Length // // NOTE: We do NOT send NOTIFICATION in response to a notification return; } // // Parse the NOTIFICATION message // int errorCode = message.readUnsignedByte(); int errorSubcode = message.readUnsignedByte(); int dataLength = message.readableBytes(); log.debug("BGP RX NOTIFICATION message from {}: Error Code {} " + "Error Subcode {} Data Length {}", bgpSession.remoteInfo().address(), errorCode, errorSubcode, dataLength); // // NOTE: If the peer sent a NOTIFICATION, we leave it to the peer to // close the connection. // // Start the Session Timeout timer bgpSession.restartSessionTimeoutTimer(ctx); }
Example 6
Source File: TestBgpPeerFrameDecoder.java From onos with Apache License 2.0 | 5 votes |
/** * Processes BGP OPEN message. * * @param ctx the Channel Handler Context. * @param message the message to process. */ private void processBgpOpen(ChannelHandlerContext ctx, ChannelBuffer message) { int minLength = BgpConstants.BGP_OPEN_MIN_LENGTH - BgpConstants.BGP_HEADER_LENGTH; if (message.readableBytes() < minLength) { // ERROR: Bad Message Length. Close the channel. ctx.getChannel().close(); return; } // // Parse the OPEN message // remoteInfo.setBgpVersion(message.readUnsignedByte()); remoteInfo.setAsNumber(message.readUnsignedShort()); remoteInfo.setHoldtime(message.readUnsignedShort()); remoteInfo.setBgpId(Ip4Address.valueOf((int) message.readUnsignedInt())); // Optional Parameters int optParamLen = message.readUnsignedByte(); if (message.readableBytes() < optParamLen) { // ERROR: Bad Message Length. Close the channel. ctx.getChannel().close(); return; } message.readBytes(optParamLen); // NOTE: data ignored // BGP OPEN message successfully received receivedOpenMessageLatch.countDown(); }
Example 7
Source File: MemcachedBinaryCommandDecoder.java From fqueue with Apache License 2.0 | 4 votes |
protected Object decode(ChannelHandlerContext channelHandlerContext, Channel channel, ChannelBuffer channelBuffer) throws Exception { // need at least 24 bytes, to get header if (channelBuffer.readableBytes() < 24) return null; // get the header channelBuffer.markReaderIndex(); ChannelBuffer headerBuffer = ChannelBuffers.buffer(ByteOrder.BIG_ENDIAN, 24); channelBuffer.readBytes(headerBuffer); short magic = headerBuffer.readUnsignedByte(); // magic should be 0x80 if (magic != 0x80) { headerBuffer.resetReaderIndex(); throw new MalformedCommandException("binary request payload is invalid, magic byte incorrect"); } short opcode = headerBuffer.readUnsignedByte(); short keyLength = headerBuffer.readShort(); short extraLength = headerBuffer.readUnsignedByte(); short dataType = headerBuffer.readUnsignedByte(); // unused short reserved = headerBuffer.readShort(); // unused int totalBodyLength = headerBuffer.readInt(); int opaque = headerBuffer.readInt(); long cas = headerBuffer.readLong(); // we want the whole of totalBodyLength; otherwise, keep waiting. if (channelBuffer.readableBytes() < totalBodyLength) { channelBuffer.resetReaderIndex(); return null; } // This assumes correct order in the enum. If that ever changes, we will have to scan for 'code' field. BinaryCommand bcmd = BinaryCommand.values()[opcode]; Command cmdType = bcmd.correspondingCommand; CommandMessage cmdMessage = CommandMessage.command(cmdType); cmdMessage.noreply = bcmd.noreply; cmdMessage.cas_key = cas; cmdMessage.opaque = opaque; cmdMessage.addKeyToResponse = bcmd.addKeyToResponse; // get extras. could be empty. ChannelBuffer extrasBuffer = ChannelBuffers.buffer(ByteOrder.BIG_ENDIAN, extraLength); channelBuffer.readBytes(extrasBuffer); // get the key if any if (keyLength != 0) { ChannelBuffer keyBuffer = ChannelBuffers.buffer(ByteOrder.BIG_ENDIAN, keyLength); channelBuffer.readBytes(keyBuffer); ArrayList<String> keys = new ArrayList<String>(); String key = keyBuffer.toString(USASCII); keys.add(key); // TODO this or UTF-8? ISO-8859-1? cmdMessage.keys = keys; if (cmdType == Command.ADD || cmdType == Command.SET || cmdType == Command.REPLACE || cmdType == Command.APPEND || cmdType == Command.PREPEND) { // TODO these are backwards from the spec, but seem to be what spymemcached demands -- which has the mistake?! short expire = (short) (extrasBuffer.capacity() != 0 ? extrasBuffer.readUnsignedShort() : 0); short flags = (short) (extrasBuffer.capacity() != 0 ? extrasBuffer.readUnsignedShort() : 0); // the remainder of the message -- that is, totalLength - (keyLength + extraLength) should be the payload int size = totalBodyLength - keyLength - extraLength; cmdMessage.element = new LocalCacheElement(key, flags, expire != 0 && expire < CacheElement.THIRTY_DAYS ? LocalCacheElement.Now() + expire : expire, 0L); cmdMessage.element.setData(new byte[size]); channelBuffer.readBytes(cmdMessage.element.getData(), 0, size); } else if (cmdType == Command.INCR || cmdType == Command.DECR) { long initialValue = extrasBuffer.readUnsignedInt(); long amount = extrasBuffer.readUnsignedInt(); long expiration = extrasBuffer.readUnsignedInt(); cmdMessage.incrAmount = (int) amount; cmdMessage.incrDefault = (int) initialValue; cmdMessage.incrExpiry = (int) expiration; } } return cmdMessage; }
Example 8
Source File: MemcachedBinaryCommandDecoder.java From fqueue with Apache License 2.0 | 4 votes |
protected Object decode(ChannelHandlerContext channelHandlerContext, Channel channel, ChannelBuffer channelBuffer) throws Exception { // need at least 24 bytes, to get header if (channelBuffer.readableBytes() < 24) return null; // get the header channelBuffer.markReaderIndex(); ChannelBuffer headerBuffer = ChannelBuffers.buffer(ByteOrder.BIG_ENDIAN, 24); channelBuffer.readBytes(headerBuffer); short magic = headerBuffer.readUnsignedByte(); // magic should be 0x80 if (magic != 0x80) { headerBuffer.resetReaderIndex(); throw new MalformedCommandException("binary request payload is invalid, magic byte incorrect"); } short opcode = headerBuffer.readUnsignedByte(); short keyLength = headerBuffer.readShort(); short extraLength = headerBuffer.readUnsignedByte(); short dataType = headerBuffer.readUnsignedByte(); // unused short reserved = headerBuffer.readShort(); // unused int totalBodyLength = headerBuffer.readInt(); int opaque = headerBuffer.readInt(); long cas = headerBuffer.readLong(); // we want the whole of totalBodyLength; otherwise, keep waiting. if (channelBuffer.readableBytes() < totalBodyLength) { channelBuffer.resetReaderIndex(); return null; } // This assumes correct order in the enum. If that ever changes, we will have to scan for 'code' field. BinaryCommand bcmd = BinaryCommand.values()[opcode]; Command cmdType = bcmd.correspondingCommand; CommandMessage cmdMessage = CommandMessage.command(cmdType); cmdMessage.noreply = bcmd.noreply; cmdMessage.cas_key = cas; cmdMessage.opaque = opaque; cmdMessage.addKeyToResponse = bcmd.addKeyToResponse; // get extras. could be empty. ChannelBuffer extrasBuffer = ChannelBuffers.buffer(ByteOrder.BIG_ENDIAN, extraLength); channelBuffer.readBytes(extrasBuffer); // get the key if any if (keyLength != 0) { ChannelBuffer keyBuffer = ChannelBuffers.buffer(ByteOrder.BIG_ENDIAN, keyLength); channelBuffer.readBytes(keyBuffer); ArrayList<String> keys = new ArrayList<String>(); String key = keyBuffer.toString(USASCII); keys.add(key); // TODO this or UTF-8? ISO-8859-1? cmdMessage.keys = keys; if (cmdType == Command.ADD || cmdType == Command.SET || cmdType == Command.REPLACE || cmdType == Command.APPEND || cmdType == Command.PREPEND) { // TODO these are backwards from the spec, but seem to be what spymemcached demands -- which has the mistake?! short expire = (short) (extrasBuffer.capacity() != 0 ? extrasBuffer.readUnsignedShort() : 0); short flags = (short) (extrasBuffer.capacity() != 0 ? extrasBuffer.readUnsignedShort() : 0); // the remainder of the message -- that is, totalLength - (keyLength + extraLength) should be the payload int size = totalBodyLength - keyLength - extraLength; cmdMessage.element = new LocalCacheElement(key, flags, expire != 0 && expire < CacheElement.THIRTY_DAYS ? LocalCacheElement.Now() + expire : expire, 0L); cmdMessage.element.setData(new byte[size]); channelBuffer.readBytes(cmdMessage.element.getData(), 0, size); } else if (cmdType == Command.INCR || cmdType == Command.DECR) { long initialValue = extrasBuffer.readUnsignedInt(); long amount = extrasBuffer.readUnsignedInt(); long expiration = extrasBuffer.readUnsignedInt(); cmdMessage.incrAmount = (int) amount; cmdMessage.incrDefault = (int) initialValue; cmdMessage.incrExpiry = (int) expiration; } } return cmdMessage; }
Example 9
Source File: LsPdu.java From onos with Apache License 2.0 | 4 votes |
@Override public void readFrom(ChannelBuffer channelBuffer) { this.setPduLength(channelBuffer.readUnsignedShort()); this.setRemainingLifeTime(channelBuffer.readUnsignedShort()); //lsp id + 2 value byte[] tempByteArray = new byte[IsisUtil.ID_PLUS_TWO_BYTE]; channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_TWO_BYTE); this.setLspId(IsisUtil.systemIdPlus(tempByteArray)); //sequence number 4 this.setSequenceNumber(channelBuffer.readInt()); this.setCheckSum(channelBuffer.readUnsignedShort()); int typeTemp = channelBuffer.readUnsignedByte(); byte isTypeByte = (byte) typeTemp; String tempValue = String.format("%8s", Integer.toBinaryString(isTypeByte & 0xFF)).replace(' ', '0'); int pBit = Integer.parseInt(new Character(tempValue.charAt(0)).toString()); if (pBit == 1) { this.setPartitionRepair(true); } else { this.setPartitionRepair(false); } int attValue = Integer.parseInt(tempValue.substring(1, 5), 2); switch (AttachedToOtherAreas.get(attValue)) { case DEFAULTMETRIC: this.setAttachedToOtherAreas(AttachedToOtherAreas.DEFAULTMETRIC); break; case DELAYMETRIC: this.setAttachedToOtherAreas(AttachedToOtherAreas.DELAYMETRIC); break; case EXPENSEMETRIC: this.setAttachedToOtherAreas(AttachedToOtherAreas.EXPENSEMETRIC); break; case ERRORMETRIC: this.setAttachedToOtherAreas(AttachedToOtherAreas.ERRORMETRIC); break; case NONE: this.setAttachedToOtherAreas(AttachedToOtherAreas.NONE); break; default: break; } int lspdbol = Integer.parseInt(new Character(tempValue.charAt(5)).toString()); if (lspdbol == 1) { this.setLspDbol(true); } else { this.setLspDbol(false); } int isType = Integer.parseInt(tempValue.substring(6, 8), 2); byte isTypeByteValue = (byte) isType; this.setIntermediateSystemType(isTypeByteValue); //tlv here while (channelBuffer.readableBytes() > 0) { TlvHeader tlvHeader = new TlvHeader(); tlvHeader.setTlvType(channelBuffer.readUnsignedByte()); tlvHeader.setTlvLength(channelBuffer.readUnsignedByte()); TlvType tlvValue = TlvType.get(tlvHeader.tlvType()); if (tlvValue != null) { IsisTlv tlv = TlvFinder.findTlv(tlvHeader, channelBuffer.readBytes(tlvHeader.tlvLength())); if (tlv != null) { this.variableLengths.add(tlv); } } else { channelBuffer.readBytes(tlvHeader.tlvLength()); } } }