-
- All Implemented Interfaces:
-
org.ice4j.stack.MessageEventHandler
public class StunStack implements MessageEventHandler
The entry point to the Stun4J stack. The class is used to start, stop and configure the stack.
-
-
Field Summary
Fields Modifier and Type Field Description private final CredentialsManagercredentialsManagerprivate static PacketLoggerpacketLogger
-
Constructor Summary
Constructors Constructor Description StunStack(PeerUdpMessageEventHandler peerUdpMessageEventHandler, ChannelDataEventHandler channelDataEventHandler)Initializes a new StunStack instance with given peerUdpMessageEventHandler and channelDataEventHandler. StunStack()Initializes a new StunStack instance.
-
Method Summary
Modifier and Type Method Description CredentialsManagergetCredentialsManager()Returns the CredentialsManager that this stack is using for verification of MessageIntegrityAttributes. static PacketLoggergetPacketLogger()Returns the currently set packet logger. static voidsetPacketLogger(PacketLogger packetLogger)Setting a packet logger for the stack. voidaddSocket(IceSocketWrapper sock)Creates and starts a Network Access Point (Connector) based on the specified socket. voidaddSocket(IceSocketWrapper sock, TransportAddress remoteAddress)Creates and starts a Network Access Point (Connector) based on the specified socket and the specified remote address. voidremoveSocket(TransportAddress localAddr)Stops and deletes the connector listening on the specified local address. voidremoveSocket(TransportAddress localAddr, TransportAddress remoteAddr)Stops and deletes the connector listening on the specified local address and remote address. voidcancelTransaction(TransactionID transactionID)Cancels the StunClientTransaction with the specified transactionID. voidsendChannelData(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. voidsendUdpMessage(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. voidsendIndication(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. TransactionIDsendRequest(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. TransactionIDsendRequest(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. TransactionIDsendRequest(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. TransactionIDsendRequest(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. voidsendResponse(Array<byte> transactionID, Response response, TransportAddress sendThrough, TransportAddress sendTo)Sends the specified response message through the specified access point. voidaddIndicationListener(TransportAddress localAddr, MessageEventHandler indicationListener)Adds a new MessageEventHandler which is to be notified about STUN indications received at a specific local TransportAddress. voidaddOldIndicationListener(TransportAddress localAddr, MessageEventHandler indicationListener)Adds a new MessageEventHandler which is to be notified about old indications received at a specific local TransportAddress. voidaddRequestListener(RequestListener requestListener)Sets the listener that should be notified when a new Request is received. voidremoveIndicationListener(TransportAddress localAddr, MessageEventHandler indicationListener)Removes an existing MessageEventHandler to no longer be notified about STUN indications received at a specific local TransportAddress. voidremoveRequestListener(RequestListener listener)Removes the specified listener from the local listener list. voidaddRequestListener(TransportAddress localAddress, RequestListener listener)Add a RequestListener for requests coming from a specific NetAccessPoint. voidhandleMessageEvent(StunMessageEvent ev)Called to notify this provider for an incoming message. voidshutDown()Cancels all running transactions and prepares for garbage collection booleanvalidateMessageIntegrity(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. static booleanisPacketLoggerEnabled()Checks whether packet logger is set and enabled. ResponsecreateCorrespondingErrorResponse(char requestType, char errorCode, String reasonPhrase, Array<char> unknownAttributes)Returns the Error Response object with specified errorCode and reasonPhrase corresponding to input type. static voidlogPacketToPcap(DatagramPacket p, boolean isSent, InetAddress interfaceAddress, int interfacePort)Logs a specific DatagramPacket using the packet logger of the StunStack. -
-
Constructor Detail
-
StunStack
StunStack(PeerUdpMessageEventHandler peerUdpMessageEventHandler, ChannelDataEventHandler channelDataEventHandler)
Initializes a new StunStack instance with given peerUdpMessageEventHandler and channelDataEventHandler.- Parameters:
peerUdpMessageEventHandler- the PeerUdpMessageEventHandlerthat 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.
-
StunStack
StunStack()
Initializes a new StunStack instance.
-
-
Method Detail
-
getCredentialsManager
CredentialsManager getCredentialsManager()
Returns the CredentialsManager that this stack is using for verification of MessageIntegrityAttributes.
-
getPacketLogger
static PacketLogger getPacketLogger()
Returns the currently set packet logger.
-
setPacketLogger
static void setPacketLogger(PacketLogger packetLogger)
Setting a packet logger for the stack.
- Parameters:
packetLogger- the packet logger to use.
-
addSocket
void addSocket(IceSocketWrapper sock)
Creates and starts a Network Access Point (Connector) based on the specified socket.
- Parameters:
sock- The socket that the new access point should represent.
-
addSocket
void addSocket(IceSocketWrapper sock, TransportAddress remoteAddress)
Creates and starts a Network Access Point (Connector) based on the specified socket and the specified remote address.
- Parameters:
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.
-
removeSocket
void removeSocket(TransportAddress localAddr)
Stops and deletes the connector listening on the specified local address. Note this removes connectors with UDP sockets only, use removeSocket with the appropriate remote address for TCP.
- Parameters:
localAddr- the local address of the socket to remove.
-
removeSocket
void removeSocket(TransportAddress localAddr, TransportAddress remoteAddr)
Stops and deletes the connector listening on the specified local address and remote address.
- Parameters:
localAddr- the local address of the socket to remove.remoteAddr- the remote address of the socket to remove.
-
cancelTransaction
void cancelTransaction(TransactionID transactionID)
Cancels the 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.
- Parameters:
transactionID- the TransactionID of the StunClientTransaction to cancel
-
sendChannelData
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.
- Parameters:
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 sent
-
sendUdpMessage
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.
- Parameters:
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 sent
-
sendIndication
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.
- Parameters:
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 sent
-
sendRequest
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.
- Parameters:
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 timeouts
-
sendRequest
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.
- Parameters:
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.
-
sendRequest
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.
- Parameters:
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.maxRetransmissions- Maximum number of retransmissions.
-
sendRequest
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.
- Parameters:
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 timeouts
-
sendResponse
void sendResponse(Array<byte> transactionID, Response response, TransportAddress sendThrough, TransportAddress sendTo)
Sends the specified response message through the specified access point.
- Parameters:
transactionID- the id of the transaction to use when sending the response.response- the message to send.sendThrough- the local address to use when sending the message.sendTo- the destination of the message.
-
addIndicationListener
void addIndicationListener(TransportAddress localAddr, MessageEventHandler indicationListener)
Adds a new MessageEventHandler which is to be notified about STUN indications received at a specific local TransportAddress.
- Parameters:
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 TransportAddress
-
addOldIndicationListener
void addOldIndicationListener(TransportAddress localAddr, MessageEventHandler indicationListener)
Adds a new MessageEventHandler which is to be notified about old indications received at a specific local TransportAddress.
- Parameters:
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 TransportAddress
-
addRequestListener
void addRequestListener(RequestListener requestListener)
Sets the listener that should be notified when a new Request is received.
- Parameters:
requestListener- the listener interested in incoming requests.
-
removeIndicationListener
void removeIndicationListener(TransportAddress localAddr, MessageEventHandler indicationListener)
Removes an existing MessageEventHandler to no longer be notified about STUN indications received at a specific local TransportAddress.
- Parameters:
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 TransportAddress
-
removeRequestListener
void removeRequestListener(RequestListener listener)
Removes the specified listener from the local listener list. (If any instances of this listener have been registered for a particular access point, they will not be removed).
- Parameters:
listener- the RequestListener listener to unregister
-
addRequestListener
void addRequestListener(TransportAddress localAddress, RequestListener listener)
Add a RequestListener for requests coming from a specific NetAccessPoint. The listener will be invoked only when a request event is received on that specific property.
- Parameters:
localAddress- The local TransportAddress that we would like to listen on.listener- The ConfigurationChangeListener to be added
-
handleMessageEvent
void handleMessageEvent(StunMessageEvent ev)
Called to notify this provider for an incoming message.
- Parameters:
ev- the event object that contains the new message.
-
shutDown
void shutDown()
Cancels all running transactions and prepares for garbage collection
-
validateMessageIntegrity
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.
- Parameters:
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.
-
isPacketLoggerEnabled
static boolean isPacketLoggerEnabled()
Checks whether packet logger is set and enabled.
-
createCorrespondingErrorResponse
Response createCorrespondingErrorResponse(char requestType, char errorCode, String reasonPhrase, Array<char> unknownAttributes)
Returns the Error Response object with specified errorCode and reasonPhrase corresponding to input type.
- Parameters:
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.
-
logPacketToPcap
static void logPacketToPcap(DatagramPacket p, boolean isSent, InetAddress interfaceAddress, int interfacePort)
Logs a specific DatagramPacket using the packet logger of the StunStack.
- Parameters:
p- The DatagramPacket to log.isSent- true if the packet is sent, or falseif 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).
-
-
-
-