Search in sources :

Example 6 with MessageException

use of com.biglybt.core.peermanager.messaging.MessageException in project BiglyBT by BiglySoftware.

the class AZHandshake method deserialize.

@Override
public Message deserialize(DirectByteBuffer data, byte version) throws MessageException {
    Map root = MessagingUtil.convertBencodedByteStreamToPayload(data, 100, getID());
    byte[] id = (byte[]) root.get("identity");
    if (id == null)
        throw new MessageException("id == null");
    if (id.length != 20)
        throw new MessageException("id.length != 20: " + id.length);
    byte[] session = (byte[]) root.get("session");
    byte[] reconnect = (byte[]) root.get("reconn");
    byte[] raw_name = (byte[]) root.get("client");
    if (raw_name == null)
        throw new MessageException("raw_name == null");
    String name = new String(raw_name);
    byte[] raw_ver = (byte[]) root.get("version");
    if (raw_ver == null)
        throw new MessageException("raw_ver == null");
    String client_version = new String(raw_ver);
    Long tcp_lport = (Long) root.get("tcp_port");
    if (tcp_lport == null) {
        // old handshake
        tcp_lport = new Long(0);
    }
    Long udp_lport = (Long) root.get("udp_port");
    if (udp_lport == null) {
        // old handshake
        udp_lport = new Long(0);
    }
    Long udp2_lport = (Long) root.get("udp2_port");
    if (udp2_lport == null) {
        // old handshake
        udp2_lport = udp_lport;
    }
    Long h_type = (Long) root.get("handshake_type");
    if (h_type == null) {
        // only 2307+ send type
        h_type = new Long(HANDSHAKE_TYPE_PLAIN);
    }
    InetAddress ipv6 = null;
    if (root.get("ipv6") instanceof byte[]) {
        try {
            InetAddress.getByAddress((byte[]) root.get("ipv6"));
        } catch (Exception e) {
        }
    }
    int md_size = 0;
    Long mds = (Long) root.get("mds");
    if (mds != null) {
        md_size = mds.intValue();
    }
    List raw_msgs = (List) root.get("messages");
    if (raw_msgs == null)
        throw new MessageException("raw_msgs == null");
    String[] ids = new String[raw_msgs.size()];
    byte[] vers = new byte[raw_msgs.size()];
    int pos = 0;
    for (Iterator i = raw_msgs.iterator(); i.hasNext(); ) {
        Map msg = (Map) i.next();
        byte[] mid = (byte[]) msg.get("id");
        if (mid == null)
            throw new MessageException("mid == null");
        ids[pos] = new String(mid);
        byte[] ver = (byte[]) msg.get("ver");
        if (ver == null)
            throw new MessageException("ver == null");
        if (ver.length != 1)
            throw new MessageException("ver.length != 1");
        vers[pos] = ver[0];
        pos++;
    }
    Long ulOnly = (Long) root.get("upload_only");
    boolean uploadOnly = ulOnly != null && ulOnly.longValue() > 0L ? true : false;
    if (name.equals(Constants.AZUREUS_PROTOCOL_NAME_PRE_4813)) {
        name = Constants.AZUREUS_PROTOCOL_NAME;
    }
    return new AZHandshake(id, session == null ? null : new HashWrapper(session), reconnect == null ? null : new HashWrapper(reconnect), name, client_version, tcp_lport.intValue(), udp_lport.intValue(), udp2_lport.intValue(), ipv6, md_size, ids, vers, h_type.intValue(), version, uploadOnly);
}
Also used : MessageException(com.biglybt.core.peermanager.messaging.MessageException) MessageException(com.biglybt.core.peermanager.messaging.MessageException) InetAddress(java.net.InetAddress)

Example 7 with MessageException

use of com.biglybt.core.peermanager.messaging.MessageException in project BiglyBT by BiglySoftware.

the class BTMessageDecoder method postReadProcess.

private int postReadProcess() throws IOException {
    int prot_bytes_read = 0;
    int data_bytes_read = 0;
    if (!reading_length_mode && !destroyed) {
        // reading payload data mode
        // ensure-restore proper buffer limits
        payload_buffer.limit(SS, message_length);
        length_buffer.limit(SS, 4);
        int read = payload_buffer.position(SS) - pre_read_start_position;
        if (payload_buffer.position(SS) > 0) {
            // need to have read the message id first byte
            if (BTMessageFactory.getMessageType(payload_buffer) == Message.TYPE_DATA_PAYLOAD) {
                data_bytes_read += read;
            } else {
                prot_bytes_read += read;
            }
        }
        if (!payload_buffer.hasRemaining(SS) && !is_paused) {
            // full message received!
            payload_buffer.position(SS, 0);
            DirectByteBuffer ref_buff = payload_buffer;
            payload_buffer = null;
            if (reading_handshake_message) {
                // decode handshake
                reading_handshake_message = false;
                DirectByteBuffer handshake_data = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_MSG_BT_HAND, 68);
                handshake_data.putInt(SS, HANDSHAKE_FAKE_LENGTH);
                handshake_data.put(SS, ref_buff);
                handshake_data.flip(SS);
                ref_buff.returnToPool();
                try {
                    Message handshake = MessageManager.getSingleton().createMessage(BTMessage.ID_BT_HANDSHAKE_BYTES, handshake_data, (byte) 1);
                    messages_last_read.add(handshake);
                } catch (MessageException me) {
                    handshake_data.returnToPool();
                    throw new IOException("BT message decode failed: " + me.getMessage());
                }
                // we need to auto-pause decoding until we're told to start again externally,
                // as we don't want to accidentally read the next message on the stream if it's an AZ-format handshake
                pauseDecoding();
            } else {
                // decode normal message
                try {
                    messages_last_read.add(createMessage(ref_buff));
                } catch (Throwable e) {
                    ref_buff.returnToPoolIfNotFree();
                    if (e instanceof RuntimeException) {
                        throw ((RuntimeException) e);
                    }
                    throw new IOException("BT message decode failed: " + e.getMessage());
                }
            }
            // see if we've already read the next message's length
            reading_length_mode = true;
            // reset receive percentage
            percent_complete = -1;
        } else {
            // only partial received so far
            // compute receive percentage
            percent_complete = (payload_buffer.position(SS) * 100) / message_length;
        }
    }
    if (reading_length_mode && !destroyed) {
        // ensure proper buffer limit
        length_buffer.limit(SS, 4);
        prot_bytes_read += (pre_read_start_buffer == 1) ? length_buffer.position(SS) - pre_read_start_position : length_buffer.position(SS);
        if (!length_buffer.hasRemaining(SS)) {
            // done reading the length
            reading_length_mode = false;
            length_buffer.position(SS, 0);
            message_length = length_buffer.getInt(SS);
            // reset it for next length read
            length_buffer.position(SS, 0);
            if (message_length == HANDSHAKE_FAKE_LENGTH) {
                // handshake message
                reading_handshake_message = true;
                // restore 'real' length
                message_length = 64;
                payload_buffer = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_MSG_BT_HAND, message_length);
            } else if (message_length == 0) {
                // keep-alive message
                reading_length_mode = true;
                last_received_was_keepalive = true;
                try {
                    Message keep_alive = MessageManager.getSingleton().createMessage(BTMessage.ID_BT_KEEP_ALIVE_BYTES, null, (byte) 1);
                    messages_last_read.add(keep_alive);
                } catch (MessageException me) {
                    throw new IOException("BT message decode failed: " + me.getMessage());
                }
            } else if (message_length < MIN_MESSAGE_LENGTH || message_length > MAX_MESSAGE_LENGTH) {
                throw new IOException("Invalid message length given for BT message decode: " + message_length);
            } else {
                // normal message
                payload_buffer = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_MSG_BT_PAYLOAD, message_length);
            }
        }
    }
    protocol_bytes_last_read += prot_bytes_read;
    data_bytes_last_read += data_bytes_read;
    return prot_bytes_read + data_bytes_read;
}
Also used : Message(com.biglybt.core.peermanager.messaging.Message) MessageException(com.biglybt.core.peermanager.messaging.MessageException) IOException(java.io.IOException) DirectByteBuffer(com.biglybt.core.util.DirectByteBuffer)

Example 8 with MessageException

use of com.biglybt.core.peermanager.messaging.MessageException in project BiglyBT by BiglySoftware.

the class BTDHTPort method deserialize.

@Override
public Message deserialize(DirectByteBuffer data, byte version) throws MessageException {
    if (data == null)
        throw new MessageException("[" + getID() + "] decode error: data == null");
    if (data.remaining(DirectByteBuffer.SS_MSG) != 2)
        throw new MessageException("[" + getID() + "] decode error: payload.remaining[" + data.remaining(DirectByteBuffer.SS_MSG) + "] != 2");
    short s_port = data.getShort(DirectByteBuffer.SS_MSG);
    data.returnToPool();
    return new BTDHTPort(0xFFFF & s_port);
}
Also used : MessageException(com.biglybt.core.peermanager.messaging.MessageException)

Example 9 with MessageException

use of com.biglybt.core.peermanager.messaging.MessageException in project BiglyBT by BiglySoftware.

the class LTHandshake method deserialize.

@Override
public Message deserialize(DirectByteBuffer data, byte version) throws MessageException {
    if (data == null) {
        throw new MessageException("[" + getID() + "] decode error: data == null");
    }
    if (data.remaining(DirectByteBuffer.SS_MSG) < 1) {
        throw new MessageException("[" + getID() + "] decode error: less than 1 byte in payload");
    }
    // Try decoding the data now.
    Map res_data_dict = MessagingUtil.convertBencodedByteStreamToPayload(data, 1, getID());
    LTHandshake result = new LTHandshake(res_data_dict, this.version);
    return result;
}
Also used : MessageException(com.biglybt.core.peermanager.messaging.MessageException) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

MessageException (com.biglybt.core.peermanager.messaging.MessageException)9 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Message (com.biglybt.core.peermanager.messaging.Message)1 AZUTMetaData (com.biglybt.core.peermanager.messaging.azureus.AZUTMetaData)1 PeerItem (com.biglybt.core.peermanager.peerdb.PeerItem)1 DirectByteBuffer (com.biglybt.core.util.DirectByteBuffer)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1