Class Connection


  • public class Connection
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static byte ACK
      Acknowledgement of a message part.
      protected SymCipherManagement ciphermgmt
      Management object for symmetric ciphers.
      protected boolean connected
      Flag that defines if the connection is currently active.
      protected java.util.List<IConnectionListener> connectionlisteners
      Listeners for new connections.
      static byte DATA
      Data package after connection handshake.
      protected static boolean DEBUG
      Debug flag.
      protected static byte FIN
      Packet type for closing a connection.
      protected static byte FINACK
      Packet type for confirming a closed connection.
      protected IIncomingListener inclistener
      Listener for incoming messages and packets.
      protected java.util.Random insecrandom
      Insecure PRNG.
      protected static byte INT_MESSAGE_PART
      Part of an internal message.
      protected java.util.concurrent.LinkedBlockingQueue<byte[]> internalpackets
      Arrived packets that are used to manage the connection
      protected static byte KEEPALIVE
      Keepalive packet.
      protected long keepalivereceived
      Timestamp of the last received keepalive packet.
      protected long keepalivesent
      Timestamp of the last sent keepalive packet.
      protected byte[] keypackage
      Key package built during connection establishment.
      protected java.util.List<IKeyVerifier> keyverifiers
      List of externally-provided key verifiers.
      protected long latency
      Current latency.
      protected static byte MESSAGE_PART
      Part of a message.
      protected java.util.Queue<OutgoingMessage> messagequeue
      Queue of scheduled messages.
      protected java.util.Map<java.lang.Long,​IncomingMessage> messages
      Incoming messages
      protected static byte PACKET
      Packet with data requiring no confirmation.
      protected java.util.LinkedList<MessagePart> queuedmessageparts
      Parts of messages that have been queued and awaiting transmission.
      protected static byte RDY
      Response to a SYNACK, confirming readiness by the initiator.
      protected static byte REKEY
      Message type used to rekey the connection.
      protected IUdpCallback<java.lang.Boolean> rekeycallback  
      protected java.net.SocketAddress remoteaddr
      The remote peer this connection targets.
      protected java.util.Map<java.lang.Long,​MessagePart> sentwindow
      Message parts that have been sent and await confirmation.
      protected java.net.DatagramSocket socket
      The socket.
      protected static byte SYN
      Packet type for initiating a connection.
      protected static byte SYNACK
      Response to a SYN, finalizing the key exchange.
      protected IThreadExecutor texec
      The thread executor.
      protected java.util.concurrent.LinkedBlockingQueue<byte[]> userpackets
      User packets with a payload.
      protected int windowsize
      Current window size.
      protected int windowthres
      Window size threshold.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected byte[] buildSignedKeyPackage​(byte[] localpubkey, byte[] remotepubkey)
      Builds a signed package of local and remote public key.
      protected void clearQueues()
      Clears internal buffers.
      protected void connectionEstablished​(UdpConnectionHandler handler, java.net.DatagramPacket confirmation)
      Called when the connection has been established.
      protected byte[] createUserPacket​(byte[] packet)
      Creates a prepared packet as encrypted user packet.
      protected void disconnect()
      Notifies the remote host of a disconnect and terminates the connection.
      protected byte[] extractAndVerifyPubKey​(byte[] localpubkey, byte[] buffer, int offset)
      Verifies a signed package of local and remote public key.
      java.net.SocketAddress getRemoteAddress()
      Returns the remote address.
      protected void initiate​(UdpConnectionHandler handler)
      Method for initiating the connection.
      boolean isConnected()
      Checks if the connection is working.
      protected void processInternalMessage​(byte[] incmsg)
      Processes an internal message.
      protected void rawPacketReceived​(byte[] data)
      Called when a package is received.
      byte[] receive()
      Receives a message / packet (blocking).
      byte[] receive​(long timeout)
      Receives a message / packet (blocking).
      protected void respond​(byte[] initial, UdpConnectionHandler handler)
      Response actions for an attempt to initate a connection.
      protected void scheduleMessage​(OutgoingMessage msg, boolean internal)  
      protected void sendLoop()
      Loop for dispatching packets to the remote host.
      void sendMessage​(byte[] msg)
      Send a message.
      void sendMessage​(byte[] msg, IUdpCallback<java.lang.Boolean> callback)
      Send a message.
      void sendPacket​(byte[] data)
      Sends a packet.
      protected void userPacketReceived​(SymCipherSuite ciphersuite, byte[] data)
      Method for handling decrypted user-level packets.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SYN

        protected static final byte SYN
        Packet type for initiating a connection.
        See Also:
        Constant Field Values
      • SYNACK

        protected static final byte SYNACK
        Response to a SYN, finalizing the key exchange.
        See Also:
        Constant Field Values
      • RDY

        protected static final byte RDY
        Response to a SYNACK, confirming readiness by the initiator.
        See Also:
        Constant Field Values
      • DATA

        public static final byte DATA
        Data package after connection handshake.
        See Also:
        Constant Field Values
      • FIN

        protected static final byte FIN
        Packet type for closing a connection.
        See Also:
        Constant Field Values
      • FINACK

        protected static final byte FINACK
        Packet type for confirming a closed connection.
        See Also:
        Constant Field Values
      • ACK

        protected static final byte ACK
        Acknowledgement of a message part.
        See Also:
        Constant Field Values
      • PACKET

        protected static final byte PACKET
        Packet with data requiring no confirmation.
        See Also:
        Constant Field Values
      • MESSAGE_PART

        protected static final byte MESSAGE_PART
        Part of a message.
        See Also:
        Constant Field Values
      • INT_MESSAGE_PART

        protected static final byte INT_MESSAGE_PART
        Part of an internal message.
        See Also:
        Constant Field Values
      • REKEY

        protected static final byte REKEY
        Message type used to rekey the connection.
        See Also:
        Constant Field Values
      • socket

        protected java.net.DatagramSocket socket
        The socket.
      • connectionlisteners

        protected java.util.List<IConnectionListener> connectionlisteners
        Listeners for new connections.
      • keyverifiers

        protected java.util.List<IKeyVerifier> keyverifiers
        List of externally-provided key verifiers.
      • remoteaddr

        protected java.net.SocketAddress remoteaddr
        The remote peer this connection targets.
      • messagequeue

        protected java.util.Queue<OutgoingMessage> messagequeue
        Queue of scheduled messages.
      • queuedmessageparts

        protected java.util.LinkedList<MessagePart> queuedmessageparts
        Parts of messages that have been queued and awaiting transmission.
      • sentwindow

        protected java.util.Map<java.lang.Long,​MessagePart> sentwindow
        Message parts that have been sent and await confirmation.
      • messages

        protected java.util.Map<java.lang.Long,​IncomingMessage> messages
        Incoming messages
      • windowsize

        protected volatile int windowsize
        Current window size.
      • windowthres

        protected volatile int windowthres
        Window size threshold.
      • latency

        protected volatile long latency
        Current latency.
      • insecrandom

        protected java.util.Random insecrandom
        Insecure PRNG.
      • internalpackets

        protected java.util.concurrent.LinkedBlockingQueue<byte[]> internalpackets
        Arrived packets that are used to manage the connection
      • userpackets

        protected java.util.concurrent.LinkedBlockingQueue<byte[]> userpackets
        User packets with a payload.
      • inclistener

        protected IIncomingListener inclistener
        Listener for incoming messages and packets.
      • keypackage

        protected volatile byte[] keypackage
        Key package built during connection establishment.
      • ciphermgmt

        protected SymCipherManagement ciphermgmt
        Management object for symmetric ciphers.
      • rekeycallback

        protected IUdpCallback<java.lang.Boolean> rekeycallback
      • connected

        protected volatile boolean connected
        Flag that defines if the connection is currently active.
      • keepalivereceived

        protected volatile long keepalivereceived
        Timestamp of the last received keepalive packet.
      • keepalivesent

        protected volatile long keepalivesent
        Timestamp of the last sent keepalive packet.
    • Method Detail

      • isConnected

        public boolean isConnected()
        Checks if the connection is working.
        Returns:
        True, if data can be send.
      • sendPacket

        public void sendPacket​(byte[] data)
        Sends a packet. Maximum size is at least 60000 bytes, delivery is not guaranteed.
        Parameters:
        data - The data being send.
      • sendMessage

        public void sendMessage​(byte[] msg)
        Send a message. Size is not limited, delivery is guaranteed unless a failure occurs.
        Parameters:
        msg - The message.
      • sendMessage

        public void sendMessage​(byte[] msg,
                                IUdpCallback<java.lang.Boolean> callback)
        Send a message. Size is not limited, delivery is guaranteed unless a failure occurs.
        Parameters:
        msg - The message.
        callback - Callback invoked with result of the send operation, true if the message was received, false on failure.
      • receive

        public byte[] receive()
        Receives a message / packet (blocking).
        Returns:
        The message/packet.
      • receive

        public byte[] receive​(long timeout)
        Receives a message / packet (blocking).
        Returns:
        The message/packet.
      • getRemoteAddress

        public java.net.SocketAddress getRemoteAddress()
        Returns the remote address.
        Returns:
        Remote address.
      • initiate

        protected void initiate​(UdpConnectionHandler handler)
        Method for initiating the connection.
        Parameters:
        handler - The handler.
      • respond

        protected void respond​(byte[] initial,
                               UdpConnectionHandler handler)
        Response actions for an attempt to initate a connection.
        Parameters:
        initial - Initiation packaet data received that caused the response activity.
        handler - The handler.
      • connectionEstablished

        protected void connectionEstablished​(UdpConnectionHandler handler,
                                             java.net.DatagramPacket confirmation)
        Called when the connection has been established.
        Parameters:
        confirmation - Confirmation packet.
      • disconnect

        protected void disconnect()
        Notifies the remote host of a disconnect and terminates the connection.
      • rawPacketReceived

        protected void rawPacketReceived​(byte[] data)
        Called when a package is received.
        Parameters:
        data - Raw package data.
      • userPacketReceived

        protected void userPacketReceived​(SymCipherSuite ciphersuite,
                                          byte[] data)
                                   throws java.lang.Exception
        Method for handling decrypted user-level packets.
        Parameters:
        data - Packet data.
        Throws:
        java.lang.Exception - Any exception that may occur and needs to be handled on a higher level.
      • sendLoop

        protected void sendLoop()
        Loop for dispatching packets to the remote host.
      • createUserPacket

        protected byte[] createUserPacket​(byte[] packet)
        Creates a prepared packet as encrypted user packet.
        Parameters:
        packet - The inner packet.
      • buildSignedKeyPackage

        protected byte[] buildSignedKeyPackage​(byte[] localpubkey,
                                               byte[] remotepubkey)
        Builds a signed package of local and remote public key.
        Parameters:
        localpubkey - Local public key.
        remotepubkey - Remote public key.
        Returns:
        Signed package.
      • extractAndVerifyPubKey

        protected byte[] extractAndVerifyPubKey​(byte[] localpubkey,
                                                byte[] buffer,
                                                int offset)
        Verifies a signed package of local and remote public key.
        Parameters:
        localpubkey - Local public key for comparison
        buffer - Buffer containing package.
        offset - Offset of the package.
        Returns:
        Extracted remote public key if verified, null if verification failed.
      • processInternalMessage

        protected void processInternalMessage​(byte[] incmsg)
        Processes an internal message.
        Parameters:
        incmsg - The message.
      • scheduleMessage

        protected void scheduleMessage​(OutgoingMessage msg,
                                       boolean internal)
      • clearQueues

        protected void clearQueues()
        Clears internal buffers.