Adds Constructor overload, sendChannelData method, sendUdpMessage method, and other functions for TurnStack and relaying in StunStack. Adds new messageEvent classes with their listener interfaces. Patch by Aakash Garg.
This commit is contained in:
parent
30b940732f
commit
d6b41e2251
118
src/org/ice4j/ChannelDataMessageEvent.java
Normal file
118
src/org/ice4j/ChannelDataMessageEvent.java
Normal file
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the SIP Communicator community (http://sip-communicator.org).
|
||||
*
|
||||
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.ice4j.message.*;
|
||||
import org.ice4j.stack.*;
|
||||
|
||||
/**
|
||||
* The class is used to dispatch incoming ChannelData messages. Apart from the
|
||||
* message itself one could also obtain the address from where the message is
|
||||
* coming (used by a server implementation to determine the mapped address) as
|
||||
* well as the Descriptor of the NetAccessPoint that received it (In case the
|
||||
* stack is used on more than one ports/addresses).
|
||||
*
|
||||
* @author Aakash Garg
|
||||
*/
|
||||
public class ChannelDataMessageEvent
|
||||
extends EventObject
|
||||
{
|
||||
/**
|
||||
* A dummy version UID to suppress warnings.
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* The <tt>StunStack</tt> associated with this instance.
|
||||
*/
|
||||
private final StunStack stunStack;
|
||||
|
||||
/**
|
||||
* Remote address causing this event.
|
||||
*/
|
||||
private final TransportAddress remoteAddress;
|
||||
|
||||
/**
|
||||
* Local address receiving this event.
|
||||
*/
|
||||
private final TransportAddress localAddress;
|
||||
|
||||
/**
|
||||
* The ChannelData <tt>Message</tt> associated with this event.
|
||||
*/
|
||||
private final ChannelData channelDataMessage;
|
||||
|
||||
/**
|
||||
* Initializes a new <tt>ChannelDataMessageEvent</tt> associated with a
|
||||
* specific ChannelData <tt>Message</tt>.
|
||||
*
|
||||
* @param stunStack the <tt>StunStack</tt> to be associated with the new
|
||||
* instance.
|
||||
* @param remoteAddress the <tt>TransportAddress</tt> which is to be
|
||||
* reported as the source of the new event.
|
||||
* @param localAddress the <tt>TransportAddress</tt> which is to be reported
|
||||
* as the receiving location of the new event.
|
||||
* @param channelDataMessage the ChannelData <tt>Message</tt> associated
|
||||
* with the new event.
|
||||
*/
|
||||
public ChannelDataMessageEvent(StunStack stunStack,
|
||||
TransportAddress remoteAddress,
|
||||
TransportAddress localAddress,
|
||||
ChannelData channelDataMessage)
|
||||
{
|
||||
super(remoteAddress);
|
||||
|
||||
this.remoteAddress = remoteAddress;
|
||||
this.localAddress = localAddress;
|
||||
this.stunStack = stunStack;
|
||||
this.channelDataMessage = channelDataMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the ChannelData <tt>Message</tt> associated with this event.
|
||||
*
|
||||
* @return the ChannelData <tt>Message</tt> associated with this event
|
||||
*/
|
||||
public ChannelData getChannelDataMessage()
|
||||
{
|
||||
return channelDataMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the <tt>TransportAddress</tt> which is the remote address of this
|
||||
* event.
|
||||
*
|
||||
* @return the <tt>TransportAddress</tt> which is the address who caused
|
||||
* this event
|
||||
*/
|
||||
public TransportAddress getRemoteAddress()
|
||||
{
|
||||
return this.remoteAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the <tt>TransportAddress</tt> which is local address on which this
|
||||
* event was received.
|
||||
*/
|
||||
public TransportAddress getLocalAddress()
|
||||
{
|
||||
return this.localAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the <tt>StunStack</tt> associated with this instance.
|
||||
*
|
||||
* @return the <tt>StunStack</tt> associated with this instance
|
||||
*/
|
||||
public StunStack getStunStack()
|
||||
{
|
||||
return stunStack;
|
||||
}
|
||||
|
||||
}
|
||||
108
src/org/ice4j/PeerUdpMessageEvent.java
Normal file
108
src/org/ice4j/PeerUdpMessageEvent.java
Normal file
@ -0,0 +1,108 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the SIP Communicator community (http://sip-communicator.org).
|
||||
*
|
||||
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.ice4j.stack.*;
|
||||
|
||||
/**
|
||||
* The class is used to dispatch incoming UDP peer messages. Apart from the
|
||||
* message itself one could also obtain the address from where the message is
|
||||
* coming (used by a server implementation to determine the mapped address) as
|
||||
* well as the Descriptor of the NetAccessPoint that received it (In case the
|
||||
* stack is used on more than one ports/addresses).
|
||||
*
|
||||
* @author Aakash Garg
|
||||
*/
|
||||
public class PeerUdpMessageEvent
|
||||
extends EventObject
|
||||
{
|
||||
/**
|
||||
* A dummy version UID to suppress warnings.
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* The <tt>StunStack</tt> associated with this instance.
|
||||
*/
|
||||
private final StunStack stunStack;
|
||||
|
||||
/**
|
||||
* The Peer UDP <tt>Message</tt> associated with this event. Stored as a
|
||||
* RawMessage.
|
||||
*/
|
||||
private final RawMessage udpMessage;
|
||||
|
||||
/**
|
||||
* Initializes a new <tt>PeerUdpMessageEvent</tt> associated with a
|
||||
* specific PeerUdp <tt>Message</tt>.
|
||||
*
|
||||
* @param stunStack the <tt>StunStack</tt> to be associated with the new
|
||||
* instance.
|
||||
* @param udpMessage the Peer UDP <tt>Message</tt> associated
|
||||
* with the new event.
|
||||
*/
|
||||
public PeerUdpMessageEvent(StunStack stunStack, RawMessage udpMessage)
|
||||
{
|
||||
super(udpMessage.getRemoteAddress());
|
||||
|
||||
this.stunStack = stunStack;
|
||||
this.udpMessage = udpMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message itself.
|
||||
*
|
||||
* @return a binary array containing the message data.
|
||||
*/
|
||||
public byte[] getBytes()
|
||||
{
|
||||
return this.udpMessage.getBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message length.
|
||||
*
|
||||
* @return a the length of the message.
|
||||
*/
|
||||
public int getMessageLength()
|
||||
{
|
||||
return this.udpMessage.getMessageLength();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the address and port of the host that sent the message
|
||||
*
|
||||
* @return the [address]:[port] pair that sent the message.
|
||||
*/
|
||||
public TransportAddress getRemoteAddress()
|
||||
{
|
||||
return this.udpMessage.getRemoteAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the address that this message was received on.
|
||||
*
|
||||
* @return the address that this message was received on.
|
||||
*/
|
||||
public TransportAddress getLocalAddress()
|
||||
{
|
||||
return this.udpMessage.getLocalAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the <tt>StunStack</tt> associated with this instance.
|
||||
*
|
||||
* @return the <tt>StunStack</tt> associated with this instance
|
||||
*/
|
||||
public StunStack getStunStack()
|
||||
{
|
||||
return stunStack;
|
||||
}
|
||||
|
||||
}
|
||||
28
src/org/ice4j/stack/ChannelDataEventHandler.java
Normal file
28
src/org/ice4j/stack/ChannelDataEventHandler.java
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the SIP Communicator community (http://sip-communicator.org).
|
||||
*
|
||||
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.stack;
|
||||
|
||||
import org.ice4j.*;
|
||||
|
||||
/**
|
||||
* The interface is used for collecting incoming ChannelData messages from the
|
||||
* NetAccessManager (and more precisely - MessageProcessors). This is our way of
|
||||
* keeping scalable network and stun layers.
|
||||
*
|
||||
* @author Aakash Garg
|
||||
*/
|
||||
public interface ChannelDataEventHandler
|
||||
{
|
||||
/**
|
||||
* Called when an incoming Channel Data message has been received, parsed
|
||||
* and is ready for delivery.
|
||||
*
|
||||
* @param messageEvent the event object that encapsulates the newly received
|
||||
* message.
|
||||
*/
|
||||
public void handleMessageEvent(ChannelDataMessageEvent messageEvent);
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the SIP Communicator community (http://sip-communicator.org).
|
||||
*
|
||||
*
|
||||
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.stack;
|
||||
@ -20,8 +20,9 @@ import org.ice4j.socket.*;
|
||||
* abstractions. Instances that operate with the NetAccessManager are only
|
||||
* supposed to understand STUN talk and shouldn't be aware of datagrams sockets,
|
||||
* and etc.
|
||||
*
|
||||
*
|
||||
* @author Emil Ivov
|
||||
* @author Aakash Garg
|
||||
*/
|
||||
class NetAccessManager
|
||||
implements ErrorHandler
|
||||
@ -73,6 +74,18 @@ class NetAccessManager
|
||||
*/
|
||||
private final MessageEventHandler messageEventHandler;
|
||||
|
||||
/**
|
||||
* The instance that should be notified when an incoming UDP message has
|
||||
* been processed and ready for delivery
|
||||
*/
|
||||
private final PeerUdpMessageEventHandler peerUdpMessageEventHandler;
|
||||
|
||||
/**
|
||||
* The instance that should be notified when an incoming ChannelData message
|
||||
* has been processed and ready for delivery
|
||||
*/
|
||||
private final ChannelDataEventHandler channelDataEventHandler;
|
||||
|
||||
/**
|
||||
* The size of the thread pool to start with.
|
||||
*/
|
||||
@ -92,10 +105,32 @@ class NetAccessManager
|
||||
* message requests should be passed to
|
||||
*/
|
||||
NetAccessManager(StunStack stunStack)
|
||||
{
|
||||
this(stunStack, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a NetAccessManager with given peerUdpMessageEventHandler and
|
||||
* channelDataEventHandler.
|
||||
*
|
||||
* @param stunStack the <tt>StunStack</tt> which is creating the new
|
||||
* instance, is going to be its owner and is the handler that
|
||||
* incoming message requests should be passed to
|
||||
* @param peerUdpMessageEventHandler the <tt>PeerUdpMessageEventHandler</tt>
|
||||
* that will handle incoming UDP messages which are not STUN
|
||||
* messages and ChannelData messages.
|
||||
* @param channelDataEventHandler the <tt>ChannelDataEventHandler</tt> that
|
||||
* will handle incoming UDP messages which are ChannelData
|
||||
* messages.
|
||||
*/
|
||||
NetAccessManager(StunStack stunStack,
|
||||
PeerUdpMessageEventHandler peerUdpMessageEventHandler,
|
||||
ChannelDataEventHandler channelDataEventHandler)
|
||||
{
|
||||
this.stunStack = stunStack;
|
||||
this.messageEventHandler = stunStack;
|
||||
|
||||
this.peerUdpMessageEventHandler = peerUdpMessageEventHandler;
|
||||
this.channelDataEventHandler = channelDataEventHandler;
|
||||
initThreadPool();
|
||||
}
|
||||
|
||||
@ -113,6 +148,35 @@ class NetAccessManager
|
||||
return messageEventHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the <tt>PeerUdpMessageEventHandler</tt> of this
|
||||
* <tt>NetAccessManager</tt> which is to be notified when incoming UDP
|
||||
* messages have been processed and are ready for delivery.
|
||||
*
|
||||
* @return the <tt>PeerUdpMessageEventHandler</tt> of this
|
||||
* <tt>NetAccessManager</tt> which is to be notified when incoming
|
||||
* UDP messages have been processed and are ready for delivery
|
||||
*/
|
||||
public PeerUdpMessageEventHandler getUdpMessageEventHandler()
|
||||
{
|
||||
return peerUdpMessageEventHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the <tt>ChannelDataEventHandler</tt> of this
|
||||
* <tt>NetAccessManager</tt> which is to be notified when incoming
|
||||
* ChannelData messages have been processed and are ready for delivery.
|
||||
*
|
||||
* @return the <tt>ChannelDataEventHandler</tt> of this
|
||||
* <tt>NetAccessManager</tt> which is to be notified when incoming
|
||||
* ChannelData messages have been processed and are ready for
|
||||
* delivery
|
||||
*/
|
||||
public ChannelDataEventHandler getChannelDataMessageEventHandler()
|
||||
{
|
||||
return channelDataEventHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the <tt>MessageQueue</tt> of this <tt>NetAccessManager</tt> in which
|
||||
* incoming messages are stocked for processing.
|
||||
@ -142,6 +206,7 @@ class NetAccessManager
|
||||
* @param message a description of the error
|
||||
* @param error the error that has occurred
|
||||
*/
|
||||
@Override
|
||||
public void handleError(String message, Throwable error)
|
||||
{
|
||||
/**
|
||||
@ -160,6 +225,7 @@ class NetAccessManager
|
||||
* @param message A description of the error
|
||||
* @param error The error itself
|
||||
*/
|
||||
@Override
|
||||
public void handleFatalError(Runnable callingThread,
|
||||
String message,
|
||||
Throwable error)
|
||||
@ -374,4 +440,74 @@ class NetAccessManager
|
||||
|
||||
ap.sendMessage(bytes, remoteAddr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the specified stun message through the specified access point.
|
||||
*
|
||||
* @param channelData the message to send
|
||||
* @param srcAddr the access point to use to send the message
|
||||
* @param remoteAddr the destination of the message.
|
||||
*
|
||||
* @throws IllegalArgumentException if the apDescriptor references an
|
||||
* access point that had not been installed,
|
||||
* @throws IOException if an error occurs while sending message bytes
|
||||
* through the network socket.
|
||||
* @throws StunException
|
||||
*/
|
||||
void sendMessage(
|
||||
ChannelData channelData,
|
||||
TransportAddress srcAddr,
|
||||
TransportAddress remoteAddr)
|
||||
throws IllegalArgumentException,
|
||||
IOException, StunException
|
||||
{
|
||||
byte[] bytes = channelData.encode();
|
||||
|
||||
Connector ap = null;
|
||||
|
||||
if (srcAddr.getTransport() == Transport.UDP)
|
||||
ap = netUDPAccessPoints.get(srcAddr);
|
||||
else if (srcAddr.getTransport() == Transport.TCP)
|
||||
ap = netTCPAccessPoints.get(srcAddr);
|
||||
if (ap == null)
|
||||
{
|
||||
throw new IllegalArgumentException(
|
||||
"No socket has been added for source address: " + srcAddr);
|
||||
}
|
||||
|
||||
ap.sendMessage(bytes, remoteAddr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the specified bytes through the specified access point.
|
||||
*
|
||||
* @param bytes the bytes to send.
|
||||
* @param srcAddr the access point to use to send the bytes.
|
||||
* @param remoteAddr the destination of the message.
|
||||
*
|
||||
* @throws IllegalArgumentException if the apDescriptor references an
|
||||
* access point that had not been installed,
|
||||
* @throws IOException if an error occurs while sending message bytes
|
||||
* through the network socket.
|
||||
* @throws StunException
|
||||
*/
|
||||
void sendMessage(
|
||||
byte[] bytes,
|
||||
TransportAddress srcAddr,
|
||||
TransportAddress remoteAddr)
|
||||
throws IllegalArgumentException,
|
||||
IOException, StunException
|
||||
{
|
||||
Connector ap = null;
|
||||
if (srcAddr.getTransport() == Transport.UDP)
|
||||
ap = netUDPAccessPoints.get(srcAddr);
|
||||
else if (srcAddr.getTransport() == Transport.TCP)
|
||||
ap = netTCPAccessPoints.get(srcAddr);
|
||||
if (ap == null)
|
||||
{
|
||||
throw new IllegalArgumentException(
|
||||
"No socket has been added for source address: " + srcAddr);
|
||||
}
|
||||
ap.sendMessage(bytes, remoteAddr);
|
||||
}
|
||||
}
|
||||
|
||||
28
src/org/ice4j/stack/PeerUdpMessageEventHandler.java
Normal file
28
src/org/ice4j/stack/PeerUdpMessageEventHandler.java
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||
* Maintained by the SIP Communicator community (http://sip-communicator.org).
|
||||
*
|
||||
* Distributable under LGPL license. See terms of license at gnu.org.
|
||||
*/
|
||||
package org.ice4j.stack;
|
||||
|
||||
import org.ice4j.*;
|
||||
|
||||
/**
|
||||
* The interface is used for collecting incoming Peer UDP Message messages from
|
||||
* the NetAccessManager (and more precisely - MessageProcessors) if it is of no
|
||||
* other type. This is our way of keeping scalable network and stun layers.
|
||||
*
|
||||
* @author Aakash Garg
|
||||
*/
|
||||
public interface PeerUdpMessageEventHandler
|
||||
{
|
||||
/**
|
||||
* Called when an incoming Peer UDP message has been received, parsed
|
||||
* and is ready for delivery.
|
||||
*
|
||||
* @param messageEvent the event object that encapsulates the newly received
|
||||
* message.
|
||||
*/
|
||||
public void handleMessageEvent(PeerUdpMessageEvent messageEvent);
|
||||
}
|
||||
@ -26,6 +26,7 @@ import org.ice4j.socket.*;
|
||||
*
|
||||
* @author Emil Ivov
|
||||
* @author Lyubomir Marinov
|
||||
* @author Aakash Garg.
|
||||
*/
|
||||
public class StunStack
|
||||
implements MessageEventHandler
|
||||
@ -317,9 +318,11 @@ public class StunStack
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a new <tt>StunStack</tt> instance.
|
||||
* Initializes a new <tt>StunStack</tt> instance with given
|
||||
* peerUdpMessageEventHandler and channelDataEventHandler.
|
||||
*/
|
||||
public StunStack()
|
||||
public StunStack(PeerUdpMessageEventHandler peerUdpMessageEventHandler,
|
||||
ChannelDataEventHandler channelDataEventHandler)
|
||||
{
|
||||
/*
|
||||
* The Mac instantiation used in MessageIntegrityAttribute could take
|
||||
@ -343,9 +346,19 @@ public class StunStack
|
||||
}
|
||||
}
|
||||
}
|
||||
netAccessManager = new NetAccessManager(this);
|
||||
netAccessManager =
|
||||
new NetAccessManager(this, peerUdpMessageEventHandler,
|
||||
channelDataEventHandler);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initializes a new <tt>StunStack</tt> instance.
|
||||
*/
|
||||
public StunStack()
|
||||
{
|
||||
this(null,null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the currently active instance of NetAccessManager.
|
||||
* @return the currently active instance of NetAccessManager.
|
||||
@ -355,6 +368,105 @@ public class StunStack
|
||||
return netAccessManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a specific STUN <tt>Indication</tt> to a specific destination
|
||||
* <tt>TransportAddress</tt> through a socket registered with this
|
||||
* <tt>StunStack</tt> using a specific <tt>TransportAddress</tt>.
|
||||
*
|
||||
* @param channelData the STUN <tt>Indication</tt> to be sent to the
|
||||
* specified destination <tt>TransportAddress</tt> through the socket with
|
||||
* the specified <tt>TransportAddress</tt>
|
||||
* @param sendTo the <tt>TransportAddress</tt> of the destination to which
|
||||
* the specified <tt>indication</tt> is to be sent
|
||||
* @param sendThrough the <tt>TransportAddress</tt> of the socket registered
|
||||
* with this <tt>StunStack</tt> through which the specified
|
||||
* <tt>indication</tt> is to be sent
|
||||
* @throws StunException if anything goes wrong while sending the specified
|
||||
* <tt>indication</tt> to the destination <tt>sendTo</tt> through the socket
|
||||
* identified by <tt>sendThrough</tt>
|
||||
*/
|
||||
public void sendChannelData(
|
||||
ChannelData channelData,
|
||||
TransportAddress sendTo,
|
||||
TransportAddress sendThrough)
|
||||
throws StunException
|
||||
{
|
||||
try
|
||||
{
|
||||
getNetAccessManager().sendMessage(channelData, sendThrough, sendTo);
|
||||
}
|
||||
catch (StunException stex)
|
||||
{
|
||||
throw stex;
|
||||
}
|
||||
catch (IllegalArgumentException iaex)
|
||||
{
|
||||
throw new StunException(
|
||||
StunException.ILLEGAL_ARGUMENT,
|
||||
"Failed to send STUN indication: " + channelData,
|
||||
iaex);
|
||||
}
|
||||
catch (IOException ioex)
|
||||
{
|
||||
throw new StunException(
|
||||
StunException.NETWORK_ERROR,
|
||||
"Failed to send STUN indication: " + channelData,
|
||||
ioex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sends a specific STUN <tt>Indication</tt> to a specific destination
|
||||
* <tt>TransportAddress</tt> through a socket registered with this
|
||||
* <tt>StunStack</tt> using a specific <tt>TransportAddress</tt>.
|
||||
*
|
||||
* @param udpMessage the <tt>RawMessage</tt> to be sent to the
|
||||
* specified destination <tt>TransportAddress</tt> through the socket with
|
||||
* the specified <tt>TransportAddress</tt>
|
||||
* @param sendTo the <tt>TransportAddress</tt> of the destination to which
|
||||
* the specified <tt>indication</tt> is to be sent
|
||||
* @param sendThrough the <tt>TransportAddress</tt> of the socket registered
|
||||
* with this <tt>StunStack</tt> through which the specified
|
||||
* <tt>indication</tt> is to be sent
|
||||
* @throws StunException if anything goes wrong while sending the specified
|
||||
* <tt>indication</tt> to the destination <tt>sendTo</tt> through the socket
|
||||
* identified by <tt>sendThrough</tt>
|
||||
*/
|
||||
public void sendUdpMessage(
|
||||
RawMessage udpMessage,
|
||||
TransportAddress sendTo,
|
||||
TransportAddress sendThrough)
|
||||
throws StunException
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
getNetAccessManager().sendMessage(
|
||||
udpMessage.getBytes(), sendThrough, sendTo);
|
||||
}
|
||||
catch (StunException stex)
|
||||
{
|
||||
throw stex;
|
||||
}
|
||||
catch (IllegalArgumentException iaex)
|
||||
{
|
||||
throw new StunException(
|
||||
StunException.ILLEGAL_ARGUMENT,
|
||||
"Failed to send STUN indication: " + udpMessage,
|
||||
iaex);
|
||||
}
|
||||
catch (IOException ioex)
|
||||
{
|
||||
throw new StunException(
|
||||
StunException.NETWORK_ERROR,
|
||||
"Failed to send STUN indication: " + udpMessage,
|
||||
ioex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* Sends a specific STUN <tt>Indication</tt> to a specific destination
|
||||
* <tt>TransportAddress</tt> through a socket registered with this
|
||||
@ -727,6 +839,7 @@ public class StunStack
|
||||
*
|
||||
* @param ev the event object that contains the new message.
|
||||
*/
|
||||
@Override
|
||||
public void handleMessageEvent(StunMessageEvent ev)
|
||||
{
|
||||
Message msg = ev.getMessage();
|
||||
@ -841,14 +954,16 @@ public class StunStack
|
||||
if(t instanceof IllegalArgumentException)
|
||||
{
|
||||
error
|
||||
= MessageFactory.createBindingErrorResponse(
|
||||
= createCorrespondingErrorResponse(
|
||||
msg.getMessageType(),
|
||||
ErrorCodeAttribute.BAD_REQUEST,
|
||||
t.getMessage());
|
||||
}
|
||||
else
|
||||
{
|
||||
error
|
||||
= MessageFactory.createBindingErrorResponse(
|
||||
= createCorrespondingErrorResponse(
|
||||
msg.getMessageType(),
|
||||
ErrorCodeAttribute.SERVER_ERROR,
|
||||
"Oops! Something went wrong on our side :(");
|
||||
}
|
||||
@ -977,7 +1092,8 @@ public class StunStack
|
||||
username = LongTermCredential.toString(unameAttr.getUsername());
|
||||
if (!validateUsername(username))
|
||||
{
|
||||
Response error = MessageFactory.createBindingErrorResponse(
|
||||
Response error = createCorrespondingErrorResponse(
|
||||
request.getMessageType(),
|
||||
ErrorCodeAttribute.UNAUTHORIZED,
|
||||
"unknown user " + username);
|
||||
|
||||
@ -1000,7 +1116,8 @@ public class StunStack
|
||||
//we should complain if we have msg integrity and no username.
|
||||
if (unameAttr == null)
|
||||
{
|
||||
Response error = MessageFactory.createBindingErrorResponse(
|
||||
Response error = createCorrespondingErrorResponse(
|
||||
request.getMessageType(),
|
||||
ErrorCodeAttribute.BAD_REQUEST,
|
||||
"missing username");
|
||||
|
||||
@ -1018,7 +1135,8 @@ public class StunStack
|
||||
true,
|
||||
evt.getRawMessage()))
|
||||
{
|
||||
Response error = MessageFactory.createBindingErrorResponse(
|
||||
Response error = createCorrespondingErrorResponse(
|
||||
request.getMessageType(),
|
||||
ErrorCodeAttribute.UNAUTHORIZED,
|
||||
"Wrong MESSAGE-INTEGRITY value");
|
||||
|
||||
@ -1033,7 +1151,8 @@ public class StunStack
|
||||
else if(Boolean.getBoolean(StackProperties.REQUIRE_MESSAGE_INTEGRITY))
|
||||
{
|
||||
// no message integrity
|
||||
Response error = MessageFactory.createBindingErrorResponse(
|
||||
Response error = createCorrespondingErrorResponse(
|
||||
request.getMessageType(),
|
||||
ErrorCodeAttribute.UNAUTHORIZED,
|
||||
"Missing MESSAGE-INTEGRITY.");
|
||||
|
||||
@ -1057,7 +1176,8 @@ public class StunStack
|
||||
|
||||
if (sBuff.length() > 0)
|
||||
{
|
||||
Response error = MessageFactory.createBindingErrorResponse(
|
||||
Response error = createCorrespondingErrorResponse(
|
||||
request.getMessageType(),
|
||||
ErrorCodeAttribute.UNKNOWN_ATTRIBUTE,
|
||||
"unknown attribute ", sBuff.toString().toCharArray());
|
||||
|
||||
@ -1395,4 +1515,35 @@ public class StunStack
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Error Response object with specified errorCode and
|
||||
* reasonPhrase corresponding to input type.
|
||||
*
|
||||
* @param requestType the message type of Request.
|
||||
* @param errorCode the errorCode for Error Response object.
|
||||
* @param reasonPhrase the reasonPhrase for the Error Response object.
|
||||
* @return corresponding Error Response object.
|
||||
*/
|
||||
public Response createCorrespondingErrorResponse(char requestType,
|
||||
char errorCode, String reasonPhrase,char... unknownAttributes)
|
||||
{
|
||||
if (requestType == Message.BINDING_REQUEST)
|
||||
{
|
||||
if (unknownAttributes != null)
|
||||
{
|
||||
return MessageFactory.createBindingErrorResponse(
|
||||
errorCode, reasonPhrase, unknownAttributes);
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessageFactory.createBindingErrorResponse(
|
||||
errorCode, reasonPhrase);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user