Search in sources :

Example 1 with MessageStreamEncoder

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

the class PEPeerTransportProtocol method doPostHandshakeProcessing.

/**
 * this method might be called repeatedly since LTEP allows headers to be sent more than once
 * make sure that multiple invokations do not have unintended consequences
 */
private void doPostHandshakeProcessing() {
    // peer exchange registration
    if (manager.isPeerExchangeEnabled()) {
        // try and register all connections for their peer exchange info
        PeerExchangerItem pex_item = peer_exchange_item;
        if (pex_item == null && canBePeerExchanged()) {
            pex_item = peer_exchange_item = manager.createPeerExchangeConnection(this);
        }
        if (pex_item != null) {
            if (ut_pex_enabled || peerSupportsMessageType(AZMessage.ID_AZ_PEER_EXCHANGE)) {
                peer_exchange_supported = true;
                pex_item.enableStateMaintenance();
            } else {
                MessageStreamEncoder encoder = connection.getOutgoingMessageQueue().getEncoder();
                if (encoder instanceof LTMessageEncoder && ((LTMessageEncoder) encoder).hasCustomExtensionHandler(LTMessageEncoder.CET_PEX)) {
                    peer_exchange_supported = true;
                    pex_item.enableStateMaintenance();
                } else {
                    // no need to maintain internal states as we wont be sending/receiving peer exchange messages
                    pex_item.disableStateMaintenance();
                }
            }
        }
    }
    request_hint_supported = peerSupportsMessageType(AZMessage.ID_AZ_REQUEST_HINT);
    bad_piece_supported = peerSupportsMessageType(AZMessage.ID_AZ_BAD_PIECE);
    stats_request_supported = peerSupportsMessageType(AZMessage.ID_AZ_STAT_REQUEST);
    stats_reply_supported = peerSupportsMessageType(AZMessage.ID_AZ_STAT_REPLY);
    az_metadata_supported = peerSupportsMessageType(AZMessage.ID_AZ_METADATA);
    if (is_metadata_download) {
        if (az_metadata_supported) {
            int mds = manager.getTorrentInfoDictSize();
            if (mds > 0) {
                spoofMDAvailability(mds);
            }
        }
    }
}
Also used : PeerExchangerItem(com.biglybt.core.peermanager.peerdb.PeerExchangerItem) MessageStreamEncoder(com.biglybt.core.peermanager.messaging.MessageStreamEncoder)

Example 2 with MessageStreamEncoder

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

the class PEPeerTransportProtocol method updatePeerExchange.

@Override
public void updatePeerExchange() {
    if (current_peer_state != TRANSFERING)
        return;
    if (!peer_exchange_supported)
        return;
    PeerExchangerItem pex_item = peer_exchange_item;
    if (pex_item != null && manager.isPeerExchangeEnabled()) {
        if (peer_item_identity.getNetwork() == AENetworkClassifier.AT_PUBLIC) {
            final PeerItem[] adds = pex_item.getNewlyAddedPeerConnections(AENetworkClassifier.AT_PUBLIC);
            final PeerItem[] drops = pex_item.getNewlyDroppedPeerConnections(AENetworkClassifier.AT_PUBLIC);
            if ((adds != null && adds.length > 0) || (drops != null && drops.length > 0)) {
                if (ut_pex_enabled) {
                    connection.getOutgoingMessageQueue().addMessage(new UTPeerExchange(adds, drops, null, (byte) 0), false);
                } else {
                    connection.getOutgoingMessageQueue().addMessage(new AZPeerExchange(manager.getHash(), adds, drops, other_peer_pex_version), false);
                }
            }
        } else {
            MessageStreamEncoder encoder = connection.getOutgoingMessageQueue().getEncoder();
            if (encoder instanceof LTMessageEncoder) {
                ((LTMessageEncoder) encoder).handleCustomExtension(LTMessageEncoder.CET_PEX, new Object[] { pex_item });
            }
        }
    }
}
Also used : PeerItem(com.biglybt.core.peermanager.peerdb.PeerItem) PeerExchangerItem(com.biglybt.core.peermanager.peerdb.PeerExchangerItem) MessageStreamEncoder(com.biglybt.core.peermanager.messaging.MessageStreamEncoder)

Example 3 with MessageStreamEncoder

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

the class MessageManagerImpl method registerGenericMessageType.

@Override
public GenericMessageRegistration registerGenericMessageType(final String _type, final String description, final int stream_crypto, final GenericMessageHandler handler) throws MessageException {
    final String type = "AEGEN:" + _type;
    final byte[] type_bytes = type.getBytes();
    final byte[][] shared_secrets = new byte[][] { new SHA1Simple().calculateHash(type_bytes) };
    synchronized (message_handlers) {
        message_handlers.put(type, handler);
    }
    final NetworkManager.ByteMatcher matcher = new NetworkManager.ByteMatcher() {

        @Override
        public int matchThisSizeOrBigger() {
            return (maxSize());
        }

        @Override
        public int maxSize() {
            return type_bytes.length;
        }

        @Override
        public int minSize() {
            return maxSize();
        }

        @Override
        public Object matches(TransportHelper transport, ByteBuffer to_compare, int port) {
            int old_limit = to_compare.limit();
            to_compare.limit(to_compare.position() + maxSize());
            boolean matches = to_compare.equals(ByteBuffer.wrap(type_bytes));
            // restore buffer structure
            to_compare.limit(old_limit);
            return matches ? "" : null;
        }

        @Override
        public Object minMatches(TransportHelper transport, ByteBuffer to_compare, int port) {
            return (matches(transport, to_compare, port));
        }

        @Override
        public byte[][] getSharedSecrets() {
            return (shared_secrets);
        }

        @Override
        public int getSpecificPort() {
            return (-1);
        }
    };
    NetworkManager.getSingleton().requestIncomingConnectionRouting(matcher, new NetworkManager.RoutingListener() {

        @Override
        public void connectionRouted(final NetworkConnection connection, Object routing_data) {
            try {
                ByteBuffer[] skip_buffer = { ByteBuffer.allocate(type_bytes.length) };
                connection.getTransport().read(skip_buffer, 0, 1);
                if (skip_buffer[0].remaining() != 0) {
                    Debug.out("incomplete read");
                }
                GenericMessageEndpointImpl endpoint = new GenericMessageEndpointImpl(connection.getEndpoint());
                GenericMessageConnectionDirect direct_connection = GenericMessageConnectionDirect.receive(endpoint, type, description, stream_crypto, shared_secrets);
                GenericMessageConnectionImpl new_connection = new GenericMessageConnectionImpl(MessageManagerImpl.this, direct_connection);
                direct_connection.connect(connection);
                if (handler.accept(new_connection)) {
                    new_connection.accepted();
                } else {
                    connection.close("connection not accepted");
                }
            } catch (Throwable e) {
                Debug.printStackTrace(e);
                connection.close(e == null ? null : Debug.getNestedExceptionMessage(e));
            }
        }

        @Override
        public boolean autoCryptoFallback() {
            return (stream_crypto != MessageManager.STREAM_ENCRYPTION_RC4_REQUIRED);
        }
    }, new MessageStreamFactory() {

        @Override
        public MessageStreamEncoder createEncoder() {
            return new GenericMessageEncoder();
        }

        @Override
        public MessageStreamDecoder createDecoder() {
            return new GenericMessageDecoder(type, description);
        }
    });
    return (new GenericMessageRegistration() {

        @Override
        public GenericMessageEndpoint createEndpoint(InetSocketAddress notional_target) {
            return (new GenericMessageEndpointImpl(notional_target));
        }

        @Override
        public GenericMessageConnection createConnection(GenericMessageEndpoint endpoint) throws MessageException {
            return (new GenericMessageConnectionImpl(MessageManagerImpl.this, type, description, (GenericMessageEndpointImpl) endpoint, stream_crypto, shared_secrets));
        }

        @Override
        public void cancel() {
            NetworkManager.getSingleton().cancelIncomingConnectionRouting(matcher);
            synchronized (message_handlers) {
                message_handlers.remove(type);
            }
        }
    });
}
Also used : MessageStreamFactory(com.biglybt.core.peermanager.messaging.MessageStreamFactory) InetSocketAddress(java.net.InetSocketAddress) MessageException(com.biglybt.pif.messaging.MessageException) MessageStreamEncoder(com.biglybt.core.peermanager.messaging.MessageStreamEncoder) GenericMessageRegistration(com.biglybt.pif.messaging.generic.GenericMessageRegistration) SHA1Simple(com.biglybt.core.util.SHA1Simple) NetworkConnection(com.biglybt.core.networkmanager.NetworkConnection) TransportHelper(com.biglybt.core.networkmanager.impl.TransportHelper) ByteBuffer(java.nio.ByteBuffer) GenericMessageEndpoint(com.biglybt.pif.messaging.generic.GenericMessageEndpoint) MessageStreamDecoder(com.biglybt.core.peermanager.messaging.MessageStreamDecoder) GenericMessageEndpoint(com.biglybt.pif.messaging.generic.GenericMessageEndpoint) NetworkManager(com.biglybt.core.networkmanager.NetworkManager) GenericMessageConnection(com.biglybt.pif.messaging.generic.GenericMessageConnection)

Example 4 with MessageStreamEncoder

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

the class PeerManager method init.

protected void init() {
    // ensure it gets initialized
    MessageManager.getSingleton().initialize();
    NetworkManager.ByteMatcher matcher = new NetworkManager.ByteMatcher() {

        @Override
        public int matchThisSizeOrBigger() {
            return (48);
        }

        @Override
        public int maxSize() {
            return 48;
        }

        @Override
        public int minSize() {
            return 20;
        }

        @Override
        public Object matches(TransportHelper transport, ByteBuffer to_compare, int port) {
            InetSocketAddress address = transport.getAddress();
            int old_limit = to_compare.limit();
            int old_position = to_compare.position();
            to_compare.limit(old_position + 20);
            PeerManagerRegistrationImpl routing_data = null;
            if (to_compare.equals(legacy_handshake_header)) {
                // compare header
                to_compare.limit(old_position + 48);
                to_compare.position(old_position + 28);
                byte[] hash = new byte[to_compare.remaining()];
                to_compare.get(hash);
                try {
                    managers_mon.enter();
                    List<PeerManagerRegistrationImpl> registrations = registered_legacy_managers.get(new HashWrapper(hash));
                    if (registrations != null) {
                        routing_data = registrations.get(0);
                    }
                } finally {
                    managers_mon.exit();
                }
            }
            // restore buffer structure
            to_compare.limit(old_limit);
            to_compare.position(old_position);
            if (routing_data != null) {
                if (!routing_data.isActive()) {
                    if (routing_data.isKnownSeed(address)) {
                        String reason = "Activation request from " + address + " denied as known seed";
                        if (Logger.isEnabled()) {
                            Logger.log(new LogEvent(LOGID, reason));
                        }
                        transport.close(reason);
                        routing_data = null;
                    } else {
                        if (!routing_data.getAdapter().activateRequest(address)) {
                            String reason = "Activation request from " + address + " denied by rules";
                            if (Logger.isEnabled()) {
                                Logger.log(new LogEvent(LOGID, reason));
                            }
                            transport.close(reason);
                            routing_data = null;
                        }
                    }
                }
            }
            return routing_data;
        }

        @Override
        public Object minMatches(TransportHelper transport, ByteBuffer to_compare, int port) {
            boolean matches = false;
            int old_limit = to_compare.limit();
            int old_position = to_compare.position();
            to_compare.limit(old_position + 20);
            if (to_compare.equals(legacy_handshake_header)) {
                matches = true;
            }
            // restore buffer structure
            to_compare.limit(old_limit);
            to_compare.position(old_position);
            return matches ? "" : null;
        }

        @Override
        public byte[][] getSharedSecrets() {
            // registered manually above
            return (null);
        }

        @Override
        public int getSpecificPort() {
            return (-1);
        }
    };
    // register for incoming connection routing
    NetworkManager.getSingleton().requestIncomingConnectionRouting(matcher, new NetworkManager.RoutingListener() {

        @Override
        public void connectionRouted(NetworkConnection connection, Object routing_data) {
            PeerManagerRegistrationImpl registration = (PeerManagerRegistrationImpl) routing_data;
            registration.route(connection, null);
        }

        @Override
        public boolean autoCryptoFallback() {
            return (false);
        }
    }, new MessageStreamFactory() {

        @Override
        public MessageStreamEncoder createEncoder() {
            return new BTMessageEncoder();
        }

        @Override
        public MessageStreamDecoder createDecoder() {
            return new BTMessageDecoder();
        }
    });
}
Also used : MessageStreamFactory(com.biglybt.core.peermanager.messaging.MessageStreamFactory) LogEvent(com.biglybt.core.logging.LogEvent) InetSocketAddress(java.net.InetSocketAddress) NetworkConnection(com.biglybt.core.networkmanager.NetworkConnection) TransportHelper(com.biglybt.core.networkmanager.impl.TransportHelper) BTMessageDecoder(com.biglybt.core.peermanager.messaging.bittorrent.BTMessageDecoder) ByteBuffer(java.nio.ByteBuffer) ProtocolEndpoint(com.biglybt.core.networkmanager.ProtocolEndpoint) ConnectionEndpoint(com.biglybt.core.networkmanager.ConnectionEndpoint) BTMessageEncoder(com.biglybt.core.peermanager.messaging.bittorrent.BTMessageEncoder) MessageStreamDecoder(com.biglybt.core.peermanager.messaging.MessageStreamDecoder) MessageStreamEncoder(com.biglybt.core.peermanager.messaging.MessageStreamEncoder) NetworkManager(com.biglybt.core.networkmanager.NetworkManager)

Aggregations

MessageStreamEncoder (com.biglybt.core.peermanager.messaging.MessageStreamEncoder)4 NetworkConnection (com.biglybt.core.networkmanager.NetworkConnection)2 NetworkManager (com.biglybt.core.networkmanager.NetworkManager)2 TransportHelper (com.biglybt.core.networkmanager.impl.TransportHelper)2 MessageStreamDecoder (com.biglybt.core.peermanager.messaging.MessageStreamDecoder)2 MessageStreamFactory (com.biglybt.core.peermanager.messaging.MessageStreamFactory)2 PeerExchangerItem (com.biglybt.core.peermanager.peerdb.PeerExchangerItem)2 InetSocketAddress (java.net.InetSocketAddress)2 ByteBuffer (java.nio.ByteBuffer)2 LogEvent (com.biglybt.core.logging.LogEvent)1 ConnectionEndpoint (com.biglybt.core.networkmanager.ConnectionEndpoint)1 ProtocolEndpoint (com.biglybt.core.networkmanager.ProtocolEndpoint)1 BTMessageDecoder (com.biglybt.core.peermanager.messaging.bittorrent.BTMessageDecoder)1 BTMessageEncoder (com.biglybt.core.peermanager.messaging.bittorrent.BTMessageEncoder)1 PeerItem (com.biglybt.core.peermanager.peerdb.PeerItem)1 SHA1Simple (com.biglybt.core.util.SHA1Simple)1 MessageException (com.biglybt.pif.messaging.MessageException)1 GenericMessageConnection (com.biglybt.pif.messaging.generic.GenericMessageConnection)1 GenericMessageEndpoint (com.biglybt.pif.messaging.generic.GenericMessageEndpoint)1 GenericMessageRegistration (com.biglybt.pif.messaging.generic.GenericMessageRegistration)1