Beispiel #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);
            }
        }
Beispiel #2
0
        public void ValidateMessage_WithValidRequest_ExpectResultIsValidIsTrue()
        {
            SipValidator v      = CreateSipValidator();
            var          result = v.ValidateMessage(new SipRequestBuilder().Build());

            result.IsValid.Should().BeTrue();
        }
Beispiel #3
0
        public void ValidateMessage_WithoutMaxForwardsHeader_ExpectResultIsFalseAndHasRequiredHeadersMissingIsTrue()
        {
            SipValidator v       = CreateSipValidator();
            var          request = new SipRequestBuilder().WithMaxForwards(null).Build();
            var          result  = v.ValidateMessage(request);

            result.IsValid.Should().BeFalse();
            result.HasRequiredHeadersMissing.Should().BeTrue();
        }
Beispiel #4
0
        public void ValidateMessage_WithRequestLineVersionSipTwoDotZero_ExpectResultIsTrue()
        {
            SipValidator v       = CreateSipValidator();
            var          request = new SipRequestBuilder()
                                   .WithRequestLine(new SipRequestLineBuilder().WithVersion("SIP/2.0").Build())
                                   .Build();
            var result = v.ValidateMessage(request);

            result.IsValid.Should().BeTrue();
        }
Beispiel #5
0
        public void ValidateMessage_WithRequestLineVersionSipThreeDotZero_ExpectResultIsFalseAndHasUnSupportedSipVersionIsTrue()
        {
            SipValidator v       = CreateSipValidator();
            var          request = new SipRequestBuilder()
                                   .WithRequestLine(new SipRequestLineBuilder().WithVersion("SIP/3.0").Build())
                                   .Build();
            var result = v.ValidateMessage(request);

            result.IsValid.Should().BeFalse();
            result.HasUnSupportedSipVersion.Should().BeTrue();
        }
Beispiel #6
0
        public void ValidateMessage_WithCSeqMethodDifferentFromRequestLineMethod_ExpectResultIsFalseAndHasInvalidSCeqMethodIsTrue()
        {
            SipValidator v       = CreateSipValidator();
            var          request = new SipRequestBuilder()
                                   .WithRequestLine(new SipRequestLineBuilder().WithMethod(SipMethods.Register).Build())
                                   .WithCSeq(new SipCSeqHeaderBuilder().WithCommand(SipMethods.Invite).Build())
                                   .Build();
            var result = v.ValidateMessage(request);

            result.IsValid.Should().BeFalse();
            (result as ValidateRequestResult).Should().NotBeNull();
            (result as ValidateRequestResult).HasInvalidSCeqMethod.Should().BeTrue();
        }
Beispiel #7
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);
            }
        }
Beispiel #8
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);
                }
            }
        }
Beispiel #9
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);
                }
            }
        }
Beispiel #10
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);
        }
Beispiel #11
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);
        }