Example usage for io.netty.buffer ByteBuf readUnsignedShort

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

Introduction

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

Prototype

public abstract int readUnsignedShort();

Source Link

Document

Gets an unsigned 16-bit short integer at the current readerIndex and increases the readerIndex by 2 in this buffer.

Usage

From source file:de.unipassau.isl.evs.ssh.core.sec.DeviceConnectInformation.java

License:Open Source License

/**
 * Read a DeviceConnectInformation from a Base64 encoded String, which was read from a QR Code.
 */// w  w w .  ja  v a 2  s . c om
public static DeviceConnectInformation fromDataString(String data) throws IOException {
    final ByteBuf base64 = UnpooledByteBufAllocator.DEFAULT.heapBuffer(data.length());
    ByteBufUtil.writeAscii(base64, data);
    final ByteBuf byteBuf = decode(base64);
    if (byteBuf.readableBytes() != DATA_LENGTH) {
        throw new IOException("too many bytes encoded");
    }

    final byte[] addressData = new byte[ADDRESS_LENGTH];
    byteBuf.readBytes(addressData);
    final InetAddress address = InetAddress.getByAddress(addressData);
    final int port = byteBuf.readUnsignedShort();
    final byte[] idData = new byte[DeviceID.ID_LENGTH];
    byteBuf.readBytes(idData);
    final DeviceID id = new DeviceID(idData);
    final byte[] encodedToken = new byte[TOKEN_BASE64_LENGTH];
    byteBuf.readBytes(encodedToken);
    final byte[] token = decodeToken(new String(encodedToken));

    return new DeviceConnectInformation(address, port, id, token);
}

From source file:dorkbox.network.pipeline.discovery.BroadcastServer.java

License:Apache License

/**
 * @return true if this is a broadcast response, false if it was not a broadcast response
 *//*from   w  w w. j av a 2 s  .  c  o m*/
public static boolean isDiscoveryResponse(ByteBuf byteBuf, final InetAddress remoteAddress,
        final Channel channel) {
    if (byteBuf.readableBytes() <= MagicBytes.maxPacketSize) {
        // this is a BROADCAST discovery RESPONSE event. Don't read the byte unless it is...
        if (byteBuf.getByte(0) == MagicBytes.broadcastResponseID) {
            byteBuf.readByte(); // read the byte to consume it (now that we verified it is a broadcast byte)

            // either it will be TCP or UDP, or BOTH
            int typeID = byteBuf.readByte();

            int tcpPort = 0;
            int udpPort = 0;

            // TCP is always first
            if ((typeID & MagicBytes.HAS_TCP) == MagicBytes.HAS_TCP) {
                tcpPort = byteBuf.readUnsignedShort();
            }

            if ((typeID & MagicBytes.HAS_UDP) == MagicBytes.HAS_UDP) {
                udpPort = byteBuf.readUnsignedShort();
            }

            channel.attr(ClientDiscoverHostHandler.STATE)
                    .set(new BroadcastResponse(remoteAddress, tcpPort, udpPort));

            byteBuf.release();
            return true;
        }
    }

    return false;
}

From source file:eu.xworlds.util.raknet.protocol.BaseMessage.java

License:Open Source License

/**
 * Reads string from byte buf with usigned short length and utf8 encoding.
 * /*  ww  w.  jav  a  2  s .c  o  m*/
 * @param src source byte buffer
 * @return string
 */
protected String readStringUtf8UShort(ByteBuf src) {
    final int length = src.readUnsignedShort();
    return new String(src.readBytes(length).array(), StandardCharsets.UTF_8);
}

From source file:eu.xworlds.util.raknet.protocol.OpenConnectionReply1.java

License:Open Source License

@Override
protected void parseMessage(ByteBuf buf) {
    this.magic = new byte[MAGIC_BYTES];
    buf.readBytes(this.magic);
    this.serverGuid = readGuid(buf);
    this.hasSecurity = buf.readBoolean();
    if (this.hasSecurity) {
        this.securityCookie = buf.readInt();
        this.publicKey = new byte[EASYHANDSHAKE_PUBLIC_KEY_BYTES];
        buf.readBytes(this.publicKey);
    }//  ww w . ja  va2 s  . co  m
    this.mtuSize = buf.readUnsignedShort();
}

From source file:eu.xworlds.util.raknet.protocol.OpenConnectionReply2.java

License:Open Source License

@Override
protected void parseMessage(ByteBuf buf) {
    this.magic = new byte[MAGIC_BYTES];
    buf.readBytes(this.magic);
    this.serverGuid = readGuid(buf);
    this.port = buf.readUnsignedShort();
    this.mtuSize = buf.readUnsignedShort();
    this.doSecurity = buf.readBoolean();
    if (this.doSecurity) {
        this.securityAnswer = new byte[EASYHANDSHAKE_ANSWER_BYTES];
        buf.readBytes(this.securityAnswer);
    }//from w ww  .j a  v a 2s.  co m
}

From source file:eu.xworlds.util.raknet.protocol.OpenConnectionRequest2.java

License:Open Source License

@Override
protected void parseMessage(ByteBuf buf) {
    this.magic = new byte[MAGIC_BYTES];
    buf.readBytes(this.magic);
    // TODO how to get the security flag?
    if (this.useSecurity) {
        this.cookie = buf.readInt();
        this.clientWroteChallenge = buf.readBoolean();
        if (this.clientWroteChallenge) {
            this.clientChallenge = new byte[EASYHANDSHAKE_CHALLENGE_BYTES];
            buf.readBytes(this.clientChallenge);
        }/* www  .j a  v a 2 s  . c o  m*/
    }
    this.bindingAddress = readIPv4Address(buf);
    this.mtuSize = buf.readUnsignedShort();
    this.guid = readGuid(buf);
}

From source file:io.advantageous.conekt.dns.impl.netty.decoder.ServiceDecoder.java

License:Open Source License

/**
 * Returns a decoded SRV (service) resource record, stored as an instance of
 * {@link ServiceRecord}./*from w  w  w.ja  v  a  2  s. c  o m*/
 *
 * @param response the DNS response that contains the resource record being
 *                 decoded
 * @param resource the resource record being decoded
 */
@Override
public ServiceRecord decode(DnsResponse response, DnsResource resource) {
    ByteBuf packet = response.content().readerIndex(resource.contentIndex());
    int priority = packet.readShort();
    int weight = packet.readShort();
    int port = packet.readUnsignedShort();
    String target = DnsResponseDecoder.readName(packet);
    return new ServiceRecord(resource.name(), priority, weight, port, target);
}

From source file:io.advantageous.conekt.dns.impl.netty.DnsResponseDecoder.java

License:Open Source License

/**
 * Decodes a question, given a DNS packet in a byte buffer.
 *
 * @param buf the byte buffer containing the DNS packet
 * @return a decoded {@link DnsQuestion}
 *///from   w  w  w .  j  a v a2s  .com
private static DnsQuestion decodeQuestion(ByteBuf buf) {
    String name = readName(buf);
    int type = buf.readUnsignedShort();
    int qClass = buf.readUnsignedShort();
    return new DnsQuestion(name, type, qClass);
}

From source file:io.advantageous.conekt.dns.impl.netty.DnsResponseDecoder.java

License:Open Source License

/**
 * Decodes a resource record, given a DNS packet in a byte buffer.
 *
 * @param buf the byte buffer containing the DNS packet
 * @return a {@link DnsResource} record containing response data
 *///from  ww w .  ja v  a 2 s  .co  m
private static DnsResource decodeResource(ByteBuf buf, ByteBufAllocator allocator) {
    String name = readName(buf);
    int type = buf.readUnsignedShort();
    int aClass = buf.readUnsignedShort();
    long ttl = buf.readUnsignedInt();
    int len = buf.readUnsignedShort();
    ByteBuf resourceData = allocator.buffer(len);
    int contentIndex = buf.readerIndex();
    resourceData.writeBytes(buf, len);
    return new DnsResource(name, type, aClass, ttl, contentIndex, resourceData);
}

From source file:io.advantageous.conekt.dns.impl.netty.DnsResponseDecoder.java

License:Open Source License

/**
 * Decodes a DNS response header, given a DNS packet in a byte buffer.
 *
 * @param parent the parent {@link DnsResponse} to this header
 * @param buf    the byte buffer containing the DNS packet
 * @return a {@link DnsResponseHeader} containing the response's header
 * information/*  w  w  w. j  a va 2 s.com*/
 */
private static DnsResponseHeader decodeHeader(DnsResponse parent, ByteBuf buf) {
    int id = buf.readUnsignedShort();
    DnsResponseHeader header = new DnsResponseHeader(parent, id);
    int flags = buf.readUnsignedShort();
    header.setType(flags >> 15);
    header.setOpcode(flags >> 11 & 0xf);
    header.setRecursionDesired((flags >> 8 & 1) == 1);
    header.setAuthoritativeAnswer((flags >> 10 & 1) == 1);
    header.setTruncated((flags >> 9 & 1) == 1);
    header.setRecursionAvailable((flags >> 7 & 1) == 1);
    header.setZ(flags >> 4 & 0x7);
    header.setResponseCode(flags & 0xf);
    header.setReadQuestions(buf.readUnsignedShort());
    header.setReadAnswers(buf.readUnsignedShort());
    header.setReadAuthorityResources(buf.readUnsignedShort());
    header.setReadAdditionalResources(buf.readUnsignedShort());
    return header;
}