public class StunStack extends Object implements MessageEventHandler
Modifier and Type | Class and Description |
---|---|
private class |
StunStack.ExpiredServerTransactionsCollector
Class which performs periodic collection of expired transactions.
|
Modifier and Type | Field and Description |
---|---|
private Hashtable<TransactionID,StunClientTransaction> |
clientTransactions
Stores active client transactions mapped against TransactionID-s.
|
private CredentialsManager |
credentialsManager
The
CredentialsManager that we are using for retrieving
passwords. |
private EventDispatcher |
eventDispatcher
A dispatcher for incoming requests event;
|
private StunStack.ExpiredServerTransactionsCollector |
expiredTransactionsCollector
The ExpiredServerTransactionsCollector which expires
the StunServerTransactions of this StunStack and
removes them from
serverTransactions . |
private static Logger |
logger
The Logger used by the StunStack class and its
instances for logging output.
|
private static Mac |
mac
The indicator which determines whether
Mac.getInstance(MessageIntegrityAttribute.HMAC_SHA1_ALGORITHM)
has been called. |
private NetAccessManager |
netAccessManager
Our network gateway.
|
private static PacketLogger |
packetLogger
The packet logger instance.
|
private Hashtable<TransactionID,StunServerTransaction> |
serverTransactions
Currently open server transactions.
|
private static ScheduledExecutorService |
tasksScheduler
The ScheduledExecutorService to execute StunStack scheduled tasks,
in particular - expired server transactions collector.
|
Constructor and Description |
---|
StunStack()
Initializes a new StunStack instance.
|
StunStack(PeerUdpMessageEventHandler peerUdpMessageEventHandler,
ChannelDataEventHandler channelDataEventHandler)
Initializes a new StunStack instance with given
peerUdpMessageEventHandler and channelDataEventHandler.
|
Modifier and Type | Method and Description |
---|---|
void |
addIndicationListener(TransportAddress localAddr,
MessageEventHandler indicationListener)
Adds a new MessageEventHandler which is to be notified about
STUN indications received at a specific local TransportAddress.
|
void |
addOldIndicationListener(TransportAddress localAddr,
MessageEventHandler indicationListener)
Adds a new MessageEventHandler which is to be notified about
old indications received at a specific local TransportAddress.
|
void |
addRequestListener(RequestListener requestListener)
Sets the listener that should be notified when a new Request is received.
|
void |
addRequestListener(TransportAddress localAddress,
RequestListener listener)
Add a RequestListener for requests coming from a specific NetAccessPoint.
|
void |
addSocket(IceSocketWrapper sock)
Creates and starts a Network Access Point (Connector) based on the
specified socket.
|
void |
addSocket(IceSocketWrapper sock,
TransportAddress remoteAddress)
Creates and starts a Network Access Point (Connector) based on the
specified socket and the specified remote address.
|
void |
cancelTransaction(TransactionID transactionID)
Cancels the
StunClientTransaction with the specified
transactionID. |
private void |
cancelTransactionsForAddress(TransportAddress localAddr,
TransportAddress remoteAddr)
Stops all transactions for the specified localAddr so that they
won't send messages through any longer and so that we could remove the
associated socket.
|
Response |
createCorrespondingErrorResponse(char requestType,
char errorCode,
String reasonPhrase,
char... unknownAttributes)
Returns the Error Response object with specified errorCode and
reasonPhrase corresponding to input type.
|
protected StunClientTransaction |
getClientTransaction(byte[] transactionID)
Returns the transaction with the specified transactionID or
null if no such transaction exists.
|
CredentialsManager |
getCredentialsManager()
Returns the
CredentialsManager that this stack is using for
verification of MessageIntegrityAttribute s. |
(package private) NetAccessManager |
getNetAccessManager()
Returns the currently active instance of NetAccessManager.
|
static PacketLogger |
getPacketLogger()
Returns the currently set packet logger.
|
protected StunServerTransaction |
getServerTransaction(byte[] transactionID)
Returns the transaction with the specified transactionID or
null if no such transaction exists.
|
protected StunServerTransaction |
getServerTransaction(TransactionID transactionID)
Returns the transaction with the specified transactionID or
null if no such transaction exists.
|
void |
handleMessageEvent(StunMessageEvent ev)
Called to notify this provider for an incoming message.
|
static boolean |
isPacketLoggerEnabled()
Checks whether packet logger is set and enabled.
|
static void |
logPacketToPcap(DatagramPacket p,
boolean isSent,
InetAddress interfaceAddress,
int interfacePort)
Logs a specific DatagramPacket using the packet logger of the
StunStack.
|
(package private) void |
removeClientTransaction(StunClientTransaction tran)
Removes a client transaction from this providers client transactions
list.
|
void |
removeIndicationListener(TransportAddress localAddr,
MessageEventHandler indicationListener)
Removes an existing MessageEventHandler to no longer be notified
about STUN indications received at a specific local
TransportAddress.
|
void |
removeRequestListener(RequestListener listener)
Removes the specified listener from the local listener list.
|
(package private) void |
removeServerTransaction(StunServerTransaction tran)
Removes a server transaction from this provider's server transactions
list.
|
void |
removeSocket(TransportAddress localAddr)
Stops and deletes the connector listening on the specified local address.
|
void |
removeSocket(TransportAddress localAddr,
TransportAddress remoteAddr)
Stops and deletes the connector listening on the specified local address
and remote address.
|
void |
sendChannelData(ChannelData channelData,
TransportAddress sendTo,
TransportAddress sendThrough)
Sends a specific STUN Indication to a specific destination
TransportAddress through a socket registered with this
StunStack using a specific TransportAddress.
|
void |
sendIndication(Indication indication,
TransportAddress sendTo,
TransportAddress sendThrough)
Sends a specific STUN Indication to a specific destination
TransportAddress through a socket registered with this
StunStack using a specific TransportAddress.
|
TransactionID |
sendRequest(Request request,
TransportAddress sendTo,
DatagramSocket sendThrough,
ResponseCollector collector)
Sends the specified request through the specified access point, and
registers the specified ResponseCollector for later notification.
|
TransactionID |
sendRequest(Request request,
TransportAddress sendTo,
TransportAddress sendThrough,
ResponseCollector collector)
Sends the specified request through the specified access point, and
registers the specified ResponseCollector for later notification.
|
TransactionID |
sendRequest(Request request,
TransportAddress sendTo,
TransportAddress sendThrough,
ResponseCollector collector,
TransactionID transactionID)
Sends the specified request through the specified access point, and
registers the specified ResponseCollector for later notification.
|
TransactionID |
sendRequest(Request request,
TransportAddress sendTo,
TransportAddress sendThrough,
ResponseCollector collector,
TransactionID transactionID,
int originalWaitInterval,
int maxWaitInterval,
int maxRetransmissions)
Sends the specified request through the specified access point, and
registers the specified ResponseCollector for later notification.
|
void |
sendResponse(byte[] transactionID,
Response response,
TransportAddress sendThrough,
TransportAddress sendTo)
Sends the specified response message through the specified access point.
|
void |
sendUdpMessage(RawMessage udpMessage,
TransportAddress sendTo,
TransportAddress sendThrough)
Sends a specific STUN Indication to a specific destination
TransportAddress through a socket registered with this
StunStack using a specific TransportAddress.
|
static void |
setPacketLogger(PacketLogger packetLogger)
Setting a packet logger for the stack.
|
void |
shutDown()
Cancels all running transactions and prepares for garbage collection
|
private static String |
toHexString(byte[] bytes)
Returns a String representation of a specific byte
array as an unsigned integer in base 16.
|
boolean |
validateMessageIntegrity(MessageIntegrityAttribute msgInt,
String username,
boolean shortTermCredentialMechanism,
RawMessage message)
Recalculates the HMAC-SHA1 signature of the message array so
that we could compare it with the value brought by the
MessageIntegrityAttribute . |
private void |
validateRequestAttributes(StunMessageEvent evt)
Executes actions related specific attributes like asserting proper
checksums or verifying the validity of user names.
|
private boolean |
validateUsername(String username)
Asserts the validity of a specific username (e.g.
|
private static final Logger logger
private static Mac mac
Mac.getInstance(MessageIntegrityAttribute.HMAC_SHA1_ALGORITHM)
has been called.StunStack()
private static final ScheduledExecutorService tasksScheduler
private final NetAccessManager netAccessManager
private final CredentialsManager credentialsManager
CredentialsManager
that we are using for retrieving
passwords.private final Hashtable<TransactionID,StunClientTransaction> clientTransactions
private StunStack.ExpiredServerTransactionsCollector expiredTransactionsCollector
serverTransactions
.private final Hashtable<TransactionID,StunServerTransaction> serverTransactions
private final EventDispatcher eventDispatcher
private static PacketLogger packetLogger
public StunStack(PeerUdpMessageEventHandler peerUdpMessageEventHandler, ChannelDataEventHandler channelDataEventHandler)
peerUdpMessageEventHandler
- the PeerUdpMessageEventHandler
that will handle incoming UDP messages which are not STUN
messages and ChannelData messages.channelDataEventHandler
- the ChannelDataEventHandler that
will handle incoming UDP messages which are ChannelData
messages.public StunStack()
public void addSocket(IceSocketWrapper sock)
sock
- The socket that the new access point should represent.public void addSocket(IceSocketWrapper sock, TransportAddress remoteAddress)
sock
- The socket that the new access point should represent.remoteAddress
- the remote address of the socket of the
Connector
to be created if it is a TCP socket, or null if it
is UDP.public void removeSocket(TransportAddress localAddr)
removeSocket(org.ice4j.TransportAddress, org.ice4j.TransportAddress)
with the appropriate remote address for TCP.localAddr
- the local address of the socket to remove.public void removeSocket(TransportAddress localAddr, TransportAddress remoteAddr)
localAddr
- the local address of the socket to remove.remoteAddr
- the remote address of the socket to remove. Use
null for UDP.protected StunClientTransaction getClientTransaction(byte[] transactionID)
transactionID
- the ID of the transaction we are looking for.StunClientTransaction
we are looking for.protected StunServerTransaction getServerTransaction(byte[] transactionID)
transactionID
- the ID of the transaction we are looking for.StunClientTransaction
we are looking for.protected StunServerTransaction getServerTransaction(TransactionID transactionID)
transactionID
- the ID of the transaction we are looking for.StunClientTransaction
we are looking for.public void cancelTransaction(TransactionID transactionID)
StunClientTransaction
with the specified
transactionID. Cancellation means that the stack will not
retransmit the request, will not treat the lack of response to be a
failure, but will wait the duration of the transaction timeout for a
response.transactionID
- the TransactionID
of the
StunClientTransaction
to cancelprivate void cancelTransactionsForAddress(TransportAddress localAddr, TransportAddress remoteAddr)
localAddr
- the TransportAddress that we'd like to remove
transactions for.remoteAddr
- the remote TransportAddress that we'd like to
remove transactions for. If null, then it will not be taken
into account (that is, all transactions with for localAddr will
be cancelled).NetAccessManager getNetAccessManager()
public void sendChannelData(ChannelData channelData, TransportAddress sendTo, TransportAddress sendThrough) throws StunException
channelData
- the STUN Indication to be sent to the
specified destination TransportAddress through the socket with
the specified TransportAddresssendTo
- the TransportAddress of the destination to which
the specified indication is to be sentsendThrough
- the TransportAddress of the socket registered
with this StunStack through which the specified
indication is to be sentStunException
- if anything goes wrong while sending the specified
indication to the destination sendTo through the socket
identified by sendThroughpublic void sendUdpMessage(RawMessage udpMessage, TransportAddress sendTo, TransportAddress sendThrough) throws StunException
udpMessage
- the RawMessage to be sent to the
specified destination TransportAddress through the socket with
the specified TransportAddresssendTo
- the TransportAddress of the destination to which
the specified indication is to be sentsendThrough
- the TransportAddress of the socket registered
with this StunStack through which the specified
indication is to be sentStunException
- if anything goes wrong while sending the specified
indication to the destination sendTo through the socket
identified by sendThroughpublic void sendIndication(Indication indication, TransportAddress sendTo, TransportAddress sendThrough) throws StunException
indication
- the STUN Indication to be sent to the
specified destination TransportAddress through the socket with
the specified TransportAddresssendTo
- the TransportAddress of the destination to which
the specified indication is to be sentsendThrough
- the TransportAddress of the socket registered
with this StunStack through which the specified
indication is to be sentStunException
- if anything goes wrong while sending the specified
indication to the destination sendTo through the socket
identified by sendThroughpublic TransactionID sendRequest(Request request, TransportAddress sendTo, TransportAddress sendThrough, ResponseCollector collector) throws IOException, IllegalArgumentException
request
- the request to sendsendTo
- the destination address of the request.sendThrough
- the local address to use when sending the requestcollector
- the instance to notify when a response arrives or the
the transaction timeoutsIOException
- if an error occurs while sending message bytes
through the network socket.IllegalArgumentException
- if the apDescriptor references an
access point that had not been installed,public TransactionID sendRequest(Request request, TransportAddress sendTo, TransportAddress sendThrough, ResponseCollector collector, TransactionID transactionID) throws IllegalArgumentException, IOException
request
- the request to sendsendTo
- the destination address of the request.sendThrough
- the local address to use when sending the requestcollector
- the instance to notify when a response arrives or the
the transaction timeoutstransactionID
- the ID that we'd like the new transaction to use
in case the application created it in order to use it for application
data correlation.IllegalArgumentException
- if the apDescriptor references an
access point that had not been installed,IOException
- if an error occurs while sending message bytes
through the network socket.public TransactionID sendRequest(Request request, TransportAddress sendTo, TransportAddress sendThrough, ResponseCollector collector, TransactionID transactionID, int originalWaitInterval, int maxWaitInterval, int maxRetransmissions) throws IllegalArgumentException, IOException
request
- the request to sendsendTo
- the destination address of the request.sendThrough
- the local address to use when sending the requestcollector
- the instance to notify when a response arrives or the
the transaction timeoutstransactionID
- the ID that we'd like the new transaction to use
in case the application created it in order to use it for application
data correlation.originalWaitInterval
- The number of milliseconds to wait before
the first retransmission of the request.maxWaitInterval
- The maximum wait interval. Once this interval is
reached we should stop doubling its value.maxRetransmissions
- Maximum number of retransmissions. Once this
number is reached and if no response is received after maxWaitInterval
milliseconds the request is considered unanswered.IllegalArgumentException
- if the apDescriptor references an
access point that had not been installed,IOException
- if an error occurs while sending message bytes
through the network socket.public TransactionID sendRequest(Request request, TransportAddress sendTo, DatagramSocket sendThrough, ResponseCollector collector) throws IOException, IllegalArgumentException
request
- the request to sendsendTo
- the destination address of the request.sendThrough
- the socket that we should send the request through.collector
- the instance to notify when a response arrives or the
the transaction timeoutsIOException
- if an error occurs while sending message bytes
through the network socket.IllegalArgumentException
- if the apDescriptor references an
access point that had not been installed,public void sendResponse(byte[] transactionID, Response response, TransportAddress sendThrough, TransportAddress sendTo) throws StunException, IOException, IllegalArgumentException
transactionID
- the id of the transaction to use when sending the
response. Actually we are getting kind of redundant here as we already
have the id in the response object, but I am bringing out as an extra
parameter as the user might otherwise forget to explicitly set it.response
- the message to send.sendThrough
- the local address to use when sending the message.sendTo
- the destination of the message.IOException
- if an error occurs while sending message bytes
through the network socket.IllegalArgumentException
- if the apDescriptor references an
access point that had not been installed,StunException
- if message encoding failspublic void addIndicationListener(TransportAddress localAddr, MessageEventHandler indicationListener)
localAddr
- the TransportAddress of the local socket for
which received STUN indications are to be reported to the specified
MessageEventHandlerindicationListener
- the MessageEventHandler which is to be
registered for notifications about STUN indications received at the
specified local TransportAddresspublic void addOldIndicationListener(TransportAddress localAddr, MessageEventHandler indicationListener)
localAddr
- the TransportAddress of the local socket for
which received STUN indications are to be reported to the specified
MessageEventHandlerindicationListener
- the MessageEventHandler which is to be
registered for notifications about old indications received at the
specified local TransportAddresspublic void addRequestListener(RequestListener requestListener)
requestListener
- the listener interested in incoming requests.public void removeIndicationListener(TransportAddress localAddr, MessageEventHandler indicationListener)
localAddr
- the TransportAddress of the local socket for
which received STUN indications are to no longer be reported to the
specified MessageEventHandlerindicationListener
- the MessageEventHandler which is to be
unregistered for notifications about STUN indications received at the
specified local TransportAddresspublic void removeRequestListener(RequestListener listener)
listener
- the RequestListener listener to unregisterpublic void addRequestListener(TransportAddress localAddress, RequestListener listener)
localAddress
- The local TransportAddress that we would
like to listen on.listener
- The ConfigurationChangeListener to be addedvoid removeClientTransaction(StunClientTransaction tran)
tran
- the transaction to remove.void removeServerTransaction(StunServerTransaction tran)
tran
- the transaction to remove.public void handleMessageEvent(StunMessageEvent ev)
handleMessageEvent
in interface MessageEventHandler
ev
- the event object that contains the new message.public CredentialsManager getCredentialsManager()
CredentialsManager
that this stack is using for
verification of MessageIntegrityAttribute
s.CredentialsManager
that this stack is using for
verification of MessageIntegrityAttribute
s.public void shutDown()
private void validateRequestAttributes(StunMessageEvent evt) throws IllegalArgumentException, StunException, IOException
evt
- the StunMessageEvent
that contains the Request
that we need to validate.IllegalArgumentException
- if there's something in the
attribute that caused us to discard the whole message (e.g. an
invalid checksum
or username)StunException
- if we fail while sending an error response.IOException
- if we fail while sending an error response.public boolean validateMessageIntegrity(MessageIntegrityAttribute msgInt, String username, boolean shortTermCredentialMechanism, RawMessage message)
MessageIntegrityAttribute
.msgInt
- the attribute that we need to validate.username
- the user name that the message integrity checksum is
supposed to have been built for.shortTermCredentialMechanism
- true if msgInt is
to be validated as part of the STUN short-term credential mechanism or
false for the STUN long-term credential mechanismmessage
- the message whose SHA1 checksum we'd need to recalculate.private static String toHexString(byte[] bytes)
bytes
- the byte to get the String representation
of as an unsigned integer in base 16private boolean validateUsername(String username)
username
- the username to be validatedpublic static PacketLogger getPacketLogger()
public static void setPacketLogger(PacketLogger packetLogger)
packetLogger
- the packet logger to use.public static boolean isPacketLoggerEnabled()
public Response createCorrespondingErrorResponse(char requestType, char errorCode, String reasonPhrase, char... unknownAttributes)
requestType
- the message type of Request.errorCode
- the errorCode for Error Response object.reasonPhrase
- the reasonPhrase for the Error Response object.unknownAttributes
- char[] array containing the ids of one or more
attributes that had not been recognized.public static void logPacketToPcap(DatagramPacket p, boolean isSent, InetAddress interfaceAddress, int interfacePort)
p
- The DatagramPacket to log.isSent
- true if the packet is sent, or false
if the packet is received.interfaceAddress
- The InetAddress to use as source (if
the packet was sent) or destination (if the packet was received).interfacePort
- The port to use as source (if the packet was sent)
or destination (if the packet was received).Copyright © 2018. All rights reserved.