Example #1
0
        public void SendResponse(SipResponse response)
        {
            Check.Require(response, "response");

            var result = new SipValidator().ValidateMessage(response);

            if (!result.IsValid)
            {
                ThrowSipException(result);
            }

            var sendToEndPoint = SipProvider.DetermineSendTo(response);

            var bytes = SipFormatter.FormatMessage(response);

            SendBytes(bytes, sendToEndPoint);

            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("Send response '{0}' --> {1}. # bytes:{2}.", response.StatusLine.StatusCode, sendToEndPoint, bytes.Length);
            }

            if (_responseSentObserver != null)
            {
                _responseSentObserver.OnNext(response);
            }
        }
Example #2
0
        public void SendRequest(SipRequest request)
        {
            Check.Require(request, "request");

            if (_logger.IsTraceEnabled)
            {
                _logger.Trace("Sending request...");
            }

            var result = new SipValidator().ValidateRequest(request);

            if (!result.IsValid)
            {
                ThrowSipException(result);
            }

            var via = request.Vias.GetTopMost();

            if (string.IsNullOrEmpty(via.Branch))
            {
                via.Branch = SipUtil.CreateBranch();
            }

            var bytes = SipFormatter.FormatMessage(request);

            SipUri sipUri = GetDestinationUri(request);

            IPEndPoint ipEndPoint = GetDestinationEndPoint(sipUri);

            SendBytes(bytes, ipEndPoint);

            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("Send request '{0}' --> {1}. # bytes:{2}.", request.RequestLine.Method, ipEndPoint, bytes.Length);
            }

            if (_requestSentObserver != null)
            {
                _requestSentObserver.OnNext(request);
            }
        }
Example #3
0
        private void SendErrorResponse(Exception exception, SipRequestEvent requestEvent)
        {
            Check.Require(exception, "exception");
            Check.Require(requestEvent, "requestEvent");
            Check.Require(requestEvent.Request, "requestEvent.Request");

            var request      = requestEvent.Request;
            var sipException = exception as SipException;

            string      responseCode = sipException != null ? sipException.ResponseCode : SipResponseCodes.x500_Server_Internal_Error;
            SipResponse response     = sipException != null?_stack.CreateMessageFactory().CreateResponse(request, responseCode) :
                                           _stack.CreateMessageFactory().CreateResponse(request, responseCode + ", " + exception.Message);

            if (requestEvent.ServerTransaction != null)
            {
                requestEvent.ServerTransaction.SendResponse(response);
            }
            else if (response.Vias.GetTopMost() != null)
            {
                IPEndPoint ipEndPoint = SipProvider.DetermineSendTo(response);

                try
                {
                    _contextSource.SendTo(SipFormatter.FormatMessage(response), ipEndPoint);
                    if (_responseSentObserver != null)
                    {
                        _responseSentObserver.OnNext(response);
                    }
                }
                catch (SocketException err)
                {
                    _logger.Error("Failed to send response to " + ipEndPoint.ToString(), err);
                }
            }
            else
            {
                _logger.Warn("Response can not be sent. Via TopMost header missing.");
            }
        }
Example #4
0
        private void OnIncomingRequestContext(SipContext context)
        {
            if (_requestReceivedObserver != null)
            {
                _requestReceivedObserver.OnNext(context.Request);
            }

            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("Validating the request...");
            }

            var result = new SipValidator().ValidateMessage(context.Request);

            if (!result.IsValid)
            {
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug("The received request is not valid. Throwing a sip exception.");
                }

                ThrowSipException(result);
            }

            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("Request valid.");
            }

            /*firewall traversal not supported. SupportFirewalTraversal(context);*/

            var requestEvent = new SipRequestEvent(context);

            ISipRequestProcessor sipRequestProcessor = _sipListener;

            //8.2.2.2: merged requests, not implemented

            SipAbstractServerTransaction stx;

            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("Searching the table for a matching tx..");
            }

            if (_stxTable.TryGetValue(GetServerTransactionId(context.Request), out stx))
            {
                if (_logger.IsTraceEnabled)
                {
                    _logger.Trace("Found a matching tx. Setting it as the requestprocessor.");
                }

                sipRequestProcessor = stx;

                //if (IsDialogInitiatingRequest(stx.Request))
                //{
                //if (_logger.IsDebugEnabled) _logger.Debug("The received request is an 'INVITE' request. Try setting the dialog on tx...");

                //set the dialog, so it can initiate
                SipAbstractDialog found;
                if (TryGetDialogOnTx(context.Request, out found))
                {
                    requestEvent.Dialog = found;
                }
                //}
            }
            else
            {
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug("Could not find a matching tx..");
                }

                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug("Searching the table for a matching dialog...");
                }

                SipAbstractDialog dialog;
                if (_dialogTable.TryGetValue(GetDialogId(context.Request, true), out dialog))
                {
                    if (_logger.IsTraceEnabled)
                    {
                        _logger.Trace("Found a matching dialog. Setting it as the requestprocessor.");
                    }

                    sipRequestProcessor = dialog;
                    requestEvent.Dialog = dialog;
                }
                else
                {
                    if (_logger.IsTraceEnabled)
                    {
                        _logger.Trace("Could not find a matching dialog. Using the SipProvider's SipListener as the requestprocessor.");
                    }
                }
            }

            try
            {
                sipRequestProcessor.ProcessRequest(requestEvent);

                if (requestEvent.IsSent)
                {
                    if (_logger.IsDebugEnabled)
                    {
                        _logger.Debug("Processed '{0}' request. The response has already been sent. Skipping automatic response sending.", requestEvent.Request.RequestLine.Method);
                    }
                    return;
                }

                if (!ShouldHaveResponse(requestEvent.Request))
                {
                    if (_logger.IsDebugEnabled)
                    {
                        _logger.Debug("Processed '{0}' request. The request does not require a response. Skipping automatic response sending.", requestEvent.Request.RequestLine.Method);
                    }
                    return;
                }

                if (requestEvent.Response == null)
                {
                    throw new SipCoreException("Response to send can not be null. The ProcessRequest method is supposed to create a response message that is to be sent.");
                }

                if (!requestEvent.IsSent)
                {
                    var response = requestEvent.Response;

                    var remoteEndPoint = SipProvider.DetermineSendTo(response);

                    _contextSource.SendTo(SipFormatter.FormatMessage(response), remoteEndPoint);

                    if (_responseSentObserver != null)
                    {
                        _responseSentObserver.OnNext(response);
                    }
                }
            }
            catch (SipCoreException coreException)
            {
                throw coreException;
            }
            catch (SipException sipException)
            {
                if (ShouldHaveResponse(requestEvent.Request))
                {
                    SendErrorResponse(sipException, requestEvent);
                }
            }
            catch (Exception err)
            {
                _logger.ErrorException("Request failed.", err);

                try
                {
                    if (ShouldHaveResponse(requestEvent.Request))
                    {
                        SendErrorResponse(err, requestEvent);
                    }
                }
                catch (Exception errr)
                {
                    _logger.Debug("Failed to send the response.", errr);
                }
            }
        }