Search in sources :

Example 1 with GenericMessageEndpoint

use of com.biglybt.pif.messaging.generic.GenericMessageEndpoint 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 2 with GenericMessageEndpoint

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

the class BuddyPluginBuddy method outgoingConnection.

protected GenericMessageConnection outgoingConnection() throws BuddyPluginException {
    GenericMessageRegistration msg_registration = plugin.getMessageRegistration();
    if (msg_registration == null) {
        throw (new BuddyPluginException("Messaging system unavailable"));
    }
    InetAddress ip = getIP();
    if (ip == null) {
        throw (new BuddyPluginException("Friend offline (no usable IP address)"));
    }
    InetSocketAddress tcp_target = null;
    InetSocketAddress udp_target = null;
    int tcp_port = getTCPPort();
    if (tcp_port > 0) {
        tcp_target = new InetSocketAddress(ip, tcp_port);
    }
    int udp_port = getUDPPort();
    if (udp_port > 0) {
        udp_target = new InetSocketAddress(ip, udp_port);
    }
    InetSocketAddress notional_target = tcp_target;
    if (notional_target == null) {
        notional_target = udp_target;
    }
    if (notional_target == null) {
        throw (new BuddyPluginException("Friend offline (no usable protocols)"));
    }
    GenericMessageEndpoint endpoint = msg_registration.createEndpoint(notional_target);
    if (tcp_target != null) {
        endpoint.addTCP(tcp_target);
    }
    if (udp_target != null) {
        endpoint.addUDP(udp_target);
    }
    GenericMessageConnection con = null;
    try {
        last_connect_attempt = SystemTime.getCurrentTime();
        con = msg_registration.createConnection(endpoint);
        plugin.addRateLimiters(con);
        String reason = "Friend: Outgoing connection establishment";
        SESecurityManager sec_man = plugin.getSecurityManager();
        con = sec_man.getSTSConnection(con, sec_man.getPublicKey(SEPublicKey.KEY_TYPE_ECC_192, reason), new SEPublicKeyLocator() {

            @Override
            public boolean accept(Object context, SEPublicKey other_key) {
                String other_key_str = Base32.encode(other_key.encodeRawPublicKey());
                if (other_key_str.equals(public_key)) {
                    consec_connect_fails = 0;
                    return (true);
                } else {
                    log(getString() + ": connection failed due to pk mismatch");
                    return (false);
                }
            }
        }, reason, BuddyPlugin.BLOCK_CRYPTO);
        con.connect();
        return (con);
    } catch (Throwable e) {
        if (con != null) {
            consec_connect_fails++;
            try {
                con.close();
            } catch (Throwable f) {
                log("Failed to close connection", f);
            }
        }
        throw (new BuddyPluginException("Failed to send message", e));
    }
}
Also used : SEPublicKey(com.biglybt.pif.utils.security.SEPublicKey) InetSocketAddress(java.net.InetSocketAddress) GenericMessageEndpoint(com.biglybt.pif.messaging.generic.GenericMessageEndpoint) SESecurityManager(com.biglybt.pif.utils.security.SESecurityManager) SEPublicKeyLocator(com.biglybt.pif.utils.security.SEPublicKeyLocator) GenericMessageEndpoint(com.biglybt.pif.messaging.generic.GenericMessageEndpoint) GenericMessageRegistration(com.biglybt.pif.messaging.generic.GenericMessageRegistration) InetAddress(java.net.InetAddress) GenericMessageConnection(com.biglybt.pif.messaging.generic.GenericMessageConnection)

Aggregations

GenericMessageConnection (com.biglybt.pif.messaging.generic.GenericMessageConnection)2 GenericMessageEndpoint (com.biglybt.pif.messaging.generic.GenericMessageEndpoint)2 GenericMessageRegistration (com.biglybt.pif.messaging.generic.GenericMessageRegistration)2 InetSocketAddress (java.net.InetSocketAddress)2 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 SHA1Simple (com.biglybt.core.util.SHA1Simple)1 MessageException (com.biglybt.pif.messaging.MessageException)1 SEPublicKey (com.biglybt.pif.utils.security.SEPublicKey)1 SEPublicKeyLocator (com.biglybt.pif.utils.security.SEPublicKeyLocator)1 SESecurityManager (com.biglybt.pif.utils.security.SESecurityManager)1 InetAddress (java.net.InetAddress)1 ByteBuffer (java.nio.ByteBuffer)1