SIPServerTransaction sipServerTransaction = sipStack
.findMergedTransaction(sipRequest);
if (sipServerTransaction != null
&& !sipServerTransaction.isMessagePartOfTransaction(sipRequest) &&
sipServerTransaction.getState() != TransactionState.TERMINATED ) {
SIPResponse response = sipRequest.createResponse(Response.LOOP_DETECTED);
if (sipStack.getLogWriter().isLoggingEnabled())
sipStack.getLogWriter().logError("Loop detected while processing request");
try {
sipProvider.sendResponse(response);
} catch (SipException e) {
if (sipStack.getLogWriter().isLoggingEnabled())
sipStack.getLogWriter().logError("Error sending response");
}
return;
}
}
if (sipStack.isLoggingEnabled()) {
sipStack.getLogWriter().logDebug("dialogId = " + dialogId);
sipStack.getLogWriter().logDebug("dialog = " + dialog);
}
/*
* RFC 3261 Section 16.4 If the first value in the Route header field indicates this
* proxy,the proxy MUST remove that value from the request .
*/
// If the message is being processed
// by a Proxy, then the proxy will take care of stripping the
// Route header. If the request is being processed by an
// endpoint, then the stack strips off the route header.
if (sipRequest.getHeader(Route.NAME) != null && transaction.getDialog() != null) {
RouteList routes = sipRequest.getRouteHeaders();
Route route = (Route) routes.getFirst();
SipUri uri = (SipUri) route.getAddress().getURI();
int port;
if (uri.getHostPort().hasPort()) {
port = uri.getHostPort().getPort();
} else {
if (listeningPoint.getTransport().equalsIgnoreCase("TLS"))
port = 5061;
else
port = 5060;
}
String host = uri.getHost();
if ((host.equals(listeningPoint.getIPAddress()) || host
.equalsIgnoreCase(listeningPoint.getSentBy()))
&& port == listeningPoint.getPort()) {
if (routes.size() == 1)
sipRequest.removeHeader(Route.NAME);
else
routes.removeFirst();
}
}
if (sipRequest.getMethod().equals(Request.REFER)) {
/*
* An agent responding to a REFER method MUST return a 400 (Bad Request) if the
* request contained zero or more than one Refer-To header field values.
*/
ReferToHeader sipHeader = (ReferToHeader) sipRequest.getHeader(ReferTo.NAME);
if (sipHeader == null && dialog != null) {
SIPResponse badRequest = sipRequest.createResponse(Response.BAD_REQUEST);
badRequest.setReasonPhrase("Refer-To header missing");
try {
sipProvider.sendResponse(badRequest);
} catch (SipException e) {
sipStack.getLogWriter().logError("error sending response", e);
}
if (transaction != null) {
sipStack.removeTransaction(transaction);
transaction.releaseSem();
}
return;
}
} else if (sipRequest.getMethod().equals(Request.UPDATE)) {
/*
* Got an UPDATE method and the user dialog does not exist and the user wants to be a
* User agent.
*
*/
if (sipProvider.isAutomaticDialogSupportEnabled() && dialog == null) {
Response notExist = sipRequest
.createResponse(Response.CALL_OR_TRANSACTION_DOES_NOT_EXIST);
try {
sipProvider.sendResponse(notExist);
} catch (SipException e) {
sipStack.getLogWriter().logError("error sending response", e);
}
if (transaction != null) {
sipStack.removeTransaction(transaction);
transaction.releaseSem();
}
return;
}
} else if (sipRequest.getMethod().equals(Request.ACK)) {
if (transaction != null && transaction.isInviteTransaction()) {
// This is an ack for a 3xx-6xx response. Just let the tx laer
// take care of it.
if (sipStack.isLoggingEnabled())
sipStack.getLogWriter().logDebug("Processing ACK for INVITE Tx ");
} else {
if (sipStack.isLoggingEnabled())
sipStack.getLogWriter().logDebug("Processing ACK for dialog " + dialog);
if (dialog == null) {
if (sipStack.isLoggingEnabled()) {
sipStack.getLogWriter().logDebug(
"Dialog does not exist " + sipRequest.getFirstLine()
+ " isServerTransaction = " + true);
}
SIPServerTransaction st = sipStack
.getRetransmissionAlertTransaction(dialogId);
if (st != null && st.isRetransmissionAlertEnabled()) {
st.disableRetransmissionAlerts();
}
/*
* JvB: must never drop ACKs that dont match a transaction! One cannot be sure
* if it isn't an ACK for a 2xx response
*
*/
} else {
if (!dialog.handleAck(transaction)) {
if(sipStack.isLooseDialogValidation()) {
if (sipStack.isLoggingEnabled()) {
sipStack.getLogWriter().logDebug(
"Dialog exists with loose dialog validation " + sipRequest.getFirstLine()
+ " isServerTransaction = " + true + " dialog = " + dialog.getDialogId());
}
SIPServerTransaction st = sipStack
.getRetransmissionAlertTransaction(dialogId);
if (st != null && st.isRetransmissionAlertEnabled()) {
st.disableRetransmissionAlerts();
}
} else {
return;
}
} else {
transaction.passToListener();
dialog.addTransaction(transaction);
dialog.addRoute(sipRequest);
transaction.setDialog(dialog, dialogId);
if (sipStack.isDialogCreated(sipRequest.getMethod())) {
sipStack.putInMergeTable(transaction, sipRequest);
}
/*
* Note that ACK is a pseudo transaction. It is never added to the stack
* and you do not get transaction terminated events on ACK.
*/
if (sipStack.deliverTerminatedEventForAck) {
try {
sipStack.addTransaction(transaction);
transaction.scheduleAckRemoval();
} catch (IOException ex) {
}
} else {
transaction.setMapped(true);
}
/*
* try { sipStack.addTransaction(transaction); } catch (IOException ex) { //
* should never happen. }
*/
}
}
}
} else if (sipRequest.getMethod().equals(Request.PRACK)) {
/*
* RFC 3262: A matching PRACK is defined as one within the same dialog as the
* response, and whose method, CSeq-num, and response-num in the RAck header field
* match, respectively, the method from the CSeq, the sequence number from the CSeq,
* and the sequence number from the RSeq of the reliable provisional response.
*/
if (sipStack.isLoggingEnabled())
sipStack.getLogWriter().logDebug("Processing PRACK for dialog " + dialog);
if (dialog == null && sipProvider.isAutomaticDialogSupportEnabled()) {
if (sipStack.isLoggingEnabled()) {
sipStack.getLogWriter().logDebug(
"Dialog does not exist " + sipRequest.getFirstLine()
+ " isServerTransaction = " + true);
}
if (sipStack.isLoggingEnabled()) {
sipStack
.getLogWriter()
.logDebug(
"Sending 481 for PRACK - automatic dialog support is enabled -- cant find dialog!");
}
SIPResponse notExist = sipRequest
.createResponse(Response.CALL_OR_TRANSACTION_DOES_NOT_EXIST);
try {
sipProvider.sendResponse(notExist);
} catch (SipException e) {
sipStack.getLogWriter().logError("error sending response", e);
}
if (transaction != null) {
sipStack.removeTransaction(transaction);
transaction.releaseSem();
}
return;
} else if (dialog != null) {
if (!dialog.handlePrack(sipRequest)) {
sipStack.getLogWriter().logDebug("Dropping out of sequence PRACK ");
if (transaction != null) {
sipStack.removeTransaction(transaction);
transaction.releaseSem();
}
return;
} else {
try {
sipStack.addTransaction(transaction);
dialog.addTransaction(transaction);
dialog.addRoute(sipRequest);
transaction.setDialog(dialog, dialogId);
} catch (Exception ex) {
InternalErrorHandler.handleException(ex);
}
}
} else {
sipStack.getLogWriter().logDebug(
"Processing PRACK without a DIALOG -- this must be a proxy element");
}
} else if (sipRequest.getMethod().equals(Request.BYE)) {
// Check for correct sequence numbering of the BYE
if (dialog != null && !dialog.isRequestConsumable(sipRequest)) {
if (sipStack.isLoggingEnabled())
sipStack.getLogWriter().logDebug(
"Dropping out of sequence BYE " + dialog.getRemoteSeqNumber() + " "
+ sipRequest.getCSeq().getSeqNumber());
if (dialog.getRemoteSeqNumber() >= sipRequest.getCSeq().getSeqNumber()
&& transaction.getState() == TransactionState.TRYING) {
this.send500Response(sipRequest, transaction);
}
// If the stack knows about the tx, then remove it.
if (transaction != null)
sipStack.removeTransaction(transaction);
return;
} else if (dialog == null && sipProvider.isAutomaticDialogSupportEnabled()) {
// Drop bye's with 481 if dialog does not exist.
// If dialog support is enabled then
// there must be a dialog associated with the bye
// No dialog could be found and requests on this
// provider. Must act like a user agent -- so drop the request.
// NOTE: if Automatic dialog support is not enabled,
// then it is the application's responsibility to
// take care of this error condition possibly.
SIPResponse response = sipRequest
.createResponse(Response.CALL_OR_TRANSACTION_DOES_NOT_EXIST);
response.setReasonPhrase("Dialog Not Found");
sipStack.getLogWriter().logDebug(
"dropping request -- automatic dialog "
+ "support enabled and dialog does not exist!");
try {
transaction.sendResponse(response);
} catch (SipException ex) {
sipStack.getLogWriter().logError("Error in sending response", ex);
}
// If the stack knows about the tx, then remove it.
if (transaction != null) {
sipStack.removeTransaction(transaction);
transaction.releaseSem();
transaction = null;
}
return;
}
// note that the transaction may be null (which
// happens when no dialog for the bye was found.
// and automatic dialog support is disabled (i.e. the app wants
// to manage its own dialog layer.
if (transaction != null && dialog != null) {
try {
if (sipProvider == dialog.getSipProvider()) {
sipStack.addTransaction(transaction);
dialog.addTransaction(transaction);
transaction.setDialog(dialog, dialogId);
}
} catch (IOException ex) {
InternalErrorHandler.handleException(ex);
}
}
if (sipStack.getLogWriter().isLoggingEnabled()) {
sipStack.getLogWriter().logDebug(
"BYE Tx = " + transaction + " isMapped ="
+ transaction.isTransactionMapped());
}
} else if (sipRequest.getMethod().equals(Request.CANCEL)) {
SIPServerTransaction st = (SIPServerTransaction) sipStack.findCancelTransaction(
sipRequest, true);
if (sipStack.getLogWriter().isLoggingEnabled()) {
sipStack.getLogWriter().logDebug(
"Got a CANCEL, InviteServerTx = " + st + " cancel Server Tx ID = "
+ transaction + " isMapped = "
+ transaction.isTransactionMapped());
}
// Processing incoming CANCEL.
// Check if we can process the CANCEL request.
if (sipRequest.getMethod().equals(Request.CANCEL)) {
// If the CANCEL comes in too late, there's not
// much that the Listener can do so just do the
// default action and avoid bothering the listener.
if (st != null && st.getState() == SIPTransaction.TERMINATED_STATE) {
// If transaction already exists but it is
// too late to cancel the transaction then
// just respond OK to the CANCEL and bail.
if (sipStack.isLoggingEnabled())
sipStack.getLogWriter().logDebug("Too late to cancel Transaction");
// send OK and just ignore the CANCEL.
try {
transaction.sendResponse(sipRequest.createResponse(Response.OK));
} catch (Exception ex) {
if (ex.getCause() != null && ex.getCause() instanceof IOException) {
st.raiseIOExceptionEvent();
}
}
return;
}
if (sipStack.isLoggingEnabled())
sipStack.getLogWriter().logDebug("Cancel transaction = " + st);
}
if (transaction != null && st != null && st.getDialog() != null) {
// Found an invite tx corresponding to the CANCEL.
// Set up the client tx and pass up to listener.
transaction.setDialog((SIPDialog) st.getDialog(), dialogId);
dialog = (SIPDialog) st.getDialog();
} else if (st == null && sipProvider.isAutomaticDialogSupportEnabled()
&& transaction != null) {
// Could not find a invite tx corresponding to the CANCEL.
// Automatic dialog support is enabled so I must behave like
// an endpoint on this provider.
// Send the error response for the cancel.
SIPResponse response = sipRequest
.createResponse(Response.CALL_OR_TRANSACTION_DOES_NOT_EXIST);
if (sipStack.isLoggingEnabled()) {
sipStack.getLogWriter().logDebug(
"dropping request -- automatic dialog support "
+ "enabled and INVITE ST does not exist!");
}
try {
sipProvider.sendResponse(response);
} catch (SipException ex) {
InternalErrorHandler.handleException(ex);
}
if (transaction != null) {
sipStack.removeTransaction(transaction);
transaction.releaseSem();
}
return;
}
// INVITE was handled statefully so the CANCEL must also be
// statefully handled.
if (st != null) {
try {
if (transaction != null) {
sipStack.addTransaction(transaction);
transaction.setPassToListener();
transaction.setInviteTransaction(st);
// Dont let the INVITE and CANCEL be concurrently
// processed.
st.acquireSem();
}
} catch (Exception ex) {
InternalErrorHandler.handleException(ex);
}
}
} else if (sipRequest.getMethod().equals(Request.INVITE)) {
SIPTransaction lastTransaction = dialog == null ? null : dialog
.getInviteTransaction();
/*
* RFC 3261 Chapter 14. A UAS that receives a second INVITE before it sends the final
* response to a first INVITE with a lower CSeq sequence number on the same dialog
* MUST return a 500 (Server Internal Error) response to the second INVITE and MUST
* include a Retry-After header field with a randomly chosen value of between 0 and 10
* seconds.
*/
if (dialog != null && transaction != null && lastTransaction != null
&& sipRequest.getCSeq().getSeqNumber() > dialog.getRemoteSeqNumber()
&& lastTransaction instanceof SIPServerTransaction
&& lastTransaction.isInviteTransaction()
&& lastTransaction.getState() != TransactionState.COMPLETED
&& lastTransaction.getState() != TransactionState.TERMINATED
&& lastTransaction.getState() != TransactionState.CONFIRMED) {
if (sipStack.isLoggingEnabled())
sipStack.getLogWriter().logDebug(
"Sending 500 response for out of sequence message");
SIPResponse sipResponse = sipRequest
.createResponse(Response.SERVER_INTERNAL_ERROR);
RetryAfter retryAfter = new RetryAfter();
try {
retryAfter.setRetryAfter((int) (10 * Math.random()));
} catch (InvalidArgumentException ex) {
ex.printStackTrace();
}
sipResponse.addHeader(retryAfter);
try {
transaction.sendMessage(sipResponse);
} catch (IOException ex) {
transaction.raiseIOExceptionEvent();
}
return;
}
/*
* RFC 3261 Chapter 14. A UAS that receives an INVITE on a dialog while an INVITE it
* had sent on that dialog is in progress MUST return a 491 (Request Pending) response
* to the received INVITE.
*/
lastTransaction = (dialog == null ? null : dialog.getLastTransaction());
if (dialog != null && lastTransaction != null
&& lastTransaction.isInviteTransaction()
&& lastTransaction instanceof SIPClientTransaction
&& lastTransaction.getState() != TransactionState.COMPLETED
&& lastTransaction.getState() != TransactionState.TERMINATED) {
if (dialog.getRemoteSeqNumber() + 1 == sipRequest.getCSeq().getSeqNumber()) {
dialog.setRemoteSequenceNumber(sipRequest.getCSeq().getSeqNumber());
if (sipStack.isLoggingEnabled())
sipStack.getLogWriter().logDebug(
"Sending 491 response for out of sequence message");
SIPResponse sipResponse = sipRequest.createResponse(Response.REQUEST_PENDING);
try {
transaction.sendMessage(sipResponse);
} catch (IOException ex) {
transaction.raiseIOExceptionEvent();
}