Package org.activecomponents.udp
Class Connection
- java.lang.Object
-
- org.activecomponents.udp.Connection
-
public class Connection extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description protected static byteACKAcknowledgement of a message part.protected SymCipherManagementciphermgmtManagement object for symmetric ciphers.protected booleanconnectedFlag that defines if the connection is currently active.protected java.util.List<IConnectionListener>connectionlistenersListeners for new connections.static byteDATAData package after connection handshake.protected static booleanDEBUGDebug flag.protected static byteFINPacket type for closing a connection.protected static byteFINACKPacket type for confirming a closed connection.protected IIncomingListenerinclistenerListener for incoming messages and packets.protected java.util.RandominsecrandomInsecure PRNG.protected static byteINT_MESSAGE_PARTPart of an internal message.protected java.util.concurrent.LinkedBlockingQueue<byte[]>internalpacketsArrived packets that are used to manage the connectionprotected static byteKEEPALIVEKeepalive packet.protected longkeepalivereceivedTimestamp of the last received keepalive packet.protected longkeepalivesentTimestamp of the last sent keepalive packet.protected byte[]keypackageKey package built during connection establishment.protected java.util.List<IKeyVerifier>keyverifiersList of externally-provided key verifiers.protected longlatencyCurrent latency.protected static byteMESSAGE_PARTPart of a message.protected java.util.Queue<OutgoingMessage>messagequeueQueue of scheduled messages.protected java.util.Map<java.lang.Long,IncomingMessage>messagesIncoming messagesprotected static bytePACKETPacket with data requiring no confirmation.protected java.util.LinkedList<MessagePart>queuedmessagepartsParts of messages that have been queued and awaiting transmission.protected static byteRDYResponse to a SYNACK, confirming readiness by the initiator.protected static byteREKEYMessage type used to rekey the connection.protected IUdpCallback<java.lang.Boolean>rekeycallbackprotected java.net.SocketAddressremoteaddrThe remote peer this connection targets.protected java.util.Map<java.lang.Long,MessagePart>sentwindowMessage parts that have been sent and await confirmation.protected java.net.DatagramSocketsocketThe socket.protected static byteSYNPacket type for initiating a connection.protected static byteSYNACKResponse to a SYN, finalizing the key exchange.protected IThreadExecutortexecThe thread executor.protected java.util.concurrent.LinkedBlockingQueue<byte[]>userpacketsUser packets with a payload.protected intwindowsizeCurrent window size.protected intwindowthresWindow size threshold.
-
Constructor Summary
Constructors Constructor Description Connection(java.net.DatagramSocket socket, java.util.List<IConnectionListener> connectionlisteners, java.net.SocketAddress remoteaddr, IThreadExecutor texec, KeyExchangeGenerator keyexgen, int symcipherclassid, java.util.List<IKeyVerifier> keyverifiers, IIncomingListener inclistener)
-
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 voidclearQueues()Clears internal buffers.protected voidconnectionEstablished(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 voiddisconnect()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.SocketAddressgetRemoteAddress()Returns the remote address.protected voidinitiate(UdpConnectionHandler handler)Method for initiating the connection.booleanisConnected()Checks if the connection is working.protected voidprocessInternalMessage(byte[] incmsg)Processes an internal message.protected voidrawPacketReceived(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 voidrespond(byte[] initial, UdpConnectionHandler handler)Response actions for an attempt to initate a connection.protected voidscheduleMessage(OutgoingMessage msg, boolean internal)protected voidsendLoop()Loop for dispatching packets to the remote host.voidsendMessage(byte[] msg)Send a message.voidsendMessage(byte[] msg, IUdpCallback<java.lang.Boolean> callback)Send a message.voidsendPacket(byte[] data)Sends a packet.protected voiduserPacketReceived(SymCipherSuite ciphersuite, byte[] data)Method for handling decrypted user-level packets.
-
-
-
Field Detail
-
DEBUG
protected static final boolean DEBUG
Debug flag.- See Also:
- Constant Field Values
-
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
-
KEEPALIVE
protected static final byte KEEPALIVE
Keepalive packet.- 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.
-
texec
protected IThreadExecutor texec
The thread executor.
-
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.
-
-
Constructor Detail
-
Connection
public Connection(java.net.DatagramSocket socket, java.util.List<IConnectionListener> connectionlisteners, java.net.SocketAddress remoteaddr, IThreadExecutor texec, KeyExchangeGenerator keyexgen, int symcipherclassid, java.util.List<IKeyVerifier> keyverifiers, IIncomingListener inclistener)
-
-
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 comparisonbuffer- 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.
-
-