Search in sources :

Example 1 with BTMessageDecoder

use of com.biglybt.core.peermanager.messaging.bittorrent.BTMessageDecoder 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

LogEvent (com.biglybt.core.logging.LogEvent)1 ConnectionEndpoint (com.biglybt.core.networkmanager.ConnectionEndpoint)1 NetworkConnection (com.biglybt.core.networkmanager.NetworkConnection)1 NetworkManager (com.biglybt.core.networkmanager.NetworkManager)1 ProtocolEndpoint (com.biglybt.core.networkmanager.ProtocolEndpoint)1 TransportHelper (com.biglybt.core.networkmanager.impl.TransportHelper)1 MessageStreamDecoder (com.biglybt.core.peermanager.messaging.MessageStreamDecoder)1 MessageStreamEncoder (com.biglybt.core.peermanager.messaging.MessageStreamEncoder)1 MessageStreamFactory (com.biglybt.core.peermanager.messaging.MessageStreamFactory)1 BTMessageDecoder (com.biglybt.core.peermanager.messaging.bittorrent.BTMessageDecoder)1 BTMessageEncoder (com.biglybt.core.peermanager.messaging.bittorrent.BTMessageEncoder)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1