Binds the UDP port. More...
Public Member Functions | |
DatagramChannel (int localPort) | |
Constructor for the DatagramChannel object. | |
int | getLocalPort () |
Returns used local UDP port. | |
void | useSymmetricCipher (SymmetricCipher cipherEngine) |
Sets cipher to be used for PDU ciphering. | |
SymmetricCipher | getUsedSymmetricCipher () |
Gets current cipher used for PDU ciphering. | |
void | addNewPeer (RemotePeer remotePeer) |
Adds new peer to receive incoming PDUs. | |
boolean | hasRemotePeer () |
Returns if there is active remote peer. | |
RemotePeer | getRemotePeer () |
Returns the remote peer. | |
boolean | isPearDead (int maxIdleTimeMillis) |
Returns if peer seems to be dead (we are not receiving PDUs from it). | |
void | removePeer () |
Detaches peer and all its call from the UDP receiver. | |
void | stop () |
Stops PDU receiver thread. | |
void | run () |
Receives (and deciphers) PDUs from remote peers in a loop. | |
void | send (OctetBuffer pdu, InetAddress peerAddr, int peerPort) |
Encrypts and sends PDUs to remote peer. | |
Protected Member Functions | |
void | packetDump (byte[] octets, int len, InetAddress addr, int port, boolean incoming) |
Dumps information of a frame (in bytes) to standard error. | |
Private Attributes | |
int | localPort |
The local UDP port where datagram socket is bound. | |
DatagramSocket | udpReceiver |
The UDP receiver socket. | |
Thread | pduReceiverThread |
The UDP receiver worker thread. | |
volatile boolean | running |
Indicates that worker thread should be running (receiving datagrams) | |
RemotePeer | remotePeer |
Current remote peer receiving datagrams from UDP socket. | |
SymmetricCipher | usedPduCipher |
Currently used symmetric cipher. | |
Static Private Attributes | |
static final int | INBOUND_UDP_BUFFER_SIZE = 4096 |
The default size for the inbound UDP buffer. |
Binds the UDP port.
Each peer is associated with one DatagramChannel object.
Definition at line 24 of file DatagramChannel.java.
protocol.DatagramChannel.DatagramChannel | ( | int | localPort ) |
Constructor for the DatagramChannel object.
localPort | local UDP port receiving peer's PDUs |
SocketException | Thrown if the UDP socket cannot be created |
UnknownHostException | Thrown if the remoteHost cannot be resolved |
Definition at line 55 of file DatagramChannel.java.
References protocol.DatagramChannel.pduReceiverThread, protocol.DatagramChannel.remotePeer, protocol.DatagramChannel.running, protocol.DatagramChannel.udpReceiver, and protocol.DatagramChannel.usedPduCipher.
{ this.remotePeer = null; this.usedPduCipher = null; this.running = false; this.localPort = -1; for( int i = localPort; i < localPort + 100; ++i ) { try { this.udpReceiver = new DatagramSocket( i ); this.localPort = i; break; } catch( SocketException e ) { /* ignore error and continue search for unbound port */ } } /* If binded UDP port, start worker thread. */ if ( this.localPort > 0 ) { Log.trace( "Bound to UDP port " + this.localPort ); this.running = true; pduReceiverThread = new Thread( this, "UDP" ); pduReceiverThread.setPriority( Thread.MAX_PRIORITY - 1 ); pduReceiverThread.start (); } }
void protocol.DatagramChannel.addNewPeer | ( | RemotePeer | remotePeer ) |
Adds new peer to receive incoming PDUs.
Definition at line 117 of file DatagramChannel.java.
References protocol.DatagramChannel.remotePeer.
Referenced by protocol.RemotePeer.RemotePeer().
{ this.remotePeer = remotePeer; }
int protocol.DatagramChannel.getLocalPort | ( | ) |
Returns used local UDP port.
Definition at line 89 of file DatagramChannel.java.
References protocol.DatagramChannel.udpReceiver.
Referenced by CryptoPhoneApp.acceptIncomingCall(), CryptoPhoneApp.deferredOnInvite(), CryptoPhoneApp.executeCommand(), and CryptoPhoneApp.startKryptofonServices().
{ return this.udpReceiver.getLocalPort (); }
RemotePeer protocol.DatagramChannel.getRemotePeer | ( | ) |
Returns the remote peer.
Definition at line 133 of file DatagramChannel.java.
References protocol.DatagramChannel.remotePeer.
Referenced by CryptoPhoneApp.executeCommand(), CryptoPhoneApp.mainTimerEvent(), and CryptoPhoneApp.sendInstantMessage().
{ return this.remotePeer; }
SymmetricCipher protocol.DatagramChannel.getUsedSymmetricCipher | ( | ) |
Gets current cipher used for PDU ciphering.
Definition at line 109 of file DatagramChannel.java.
References protocol.DatagramChannel.usedPduCipher.
Referenced by CryptoPhoneApp.deferredOnInstantMessage(), and CryptoPhoneApp.sendInstantMessage().
{ return this.usedPduCipher; }
boolean protocol.DatagramChannel.hasRemotePeer | ( | ) |
Returns if there is active remote peer.
Definition at line 125 of file DatagramChannel.java.
References protocol.DatagramChannel.remotePeer.
Referenced by CryptoPhoneApp.acceptIncomingCall(), CryptoPhoneApp.deferredOnAccept(), CryptoPhoneApp.deferredOnBye(), CryptoPhoneApp.deferredOnInvite(), CryptoPhoneApp.deferredOnRing(), CryptoPhoneApp.executeCommand(), and CryptoPhoneApp.tryToVerifyInvitingCall().
{ return this.remotePeer != null; }
boolean protocol.DatagramChannel.isPearDead | ( | int | maxIdleTimeMillis ) |
Returns if peer seems to be dead (we are not receiving PDUs from it).
Definition at line 141 of file DatagramChannel.java.
References protocol.RemotePeer.receiverIdleTime(), and protocol.DatagramChannel.remotePeer.
Referenced by CryptoPhoneApp.mainTimerEvent().
{ if ( this.remotePeer == null ) { return false; // cannot be dead if does not exists } return this.remotePeer.receiverIdleTime () > maxIdleTimeMillis; }
void protocol.DatagramChannel.packetDump | ( | byte[] | octets, |
int | len, | ||
InetAddress | addr, | ||
int | port, | ||
boolean | incoming | ||
) | [protected] |
Dumps information of a frame (in bytes) to standard error.
octets | The octets of the in- or outgoing PDU |
len | The size of PDU |
addr | The remote host address |
port | The port number |
incoming | Indicates if it is inbound (true) or outgoing (false) PDU |
Definition at line 283 of file DatagramChannel.java.
Referenced by protocol.DatagramChannel.run(), and protocol.DatagramChannel.send().
{ if ( ! Log.isEnabled( Log.PDU ) ) { return; } StringBuffer sb = new StringBuffer( 500 ); sb.append( incoming ? "Packet from <--- " : "Packet to ---> " ); sb.append( addr.getHostAddress() ).append( ":" ).append( port ); sb.append( ", size = ").append( len ).append( "\n\n" ); sb.append( Log.toHex( octets, len, " " ) ).append( "\n" ); Log.pdu( sb.toString () ); }
void protocol.DatagramChannel.removePeer | ( | ) |
Detaches peer and all its call from the UDP receiver.
Definition at line 153 of file DatagramChannel.java.
References protocol.RemotePeer.cleanUp(), protocol.DatagramChannel.remotePeer, and protocol.DatagramChannel.usedPduCipher.
Referenced by CryptoPhoneApp.deferredOnBye(), and CryptoPhoneApp.executeCommand().
{ this.usedPduCipher = null; if ( this.remotePeer != null ) { this.remotePeer.cleanUp (); this.remotePeer = null; } }
void protocol.DatagramChannel.run | ( | ) |
Receives (and deciphers) PDUs from remote peers in a loop.
Dispatches inbound PDUs to associated instance of the RemotePeer that will handle PDUs.
Definition at line 196 of file DatagramChannel.java.
References protocol.RemotePeer.addIncomingPDU(), crypto.SymmetricCipher.decrypt(), protocol.DatagramChannel.INBOUND_UDP_BUFFER_SIZE, protocol.DatagramChannel.packetDump(), protocol.DatagramChannel.remotePeer, protocol.DatagramChannel.running, protocol.DatagramChannel.udpReceiver, and protocol.DatagramChannel.usedPduCipher.
{ Log.trace( "Thread started" ); /* Receive and dispatch datagrams */ byte[] buff = new byte[ INBOUND_UDP_BUFFER_SIZE ]; while( running ) { DatagramPacket packet = new DatagramPacket( buff, buff.length ); try { udpReceiver.receive( packet ); byte[] pdu = new byte[ packet.getLength () ]; System.arraycopy( buff, 0, pdu, 0, pdu.length ); InetAddress peerAddr = packet.getAddress (); int peerPort = packet.getPort (); // packetDump( pdu, pdu.length, peerAddr, peerPort, true ); if ( usedPduCipher != null ) { pdu = usedPduCipher.decrypt( /*randomPreambleLen*/ 8, pdu ); } if ( pdu != null ) { packetDump( pdu, pdu.length, peerAddr, peerPort, true ); if ( remotePeer != null ) { remotePeer.addIncomingPDU( pdu ); } } } catch( IOException e ) { if ( running ) { Log.exception( Log.WARN, e ); } } } }
void protocol.DatagramChannel.send | ( | OctetBuffer | pdu, |
InetAddress | peerAddr, | ||
int | peerPort | ||
) |
Encrypts and sends PDUs to remote peer.
Definition at line 245 of file DatagramChannel.java.
References crypto.SymmetricCipher.encrypt(), utils.OctetBuffer.getPosition(), utils.OctetBuffer.getStore(), protocol.DatagramChannel.packetDump(), protocol.DatagramChannel.udpReceiver, and protocol.DatagramChannel.usedPduCipher.
Referenced by protocol.RemotePeer.send().
{ try { packetDump( pdu.getStore (), pdu.getPosition (), peerAddr, peerPort, false ); byte[] datagram = new byte[ pdu.getPosition () ]; System.arraycopy( pdu.getStore (), 0, datagram, 0, datagram.length ); if ( usedPduCipher != null ) { datagram = usedPduCipher.encrypt( /*randomPreambleLen*/ 8, datagram ); } if ( datagram != null ) { // packetDump( datagram, datagram.length, peerAddr, peerPort, false ); DatagramPacket packet = new DatagramPacket( datagram, datagram.length, peerAddr, peerPort ); this.udpReceiver.send( packet ); } } catch( Exception e ) { Log.exception( Log.WARN, e ); } }
void protocol.DatagramChannel.stop | ( | ) |
Stops PDU receiver thread.
Definition at line 167 of file DatagramChannel.java.
References protocol.DatagramChannel.pduReceiverThread, protocol.DatagramChannel.running, and protocol.DatagramChannel.udpReceiver.
Referenced by CryptoPhoneApp.stopKryptofonServices().
{ running = false; if ( pduReceiverThread != null ) { udpReceiver.close (); Log.debug( "Closed Socket" ); try { pduReceiverThread.join (); Log.debug( "Joined binder thread" ); } catch( InterruptedException e ) { Log.exception( Log.WARN, e ); } pduReceiverThread = null; } }
void protocol.DatagramChannel.useSymmetricCipher | ( | SymmetricCipher | cipherEngine ) |
Sets cipher to be used for PDU ciphering.
Definition at line 97 of file DatagramChannel.java.
References crypto.SymmetricCipher.getAlgorithmDesc(), and protocol.DatagramChannel.usedPduCipher.
Referenced by CryptoPhoneApp.acceptIncomingCall(), and CryptoPhoneApp.deferredOnAccept().
{ usedPduCipher = cipherEngine; if ( cipherEngine != null ) { Log.trace( "Using PDU cipher: " + cipherEngine.getAlgorithmDesc () ); } }
final int protocol.DatagramChannel.INBOUND_UDP_BUFFER_SIZE = 4096 [static, private] |
The default size for the inbound UDP buffer.
Definition at line 27 of file DatagramChannel.java.
Referenced by protocol.DatagramChannel.run().
int protocol.DatagramChannel.localPort [private] |
The local UDP port where datagram socket is bound.
Definition at line 30 of file DatagramChannel.java.
Thread protocol.DatagramChannel.pduReceiverThread [private] |
The UDP receiver worker thread.
Definition at line 36 of file DatagramChannel.java.
Referenced by protocol.DatagramChannel.DatagramChannel(), and protocol.DatagramChannel.stop().
Current remote peer receiving datagrams from UDP socket.
Definition at line 42 of file DatagramChannel.java.
Referenced by protocol.DatagramChannel.addNewPeer(), protocol.DatagramChannel.DatagramChannel(), protocol.DatagramChannel.getRemotePeer(), protocol.DatagramChannel.hasRemotePeer(), protocol.DatagramChannel.isPearDead(), protocol.DatagramChannel.removePeer(), and protocol.DatagramChannel.run().
volatile boolean protocol.DatagramChannel.running [private] |
Indicates that worker thread should be running (receiving datagrams)
Definition at line 39 of file DatagramChannel.java.
Referenced by protocol.DatagramChannel.DatagramChannel(), protocol.DatagramChannel.run(), and protocol.DatagramChannel.stop().
DatagramSocket protocol.DatagramChannel.udpReceiver [private] |
The UDP receiver socket.
Definition at line 33 of file DatagramChannel.java.
Referenced by protocol.DatagramChannel.DatagramChannel(), protocol.DatagramChannel.getLocalPort(), protocol.DatagramChannel.run(), protocol.DatagramChannel.send(), and protocol.DatagramChannel.stop().
Currently used symmetric cipher.
Definition at line 45 of file DatagramChannel.java.
Referenced by protocol.DatagramChannel.DatagramChannel(), protocol.DatagramChannel.getUsedSymmetricCipher(), protocol.DatagramChannel.removePeer(), protocol.DatagramChannel.run(), protocol.DatagramChannel.send(), and protocol.DatagramChannel.useSymmetricCipher().