Search in sources :

Example 1 with ProtocolDecoder

use of com.biglybt.core.networkmanager.impl.ProtocolDecoder in project BiglyBT by BiglySoftware.

the class UDPConnectionManager method accept.

protected void accept(final int local_port, final InetSocketAddress remote_address, final UDPConnection connection) {
    final UDPTransportHelper helper = new UDPTransportHelper(this, remote_address, connection);
    try {
        connection.setTransport(helper);
        TransportCryptoManager.getSingleton().manageCrypto(helper, null, true, null, new TransportCryptoManager.HandshakeListener() {

            @Override
            public void handshakeSuccess(ProtocolDecoder decoder, ByteBuffer remaining_initial_data) {
                TransportHelperFilter filter = decoder.getFilter();
                ConnectionEndpoint co_ep = new ConnectionEndpoint(remote_address);
                ProtocolEndpointUDP pe_udp = (ProtocolEndpointUDP) ProtocolEndpointFactory.createEndpoint(ProtocolEndpoint.PROTOCOL_UDP, co_ep, remote_address);
                UDPTransport transport = new UDPTransport(pe_udp, filter);
                helper.setTransport(transport);
                incoming_manager.addConnection(local_port, filter, transport);
            }

            @Override
            public void handshakeFailure(Throwable failure_msg) {
                if (Logger.isEnabled()) {
                    Logger.log(new LogEvent(LOGID, "incoming crypto handshake failure: " + Debug.getNestedExceptionMessage(failure_msg)));
                }
                connection.close("handshake failure: " + Debug.getNestedExceptionMessage(failure_msg));
            }

            @Override
            public void gotSecret(byte[] session_secret) {
                helper.getConnection().setSecret(session_secret);
            }

            @Override
            public int getMaximumPlainHeaderLength() {
                return (incoming_manager.getMaxMinMatchBufferSize());
            }

            @Override
            public int matchPlainHeader(ByteBuffer buffer) {
                Object[] match_data = incoming_manager.checkForMatch(helper, local_port, buffer, true);
                if (match_data == null) {
                    return (TransportCryptoManager.HandshakeListener.MATCH_NONE);
                } else {
                    return (TransportCryptoManager.HandshakeListener.MATCH_CRYPTO_NO_AUTO_FALLBACK);
                }
            }
        });
    } catch (Throwable e) {
        Debug.printStackTrace(e);
        helper.close(Debug.getNestedExceptionMessage(e));
    }
}
Also used : TransportHelperFilter(com.biglybt.core.networkmanager.impl.TransportHelperFilter) LogEvent(com.biglybt.core.logging.LogEvent) ProtocolDecoder(com.biglybt.core.networkmanager.impl.ProtocolDecoder) ByteBuffer(java.nio.ByteBuffer) ConnectionEndpoint(com.biglybt.core.networkmanager.ConnectionEndpoint) TransportCryptoManager(com.biglybt.core.networkmanager.impl.TransportCryptoManager)

Example 2 with ProtocolDecoder

use of com.biglybt.core.networkmanager.impl.ProtocolDecoder in project BiglyBT by BiglySoftware.

the class UDPConnectionManager method connectOutbound.

public void connectOutbound(final UDPTransport udp_transport, final InetSocketAddress address, byte[][] shared_secrets, ByteBuffer initial_data, final ConnectListener listener) {
    UDPTransportHelper helper = null;
    try {
        if (address.isUnresolved()) {
            listener.connectFailure(new UnknownHostException(address.getHostName()));
            return;
        }
        int time = listener.connectAttemptStarted(-1);
        if (time != -1) {
            Debug.out("UDP connect time override not supported");
        }
        helper = new UDPTransportHelper(this, address, udp_transport);
        final UDPTransportHelper f_helper = helper;
        synchronized (this) {
            outbound_connection_count++;
            if (outbound_connection_count >= max_outbound_connections) {
                if (!max_conn_exceeded_logged) {
                    max_conn_exceeded_logged = true;
                    Debug.out("UDPConnectionManager: max outbound connection limit reached (" + max_outbound_connections + ")");
                }
            }
        }
        try {
            TransportCryptoManager.getSingleton().manageCrypto(helper, shared_secrets, false, initial_data, new TransportCryptoManager.HandshakeListener() {

                @Override
                public void handshakeSuccess(ProtocolDecoder decoder, ByteBuffer remaining_initial_data) {
                    synchronized (UDPConnectionManager.this) {
                        if (outbound_connection_count > 0) {
                            outbound_connection_count--;
                        }
                    }
                    TransportHelperFilter filter = decoder.getFilter();
                    try {
                        udp_transport.setFilter(filter);
                        if (udp_transport.isClosed()) {
                            udp_transport.close("Already closed");
                            listener.connectFailure(new Exception("Connection already closed"));
                        } else {
                            if (Logger.isEnabled()) {
                                Logger.log(new LogEvent(LOGID, "Outgoing UDP stream to " + address + " established, type = " + filter.getName(false)));
                            }
                            udp_transport.connectedOutbound();
                            listener.connectSuccess(udp_transport, remaining_initial_data);
                        }
                    } catch (Throwable e) {
                        Debug.printStackTrace(e);
                        udp_transport.close(Debug.getNestedExceptionMessageAndStack(e));
                        listener.connectFailure(e);
                    }
                }

                @Override
                public void handshakeFailure(Throwable failure_msg) {
                    synchronized (UDPConnectionManager.this) {
                        if (outbound_connection_count > 0) {
                            outbound_connection_count--;
                        }
                    }
                    f_helper.close(Debug.getNestedExceptionMessageAndStack(failure_msg));
                    listener.connectFailure(failure_msg);
                }

                @Override
                public void gotSecret(byte[] session_secret) {
                    f_helper.getConnection().setSecret(session_secret);
                }

                @Override
                public int getMaximumPlainHeaderLength() {
                    // this is outgoing
                    throw (new RuntimeException());
                }

                @Override
                public int matchPlainHeader(ByteBuffer buffer) {
                    // this is outgoing
                    throw (new RuntimeException());
                }
            });
        } catch (Throwable e) {
            synchronized (this) {
                if (outbound_connection_count > 0) {
                    outbound_connection_count--;
                }
            }
            throw (e);
        }
    } catch (Throwable e) {
        Debug.printStackTrace(e);
        if (helper != null) {
            helper.close(Debug.getNestedExceptionMessage(e));
        }
        listener.connectFailure(e);
    }
}
Also used : TransportHelperFilter(com.biglybt.core.networkmanager.impl.TransportHelperFilter) UnknownHostException(java.net.UnknownHostException) LogEvent(com.biglybt.core.logging.LogEvent) ProtocolDecoder(com.biglybt.core.networkmanager.impl.ProtocolDecoder) ByteBuffer(java.nio.ByteBuffer) ProtocolEndpoint(com.biglybt.core.networkmanager.ProtocolEndpoint) ConnectionEndpoint(com.biglybt.core.networkmanager.ConnectionEndpoint) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) TransportCryptoManager(com.biglybt.core.networkmanager.impl.TransportCryptoManager)

Aggregations

LogEvent (com.biglybt.core.logging.LogEvent)2 ConnectionEndpoint (com.biglybt.core.networkmanager.ConnectionEndpoint)2 ProtocolDecoder (com.biglybt.core.networkmanager.impl.ProtocolDecoder)2 TransportCryptoManager (com.biglybt.core.networkmanager.impl.TransportCryptoManager)2 TransportHelperFilter (com.biglybt.core.networkmanager.impl.TransportHelperFilter)2 ByteBuffer (java.nio.ByteBuffer)2 ProtocolEndpoint (com.biglybt.core.networkmanager.ProtocolEndpoint)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1