Search in sources :

Example 1 with MessageException

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

the class SESTSConnectionImpl method connect.

@Override
public void connect() throws MessageException {
    if (connection.isIncoming()) {
        connection.connect();
    } else {
        try {
            ByteBuffer buffer = ByteBuffer.allocate(32 * 1024);
            sts_engine.getKeys(buffer);
            buffer.flip();
            sent_keys = true;
            connection.connect(buffer);
        } catch (CryptoManagerException e) {
            throw (new MessageException("Failed to get initial keys", e));
        }
    }
}
Also used : CryptoManagerException(com.biglybt.core.security.CryptoManagerException) MessageException(com.biglybt.pif.messaging.MessageException) PooledByteBuffer(com.biglybt.pif.utils.PooledByteBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 2 with MessageException

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

the class GenericMessageConnectionImpl method connect.

/**
 * Outgoing connection
 * @param initial_data
 * @throws MessageException
 */
public void connect(ByteBuffer initial_data) throws MessageException {
    if (incoming) {
        throw (new MessageException("Already connected"));
    }
    if (connecting) {
        throw (new MessageException("Connect already performed"));
    }
    connecting = true;
    if (closed) {
        throw (new MessageException("Connection has been closed"));
    }
    InetSocketAddress tcp_ep = endpoint.getTCP();
    if (tcp_ep != null) {
        connectTCP(initial_data, tcp_ep);
    } else {
        InetSocketAddress udp_ep = endpoint.getUDP();
        if (udp_ep != null) {
            connectUDP(initial_data, udp_ep, false);
        } else {
            throw (new MessageException("No protocols availabld"));
        }
    }
}
Also used : MessageException(com.biglybt.pif.messaging.MessageException) InetSocketAddress(java.net.InetSocketAddress)

Example 3 with MessageException

use of com.biglybt.pif.messaging.MessageException 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 MessageException

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

the class GenericMessageConnectionDirect method close.

@Override
public void close() throws MessageException {
    if (!connected) {
        throw (new MessageException("not connected"));
    }
    if (!closed) {
        closed = true;
        connection.close(null);
    }
}
Also used : MessageException(com.biglybt.pif.messaging.MessageException)

Example 5 with MessageException

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

the class SESTSConnectionImpl method sendContent.

protected void sendContent(PooledByteBuffer message) throws MessageException {
    if (outgoing_cipher != null) {
        try {
            byte[] plain = message.toByteArray();
            byte[] enc = outgoing_cipher.doFinal(plain);
            PooledByteBuffer temp = new PooledByteBufferImpl(enc);
            try {
                connection.send(temp);
                // successfull send -> release caller's buffer
                message.returnToPool();
            } catch (Throwable e) {
                // failed semantics are to not release the caller's buffer
                temp.returnToPool();
                throw (e);
            }
        } catch (Throwable e) {
            throw (new MessageException("Failed to encrypt data", e));
        }
    } else {
        if (block_crypto != SESecurityManager.BLOCK_ENCRYPTION_NONE) {
            connection.close();
            throw (new MessageException("Crypto isn't setup"));
        }
        connection.send(message);
    }
}
Also used : PooledByteBufferImpl(com.biglybt.pifimpl.local.utils.PooledByteBufferImpl) MessageException(com.biglybt.pif.messaging.MessageException) PooledByteBuffer(com.biglybt.pif.utils.PooledByteBuffer)

Aggregations

MessageException (com.biglybt.pif.messaging.MessageException)14 GenericMessageEndpoint (com.biglybt.pif.messaging.generic.GenericMessageEndpoint)6 PooledByteBuffer (com.biglybt.pif.utils.PooledByteBuffer)5 PooledByteBufferImpl (com.biglybt.pifimpl.local.utils.PooledByteBufferImpl)4 GenericMessageConnectionListener (com.biglybt.pif.messaging.generic.GenericMessageConnectionListener)3 InetSocketAddress (java.net.InetSocketAddress)3 ByteBuffer (java.nio.ByteBuffer)3 NATTraversalObserver (com.biglybt.core.nat.NATTraversalObserver)1 NATTraverser (com.biglybt.core.nat.NATTraverser)1 NetworkConnection (com.biglybt.core.networkmanager.NetworkConnection)1 NetworkManager (com.biglybt.core.networkmanager.NetworkManager)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 CryptoManagerException (com.biglybt.core.security.CryptoManagerException)1 SHA1Simple (com.biglybt.core.util.SHA1Simple)1 GenericMessageConnection (com.biglybt.pif.messaging.generic.GenericMessageConnection)1 GenericMessageHandler (com.biglybt.pif.messaging.generic.GenericMessageHandler)1 GenericMessageRegistration (com.biglybt.pif.messaging.generic.GenericMessageRegistration)1