From d6b41e2251034f0631b27568e22ec615f64d3058 Mon Sep 17 00:00:00 2001 From: paweldomas Date: Sun, 15 Jun 2014 19:10:50 +0000 Subject: [PATCH] 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. --- src/org/ice4j/ChannelDataMessageEvent.java | 118 ++++++++++++ src/org/ice4j/PeerUdpMessageEvent.java | 108 +++++++++++ .../ice4j/stack/ChannelDataEventHandler.java | 28 +++ src/org/ice4j/stack/NetAccessManager.java | 142 +++++++++++++- .../stack/PeerUdpMessageEventHandler.java | 28 +++ src/org/ice4j/stack/StunStack.java | 173 ++++++++++++++++-- 6 files changed, 583 insertions(+), 14 deletions(-) create mode 100644 src/org/ice4j/ChannelDataMessageEvent.java create mode 100644 src/org/ice4j/PeerUdpMessageEvent.java create mode 100644 src/org/ice4j/stack/ChannelDataEventHandler.java create mode 100644 src/org/ice4j/stack/PeerUdpMessageEventHandler.java diff --git a/src/org/ice4j/ChannelDataMessageEvent.java b/src/org/ice4j/ChannelDataMessageEvent.java new file mode 100644 index 0000000..11c174c --- /dev/null +++ b/src/org/ice4j/ChannelDataMessageEvent.java @@ -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 StunStack 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 Message associated with this event. + */ + private final ChannelData channelDataMessage; + + /** + * Initializes a new ChannelDataMessageEvent associated with a + * specific ChannelData Message. + * + * @param stunStack the StunStack to be associated with the new + * instance. + * @param remoteAddress the TransportAddress which is to be + * reported as the source of the new event. + * @param localAddress the TransportAddress which is to be reported + * as the receiving location of the new event. + * @param channelDataMessage the ChannelData Message 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 Message associated with this event. + * + * @return the ChannelData Message associated with this event + */ + public ChannelData getChannelDataMessage() + { + return channelDataMessage; + } + + /** + * Gets the TransportAddress which is the remote address of this + * event. + * + * @return the TransportAddress which is the address who caused + * this event + */ + public TransportAddress getRemoteAddress() + { + return this.remoteAddress; + } + + /** + * Gets the TransportAddress which is local address on which this + * event was received. + */ + public TransportAddress getLocalAddress() + { + return this.localAddress; + } + + /** + * Gets the StunStack associated with this instance. + * + * @return the StunStack associated with this instance + */ + public StunStack getStunStack() + { + return stunStack; + } + +} diff --git a/src/org/ice4j/PeerUdpMessageEvent.java b/src/org/ice4j/PeerUdpMessageEvent.java new file mode 100644 index 0000000..57e4b10 --- /dev/null +++ b/src/org/ice4j/PeerUdpMessageEvent.java @@ -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 StunStack associated with this instance. + */ + private final StunStack stunStack; + + /** + * The Peer UDP Message associated with this event. Stored as a + * RawMessage. + */ + private final RawMessage udpMessage; + + /** + * Initializes a new PeerUdpMessageEvent associated with a + * specific PeerUdp Message. + * + * @param stunStack the StunStack to be associated with the new + * instance. + * @param udpMessage the Peer UDP Message 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 StunStack associated with this instance. + * + * @return the StunStack associated with this instance + */ + public StunStack getStunStack() + { + return stunStack; + } + +} diff --git a/src/org/ice4j/stack/ChannelDataEventHandler.java b/src/org/ice4j/stack/ChannelDataEventHandler.java new file mode 100644 index 0000000..77edcf2 --- /dev/null +++ b/src/org/ice4j/stack/ChannelDataEventHandler.java @@ -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); +} diff --git a/src/org/ice4j/stack/NetAccessManager.java b/src/org/ice4j/stack/NetAccessManager.java index ece7e90..ea87282 100644 --- a/src/org/ice4j/stack/NetAccessManager.java +++ b/src/org/ice4j/stack/NetAccessManager.java @@ -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 StunStack 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 PeerUdpMessageEventHandler + * that will handle incoming UDP messages which are not STUN + * messages and ChannelData messages. + * @param channelDataEventHandler the ChannelDataEventHandler 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 PeerUdpMessageEventHandler of this + * NetAccessManager which is to be notified when incoming UDP + * messages have been processed and are ready for delivery. + * + * @return the PeerUdpMessageEventHandler of this + * NetAccessManager which is to be notified when incoming + * UDP messages have been processed and are ready for delivery + */ + public PeerUdpMessageEventHandler getUdpMessageEventHandler() + { + return peerUdpMessageEventHandler; + } + + /** + * Gets the ChannelDataEventHandler of this + * NetAccessManager which is to be notified when incoming + * ChannelData messages have been processed and are ready for delivery. + * + * @return the ChannelDataEventHandler of this + * NetAccessManager which is to be notified when incoming + * ChannelData messages have been processed and are ready for + * delivery + */ + public ChannelDataEventHandler getChannelDataMessageEventHandler() + { + return channelDataEventHandler; + } + /** * Gets the MessageQueue of this NetAccessManager 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); + } } diff --git a/src/org/ice4j/stack/PeerUdpMessageEventHandler.java b/src/org/ice4j/stack/PeerUdpMessageEventHandler.java new file mode 100644 index 0000000..6a048ef --- /dev/null +++ b/src/org/ice4j/stack/PeerUdpMessageEventHandler.java @@ -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); +} diff --git a/src/org/ice4j/stack/StunStack.java b/src/org/ice4j/stack/StunStack.java index f014559..0de9d53 100644 --- a/src/org/ice4j/stack/StunStack.java +++ b/src/org/ice4j/stack/StunStack.java @@ -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 StunStack instance. + * Initializes a new StunStack 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 StunStack 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 Indication to a specific destination + * TransportAddress through a socket registered with this + * StunStack using a specific TransportAddress. + * + * @param channelData the STUN Indication to be sent to the + * specified destination TransportAddress through the socket with + * the specified TransportAddress + * @param sendTo the TransportAddress of the destination to which + * the specified indication is to be sent + * @param sendThrough the TransportAddress of the socket registered + * with this StunStack through which the specified + * indication is to be sent + * @throws StunException if anything goes wrong while sending the specified + * indication to the destination sendTo through the socket + * identified by sendThrough + */ + 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 Indication to a specific destination + * TransportAddress through a socket registered with this + * StunStack using a specific TransportAddress. + * + * @param udpMessage the RawMessage to be sent to the + * specified destination TransportAddress through the socket with + * the specified TransportAddress + * @param sendTo the TransportAddress of the destination to which + * the specified indication is to be sent + * @param sendThrough the TransportAddress of the socket registered + * with this StunStack through which the specified + * indication is to be sent + * @throws StunException if anything goes wrong while sending the specified + * indication to the destination sendTo through the socket + * identified by sendThrough + */ + 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 Indication to a specific destination * TransportAddress 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; + } + } }