/** * Adds the <tt>Attribute</tt>s to a specific <tt>Request</tt> which support the STUN short-term * credential mechanism if the mechanism in question is utilized by this * <tt>StunCandidateHarvest</tt> (i.e. by the associated <tt>StunCandidateHarvester</tt>). * * @param request the <tt>Request</tt> to which to add the <tt>Attribute</tt>s supporting the STUN * short-term credential mechanism if the mechanism in question is utilized by this * <tt>StunCandidateHarvest</tt> * @return <tt>true</tt> if the STUN short-term credential mechanism is actually utilized by this * <tt>StunCandidateHarvest</tt> for the specified <tt>request</tt>; otherwise, <tt>false</tt> */ protected boolean addShortTermCredentialAttributes(Request request) { String shortTermCredentialUsername = harvester.getShortTermCredentialUsername(); if (shortTermCredentialUsername != null) { request.putAttribute(AttributeFactory.createUsernameAttribute(shortTermCredentialUsername)); request.putAttribute( AttributeFactory.createMessageIntegrityAttribute(shortTermCredentialUsername)); return true; } else return false; }
@Override public void processRequest(StunMessageEvent evt) throws IllegalArgumentException { if (logger.isLoggable(Level.FINER)) { logger.setLevel(Level.FINEST); } Message message = evt.getMessage(); if (message.getMessageType() == Message.CONNECT_REQUEST) { logger.finer("Received Connect request " + evt); Character errorCode = null; XorPeerAddressAttribute peerAddress = null; FiveTuple fiveTuple = null; Response response = null; ConnectionIdAttribute connectionId = null; if (!message.containsAttribute(Attribute.XOR_PEER_ADDRESS)) { errorCode = ErrorCodeAttribute.BAD_REQUEST; } else { peerAddress = (XorPeerAddressAttribute) message.getAttribute(Attribute.XOR_PEER_ADDRESS); peerAddress.setAddress(peerAddress.getAddress(), evt.getTransactionID().getBytes()); logger.finest("Peer Address requested : " + peerAddress.getAddress()); TransportAddress clientAddress = evt.getRemoteAddress(); TransportAddress serverAddress = evt.getLocalAddress(); Transport transport = evt.getLocalAddress().getTransport(); fiveTuple = new FiveTuple(clientAddress, serverAddress, transport); Allocation allocation = this.turnStack.getServerAllocation(fiveTuple); if (allocation == null) { errorCode = ErrorCodeAttribute.ALLOCATION_MISMATCH; } else if (!allocation.isPermitted(peerAddress.getAddress())) { errorCode = ErrorCodeAttribute.FORBIDDEN; } else { // code for processing the connect request. connectionId = AttributeFactory.createConnectionIdAttribute(); logger.finest("Created ConnectionID : " + connectionId.getConnectionIdValue()); try { Socket socket = new Socket( peerAddress.getAddress().getAddress(), peerAddress.getAddress().getPort()); socket.setSoTimeout(30 * 1000); IceTcpSocketWrapper iceSocket = new IceTcpSocketWrapper(socket); this.turnStack.addSocket(iceSocket); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } /** * TODO Create a new TCP connection to the peer from relay address. wait for at-least 30 * seconds. If it fails then send 447 error code. */ this.turnStack.addUnAcknowlededConnectionId( connectionId.getConnectionIdValue(), peerAddress.getAddress(), allocation); logger.finest("Creating Connect Success Response."); response = MessageFactory.createConnectResponse(connectionId.getConnectionIdValue()); } } if (errorCode != null) { response = MessageFactory.createConnectErrorResponse(errorCode); logger.finest("error Code : " + (int) errorCode + " on ConnectRequest"); } try { logger.finest("Sending Connect Response"); turnStack.sendResponse( evt.getTransactionID().getBytes(), response, evt.getLocalAddress(), evt.getRemoteAddress()); } catch (Exception e) { System.err.println("Failed to send response"); logger.log( Level.INFO, "Failed to send " + response + " through " + evt.getLocalAddress(), e); // try to trigger a 500 response although if this one failed, throw new RuntimeException("Failed to send a response", e); } } else { return; } }