/** * Put the JAIN-SIP stack in a state where it cannot receive any data and frees the network ports * used. That is to say remove JAIN-SIP <tt>ListeningPoint</tt>s and <tt>SipProvider</tt>s. */ @SuppressWarnings("unchecked") // jain-sip legacy code private void stopListening() { try { this.secureJainSipProvider.removeSipListener(this); this.stack.deleteSipProvider(this.secureJainSipProvider); this.secureJainSipProvider = null; this.clearJainSipProvider.removeSipListener(this); this.stack.deleteSipProvider(this.clearJainSipProvider); this.clearJainSipProvider = null; Iterator<ListeningPoint> it = this.stack.getListeningPoints(); Vector<ListeningPoint> lpointsToRemove = new Vector<ListeningPoint>(); while (it.hasNext()) { lpointsToRemove.add(it.next()); } it = lpointsToRemove.iterator(); while (it.hasNext()) { this.stack.deleteListeningPoint(it.next()); } this.stack.stop(); if (logger.isTraceEnabled()) logger.trace("stopped listening"); } catch (ObjectInUseException ex) { logger.fatal("Failed to stop listening", ex); } }
/** * Adds this <tt>listener</tt> as a candidate recipient for the dispatching of new messages * received from the JAIN-SIP <tt>SipProvider</tt>s. * * @param listener a new possible target for the dispatching process. * @throws OperationFailedException if creating one of the underlying <tt>SipProvider</tt>s fails * for whatever reason. */ public void addSipListener(ProtocolProviderServiceSipImpl listener) throws OperationFailedException { synchronized (this.listeners) { if (this.listeners.size() == 0) startListening(); this.listeners.add(listener); if (logger.isTraceEnabled()) logger.trace(this.listeners.size() + " listeners now"); } }
/** * This <tt>listener</tt> will no longer be a candidate recipient for the dispatching of new * messages received from the JAIN-SIP <tt>SipProvider</tt>s. * * @param listener possible target to remove for the dispatching process. */ public void removeSipListener(ProtocolProviderServiceSipImpl listener) { synchronized (this.listeners) { this.listeners.remove(listener); int listenerCount = listeners.size(); if (logger.isTraceEnabled()) logger.trace(listenerCount + " listeners left"); if (listenerCount == 0) stopListening(); } }
/** * Place to put some hacks if needed on incoming requests. * * @param event the incoming request event. * @return status <code>true</code> if we don't need to process this message, just discard it and * <code>false</code> otherwise. */ private boolean applyNonConformanceHacks(RequestEvent event) { Request request = event.getRequest(); try { /* * Max-Forwards is required, yet there are UAs which do not * place it. SipProvider#getNewServerTransaction(Request) * will throw an exception in the case of a missing * Max-Forwards header and this method will eventually just * log it thus ignoring the whole event. */ if (request.getHeader(MaxForwardsHeader.NAME) == null) { // it appears that some buggy providers do send requests // with no Max-Forwards headers, as we are at application level // and we know there will be no endless loops // there is no problem of adding headers and process normally // this messages MaxForwardsHeader maxForwards = SipFactory.getInstance().createHeaderFactory().createMaxForwardsHeader(70); request.setHeader(maxForwards); } } catch (Throwable ex) { logger.warn("Cannot apply incoming request modification!", ex); } try { // using asterisk voice mail initial notify for messages // is ok, but on the fly received messages their notify comes // without subscription-state, so we add it in order to be able to // process message. if (request.getMethod().equals(Request.NOTIFY) && request.getHeader(EventHeader.NAME) != null && ((EventHeader) request.getHeader(EventHeader.NAME)) .getEventType() .equals(OperationSetMessageWaitingSipImpl.EVENT_PACKAGE) && request.getHeader(SubscriptionStateHeader.NAME) == null) { request.addHeader( new HeaderFactoryImpl().createSubscriptionStateHeader(SubscriptionStateHeader.ACTIVE)); } } catch (Throwable ex) { logger.warn("Cannot apply incoming request modification!", ex); } try { // receiving notify message without subscription state // used for keep-alive pings, they have done their job // and are no more need. Skip processing them to avoid // filling logs with unneeded exceptions. if (request.getMethod().equals(Request.NOTIFY) && request.getHeader(SubscriptionStateHeader.NAME) == null) { return true; } } catch (Throwable ex) { logger.warn("Cannot apply incoming request modification!", ex); } return false; }
/** * Logs the specified message and details. * * @param message the message to log * @param from the message sender * @param to the message addressee * @param status message status * @param sender determines whether we are the origin of this message. */ public void logMessage( SIPMessage message, String from, String to, String status, boolean sender) { if (!logger.isInfoEnabled()) return; String msgHeader; if (sender) msgHeader = "JAIN-SIP sent a message from=\""; else msgHeader = "JAIN-SIP received a message from=\""; if (logger.isInfoEnabled()) logger.info(msgHeader + from + "\" to=\"" + to + "\" (status: " + status + "):\n" + message); }
/** * Dispatches the event received from a JAIN-SIP <tt>SipProvider</tt> to one of our "candidate * recipient" listeners. * * @param event the event received for a <tt>SipProvider</tt>. */ public void processIOException(IOExceptionEvent event) { try { if (logger.isTraceEnabled()) logger.trace(event); // impossible to dispatch, log here if (logger.isDebugEnabled()) logger.debug("@todo implement processIOException()"); } catch (Throwable exc) { // any exception thrown within our code should be caught here // so that we could log it rather than interrupt stack activity with // it. this.logApplicationException(DialogTerminatedEvent.class, exc); } }
/** * Dispatches the event received from a JAIN-SIP <tt>SipProvider</tt> to one of our "candidate * recipient" listeners. * * @param event the event received for a <tt>SipProvider</tt>. */ public void processTimeout(TimeoutEvent event) { try { Transaction transaction; if (event.isServerTransaction()) { transaction = event.getServerTransaction(); } else { transaction = event.getClientTransaction(); } ProtocolProviderServiceSipImpl recipient = getServiceData(transaction); if (recipient == null) { logger.error( "We received a timeout which wasn't " + "marked, please report this to " + "*****@*****.**"); } else { recipient.processTimeout(event); } } catch (Throwable exc) { // any exception thrown within our code should be caught here // so that we could log it rather than interrupt stack activity with // it. this.logApplicationException(DialogTerminatedEvent.class, exc); } }
/** * Populates a specific <tt>Request</tt> instance with the headers common to dialog-creating * <tt>Request</tt>s and ones sent inside existing dialogs and specific to the general event * package subscription functionality that this instance and a specific <tt>Subscription</tt> * represent. * * @param req the <tt>Request</tt> instance to be populated with common headers and ones specific * to the event package of a specific <tt>Subscription</tt> * @param subscription the <tt>Subscription</tt> which is to be described in the specified * <tt>Request</tt> i.e. its properties are to be used to populate the specified * <tt>Request</tt> * @param expires the subscription duration to be set into the Expires header of the specified * SUBSCRIBE <tt>Request</tt> * @throws OperationFailedException if we fail parsing or populating the subscription request. */ protected void populateSubscribeRequest(Request req, Subscription subscription, int expires) throws OperationFailedException { HeaderFactory headerFactory = protocolProvider.getHeaderFactory(); // Event EventHeader evHeader; try { evHeader = headerFactory.createEventHeader(eventPackage); String eventId = subscription.getEventId(); if (eventId != null) evHeader.setEventId(eventId); } catch (ParseException e) { // these two should never happen. logger.error("An unexpected error occurred while" + "constructing the EventHeader", e); throw new OperationFailedException( "An unexpected error occurred while" + "constructing the EventHeader", OperationFailedException.INTERNAL_ERROR, e); } req.setHeader(evHeader); // Accept AcceptHeader accept; try { accept = headerFactory.createAcceptHeader("application", contentSubType); } catch (ParseException e) { logger.error("wrong accept header", e); throw new OperationFailedException( "An unexpected error occurred while" + "constructing the AcceptHeader", OperationFailedException.INTERNAL_ERROR, e); } req.setHeader(accept); // Expires ExpiresHeader expHeader; try { expHeader = headerFactory.createExpiresHeader(expires); } catch (InvalidArgumentException e) { logger.error("Invalid expires value: " + expires, e); throw new OperationFailedException( "An unexpected error occurred while" + "constructing the ExpiresHeader", OperationFailedException.INTERNAL_ERROR, e); } req.setHeader(expHeader); }
/** * Put the stack in a state where it can receive data on three UDP/TCP ports (2 for clear * communication, 1 for TLS). That is to say create the related JAIN-SIP <tt>ListeningPoint</tt>s * and <tt>SipProvider</tt>s. * * @throws OperationFailedException if creating one of the underlying <tt>SipProvider</tt>s fails * for whatever reason. */ private void startListening() throws OperationFailedException { try { int bindRetriesValue = getBindRetriesValue(); this.createProvider(this.getPreferredClearPort(), bindRetriesValue, false); this.createProvider(this.getPreferredSecurePort(), bindRetriesValue, true); this.stack.start(); if (logger.isTraceEnabled()) logger.trace("started listening"); } catch (Exception ex) { logger.error( "An unexpected error happened while creating the" + "SipProviders and ListeningPoints."); throw new OperationFailedException( "An unexpected error hapenned" + "while initializing the SIP stack", OperationFailedException.INTERNAL_ERROR, ex); } }
/** * Logs the specified message and details. * * @param message the message to log * @param from the message sender * @param to the message addressee * @param status message status * @param sender determines whether we are the origin of this message. * @param time the date this message was received at. */ public void logMessage( SIPMessage message, String from, String to, String status, boolean sender, long time) { try { logPacket(message, sender); } catch (Throwable e) { logger.error("Error logging packet", e); } }
/** * Sends the <tt>message</tt> to the destination indicated by the <tt>to</tt> contact. * * @param to the <tt>Contact</tt> to send <tt>message</tt> to * @param message the <tt>Message</tt> to send. * @throws java.lang.IllegalStateException if the underlying stack is not registered and * initialized. * @throws java.lang.IllegalArgumentException if <tt>to</tt> is not an instance of ContactImpl. */ public void sendInstantMessage(Contact to, Message message) throws IllegalStateException, IllegalArgumentException { if (!(to instanceof ContactSipImpl)) throw new IllegalArgumentException("The specified contact is not a Sip contact." + to); assertConnected(); // offline message if (to.getPresenceStatus().equals(sipStatusEnum.getStatus(SipStatusEnum.OFFLINE)) && !offlineMessageSupported) { if (logger.isDebugEnabled()) logger.debug("trying to send a message to an offline contact"); fireMessageDeliveryFailed( message, to, MessageDeliveryFailedEvent.OFFLINE_MESSAGES_NOT_SUPPORTED); return; } // create the message Request mes; try { mes = createMessageRequest(to, message); } catch (OperationFailedException ex) { logger.error("Failed to create the message.", ex); fireMessageDeliveryFailed(message, to, MessageDeliveryFailedEvent.INTERNAL_ERROR); return; } try { sendMessageRequest(mes, to, message); } catch (TransactionUnavailableException ex) { logger.error( "Failed to create messageTransaction.\n" + "This is most probably a network connection error.", ex); fireMessageDeliveryFailed(message, to, MessageDeliveryFailedEvent.NETWORK_FAILURE); return; } catch (SipException ex) { logger.error("Failed to send the message.", ex); fireMessageDeliveryFailed(message, to, MessageDeliveryFailedEvent.INTERNAL_ERROR); return; } }
/** * Return true/false if logging is enabled at a given level. * * @param logLevel the level that we'd like to check loggability for. * @return always <tt>true</tt> regardless of <tt>logLevel</tt>'s value. */ public boolean isLoggingEnabled(int logLevel) { // always enable trace messages so we can receive packets // and log them to packet logging service if (logLevel == TRACE_DEBUG) return logger.isDebugEnabled(); if (logLevel == TRACE_MESSAGES) // same as TRACE_INFO return true; if (logLevel == TRACE_NONE) return false; return true; }
/** * Dispatches the event received from a JAIN-SIP <tt>SipProvider</tt> to one of our "candidate * recipient" listeners. * * @param event the event received for a <tt>SipProvider</tt>. */ public void processResponse(ResponseEvent event) { try { // we don't have to accept the transaction since we // created the request ClientTransaction transaction = event.getClientTransaction(); if (logger.isTraceEnabled()) logger.trace( "received response: " + event.getResponse().getStatusCode() + " " + event.getResponse().getReasonPhrase()); if (transaction == null) { logger.warn("Transaction is null, probably already expired!"); return; } ProtocolProviderServiceSipImpl service = getServiceData(transaction); if (service != null) { // Mark the dialog for the dispatching of later in-dialog // responses. If there is no dialog then the initial request // sure is marked otherwise we won't have found the service with // getServiceData(). The request has to be marked in case we // receive one more response in an out-of-dialog transaction. if (event.getDialog() != null) { SipApplicationData.setApplicationData( event.getDialog(), SipApplicationData.KEY_SERVICE, service); } service.processResponse(event); } else { logger.error( "We received a response which " + "wasn't marked, please report this to " + "*****@*****.**"); } } catch (Throwable exc) { // any exception thrown within our code should be caught here // so that we could log it rather than interrupt stack activity with // it. this.logApplicationException(DialogTerminatedEvent.class, exc); } }
/** * Dispatches the event received from a JAIN-SIP <tt>SipProvider</tt> to one of our "candidate * recipient" listeners. * * @param event the event received for a <tt>SipProvider</tt>. */ public void processDialogTerminated(DialogTerminatedEvent event) { try { ProtocolProviderServiceSipImpl recipient = (ProtocolProviderServiceSipImpl) SipApplicationData.getApplicationData( event.getDialog(), SipApplicationData.KEY_SERVICE); if (recipient == null) { logger.error( "Dialog wasn't marked, please report this to " + "*****@*****.**"); } else { if (logger.isTraceEnabled()) logger.trace("service was found with dialog data"); recipient.processDialogTerminated(event); } } catch (Throwable exc) { // any exception thrown within our code should be caught here // so that we could log it rather than interrupt stack activity with // it. this.logApplicationException(DialogTerminatedEvent.class, exc); } }
/** * Retrieves and returns that ProtocolProviderService that this transaction belongs to, or * <tt>null</tt> if we couldn't associate it with a provider based on neither the request nor the * transaction itself. * * @param transaction the transaction that we'd like to determine a provider for. * @return a reference to the <tt>ProtocolProviderServiceSipImpl</tt> that <tt>transaction</tt> * was associated with or <tt>null</tt> if we couldn't determine which one it is. */ private ProtocolProviderServiceSipImpl getServiceData(Transaction transaction) { ProtocolProviderServiceSipImpl service = (ProtocolProviderServiceSipImpl) SipApplicationData.getApplicationData( transaction.getRequest(), SipApplicationData.KEY_SERVICE); if (service != null) { if (logger.isTraceEnabled()) logger.trace("service was found in request data"); return service; } service = (ProtocolProviderServiceSipImpl) SipApplicationData.getApplicationData( transaction.getDialog(), SipApplicationData.KEY_SERVICE); if (service != null) { if (logger.isTraceEnabled()) logger.trace("service was found in dialog data"); } return service; }
/** * Constructor for this class. Creates the JAIN-SIP stack. * * @throws OperationFailedException if creating the stack fails. */ SipStackSharing() throws OperationFailedException { // init of the stack try { SipFactory sipFactory = SipFactory.getInstance(); sipFactory.setPathName("org.jitsi.gov.nist"); Properties sipStackProperties = new SipStackProperties(); // Create SipStack object this.stack = sipFactory.createSipStack(sipStackProperties); if (logger.isTraceEnabled()) logger.trace("Created stack: " + this.stack); // set our custom address resolver managing SRV records AddressResolverImpl addressResolver = new AddressResolverImpl(); ((SIPTransactionStack) this.stack).setAddressResolver(addressResolver); SipActivator.getNetworkAddressManagerService().addNetworkConfigurationChangeListener(this); } catch (Exception ex) { logger.fatal("Failed to get SIP Factory.", ex); throw new OperationFailedException( "Failed to get SIP Factory", OperationFailedException.INTERNAL_ERROR, ex); } }
/** * Creates and sends a SUBSCRIBE request to a specific subscription <tt>Address</tt>/Request URI * if it matches a <tt>Subscription</tt> with an id tag of its Event header of a specific value in * the list of subscriptions managed by this instance with an Expires header value of zero in * order to terminate receiving event notifications and removes the specified * <tt>Subscription</tt> from the list of subscriptions managed by this instance. The removed * <tt>Subscription</tt> may receive notifications to process the <tt>Request</tt>s and/or * <tt>Response</tt>s which constitute the signaling session associated with it. If the attempt to * create the SUBSCRIBE request fails, the associated <tt>Subscription</tt> is not removed from * the list of subscriptions managed by this instance. If the specified <tt>Address</tt> does not * identify an existing <tt>Subscription</tt> in the list of subscriptions managed by this * instance, an assertion may optionally be performed or no reaction can be taken. * * @param toAddress a subscription <tt>Address</tt>/Request URI which identifies a * <tt>Subscription</tt> to be removed from the list of subscriptions managed by this instance * @param eventId the id tag placed in the Event header of the <tt>Subscription</tt> to be matched * if there is one or <tt>null</tt> if the <tt>Subscription</tt> should have no id tag in its * Event header * @param assertSubscribed <tt>true</tt> to assert if the specified subscription * <tt>Address</tt>/Request URI does not identify an existing <tt>Subscription</tt> in the * list of subscriptions managed by this instance; <tt>false</tt> to not assert if the * mentioned condition is met * @throws IllegalArgumentException if <tt>assertSubscribed</tt> is <tt>true</tt> and * <tt>toAddress</tt> and <tt>eventId</tt> do not identify an existing <tt>Subscription</tt> * in the list of subscriptions managed by this instance * @throws OperationFailedException if we fail constructing or sending the unSUBSCRIBE request. */ public void unsubscribe(Address toAddress, String eventId, boolean assertSubscribed) throws IllegalArgumentException, OperationFailedException { Subscription subscription = getSubscription(toAddress, eventId); if (subscription == null) if (assertSubscribed) throw new IllegalArgumentException("trying to unregister a not registered contact"); else return; Dialog dialog = subscription.getDialog(); // we stop the subscription if we're subscribed to this contact if (dialog != null) { String callId = dialog.getCallId().getCallId(); ClientTransaction subscribeTransaction; try { subscribeTransaction = createSubscription(subscription, dialog, 0); } catch (OperationFailedException e) { if (logger.isDebugEnabled()) logger.debug("failed to create the unsubscription", e); throw e; } // we are not anymore subscribed to this contact // this ensure that the response of this request will be // handled as an unsubscription response removeSubscription(callId, subscription); try { dialog.sendRequest(subscribeTransaction); } catch (SipException e) { if (logger.isDebugEnabled()) logger.debug("Can't send the request", e); throw new OperationFailedException( "Failed to send the subscription message", OperationFailedException.NETWORK_FAILURE, e); } } }
/** * Receives options requests and replies with an OK response containing methods that we support. * * @param requestEvent the incoming options request. * @return <tt>true</tt> if request has been successfully processed, <tt>false</tt> otherwise */ @Override public boolean processRequest(RequestEvent requestEvent) { Response optionsOK = null; try { optionsOK = provider.getMessageFactory().createResponse(Response.OK, requestEvent.getRequest()); // add to the allows header all methods that we support for (String method : provider.getSupportedMethods()) { // don't support REGISTERs if (!method.equals(Request.REGISTER)) optionsOK.addHeader(provider.getHeaderFactory().createAllowHeader(method)); } Iterable<String> knownEventsList = provider.getKnownEventsList(); synchronized (knownEventsList) { for (String event : knownEventsList) optionsOK.addHeader(provider.getHeaderFactory().createAllowEventsHeader(event)); } } catch (ParseException ex) { // What else could we do apart from logging? logger.warn("Failed to create an incoming OPTIONS request", ex); return false; } try { SipStackSharing.getOrCreateServerTransaction(requestEvent).sendResponse(optionsOK); } catch (TransactionUnavailableException ex) { // this means that we received an OPTIONS request outside the scope // of a transaction which could mean that someone is simply sending // us b****hit to keep a NAT connection alive, so let's not get too // excited. if (logger.isInfoEnabled()) logger.info("Failed to respond to an incoming " + "transactionless OPTIONS request"); if (logger.isTraceEnabled()) logger.trace("Exception was:", ex); return false; } catch (InvalidArgumentException ex) { // What else could we do apart from logging? logger.warn("Failed to send an incoming OPTIONS request", ex); return false; } catch (SipException ex) { // What else could we do apart from logging? logger.warn("Failed to send an incoming OPTIONS request", ex); return false; } return true; }
/** * Creates a new SUBSCRIBE request in the form of a <tt>ClientTransaction</tt> with the parameters * of a specific <tt>Subscription</tt>. * * @param subscription the <tt>Subscription</tt> to be described in a SUBSCRIBE request * @param dialog the <tt>Dialog</tt> with which this request should be associated * @param expires the subscription duration of the SUBSCRIBE request to be created * @return a new <tt>ClientTransaction</tt> initialized with a new SUBSCRIBE request which matches * the parameters of the specified <tt>Subscription</tt> and is associated with the specified * <tt>Dialog</tt> * @throws OperationFailedException if the message could not be generated */ private ClientTransaction createSubscription( Subscription subscription, Dialog dialog, int expires) throws OperationFailedException { Request req = messageFactory.createRequest(dialog, Request.SUBSCRIBE); // Address Address toAddress = dialog.getRemoteTarget(); // no Contact field if (toAddress == null) toAddress = dialog.getRemoteParty(); // MaxForwards MaxForwardsHeader maxForwards = protocolProvider.getMaxForwardsHeader(); req.setHeader(maxForwards); /* * Create the transaction and then add the via header as recommended by * the jain-sip documentation at * http://snad.ncsl.nist.gov/proj/iptel/jain-sip-1.2/javadoc * /javax/sip/Dialog.html#createRequest(String). */ ClientTransaction transac = null; try { transac = protocolProvider.getDefaultJainSipProvider().getNewClientTransaction(req); } catch (TransactionUnavailableException ex) { logger.error( "Failed to create subscriptionTransaction.\n" + "This is most probably a network connection error.", ex); throw new OperationFailedException( "Failed to create the subscription transaction", OperationFailedException.NETWORK_FAILURE); } populateSubscribeRequest(req, subscription, expires); return transac; }
/** * Handles OPTIONS requests by replying with an OK response containing methods that we support. * * @author Emil Ivov * @author Pawel Domas */ public class ClientCapabilities extends MethodProcessorAdapter { /** * The <tt>Logger</tt> used by the <tt>ClientCapabilities</tt> class and its instances for logging * output. */ private static final Logger logger = Logger.getLogger(ClientCapabilities.class); /** The protocol provider that created us. */ private final ProtocolProviderServiceSipImpl provider; /** Registration listener instance. */ private final RegistrationListener registrationListener; /** The timer that runs the keep-alive task */ private Timer keepAliveTimer = null; /** The next long to use as a cseq header value. */ private long nextCSeqValue = 1; /** * Creates a new instance of this class using the specified parent <tt>protocolProvider</tt>. * * @param protocolProvider a reference to the <tt>ProtocolProviderServiceSipImpl</tt> instance * that created us. */ public ClientCapabilities(ProtocolProviderServiceSipImpl protocolProvider) { this.provider = protocolProvider; provider.registerMethodProcessor(Request.OPTIONS, this); registrationListener = new RegistrationListener(); provider.addRegistrationStateChangeListener(registrationListener); } /** * Receives options requests and replies with an OK response containing methods that we support. * * @param requestEvent the incoming options request. * @return <tt>true</tt> if request has been successfully processed, <tt>false</tt> otherwise */ @Override public boolean processRequest(RequestEvent requestEvent) { Response optionsOK = null; try { optionsOK = provider.getMessageFactory().createResponse(Response.OK, requestEvent.getRequest()); // add to the allows header all methods that we support for (String method : provider.getSupportedMethods()) { // don't support REGISTERs if (!method.equals(Request.REGISTER)) optionsOK.addHeader(provider.getHeaderFactory().createAllowHeader(method)); } Iterable<String> knownEventsList = provider.getKnownEventsList(); synchronized (knownEventsList) { for (String event : knownEventsList) optionsOK.addHeader(provider.getHeaderFactory().createAllowEventsHeader(event)); } } catch (ParseException ex) { // What else could we do apart from logging? logger.warn("Failed to create an incoming OPTIONS request", ex); return false; } try { SipStackSharing.getOrCreateServerTransaction(requestEvent).sendResponse(optionsOK); } catch (TransactionUnavailableException ex) { // this means that we received an OPTIONS request outside the scope // of a transaction which could mean that someone is simply sending // us b****hit to keep a NAT connection alive, so let's not get too // excited. if (logger.isInfoEnabled()) logger.info("Failed to respond to an incoming " + "transactionless OPTIONS request"); if (logger.isTraceEnabled()) logger.trace("Exception was:", ex); return false; } catch (InvalidArgumentException ex) { // What else could we do apart from logging? logger.warn("Failed to send an incoming OPTIONS request", ex); return false; } catch (SipException ex) { // What else could we do apart from logging? logger.warn("Failed to send an incoming OPTIONS request", ex); return false; } return true; } /** * Returns the next long to use as a cseq header value. * * @return the next long to use as a cseq header value. */ private long getNextCSeqValue() { return nextCSeqValue++; } /** Fire event that connection has failed and we had to unregister the protocol provider. */ private void disconnect() { // don't alert the user if we're already off if (provider.getRegistrationState().equals(RegistrationState.UNREGISTERED)) { return; } provider .getRegistrarConnection() .setRegistrationState( RegistrationState.CONNECTION_FAILED, RegistrationStateChangeEvent.REASON_NOT_SPECIFIED, "A timeout occurred while trying to connect to the server."); } /** Frees allocated resources. */ void shutdown() { provider.removeRegistrationStateChangeListener(registrationListener); } /** The task would continuously send OPTIONs request that we use as a keep alive method. */ private class OptionsKeepAliveTask extends TimerTask { @Override public void run() { try { logger.logEntry(); // From FromHeader fromHeader = null; try { // this keep alive task only makes sense in case we have // a registrar so we deliberately use our AOR and do not // use the getOurSipAddress() method. fromHeader = provider .getHeaderFactory() .createFromHeader( provider.getRegistrarConnection().getAddressOfRecord(), SipMessageFactory.generateLocalTag()); } catch (ParseException ex) { // this should never happen so let's just log and bail. logger.error("Failed to generate a from header for " + "our register request.", ex); return; } // Call ID Header CallIdHeader callIdHeader = provider.getDefaultJainSipProvider().getNewCallId(); // CSeq Header CSeqHeader cSeqHeader = null; try { cSeqHeader = provider.getHeaderFactory().createCSeqHeader(getNextCSeqValue(), Request.OPTIONS); } catch (ParseException ex) { // Should never happen logger.error("Corrupt Sip Stack", ex); return; } catch (InvalidArgumentException ex) { // Should never happen logger.error("The application is corrupt", ex); return; } // To Header ToHeader toHeader = null; try { // this request isn't really going anywhere so we put our // own address in the To Header. toHeader = provider.getHeaderFactory().createToHeader(fromHeader.getAddress(), null); } catch (ParseException ex) { logger.error("Could not create a To header for address:" + fromHeader.getAddress(), ex); return; } // MaxForwardsHeader MaxForwardsHeader maxForwardsHeader = provider.getMaxForwardsHeader(); // Request Request request = null; try { // create a host-only uri for the request uri header. String domain = ((SipURI) toHeader.getAddress().getURI()).getHost(); // request URI SipURI requestURI = provider.getAddressFactory().createSipURI(null, domain); // Via Headers ArrayList<ViaHeader> viaHeaders = provider.getLocalViaHeaders(requestURI); request = provider .getMessageFactory() .createRequest( requestURI, Request.OPTIONS, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwardsHeader); if (logger.isDebugEnabled()) logger.debug("Created OPTIONS request " + request); } catch (ParseException ex) { logger.error("Could not create an OPTIONS request!", ex); return; } Iterator<String> supportedMethods = provider.getSupportedMethods().iterator(); // add to the allows header all methods that we support while (supportedMethods.hasNext()) { String method = supportedMethods.next(); // don't support REGISTERs if (method.equals(Request.REGISTER)) continue; request.addHeader(provider.getHeaderFactory().createAllowHeader(method)); } Iterator<String> events = provider.getKnownEventsList().iterator(); synchronized (provider.getKnownEventsList()) { while (events.hasNext()) { String event = events.next(); request.addHeader(provider.getHeaderFactory().createAllowEventsHeader(event)); } } // Transaction ClientTransaction optionsTrans = null; try { optionsTrans = provider.getDefaultJainSipProvider().getNewClientTransaction(request); } catch (TransactionUnavailableException ex) { logger.error("Could not create options transaction!\n", ex); return; } try { optionsTrans.sendRequest(); if (logger.isDebugEnabled()) logger.debug("sent request= " + request); } catch (SipException ex) { logger.error("Could not send out the options request!", ex); if (ex.getCause() instanceof IOException) { // IOException problem with network disconnect(); } return; } } catch (Exception ex) { logger.error("Cannot send OPTIONS keep alive", ex); } } } /** Class implements CRLF keep alive method. */ private class CRLfKeepAliveTask extends TimerTask { @Override public void run() { ProxyConnection connection = provider.getConnection(); if (connection == null) { logger.error("No connection found to send CRLF keep alive" + " with " + provider); return; } ListeningPoint lp = provider.getListeningPoint(connection.getTransport()); if (!(lp instanceof ListeningPointExt)) { logger.error("ListeningPoint is not ListeningPointExt" + "(or is null)"); return; } InetSocketAddress address = connection.getAddress(); try { ((ListeningPointExt) lp) .sendHeartbeat(address.getAddress().getHostAddress(), address.getPort()); } catch (IOException e) { logger.error("Error while sending a heartbeat", e); } } } private class RegistrationListener implements RegistrationStateChangeListener { /** * The method is called by a ProtocolProvider implementation whenever a change in the * registration state of the corresponding provider had occurred. The method is particularly * interested in events stating that the SIP provider has unregistered so that it would fire * status change events for all contacts in our buddy list. * * @param evt ProviderStatusChangeEvent the event describing the status change. */ public void registrationStateChanged(RegistrationStateChangeEvent evt) { if (evt.getNewState() == RegistrationState.UNREGISTERING || evt.getNewState() == RegistrationState.UNREGISTERED || evt.getNewState() == RegistrationState.AUTHENTICATION_FAILED || evt.getNewState() == RegistrationState.CONNECTION_FAILED) { // stop any task associated with the timer if (keepAliveTimer != null) { keepAliveTimer.cancel(); keepAliveTimer = null; } } else if (evt.getNewState().equals(RegistrationState.REGISTERED)) { String keepAliveMethod = provider .getAccountID() .getAccountPropertyString(ProtocolProviderFactory.KEEP_ALIVE_METHOD); if (logger.isTraceEnabled()) logger.trace("Keep alive method " + keepAliveMethod); // options is default keep-alive, if property is missing // then options is used if (keepAliveMethod != null && !(keepAliveMethod.equalsIgnoreCase("options") || keepAliveMethod.equalsIgnoreCase("crlf"))) return; int keepAliveInterval = provider .getAccountID() .getAccountPropertyInt(ProtocolProviderFactory.KEEP_ALIVE_INTERVAL, -1); if (logger.isTraceEnabled()) logger.trace("Keep alive interval is " + keepAliveInterval); if (keepAliveInterval > 0 && !provider.getRegistrarConnection().isRegistrarless()) { if (keepAliveTimer == null) keepAliveTimer = new Timer(); TimerTask keepAliveTask; // CRLF is used by default on Android if ((OSUtils.IS_ANDROID && keepAliveMethod == null) || "crlf".equalsIgnoreCase(keepAliveMethod)) { keepAliveTask = new CRLfKeepAliveTask(); } else { // OPTIONS keepAliveTask = new OptionsKeepAliveTask(); } if (logger.isDebugEnabled()) logger.debug("Scheduling keep alives: " + keepAliveTask); keepAliveTimer.schedule(keepAliveTask, 0, keepAliveInterval * 1000); } } } } }
/** * A straightforward implementation of the basic instant messaging operation set. * * @author Benoit Pradelle */ public class OperationSetBasicInstantMessagingSipImpl extends AbstractOperationSetBasicInstantMessaging { /** Our class logger. */ private static final Logger logger = Logger.getLogger(OperationSetBasicInstantMessagingSipImpl.class); /** A list of processors registered for incoming sip messages. */ private final List<SipMessageProcessor> messageProcessors = new Vector<SipMessageProcessor>(); /** The provider that created us. */ private final ProtocolProviderServiceSipImpl sipProvider; /** * A reference to the persistent presence operation set that we use to match incoming messages to * <tt>Contact</tt>s and vice versa. */ private OperationSetPresenceSipImpl opSetPersPresence = null; /** Hashtable containing the CSeq of each discussion */ private long seqN = hashCode(); /** Hashtable containing the message sent */ private final Map<String, Message> sentMsg = new Hashtable<String, Message>(3); /** It can be implemented in some servers. */ private final boolean offlineMessageSupported; /** Gives access to presence states for the Sip protocol. */ private final SipStatusEnum sipStatusEnum; /** * Creates an instance of this operation set. * * @param provider a ref to the <tt>ProtocolProviderServiceImpl</tt> that created us and that * we'll use for retrieving the underlying aim connection. */ OperationSetBasicInstantMessagingSipImpl(ProtocolProviderServiceSipImpl provider) { this.sipProvider = provider; provider.addRegistrationStateChangeListener(new RegistrationStateListener()); offlineMessageSupported = provider.getAccountID().getAccountPropertyBoolean("OFFLINE_MSG_SUPPORTED", false); sipProvider.registerMethodProcessor( Request.MESSAGE, new BasicInstantMessagingMethodProcessor()); this.sipStatusEnum = sipProvider.getSipStatusEnum(); } /** * Registers a SipMessageProcessor with this operation set so that it gets notifications of * successful message delivery, failure or reception of incoming messages.. * * @param processor the <tt>SipMessageProcessor</tt> to register. */ void addMessageProcessor(SipMessageProcessor processor) { synchronized (this.messageProcessors) { if (!this.messageProcessors.contains(processor)) { this.messageProcessors.add(processor); } } } /** * Unregisters <tt>processor</tt> so that it won't receive any further notifications upon * successful message delivery, failure or reception of incoming messages.. * * @param processor the <tt>SipMessageProcessor</tt> to unregister. */ void removeMessageProcessor(SipMessageProcessor processor) { synchronized (this.messageProcessors) { this.messageProcessors.remove(processor); } } public Message createMessage( String content, String contentType, String encoding, String subject) { return new MessageSipImpl(content, contentType, encoding, subject); } /** * Determines whether the protocol provider (or the protocol itself) support sending and receiving * offline messages. Most often this method would return true for protocols that support offline * messages and false for those that don't. It is however possible for a protocol to support these * messages and yet have a particular account that does not (i.e. feature not enabled on the * protocol server). In cases like this it is possible for this method to return true even when * offline messaging is not supported, and then have the sendMessage method throw an * OperationFailedException with code - OFFLINE_MESSAGES_NOT_SUPPORTED. * * @return <tt>true</tt> if the protocol supports offline messages and <tt>false</tt> otherwise. */ public boolean isOfflineMessagingSupported() { return offlineMessageSupported; } /** * Determines whether the protocol supports the supplied content type * * @param contentType the type we want to check * @return <tt>true</tt> if the protocol supports it and <tt>false</tt> otherwise. */ public boolean isContentTypeSupported(String contentType) { if (contentType.equals(DEFAULT_MIME_TYPE) || contentType.equals(HTML_MIME_TYPE)) return true; else return false; } /** * Sends the <tt>message</tt> to the destination indicated by the <tt>to</tt> contact. * * @param to the <tt>Contact</tt> to send <tt>message</tt> to * @param message the <tt>Message</tt> to send. * @throws java.lang.IllegalStateException if the underlying stack is not registered and * initialized. * @throws java.lang.IllegalArgumentException if <tt>to</tt> is not an instance of ContactImpl. */ public void sendInstantMessage(Contact to, Message message) throws IllegalStateException, IllegalArgumentException { if (!(to instanceof ContactSipImpl)) throw new IllegalArgumentException("The specified contact is not a Sip contact." + to); assertConnected(); // offline message if (to.getPresenceStatus().equals(sipStatusEnum.getStatus(SipStatusEnum.OFFLINE)) && !offlineMessageSupported) { if (logger.isDebugEnabled()) logger.debug("trying to send a message to an offline contact"); fireMessageDeliveryFailed( message, to, MessageDeliveryFailedEvent.OFFLINE_MESSAGES_NOT_SUPPORTED); return; } // create the message Request mes; try { mes = createMessageRequest(to, message); } catch (OperationFailedException ex) { logger.error("Failed to create the message.", ex); fireMessageDeliveryFailed(message, to, MessageDeliveryFailedEvent.INTERNAL_ERROR); return; } try { sendMessageRequest(mes, to, message); } catch (TransactionUnavailableException ex) { logger.error( "Failed to create messageTransaction.\n" + "This is most probably a network connection error.", ex); fireMessageDeliveryFailed(message, to, MessageDeliveryFailedEvent.NETWORK_FAILURE); return; } catch (SipException ex) { logger.error("Failed to send the message.", ex); fireMessageDeliveryFailed(message, to, MessageDeliveryFailedEvent.INTERNAL_ERROR); return; } } /** * Sends <tt>messageRequest</tt> to the specified destination and logs <tt>messageContent</tt> for * later use. * * @param messageRequest the <tt>SipRequest</tt> that we are about to send. * @param to the Contact that we are sending <tt>messageRequest</tt> to. * @param messageContent the SC <tt>Message</tt> that was used to create the <tt>Request</tt> . * @throws TransactionUnavailableException if we fail creating the transaction required to send * <tt>messageRequest</tt>. * @throws SipException if we fail sending <tt>messageRequest</tt>. */ void sendMessageRequest(Request messageRequest, Contact to, Message messageContent) throws TransactionUnavailableException, SipException { // Transaction ClientTransaction messageTransaction; SipProvider jainSipProvider = this.sipProvider.getDefaultJainSipProvider(); messageTransaction = jainSipProvider.getNewClientTransaction(messageRequest); // send the message messageTransaction.sendRequest(); // we register the reference to this message to retrieve it when // we'll receive the response message String key = ((CallIdHeader) messageRequest.getHeader(CallIdHeader.NAME)).getCallId(); this.sentMsg.put(key, messageContent); } /** * Construct a <tt>Request</tt> represent a new message. * * @param to the <tt>Contact</tt> to send <tt>message</tt> to * @param message the <tt>Message</tt> to send. * @return a Message Request destined to the contact * @throws OperationFailedException if an error occurred during the creation of the request */ Request createMessageRequest(Contact to, Message message) throws OperationFailedException { Address toAddress = null; try { toAddress = sipProvider.parseAddressString(to.getAddress()); } catch (ParseException exc) { // Shouldn't happen logger.error("An unexpected error occurred while" + "constructing the address", exc); throw new OperationFailedException( "An unexpected error occurred while" + "constructing the address", OperationFailedException.INTERNAL_ERROR, exc); } // Call ID CallIdHeader callIdHeader = this.sipProvider.getDefaultJainSipProvider().getNewCallId(); // CSeq CSeqHeader cSeqHeader = null; try { // protect seqN synchronized (this) { cSeqHeader = this.sipProvider.getHeaderFactory().createCSeqHeader(seqN++, Request.MESSAGE); } } catch (InvalidArgumentException ex) { // Shouldn't happen logger.error("An unexpected error occurred while" + "constructing the CSeqHeadder", ex); throw new OperationFailedException( "An unexpected error occurred while" + "constructing the CSeqHeadder", OperationFailedException.INTERNAL_ERROR, ex); } catch (ParseException exc) { // shouldn't happen logger.error("An unexpected error occurred while" + "constructing the CSeqHeadder", exc); throw new OperationFailedException( "An unexpected error occurred while" + "constructing the CSeqHeadder", OperationFailedException.INTERNAL_ERROR, exc); } // FromHeader and ToHeader String localTag = SipMessageFactory.generateLocalTag(); FromHeader fromHeader = null; ToHeader toHeader = null; try { // FromHeader fromHeader = this.sipProvider .getHeaderFactory() .createFromHeader(sipProvider.getOurSipAddress(toAddress), localTag); // ToHeader toHeader = this.sipProvider.getHeaderFactory().createToHeader(toAddress, null); } catch (ParseException ex) { // these two should never happen. logger.error( "An unexpected error occurred while" + "constructing the FromHeader or ToHeader", ex); throw new OperationFailedException( "An unexpected error occurred while" + "constructing the FromHeader or ToHeader", OperationFailedException.INTERNAL_ERROR, ex); } // ViaHeaders ArrayList<ViaHeader> viaHeaders = this.sipProvider.getLocalViaHeaders(toAddress); // MaxForwards MaxForwardsHeader maxForwards = this.sipProvider.getMaxForwardsHeader(); // Content params ContentTypeHeader contTypeHeader; ContentLengthHeader contLengthHeader; try { contTypeHeader = this.sipProvider .getHeaderFactory() .createContentTypeHeader(getType(message), getSubType(message)); if (!DEFAULT_MIME_ENCODING.equalsIgnoreCase(message.getEncoding())) contTypeHeader.setParameter("charset", message.getEncoding()); contLengthHeader = this.sipProvider.getHeaderFactory().createContentLengthHeader(message.getSize()); } catch (ParseException ex) { // these two should never happen. logger.error("An unexpected error occurred while" + "constructing the content headers", ex); throw new OperationFailedException( "An unexpected error occurred while" + "constructing the content headers", OperationFailedException.INTERNAL_ERROR, ex); } catch (InvalidArgumentException exc) { // these two should never happen. logger.error( "An unexpected error occurred while" + "constructing the content length header", exc); throw new OperationFailedException( "An unexpected error occurred while" + "constructing the content length header", OperationFailedException.INTERNAL_ERROR, exc); } Request req; try { req = this.sipProvider .getMessageFactory() .createRequest( toHeader.getAddress().getURI(), Request.MESSAGE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards, contTypeHeader, message.getRawData()); } catch (ParseException ex) { // shouldn't happen logger.error("Failed to create message Request!", ex); throw new OperationFailedException( "Failed to create message Request!", OperationFailedException.INTERNAL_ERROR, ex); } req.addHeader(contLengthHeader); return req; } /** * Parses the content type of a message and return the type * * @param msg the Message to scan * @return the type of the message */ private String getType(Message msg) { String type = msg.getContentType(); return type.substring(0, type.indexOf('/')); } /** * Parses the content type of a message and return the subtype * * @param msg the Message to scan * @return the subtype of the message */ private String getSubType(Message msg) { String subtype = msg.getContentType(); return subtype.substring(subtype.indexOf('/') + 1); } /** * Utility method throwing an exception if the stack is not properly initialized. * * @throws java.lang.IllegalStateException if the underlying stack is not registered and * initialized. */ private void assertConnected() throws IllegalStateException { if (this.sipProvider == null) throw new IllegalStateException( "The provider must be non-null and signed on the " + "service before being able to communicate."); if (!this.sipProvider.isRegistered()) throw new IllegalStateException( "The provider must be signed on the service before " + "being able to communicate."); } /** Our listener that will tell us when we're registered to */ private class RegistrationStateListener implements RegistrationStateChangeListener { /** * The method is called by a ProtocolProvider implementation whenever a change in the * registration state of the corresponding provider had occurred. * * @param evt ProviderStatusChangeEvent the event describing the status change. */ public void registrationStateChanged(RegistrationStateChangeEvent evt) { if (logger.isDebugEnabled()) logger.debug( "The provider changed state from: " + evt.getOldState() + " to: " + evt.getNewState()); if (evt.getNewState() == RegistrationState.REGISTERED) { opSetPersPresence = (OperationSetPresenceSipImpl) sipProvider.getOperationSet(OperationSetPersistentPresence.class); } } } /** Class for listening incoming packets. */ private class BasicInstantMessagingMethodProcessor extends MethodProcessorAdapter { @Override public boolean processTimeout(TimeoutEvent timeoutEvent) { synchronized (messageProcessors) { for (SipMessageProcessor listener : messageProcessors) if (!listener.processTimeout(timeoutEvent, sentMsg)) return true; } // this is normaly handled by the SIP stack logger.error("Timeout event thrown : " + timeoutEvent.toString()); if (timeoutEvent.isServerTransaction()) { logger.warn("The sender has probably not received our OK"); return false; } Request req = timeoutEvent.getClientTransaction().getRequest(); // get the content String content = null; try { content = new String(req.getRawContent(), getCharset(req)); } catch (UnsupportedEncodingException ex) { logger.warn("failed to convert the message charset", ex); content = new String(req.getRawContent()); } // to who this request has been sent ? ToHeader toHeader = (ToHeader) req.getHeader(ToHeader.NAME); if (toHeader == null) { logger.error("received a request without a to header"); return false; } Contact to = opSetPersPresence.resolveContactID(toHeader.getAddress().getURI().toString()); Message failedMessage = null; if (to == null) { logger.error( "timeout on a message sent to an unknown contact : " + toHeader.getAddress().getURI().toString()); // we don't know what message it concerns, so create a new // one failedMessage = createMessage(content); } else { // try to retrieve the original message String key = ((CallIdHeader) req.getHeader(CallIdHeader.NAME)).getCallId(); failedMessage = sentMsg.get(key); if (failedMessage == null) { // should never happen logger.error("Couldn't find the sent message."); // we don't know what the message is so create a new one // based on the content of the failed request. failedMessage = createMessage(content); } } // error for delivering the message fireMessageDeliveryFailed( // we don't know what message it concerns failedMessage, to, MessageDeliveryFailedEvent.INTERNAL_ERROR); return true; } /** * Process a request from a distant contact * * @param requestEvent the <tt>RequestEvent</tt> containing the newly received request. * @return <tt>true</tt> if the specified event has been handled by this processor and shouldn't * be offered to other processors registered for the same method; <tt>false</tt>, otherwise */ @Override public boolean processRequest(RequestEvent requestEvent) { synchronized (messageProcessors) { for (SipMessageProcessor listener : messageProcessors) if (!listener.processMessage(requestEvent)) return true; } // get the content String content = null; Request req = requestEvent.getRequest(); try { content = new String(req.getRawContent(), getCharset(req)); } catch (UnsupportedEncodingException ex) { if (logger.isDebugEnabled()) logger.debug("failed to convert the message charset"); content = new String(requestEvent.getRequest().getRawContent()); } // who sent this request ? FromHeader fromHeader = (FromHeader) requestEvent.getRequest().getHeader(FromHeader.NAME); if (fromHeader == null) { logger.error("received a request without a from header"); return false; } Contact from = opSetPersPresence.resolveContactID(fromHeader.getAddress().getURI().toString()); ContentTypeHeader ctheader = (ContentTypeHeader) req.getHeader(ContentTypeHeader.NAME); String ctype = null; String cencoding = null; if (ctheader == null) { ctype = DEFAULT_MIME_TYPE; } else { ctype = ctheader.getContentType() + "/" + ctheader.getContentSubType(); cencoding = ctheader.getParameter("charset"); } if (cencoding == null) cencoding = DEFAULT_MIME_ENCODING; Message newMessage = createMessage(content, ctype, cencoding, null); if (from == null) { if (logger.isDebugEnabled()) logger.debug( "received a message from an unknown contact: " + fromHeader.getAddress().getURI().toString()); // create the volatile contact from = opSetPersPresence.createVolatileContact(fromHeader.getAddress().getURI().toString()); } // answer ok try { Response ok = sipProvider.getMessageFactory().createResponse(Response.OK, requestEvent.getRequest()); SipStackSharing.getOrCreateServerTransaction(requestEvent).sendResponse(ok); } catch (ParseException exc) { logger.error("failed to build the response", exc); } catch (SipException exc) { logger.error("failed to send the response : " + exc.getMessage(), exc); } catch (InvalidArgumentException exc) { if (logger.isDebugEnabled()) logger.debug("Invalid argument for createResponse : " + exc.getMessage(), exc); } // fire an event MessageReceivedEvent msgReceivedEvt = new MessageReceivedEvent(newMessage, from, System.currentTimeMillis()); fireMessageEvent(msgReceivedEvt); return true; } /** * Process a response from a distant contact. * * @param responseEvent the <tt>ResponseEvent</tt> containing the newly received SIP response. * @return <tt>true</tt> if the specified event has been handled by this processor and shouldn't * be offered to other processors registered for the same method; <tt>false</tt>, otherwise */ @Override public boolean processResponse(ResponseEvent responseEvent) { synchronized (messageProcessors) { for (SipMessageProcessor listener : messageProcessors) if (!listener.processResponse(responseEvent, sentMsg)) return true; } Request req = responseEvent.getClientTransaction().getRequest(); int status = responseEvent.getResponse().getStatusCode(); // content of the response String content = null; try { content = new String(req.getRawContent(), getCharset(req)); } catch (UnsupportedEncodingException exc) { if (logger.isDebugEnabled()) logger.debug("failed to convert the message charset", exc); content = new String(req.getRawContent()); } // to who did we send the original message ? ToHeader toHeader = (ToHeader) req.getHeader(ToHeader.NAME); if (toHeader == null) { // should never happen logger.error("send a request without a to header"); return false; } Contact to = opSetPersPresence.resolveContactID(toHeader.getAddress().getURI().toString()); if (to == null) { logger.error( "Error received a response from an unknown contact : " + toHeader.getAddress().getURI().toString() + " : " + responseEvent.getResponse().getStatusCode() + " " + responseEvent.getResponse().getReasonPhrase()); // error for delivering the message fireMessageDeliveryFailed( // we don't know what message it concerns createMessage(content), to, MessageDeliveryFailedEvent.INTERNAL_ERROR); return false; } // we retrieve the original message String key = ((CallIdHeader) req.getHeader(CallIdHeader.NAME)).getCallId(); Message newMessage = sentMsg.get(key); if (newMessage == null) { // should never happen logger.error("Couldn't find the message sent"); // error for delivering the message fireMessageDeliveryFailed( // we don't know what message it is createMessage(content), to, MessageDeliveryFailedEvent.INTERNAL_ERROR); return true; } // status 401/407 = proxy authentification if (status >= 400 && status != 401 && status != 407) { if (logger.isInfoEnabled()) logger.info( responseEvent.getResponse().getStatusCode() + " " + responseEvent.getResponse().getReasonPhrase()); // error for delivering the message MessageDeliveryFailedEvent evt = new MessageDeliveryFailedEvent( newMessage, to, MessageDeliveryFailedEvent.NETWORK_FAILURE, System.currentTimeMillis(), responseEvent.getResponse().getStatusCode() + " " + responseEvent.getResponse().getReasonPhrase()); fireMessageEvent(evt); sentMsg.remove(key); } else if (status == 401 || status == 407) { // proxy ask for authentification if (logger.isDebugEnabled()) logger.debug( "proxy asks authentication : " + responseEvent.getResponse().getStatusCode() + " " + responseEvent.getResponse().getReasonPhrase()); ClientTransaction clientTransaction = responseEvent.getClientTransaction(); SipProvider sourceProvider = (SipProvider) responseEvent.getSource(); try { processAuthenticationChallenge( clientTransaction, responseEvent.getResponse(), sourceProvider); } catch (OperationFailedException ex) { logger.error("can't solve the challenge", ex); // error for delivering the message MessageDeliveryFailedEvent evt = new MessageDeliveryFailedEvent( newMessage, to, MessageDeliveryFailedEvent.NETWORK_FAILURE, System.currentTimeMillis(), ex.getMessage()); fireMessageEvent(evt); sentMsg.remove(key); } } else if (status >= 200) { if (logger.isDebugEnabled()) logger.debug( "Ack received from the network : " + responseEvent.getResponse().getStatusCode() + " " + responseEvent.getResponse().getReasonPhrase()); // we delivered the message MessageDeliveredEvent msgDeliveredEvt = new MessageDeliveredEvent(newMessage, to, System.currentTimeMillis()); fireMessageEvent(msgDeliveredEvt); // we don't need this message anymore sentMsg.remove(key); } return true; } /** * Try to find a charset in a MESSAGE request for the text content. If no charset is defined, * the default charset for text messages is returned. * * @param req the MESSAGE request in which to look for a charset * @return defined charset in the request or DEFAULT_MIME_ENCODING if no charset is specified */ private String getCharset(Request req) { String charset = null; Header contentTypeHeader = req.getHeader(ContentTypeHeader.NAME); if (contentTypeHeader instanceof ContentTypeHeader) charset = ((ContentTypeHeader) contentTypeHeader).getParameter("charset"); if (charset == null) charset = DEFAULT_MIME_ENCODING; return charset; } /** * Attempts to re-generate the corresponding request with the proper credentials. * * @param clientTransaction the corresponding transaction * @param response the challenge * @param jainSipProvider the provider that received the challenge * @throws OperationFailedException if processing the authentication challenge fails. */ private void processAuthenticationChallenge( ClientTransaction clientTransaction, Response response, SipProvider jainSipProvider) throws OperationFailedException { try { if (logger.isDebugEnabled()) logger.debug("Authenticating a message request."); ClientTransaction retryTran = null; // we synch here to protect seqN increment synchronized (this) { retryTran = sipProvider .getSipSecurityManager() .handleChallenge(response, clientTransaction, jainSipProvider, seqN++); } if (retryTran == null) { if (logger.isTraceEnabled()) logger.trace("No password supplied or error occured!"); return; } retryTran.sendRequest(); return; } catch (Exception exc) { logger.error("We failed to authenticate a message request.", exc); throw new OperationFailedException( "Failed to authenticate" + "a message request", OperationFailedException.INTERNAL_ERROR, exc); } } } }
/** * Construct a <tt>Request</tt> represent a new message. * * @param to the <tt>Contact</tt> to send <tt>message</tt> to * @param message the <tt>Message</tt> to send. * @return a Message Request destined to the contact * @throws OperationFailedException if an error occurred during the creation of the request */ Request createMessageRequest(Contact to, Message message) throws OperationFailedException { Address toAddress = null; try { toAddress = sipProvider.parseAddressString(to.getAddress()); } catch (ParseException exc) { // Shouldn't happen logger.error("An unexpected error occurred while" + "constructing the address", exc); throw new OperationFailedException( "An unexpected error occurred while" + "constructing the address", OperationFailedException.INTERNAL_ERROR, exc); } // Call ID CallIdHeader callIdHeader = this.sipProvider.getDefaultJainSipProvider().getNewCallId(); // CSeq CSeqHeader cSeqHeader = null; try { // protect seqN synchronized (this) { cSeqHeader = this.sipProvider.getHeaderFactory().createCSeqHeader(seqN++, Request.MESSAGE); } } catch (InvalidArgumentException ex) { // Shouldn't happen logger.error("An unexpected error occurred while" + "constructing the CSeqHeadder", ex); throw new OperationFailedException( "An unexpected error occurred while" + "constructing the CSeqHeadder", OperationFailedException.INTERNAL_ERROR, ex); } catch (ParseException exc) { // shouldn't happen logger.error("An unexpected error occurred while" + "constructing the CSeqHeadder", exc); throw new OperationFailedException( "An unexpected error occurred while" + "constructing the CSeqHeadder", OperationFailedException.INTERNAL_ERROR, exc); } // FromHeader and ToHeader String localTag = SipMessageFactory.generateLocalTag(); FromHeader fromHeader = null; ToHeader toHeader = null; try { // FromHeader fromHeader = this.sipProvider .getHeaderFactory() .createFromHeader(sipProvider.getOurSipAddress(toAddress), localTag); // ToHeader toHeader = this.sipProvider.getHeaderFactory().createToHeader(toAddress, null); } catch (ParseException ex) { // these two should never happen. logger.error( "An unexpected error occurred while" + "constructing the FromHeader or ToHeader", ex); throw new OperationFailedException( "An unexpected error occurred while" + "constructing the FromHeader or ToHeader", OperationFailedException.INTERNAL_ERROR, ex); } // ViaHeaders ArrayList<ViaHeader> viaHeaders = this.sipProvider.getLocalViaHeaders(toAddress); // MaxForwards MaxForwardsHeader maxForwards = this.sipProvider.getMaxForwardsHeader(); // Content params ContentTypeHeader contTypeHeader; ContentLengthHeader contLengthHeader; try { contTypeHeader = this.sipProvider .getHeaderFactory() .createContentTypeHeader(getType(message), getSubType(message)); if (!DEFAULT_MIME_ENCODING.equalsIgnoreCase(message.getEncoding())) contTypeHeader.setParameter("charset", message.getEncoding()); contLengthHeader = this.sipProvider.getHeaderFactory().createContentLengthHeader(message.getSize()); } catch (ParseException ex) { // these two should never happen. logger.error("An unexpected error occurred while" + "constructing the content headers", ex); throw new OperationFailedException( "An unexpected error occurred while" + "constructing the content headers", OperationFailedException.INTERNAL_ERROR, ex); } catch (InvalidArgumentException exc) { // these two should never happen. logger.error( "An unexpected error occurred while" + "constructing the content length header", exc); throw new OperationFailedException( "An unexpected error occurred while" + "constructing the content length header", OperationFailedException.INTERNAL_ERROR, exc); } Request req; try { req = this.sipProvider .getMessageFactory() .createRequest( toHeader.getAddress().getURI(), Request.MESSAGE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards, contTypeHeader, message.getRawData()); } catch (ParseException ex) { // shouldn't happen logger.error("Failed to create message Request!", ex); throw new OperationFailedException( "Failed to create message Request!", OperationFailedException.INTERNAL_ERROR, ex); } req.addHeader(contLengthHeader); return req; }
/** * Log a message into the log file. * * @param message message to log into the log file. */ public void logDebug(String message) { if (logger.isDebugEnabled()) logger.debug("Debug output from the JAIN-SIP stack: " + message); }
/** * Logs exceptions that have occurred in the application while processing events originating from * the stack. * * @param eventClass the class of the jain-sip event that we were handling when the exception was * thrown. * @param exc the exception that we need to log. */ private void logApplicationException(Class<DialogTerminatedEvent> eventClass, Throwable exc) { String message = "An error occurred while processing event of type: " + eventClass.getName(); logger.error(message, exc); if (logger.isDebugEnabled()) logger.debug(message, exc); }
/** * Find the <tt>ProtocolProviderServiceSipImpl</tt> (one of our "candidate recipient" listeners) * which this <tt>request</tt> should be dispatched to. The strategy is to look first at the * request URI, and then at the To field to find a matching candidate for dispatching. Note that * this method takes a <tt>Request</tt> as param, and not a <tt>ServerTransaction</tt>, because * sometimes <tt>RequestEvent</tt>s have no associated <tt>ServerTransaction</tt>. * * @param request the <tt>Request</tt> to find a recipient for. * @return a suitable <tt>ProtocolProviderServiceSipImpl</tt>. */ private ProtocolProviderServiceSipImpl findTargetFor(Request request) { if (request == null) { logger.error("request shouldn't be null."); return null; } List<ProtocolProviderServiceSipImpl> currentListenersCopy = new ArrayList<ProtocolProviderServiceSipImpl>(this.getSipListeners()); // Let's first narrow down candidate choice by comparing // addresses and ports (no point in delivering to a provider with a // non matching IP address since they will reject it anyway). filterByAddress(currentListenersCopy, request); if (currentListenersCopy.size() == 0) { logger.error("no listeners"); return null; } URI requestURI = request.getRequestURI(); if (requestURI.isSipURI()) { String requestUser = ((SipURI) requestURI).getUser(); List<ProtocolProviderServiceSipImpl> candidates = new ArrayList<ProtocolProviderServiceSipImpl>(); // check if the Request-URI username is // one of ours usernames for (ProtocolProviderServiceSipImpl listener : currentListenersCopy) { String ourUserID = listener.getAccountID().getUserID(); // logger.trace(ourUserID + " *** " + requestUser); if (ourUserID.equals(requestUser)) { if (logger.isTraceEnabled()) logger.trace("suitable candidate found: " + listener.getAccountID()); candidates.add(listener); } } // the perfect match // every other case is approximation if (candidates.size() == 1) { ProtocolProviderServiceSipImpl perfectMatch = candidates.get(0); if (logger.isTraceEnabled()) logger.trace("Will dispatch to \"" + perfectMatch.getAccountID() + "\""); return perfectMatch; } // more than one account match if (candidates.size() > 1) { // check if a custom param exists in the contact // address (set for registrar accounts) for (ProtocolProviderServiceSipImpl candidate : candidates) { String hostValue = ((SipURI) requestURI).getParameter(SipStackSharing.CONTACT_ADDRESS_CUSTOM_PARAM_NAME); if (hostValue == null) continue; if (hostValue.equals(candidate.getContactAddressCustomParamValue())) { if (logger.isTraceEnabled()) logger.trace( "Will dispatch to \"" + candidate.getAccountID() + "\" because " + "\" the custom param was set"); return candidate; } } // Past this point, our guess is not reliable. We try to find // the "least worst" match based on parameters like the To field // check if the To header field host part // matches any of our SIP hosts for (ProtocolProviderServiceSipImpl candidate : candidates) { URI fromURI = ((FromHeader) request.getHeader(FromHeader.NAME)).getAddress().getURI(); if (fromURI.isSipURI() == false) continue; SipURI ourURI = (SipURI) candidate.getOurSipAddress((SipURI) fromURI).getURI(); String ourHost = ourURI.getHost(); URI toURI = ((ToHeader) request.getHeader(ToHeader.NAME)).getAddress().getURI(); if (toURI.isSipURI() == false) continue; String toHost = ((SipURI) toURI).getHost(); // logger.trace(toHost + "***" + ourHost); if (toHost.equals(ourHost)) { if (logger.isTraceEnabled()) logger.trace( "Will dispatch to \"" + candidate.getAccountID() + "\" because " + "host in the To: is the same as in our AOR"); return candidate; } } // fallback on the first candidate ProtocolProviderServiceSipImpl target = candidates.iterator().next(); logger.info( "Will randomly dispatch to \"" + target.getAccountID() + "\" because there is ambiguity on the username from" + " the Request-URI"); if (logger.isTraceEnabled()) logger.trace("\n" + request); return target; } // fallback on any account ProtocolProviderServiceSipImpl target = currentListenersCopy.iterator().next(); if (logger.isDebugEnabled()) logger.debug( "Will randomly dispatch to \"" + target.getAccountID() + "\" because the username in the Request-URI " + "is unknown or empty"); if (logger.isTraceEnabled()) logger.trace("\n" + request); return target; } else { logger.error("Request-URI is not a SIP URI, dropping"); } return null; }
/** * Log an error message. * * @param message -- error message to log. */ public void logFatalError(String message) { if (logger.isTraceEnabled()) logger.trace("Fatal error from the JAIN-SIP stack: " + message); }
/** * Log an error message. * * @param message error message to log. */ public void logError(String message) { logger.error("Error from the JAIN-SIP stack: " + message); }
/** * Dispatches the event received from a JAIN-SIP <tt>SipProvider</tt> to one of our "candidate * recipient" listeners. * * @param event the event received for a <tt>SipProvider</tt>. */ public void processRequest(RequestEvent event) { try { Request request = event.getRequest(); if (logger.isTraceEnabled()) logger.trace("received request: " + request.getMethod()); /* * Create the transaction if it doesn't exist yet. If it is a * dialog-creating request, the dialog will also be automatically * created by the stack. */ if (event.getServerTransaction() == null) { try { // apply some hacks if needed on incoming request // to be compliant with some servers/clients // if needed stop further processing. if (applyNonConformanceHacks(event)) return; SipProvider source = (SipProvider) event.getSource(); ServerTransaction transaction = source.getNewServerTransaction(request); /* * Update the event, otherwise getServerTransaction() and * getDialog() will still return their previous value. */ event = new RequestEvent(source, transaction, transaction.getDialog(), request); } catch (SipException ex) { logger.error( "couldn't create transaction, please report " + "this to [email protected]", ex); } } ProtocolProviderServiceSipImpl service = getServiceData(event.getServerTransaction()); if (service != null) { service.processRequest(event); } else { service = findTargetFor(request); if (service == null) { logger.error("couldn't find a ProtocolProviderServiceSipImpl " + "to dispatch to"); if (event.getServerTransaction() != null) event.getServerTransaction().terminate(); } else { /* * Mark the dialog for the dispatching of later in-dialog * requests. If there is no dialog, we need to mark the * request to dispatch a possible timeout when sending the * response. */ Object container = event.getDialog(); if (container == null) container = request; SipApplicationData.setApplicationData(container, SipApplicationData.KEY_SERVICE, service); service.processRequest(event); } } } catch (Throwable exc) { /* * Any exception thrown within our code should be caught here so * that we could log it rather than interrupt stack activity with * it. */ this.logApplicationException(DialogTerminatedEvent.class, exc); // Unfortunately, death can hardly be ignored. if (exc instanceof ThreadDeath) throw (ThreadDeath) exc; } }
/** * Attach JAIN-SIP <tt>SipProvider</tt> and <tt>ListeningPoint</tt> to the stack either for clear * communications or TLS. Clear UDP and TCP <tt>ListeningPoint</tt>s are not handled separately as * the former is a fallback for the latter (depending on the size of the data transmitted). Both * <tt>ListeningPoint</tt>s must be bound to the same address and port in order for the related * <tt>SipProvider</tt> to be created. If a UDP or TCP <tt>ListeningPoint</tt> cannot bind, retry * for both on another port. * * @param preferredPort which port to try first to bind. * @param retries how many times should we try to find a free port to bind * @param secure whether to create the TLS SipProvider. or the clear UDP/TCP one. * @throws TransportNotSupportedException in case we try to create a provider for a transport not * currently supported by jain-sip * @throws InvalidArgumentException if we try binding to an illegal port (which we won't) * @throws ObjectInUseException if another <tt>SipProvider</tt> is already associated with this * <tt>ListeningPoint</tt>. * @throws TransportAlreadySupportedException if there is already a ListeningPoint associated to * this <tt>SipProvider</tt> with the same transport of the <tt>ListeningPoint</tt>. * @throws TooManyListenersException if we try to add a new <tt>SipListener</tt> with a * <tt>SipProvider</tt> when one was already registered. */ private void createProvider(int preferredPort, int retries, boolean secure) throws TransportNotSupportedException, InvalidArgumentException, ObjectInUseException, TransportAlreadySupportedException, TooManyListenersException { String context = (secure ? "TLS: " : "clear UDP/TCP: "); if (retries < 0) { // very unlikely to happen with the default 50 retries logger.error(context + "couldn't find free ports to listen on."); return; } ListeningPoint tlsLP = null; ListeningPoint udpLP = null; ListeningPoint tcpLP = null; try { if (secure) { tlsLP = this.stack.createListeningPoint( NetworkUtils.IN_ADDR_ANY, preferredPort, ListeningPoint.TLS); if (logger.isTraceEnabled()) logger.trace("TLS secure ListeningPoint has been created."); this.secureJainSipProvider = this.stack.createSipProvider(tlsLP); this.secureJainSipProvider.addSipListener(this); } else { udpLP = this.stack.createListeningPoint( NetworkUtils.IN_ADDR_ANY, preferredPort, ListeningPoint.UDP); tcpLP = this.stack.createListeningPoint( NetworkUtils.IN_ADDR_ANY, preferredPort, ListeningPoint.TCP); if (logger.isTraceEnabled()) logger.trace("UDP and TCP clear ListeningPoints have " + "been created."); this.clearJainSipProvider = this.stack.createSipProvider(udpLP); this.clearJainSipProvider.addListeningPoint(tcpLP); this.clearJainSipProvider.addSipListener(this); } if (logger.isTraceEnabled()) logger.trace(context + "SipProvider has been created."); } catch (InvalidArgumentException ex) { // makes sure we didn't leave an open listener // as both UDP and TCP listener have to bind to the same port if (tlsLP != null) this.stack.deleteListeningPoint(tlsLP); if (udpLP != null) this.stack.deleteListeningPoint(udpLP); if (tcpLP != null) this.stack.deleteListeningPoint(tcpLP); // FIXME: "Address already in use" is not working // as ex.getMessage() displays in the locale language in SC // (getMessage() is always supposed to be English though) // this should be a temporary workaround // if (ex.getMessage().indexOf("Address already in use") != -1) // another software is probably using the port if (ex.getCause() instanceof java.io.IOException) { if (logger.isDebugEnabled()) logger.debug("Port " + preferredPort + " seems in use for either TCP or UDP."); // tries again on a new random port int currentlyTriedPort = NetworkUtils.getRandomPortNumber(); if (logger.isDebugEnabled()) logger.debug("Retrying bind on port " + currentlyTriedPort); this.createProvider(currentlyTriedPort, retries - 1, secure); } else throw ex; } }
/** * This class is the <tt>SipListener</tt> for all JAIN-SIP <tt>SipProvider</tt>s. It is in charge of * dispatching the received messages to the suitable <tt>ProtocolProviderServiceSipImpl</tt>s * registered with <tt>addSipListener</tt>. It also contains the JAIN-SIP pieces which are common * between all <tt>ProtocolProviderServiceSipImpl</tt>s (namely 1 <tt>SipStack</tt>, 2 * <tt>SipProvider</tt>s, 3 <tt>ListeningPoint</tt>s). * * @author Emil Ivov * @author Lubomir Marinov * @author Alan Kelly * @author Sebastien Mazy */ public class SipStackSharing implements SipListener, NetworkConfigurationChangeListener { /** * We set a custom parameter in the contact address for registrar accounts, so as to ease * dispatching of incoming requests in case several accounts have the same username in their * contact address, eg: sip:[email protected]:5060;transport=udp;registering_acc=example_com */ public static final String CONTACT_ADDRESS_CUSTOM_PARAM_NAME = "registering_acc"; /** Logger for this class. */ private static final Logger logger = Logger.getLogger(SipStackSharing.class); /** Our SIP stack (provided by JAIN-SIP). */ private final SipStack stack; /** The JAIN-SIP provider that we use for clear UDP/TCP. */ private SipProvider clearJainSipProvider = null; /** The JAIN-SIP provider that we use for TLS. */ private SipProvider secureJainSipProvider = null; /** * The candidate recipients to choose from when dispatching messages received from one the * JAIN-SIP <tt>SipProvider</tt>-s. for thread safety issues reasons, better iterate on a copy of * that set using <tt>getSipListeners()</tt>. */ private final Set<ProtocolProviderServiceSipImpl> listeners = new HashSet<ProtocolProviderServiceSipImpl>(); /** The property indicating the preferred UDP and TCP port to bind to for clear communications. */ private static final String PREFERRED_CLEAR_PORT_PROPERTY_NAME = "net.java.sip.communicator.SIP_PREFERRED_CLEAR_PORT"; /** The property indicating the preferred TLS (TCP) port to bind to for secure communications. */ private static final String PREFERRED_SECURE_PORT_PROPERTY_NAME = "net.java.sip.communicator.SIP_PREFERRED_SECURE_PORT"; /** * Constructor for this class. Creates the JAIN-SIP stack. * * @throws OperationFailedException if creating the stack fails. */ SipStackSharing() throws OperationFailedException { // init of the stack try { SipFactory sipFactory = SipFactory.getInstance(); sipFactory.setPathName("org.jitsi.gov.nist"); Properties sipStackProperties = new SipStackProperties(); // Create SipStack object this.stack = sipFactory.createSipStack(sipStackProperties); if (logger.isTraceEnabled()) logger.trace("Created stack: " + this.stack); // set our custom address resolver managing SRV records AddressResolverImpl addressResolver = new AddressResolverImpl(); ((SIPTransactionStack) this.stack).setAddressResolver(addressResolver); SipActivator.getNetworkAddressManagerService().addNetworkConfigurationChangeListener(this); } catch (Exception ex) { logger.fatal("Failed to get SIP Factory.", ex); throw new OperationFailedException( "Failed to get SIP Factory", OperationFailedException.INTERNAL_ERROR, ex); } } /** * Adds this <tt>listener</tt> as a candidate recipient for the dispatching of new messages * received from the JAIN-SIP <tt>SipProvider</tt>s. * * @param listener a new possible target for the dispatching process. * @throws OperationFailedException if creating one of the underlying <tt>SipProvider</tt>s fails * for whatever reason. */ public void addSipListener(ProtocolProviderServiceSipImpl listener) throws OperationFailedException { synchronized (this.listeners) { if (this.listeners.size() == 0) startListening(); this.listeners.add(listener); if (logger.isTraceEnabled()) logger.trace(this.listeners.size() + " listeners now"); } } /** * This <tt>listener</tt> will no longer be a candidate recipient for the dispatching of new * messages received from the JAIN-SIP <tt>SipProvider</tt>s. * * @param listener possible target to remove for the dispatching process. */ public void removeSipListener(ProtocolProviderServiceSipImpl listener) { synchronized (this.listeners) { this.listeners.remove(listener); int listenerCount = listeners.size(); if (logger.isTraceEnabled()) logger.trace(listenerCount + " listeners left"); if (listenerCount == 0) stopListening(); } } /** * Returns a copy of the <tt>listeners</tt> (= candidate recipients) set. * * @return a copy of the <tt>listeners</tt> set. */ private Set<ProtocolProviderServiceSipImpl> getSipListeners() { synchronized (this.listeners) { return new HashSet<ProtocolProviderServiceSipImpl>(this.listeners); } } /** * Returns the JAIN-SIP <tt>ListeningPoint</tt> associated to the given transport string. * * @param transport a string like "UDP", "TCP" or "TLS". * @return the LP associated to the given transport. */ @SuppressWarnings("unchecked") // jain-sip legacy code public ListeningPoint getLP(String transport) { ListeningPoint lp; Iterator<ListeningPoint> it = this.stack.getListeningPoints(); while (it.hasNext()) { lp = it.next(); // FIXME: JAIN-SIP stack is not consistent with case // (reported upstream) if (lp.getTransport().toLowerCase().equals(transport.toLowerCase())) return lp; } throw new IllegalArgumentException("Invalid transport: " + transport); } /** * Put the stack in a state where it can receive data on three UDP/TCP ports (2 for clear * communication, 1 for TLS). That is to say create the related JAIN-SIP <tt>ListeningPoint</tt>s * and <tt>SipProvider</tt>s. * * @throws OperationFailedException if creating one of the underlying <tt>SipProvider</tt>s fails * for whatever reason. */ private void startListening() throws OperationFailedException { try { int bindRetriesValue = getBindRetriesValue(); this.createProvider(this.getPreferredClearPort(), bindRetriesValue, false); this.createProvider(this.getPreferredSecurePort(), bindRetriesValue, true); this.stack.start(); if (logger.isTraceEnabled()) logger.trace("started listening"); } catch (Exception ex) { logger.error( "An unexpected error happened while creating the" + "SipProviders and ListeningPoints."); throw new OperationFailedException( "An unexpected error hapenned" + "while initializing the SIP stack", OperationFailedException.INTERNAL_ERROR, ex); } } /** * Attach JAIN-SIP <tt>SipProvider</tt> and <tt>ListeningPoint</tt> to the stack either for clear * communications or TLS. Clear UDP and TCP <tt>ListeningPoint</tt>s are not handled separately as * the former is a fallback for the latter (depending on the size of the data transmitted). Both * <tt>ListeningPoint</tt>s must be bound to the same address and port in order for the related * <tt>SipProvider</tt> to be created. If a UDP or TCP <tt>ListeningPoint</tt> cannot bind, retry * for both on another port. * * @param preferredPort which port to try first to bind. * @param retries how many times should we try to find a free port to bind * @param secure whether to create the TLS SipProvider. or the clear UDP/TCP one. * @throws TransportNotSupportedException in case we try to create a provider for a transport not * currently supported by jain-sip * @throws InvalidArgumentException if we try binding to an illegal port (which we won't) * @throws ObjectInUseException if another <tt>SipProvider</tt> is already associated with this * <tt>ListeningPoint</tt>. * @throws TransportAlreadySupportedException if there is already a ListeningPoint associated to * this <tt>SipProvider</tt> with the same transport of the <tt>ListeningPoint</tt>. * @throws TooManyListenersException if we try to add a new <tt>SipListener</tt> with a * <tt>SipProvider</tt> when one was already registered. */ private void createProvider(int preferredPort, int retries, boolean secure) throws TransportNotSupportedException, InvalidArgumentException, ObjectInUseException, TransportAlreadySupportedException, TooManyListenersException { String context = (secure ? "TLS: " : "clear UDP/TCP: "); if (retries < 0) { // very unlikely to happen with the default 50 retries logger.error(context + "couldn't find free ports to listen on."); return; } ListeningPoint tlsLP = null; ListeningPoint udpLP = null; ListeningPoint tcpLP = null; try { if (secure) { tlsLP = this.stack.createListeningPoint( NetworkUtils.IN_ADDR_ANY, preferredPort, ListeningPoint.TLS); if (logger.isTraceEnabled()) logger.trace("TLS secure ListeningPoint has been created."); this.secureJainSipProvider = this.stack.createSipProvider(tlsLP); this.secureJainSipProvider.addSipListener(this); } else { udpLP = this.stack.createListeningPoint( NetworkUtils.IN_ADDR_ANY, preferredPort, ListeningPoint.UDP); tcpLP = this.stack.createListeningPoint( NetworkUtils.IN_ADDR_ANY, preferredPort, ListeningPoint.TCP); if (logger.isTraceEnabled()) logger.trace("UDP and TCP clear ListeningPoints have " + "been created."); this.clearJainSipProvider = this.stack.createSipProvider(udpLP); this.clearJainSipProvider.addListeningPoint(tcpLP); this.clearJainSipProvider.addSipListener(this); } if (logger.isTraceEnabled()) logger.trace(context + "SipProvider has been created."); } catch (InvalidArgumentException ex) { // makes sure we didn't leave an open listener // as both UDP and TCP listener have to bind to the same port if (tlsLP != null) this.stack.deleteListeningPoint(tlsLP); if (udpLP != null) this.stack.deleteListeningPoint(udpLP); if (tcpLP != null) this.stack.deleteListeningPoint(tcpLP); // FIXME: "Address already in use" is not working // as ex.getMessage() displays in the locale language in SC // (getMessage() is always supposed to be English though) // this should be a temporary workaround // if (ex.getMessage().indexOf("Address already in use") != -1) // another software is probably using the port if (ex.getCause() instanceof java.io.IOException) { if (logger.isDebugEnabled()) logger.debug("Port " + preferredPort + " seems in use for either TCP or UDP."); // tries again on a new random port int currentlyTriedPort = NetworkUtils.getRandomPortNumber(); if (logger.isDebugEnabled()) logger.debug("Retrying bind on port " + currentlyTriedPort); this.createProvider(currentlyTriedPort, retries - 1, secure); } else throw ex; } } /** * Put the JAIN-SIP stack in a state where it cannot receive any data and frees the network ports * used. That is to say remove JAIN-SIP <tt>ListeningPoint</tt>s and <tt>SipProvider</tt>s. */ @SuppressWarnings("unchecked") // jain-sip legacy code private void stopListening() { try { this.secureJainSipProvider.removeSipListener(this); this.stack.deleteSipProvider(this.secureJainSipProvider); this.secureJainSipProvider = null; this.clearJainSipProvider.removeSipListener(this); this.stack.deleteSipProvider(this.clearJainSipProvider); this.clearJainSipProvider = null; Iterator<ListeningPoint> it = this.stack.getListeningPoints(); Vector<ListeningPoint> lpointsToRemove = new Vector<ListeningPoint>(); while (it.hasNext()) { lpointsToRemove.add(it.next()); } it = lpointsToRemove.iterator(); while (it.hasNext()) { this.stack.deleteListeningPoint(it.next()); } this.stack.stop(); if (logger.isTraceEnabled()) logger.trace("stopped listening"); } catch (ObjectInUseException ex) { logger.fatal("Failed to stop listening", ex); } } /** * Returns the JAIN-SIP <tt>SipProvider</tt> in charge of this <tt>transport</tt>. * * @param transport a <tt>String</tt> like "TCP", "UDP" or "TLS" * @return the corresponding <tt>SipProvider</tt> */ public SipProvider getJainSipProvider(String transport) { SipProvider sp = null; if (transport.equalsIgnoreCase(ListeningPoint.UDP) || transport.equalsIgnoreCase(ListeningPoint.TCP)) sp = this.clearJainSipProvider; else if (transport.equalsIgnoreCase(ListeningPoint.TLS)) sp = this.secureJainSipProvider; if (sp == null) throw new IllegalArgumentException("invalid transport"); return sp; } /** * Fetches the preferred UDP and TCP port for clear communications in the user preferences or * search is default value set in settings or fallback on a default value. * * @return the preferred network port for clear communications. */ private int getPreferredClearPort() { int preferredPort = SipActivator.getConfigurationService().getInt(PREFERRED_CLEAR_PORT_PROPERTY_NAME, -1); if (preferredPort <= 1) { // check for default value preferredPort = SipActivator.getResources().getSettingsInt(PREFERRED_CLEAR_PORT_PROPERTY_NAME); } if (preferredPort <= 1) return ListeningPoint.PORT_5060; else return preferredPort; } /** * Fetches the preferred TLS (TCP) port for secure communications in the user preferences or * search is default value set in settings or fallback on a default value. * * @return the preferred network port for secure communications. */ private int getPreferredSecurePort() { int preferredPort = SipActivator.getConfigurationService().getInt(PREFERRED_SECURE_PORT_PROPERTY_NAME, -1); if (preferredPort <= 1) { // check for default value preferredPort = SipActivator.getResources().getSettingsInt(PREFERRED_SECURE_PORT_PROPERTY_NAME); } if (preferredPort <= 1) return ListeningPoint.PORT_5061; else return preferredPort; } /** * Fetches the number of times to retry when the binding of a JAIN-SIP <tt>ListeningPoint</tt> * fails. Looks in the user preferences or fallbacks on a default value. * * @return the number of times to retry a failed bind. */ private int getBindRetriesValue() { return SipActivator.getConfigurationService() .getInt( ProtocolProviderService.BIND_RETRIES_PROPERTY_NAME, ProtocolProviderService.BIND_RETRIES_DEFAULT_VALUE); } /** * Dispatches the event received from a JAIN-SIP <tt>SipProvider</tt> to one of our "candidate * recipient" listeners. * * @param event the event received for a <tt>SipProvider</tt>. */ public void processDialogTerminated(DialogTerminatedEvent event) { try { ProtocolProviderServiceSipImpl recipient = (ProtocolProviderServiceSipImpl) SipApplicationData.getApplicationData( event.getDialog(), SipApplicationData.KEY_SERVICE); if (recipient == null) { logger.error( "Dialog wasn't marked, please report this to " + "*****@*****.**"); } else { if (logger.isTraceEnabled()) logger.trace("service was found with dialog data"); recipient.processDialogTerminated(event); } } catch (Throwable exc) { // any exception thrown within our code should be caught here // so that we could log it rather than interrupt stack activity with // it. this.logApplicationException(DialogTerminatedEvent.class, exc); } } /** * Dispatches the event received from a JAIN-SIP <tt>SipProvider</tt> to one of our "candidate * recipient" listeners. * * @param event the event received for a <tt>SipProvider</tt>. */ public void processIOException(IOExceptionEvent event) { try { if (logger.isTraceEnabled()) logger.trace(event); // impossible to dispatch, log here if (logger.isDebugEnabled()) logger.debug("@todo implement processIOException()"); } catch (Throwable exc) { // any exception thrown within our code should be caught here // so that we could log it rather than interrupt stack activity with // it. this.logApplicationException(DialogTerminatedEvent.class, exc); } } /** * Dispatches the event received from a JAIN-SIP <tt>SipProvider</tt> to one of our "candidate * recipient" listeners. * * @param event the event received for a <tt>SipProvider</tt>. */ public void processRequest(RequestEvent event) { try { Request request = event.getRequest(); if (logger.isTraceEnabled()) logger.trace("received request: " + request.getMethod()); /* * Create the transaction if it doesn't exist yet. If it is a * dialog-creating request, the dialog will also be automatically * created by the stack. */ if (event.getServerTransaction() == null) { try { // apply some hacks if needed on incoming request // to be compliant with some servers/clients // if needed stop further processing. if (applyNonConformanceHacks(event)) return; SipProvider source = (SipProvider) event.getSource(); ServerTransaction transaction = source.getNewServerTransaction(request); /* * Update the event, otherwise getServerTransaction() and * getDialog() will still return their previous value. */ event = new RequestEvent(source, transaction, transaction.getDialog(), request); } catch (SipException ex) { logger.error( "couldn't create transaction, please report " + "this to [email protected]", ex); } } ProtocolProviderServiceSipImpl service = getServiceData(event.getServerTransaction()); if (service != null) { service.processRequest(event); } else { service = findTargetFor(request); if (service == null) { logger.error("couldn't find a ProtocolProviderServiceSipImpl " + "to dispatch to"); if (event.getServerTransaction() != null) event.getServerTransaction().terminate(); } else { /* * Mark the dialog for the dispatching of later in-dialog * requests. If there is no dialog, we need to mark the * request to dispatch a possible timeout when sending the * response. */ Object container = event.getDialog(); if (container == null) container = request; SipApplicationData.setApplicationData(container, SipApplicationData.KEY_SERVICE, service); service.processRequest(event); } } } catch (Throwable exc) { /* * Any exception thrown within our code should be caught here so * that we could log it rather than interrupt stack activity with * it. */ this.logApplicationException(DialogTerminatedEvent.class, exc); // Unfortunately, death can hardly be ignored. if (exc instanceof ThreadDeath) throw (ThreadDeath) exc; } } /** * Dispatches the event received from a JAIN-SIP <tt>SipProvider</tt> to one of our "candidate * recipient" listeners. * * @param event the event received for a <tt>SipProvider</tt>. */ public void processResponse(ResponseEvent event) { try { // we don't have to accept the transaction since we // created the request ClientTransaction transaction = event.getClientTransaction(); if (logger.isTraceEnabled()) logger.trace( "received response: " + event.getResponse().getStatusCode() + " " + event.getResponse().getReasonPhrase()); if (transaction == null) { logger.warn("Transaction is null, probably already expired!"); return; } ProtocolProviderServiceSipImpl service = getServiceData(transaction); if (service != null) { // Mark the dialog for the dispatching of later in-dialog // responses. If there is no dialog then the initial request // sure is marked otherwise we won't have found the service with // getServiceData(). The request has to be marked in case we // receive one more response in an out-of-dialog transaction. if (event.getDialog() != null) { SipApplicationData.setApplicationData( event.getDialog(), SipApplicationData.KEY_SERVICE, service); } service.processResponse(event); } else { logger.error( "We received a response which " + "wasn't marked, please report this to " + "*****@*****.**"); } } catch (Throwable exc) { // any exception thrown within our code should be caught here // so that we could log it rather than interrupt stack activity with // it. this.logApplicationException(DialogTerminatedEvent.class, exc); } } /** * Dispatches the event received from a JAIN-SIP <tt>SipProvider</tt> to one of our "candidate * recipient" listeners. * * @param event the event received for a <tt>SipProvider</tt>. */ public void processTimeout(TimeoutEvent event) { try { Transaction transaction; if (event.isServerTransaction()) { transaction = event.getServerTransaction(); } else { transaction = event.getClientTransaction(); } ProtocolProviderServiceSipImpl recipient = getServiceData(transaction); if (recipient == null) { logger.error( "We received a timeout which wasn't " + "marked, please report this to " + "*****@*****.**"); } else { recipient.processTimeout(event); } } catch (Throwable exc) { // any exception thrown within our code should be caught here // so that we could log it rather than interrupt stack activity with // it. this.logApplicationException(DialogTerminatedEvent.class, exc); } } /** * Dispatches the event received from a JAIN-SIP <tt>SipProvider</tt> to one of our "candidate * recipient" listeners. * * @param event the event received for a <tt>SipProvider</tt>. */ public void processTransactionTerminated(TransactionTerminatedEvent event) { try { Transaction transaction; if (event.isServerTransaction()) transaction = event.getServerTransaction(); else transaction = event.getClientTransaction(); ProtocolProviderServiceSipImpl recipient = getServiceData(transaction); if (recipient == null) { logger.error( "We received a transaction terminated which wasn't" + " marked, please report this to" + " [email protected]"); } else { recipient.processTransactionTerminated(event); } } catch (Throwable exc) { // any exception thrown within our code should be caught here // so that we could log it rather than interrupt stack activity with // it. this.logApplicationException(DialogTerminatedEvent.class, exc); } } /** * Find the <tt>ProtocolProviderServiceSipImpl</tt> (one of our "candidate recipient" listeners) * which this <tt>request</tt> should be dispatched to. The strategy is to look first at the * request URI, and then at the To field to find a matching candidate for dispatching. Note that * this method takes a <tt>Request</tt> as param, and not a <tt>ServerTransaction</tt>, because * sometimes <tt>RequestEvent</tt>s have no associated <tt>ServerTransaction</tt>. * * @param request the <tt>Request</tt> to find a recipient for. * @return a suitable <tt>ProtocolProviderServiceSipImpl</tt>. */ private ProtocolProviderServiceSipImpl findTargetFor(Request request) { if (request == null) { logger.error("request shouldn't be null."); return null; } List<ProtocolProviderServiceSipImpl> currentListenersCopy = new ArrayList<ProtocolProviderServiceSipImpl>(this.getSipListeners()); // Let's first narrow down candidate choice by comparing // addresses and ports (no point in delivering to a provider with a // non matching IP address since they will reject it anyway). filterByAddress(currentListenersCopy, request); if (currentListenersCopy.size() == 0) { logger.error("no listeners"); return null; } URI requestURI = request.getRequestURI(); if (requestURI.isSipURI()) { String requestUser = ((SipURI) requestURI).getUser(); List<ProtocolProviderServiceSipImpl> candidates = new ArrayList<ProtocolProviderServiceSipImpl>(); // check if the Request-URI username is // one of ours usernames for (ProtocolProviderServiceSipImpl listener : currentListenersCopy) { String ourUserID = listener.getAccountID().getUserID(); // logger.trace(ourUserID + " *** " + requestUser); if (ourUserID.equals(requestUser)) { if (logger.isTraceEnabled()) logger.trace("suitable candidate found: " + listener.getAccountID()); candidates.add(listener); } } // the perfect match // every other case is approximation if (candidates.size() == 1) { ProtocolProviderServiceSipImpl perfectMatch = candidates.get(0); if (logger.isTraceEnabled()) logger.trace("Will dispatch to \"" + perfectMatch.getAccountID() + "\""); return perfectMatch; } // more than one account match if (candidates.size() > 1) { // check if a custom param exists in the contact // address (set for registrar accounts) for (ProtocolProviderServiceSipImpl candidate : candidates) { String hostValue = ((SipURI) requestURI).getParameter(SipStackSharing.CONTACT_ADDRESS_CUSTOM_PARAM_NAME); if (hostValue == null) continue; if (hostValue.equals(candidate.getContactAddressCustomParamValue())) { if (logger.isTraceEnabled()) logger.trace( "Will dispatch to \"" + candidate.getAccountID() + "\" because " + "\" the custom param was set"); return candidate; } } // Past this point, our guess is not reliable. We try to find // the "least worst" match based on parameters like the To field // check if the To header field host part // matches any of our SIP hosts for (ProtocolProviderServiceSipImpl candidate : candidates) { URI fromURI = ((FromHeader) request.getHeader(FromHeader.NAME)).getAddress().getURI(); if (fromURI.isSipURI() == false) continue; SipURI ourURI = (SipURI) candidate.getOurSipAddress((SipURI) fromURI).getURI(); String ourHost = ourURI.getHost(); URI toURI = ((ToHeader) request.getHeader(ToHeader.NAME)).getAddress().getURI(); if (toURI.isSipURI() == false) continue; String toHost = ((SipURI) toURI).getHost(); // logger.trace(toHost + "***" + ourHost); if (toHost.equals(ourHost)) { if (logger.isTraceEnabled()) logger.trace( "Will dispatch to \"" + candidate.getAccountID() + "\" because " + "host in the To: is the same as in our AOR"); return candidate; } } // fallback on the first candidate ProtocolProviderServiceSipImpl target = candidates.iterator().next(); logger.info( "Will randomly dispatch to \"" + target.getAccountID() + "\" because there is ambiguity on the username from" + " the Request-URI"); if (logger.isTraceEnabled()) logger.trace("\n" + request); return target; } // fallback on any account ProtocolProviderServiceSipImpl target = currentListenersCopy.iterator().next(); if (logger.isDebugEnabled()) logger.debug( "Will randomly dispatch to \"" + target.getAccountID() + "\" because the username in the Request-URI " + "is unknown or empty"); if (logger.isTraceEnabled()) logger.trace("\n" + request); return target; } else { logger.error("Request-URI is not a SIP URI, dropping"); } return null; } /** * Removes from the specified list of candidates providers connected to a registrar that does not * match the IP address that we are receiving a request from. * * @param candidates the list of providers we've like to filter. * @param request the request that we are currently dispatching */ private void filterByAddress(List<ProtocolProviderServiceSipImpl> candidates, Request request) { Iterator<ProtocolProviderServiceSipImpl> iterPP = candidates.iterator(); while (iterPP.hasNext()) { ProtocolProviderServiceSipImpl candidate = iterPP.next(); if (candidate.getRegistrarConnection() == null) { // RegistrarLess connections are ok continue; } if (!candidate.getRegistrarConnection().isRegistrarless() && !candidate.getRegistrarConnection().isRequestFromSameConnection(request)) { iterPP.remove(); } } } /** * Retrieves and returns that ProtocolProviderService that this transaction belongs to, or * <tt>null</tt> if we couldn't associate it with a provider based on neither the request nor the * transaction itself. * * @param transaction the transaction that we'd like to determine a provider for. * @return a reference to the <tt>ProtocolProviderServiceSipImpl</tt> that <tt>transaction</tt> * was associated with or <tt>null</tt> if we couldn't determine which one it is. */ private ProtocolProviderServiceSipImpl getServiceData(Transaction transaction) { ProtocolProviderServiceSipImpl service = (ProtocolProviderServiceSipImpl) SipApplicationData.getApplicationData( transaction.getRequest(), SipApplicationData.KEY_SERVICE); if (service != null) { if (logger.isTraceEnabled()) logger.trace("service was found in request data"); return service; } service = (ProtocolProviderServiceSipImpl) SipApplicationData.getApplicationData( transaction.getDialog(), SipApplicationData.KEY_SERVICE); if (service != null) { if (logger.isTraceEnabled()) logger.trace("service was found in dialog data"); } return service; } /** * Logs exceptions that have occurred in the application while processing events originating from * the stack. * * @param eventClass the class of the jain-sip event that we were handling when the exception was * thrown. * @param exc the exception that we need to log. */ private void logApplicationException(Class<DialogTerminatedEvent> eventClass, Throwable exc) { String message = "An error occurred while processing event of type: " + eventClass.getName(); logger.error(message, exc); if (logger.isDebugEnabled()) logger.debug(message, exc); } /** * Safely returns the transaction from the event if already exists. If not a new transaction is * created. * * @param event the request event * @return the server transaction * @throws javax.sip.TransactionAlreadyExistsException if transaction exists * @throws javax.sip.TransactionUnavailableException if unavailable */ public static ServerTransaction getOrCreateServerTransaction(RequestEvent event) throws TransactionAlreadyExistsException, TransactionUnavailableException { ServerTransaction serverTransaction = event.getServerTransaction(); if (serverTransaction == null) { SipProvider jainSipProvider = (SipProvider) event.getSource(); serverTransaction = jainSipProvider.getNewServerTransaction(event.getRequest()); } return serverTransaction; } /** * Returns a local address to use with the specified TCP destination. The method forces the * JAIN-SIP stack to create s and binds (if necessary) and return a socket connected to the * specified destination address and port and then return its local address. * * @param dst the destination address that the socket would need to connect to. * @param dstPort the port number that the connection would be established with. * @param localAddress the address that we would like to bind on (null for the "any" address). * @param transport the transport that will be used TCP ot TLS * @return the SocketAddress that this handler would use when connecting to the specified * destination address and port. * @throws IOException if we fail binding the local socket */ public java.net.InetSocketAddress getLocalAddressForDestination( java.net.InetAddress dst, int dstPort, java.net.InetAddress localAddress, String transport) throws IOException { if (ListeningPoint.TLS.equalsIgnoreCase(transport)) return (java.net.InetSocketAddress) (((SipStackImpl) this.stack).getLocalAddressForTlsDst(dst, dstPort, localAddress)); else return (java.net.InetSocketAddress) (((SipStackImpl) this.stack).getLocalAddressForTcpDst(dst, dstPort, localAddress, 0)); } /** * Place to put some hacks if needed on incoming requests. * * @param event the incoming request event. * @return status <code>true</code> if we don't need to process this message, just discard it and * <code>false</code> otherwise. */ private boolean applyNonConformanceHacks(RequestEvent event) { Request request = event.getRequest(); try { /* * Max-Forwards is required, yet there are UAs which do not * place it. SipProvider#getNewServerTransaction(Request) * will throw an exception in the case of a missing * Max-Forwards header and this method will eventually just * log it thus ignoring the whole event. */ if (request.getHeader(MaxForwardsHeader.NAME) == null) { // it appears that some buggy providers do send requests // with no Max-Forwards headers, as we are at application level // and we know there will be no endless loops // there is no problem of adding headers and process normally // this messages MaxForwardsHeader maxForwards = SipFactory.getInstance().createHeaderFactory().createMaxForwardsHeader(70); request.setHeader(maxForwards); } } catch (Throwable ex) { logger.warn("Cannot apply incoming request modification!", ex); } try { // using asterisk voice mail initial notify for messages // is ok, but on the fly received messages their notify comes // without subscription-state, so we add it in order to be able to // process message. if (request.getMethod().equals(Request.NOTIFY) && request.getHeader(EventHeader.NAME) != null && ((EventHeader) request.getHeader(EventHeader.NAME)) .getEventType() .equals(OperationSetMessageWaitingSipImpl.EVENT_PACKAGE) && request.getHeader(SubscriptionStateHeader.NAME) == null) { request.addHeader( new HeaderFactoryImpl().createSubscriptionStateHeader(SubscriptionStateHeader.ACTIVE)); } } catch (Throwable ex) { logger.warn("Cannot apply incoming request modification!", ex); } try { // receiving notify message without subscription state // used for keep-alive pings, they have done their job // and are no more need. Skip processing them to avoid // filling logs with unneeded exceptions. if (request.getMethod().equals(Request.NOTIFY) && request.getHeader(SubscriptionStateHeader.NAME) == null) { return true; } } catch (Throwable ex) { logger.warn("Cannot apply incoming request modification!", ex); } return false; } /** List of currently waiting timers that will monitor the protocol provider */ Map<String, TimerTask> resetListeningPointsTimers = new HashMap<String, TimerTask>(); /** * Listens for network changes and if we have a down interface and we have a tcp/tls provider * which is staying for 20 seconds in unregistering state, it cannot unregister cause its using * the old address which is currently down, and we must recreate its listening points so it can * further reconnect. * * @param event the change event. */ public void configurationChanged(ChangeEvent event) { if (event.isInitial()) return; if (event.getType() == ChangeEvent.ADDRESS_DOWN) { for (final ProtocolProviderServiceSipImpl pp : listeners) { if (pp.getRegistrarConnection().getTransport() != null && (pp.getRegistrarConnection().getTransport().equals(ListeningPoint.TCP) || pp.getRegistrarConnection().getTransport().equals(ListeningPoint.TLS))) { ResetListeningPoint reseter; synchronized (resetListeningPointsTimers) { // we do this only once for transport if (resetListeningPointsTimers.containsKey(pp.getRegistrarConnection().getTransport())) continue; reseter = new ResetListeningPoint(pp); resetListeningPointsTimers.put(pp.getRegistrarConnection().getTransport(), reseter); } pp.addRegistrationStateChangeListener(reseter); } } } } /** * If a tcp(tls) provider stays unregistering for a long time after connection changed most * probably it won't get registered after unregistering fails, cause underlying listening point * are conncted to wrong interfaces. So we will replace them. */ private class ResetListeningPoint extends TimerTask implements RegistrationStateChangeListener { /** The time we wait before checking is the provider still unregistering. */ private static final int TIME_FOR_PP_TO_UNREGISTER = 20000; /** The protocol provider we are checking. */ private final ProtocolProviderServiceSipImpl protocolProvider; /** * Constructs this task. * * @param pp */ ResetListeningPoint(ProtocolProviderServiceSipImpl pp) { this.protocolProvider = pp; } /** * Notified when registration state changed for a provider. * * @param evt */ public void registrationStateChanged(RegistrationStateChangeEvent evt) { if (evt.getNewState() == RegistrationState.UNREGISTERING) { new Timer().schedule(this, TIME_FOR_PP_TO_UNREGISTER); } else { protocolProvider.removeRegistrationStateChangeListener(this); resetListeningPointsTimers.remove(protocolProvider.getRegistrarConnection().getTransport()); } } /** The real task work, replace listening point. */ public void run() { // if the provider is still unregistering it most probably won't // successes until we re-init the LP if (protocolProvider.getRegistrationState() == RegistrationState.UNREGISTERING) { String transport = protocolProvider.getRegistrarConnection().getTransport(); ListeningPoint old = getLP(transport); try { stack.deleteListeningPoint(old); } catch (Throwable t) { logger.warn("Error replacing ListeningPoint for " + transport, t); } try { ListeningPoint tcpLP = stack.createListeningPoint( NetworkUtils.IN_ADDR_ANY, transport.equals(ListeningPoint.TCP) ? getPreferredClearPort() : getPreferredSecurePort(), transport); clearJainSipProvider.addListeningPoint(tcpLP); } catch (Throwable t) { logger.warn( "Error replacing ListeningPoint for " + protocolProvider.getRegistrarConnection().getTransport(), t); } } resetListeningPointsTimers.remove(protocolProvider.getRegistrarConnection().getTransport()); } } }