Exemplo n.º 1
0
            public void ParseMissingCSeqOptionsResponse()
            {
                Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

                string sipMsg =
                    "SIP/2.0 200 OK" + m_CRLF +
                    "To: <sip:[email protected]:5060>;tag=eba877fbb8dd284bi0" + m_CRLF +
                    "From: <sip:213.168.225.133:5060>;tag=5880003940" + m_CRLF +
                    "Call-ID: [email protected]" + m_CRLF +
                    "Via: SIP/2.0/UDP 213.168.225.133:5060;branch=z9hG4bK1702000048" + m_CRLF +
                    "Server: Linksys/RT31P2-2.0.10(LIc)" + m_CRLF +
                    "Content-Length: 0" + m_CRLF +
                    "Allow: ACK, BYE, CANCEL, INFO, INVITE, NOTIFY, OPTIONS, REFER" + m_CRLF +
                    "Supported: x-sipura" + m_CRLF + m_CRLF;

                SIPMessage  sipMessage  = SIPMessage.ParseSIPMessage(Encoding.UTF8.GetBytes(sipMsg), null, null);
                SIPResponse optionsResp = SIPResponse.ParseSIPResponse(sipMessage);

                Console.WriteLine("CSeq=" + optionsResp.Header.CSeq + ".");
                Console.WriteLine("CSeq Method=" + optionsResp.Header.CSeqMethod + ".");

                Assert.IsTrue(optionsResp.Header.CSeq == -1, "Response CSeq was incorrect.");
                Assert.IsTrue(optionsResp.Header.CSeqMethod == SIPMethodsEnum.NONE, "Response CSeq method was incorrect.");

                Console.WriteLine("-----------------------------------------");
            }
Exemplo n.º 2
0
            public void ParseOptionsBodyResponse()
            {
                Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

                string sipMsg = "SIP/2.0 200 OK" + m_CRLF +
                                "Via: SIP/2.0/UDP 213.168.225.133:5060;branch=z9hG4bK10a1fab0" + m_CRLF +
                                "From: \"Unknown\" <sip:[email protected]>;tag=as18338373" + m_CRLF +
                                "To: <sip:[email protected]>;tag=OLg-20481" + m_CRLF +
                                "Call-ID: [email protected]" + m_CRLF +
                                "CSeq: 102 OPTIONS" + m_CRLF +
                                "content-type: application/sdp" + m_CRLF +
                                "Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, INFO, REFER, NOTIFY" + m_CRLF +
                                "Content-Length: 217" + m_CRLF +
                                m_CRLF +
                                "v=0" + m_CRLF +
                                "o=0 5972727 56415 IN IP4 0.0.0.0" + m_CRLF +
                                "s=SIP Call" + m_CRLF +
                                "c=IN IP4 0.0.0.0" + m_CRLF +
                                "t=0 0" + m_CRLF +
                                "m=audio 0 RTP/AVP 18 0 8 4 2" + m_CRLF +
                                "a=rtpmap:18 G729/8000" + m_CRLF +
                                "a=rtpmap:0 pcmu/8000" + m_CRLF +
                                "a=rtpmap:8 pcma/8000" + m_CRLF +
                                "a=rtpmap:4 g723/8000" + m_CRLF +
                                "a=rtpmap:2 g726/8000" + m_CRLF;

                SIPMessage  sipMessage = SIPMessage.ParseSIPMessage(Encoding.UTF8.GetBytes(sipMsg), null, null);
                SIPResponse okResp     = SIPResponse.ParseSIPResponse(sipMessage);

                Assert.IsTrue(okResp.Status == SIPResponseStatusCodesEnum.Ok, "The SIP response status was not parsed correctly.");
                Assert.IsTrue(okResp.Body.Length == 217, "The SIP response body length was not correct.");

                Console.WriteLine("-----------------------------------------");
            }
Exemplo n.º 3
0
            public void ParseMultipleContactsResponse()
            {
                Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

                string sipMsg =
                    "SIP/2.0 200 OK" + m_CRLF +
                    "Via: SIP/2.0/UDP 192.168.1.32:64226;branch=z9hG4bK-d87543-ac7a6a75bc519655-1--d87543-;rport=64226;received=89.100.104.191" + m_CRLF +
                    "To: \"253989\"<sip:[email protected]>;tag=cb2000b247d89723001a836145f3b053.5b6c" + m_CRLF +
                    "From: \"253989\"<sip:[email protected]>;tag=9812dd2f" + m_CRLF +
                    "Call-ID: ODllYWY1NDJiNGMwYmQ1MjVmZmViMmEyMDViMGM0Y2Y." + m_CRLF +
                    "CSeq: 2 REGISTER" + m_CRLF +
                    "Date: Fri, 17 Nov 2006 17:15:35 GMT" + m_CRLF +
                    "Contact: <sip:[email protected]>;q=0.1;expires=3298, \"Joe Bloggs\"<sip:[email protected]:64226;rinstance=5720c5fed8cbcd34>;q=0.1;expires=3600" + m_CRLF +
                    "Content-Length: 0" + m_CRLF + m_CRLF;

                SIPMessage  sipMessage = SIPMessage.ParseSIPMessage(Encoding.UTF8.GetBytes(sipMsg), null, null);
                SIPResponse okResp     = SIPResponse.ParseSIPResponse(sipMessage);

                Console.WriteLine("To: " + okResp.Header.To.ToString());

                Assert.AreEqual(SIPResponseStatusCodesEnum.Ok, okResp.Status, "Response should have been ok.");
                Assert.AreEqual(okResp.Header.Contact.Count, 2, "Response should have had two contacts.");
                Assert.AreEqual(okResp.Header.Contact[0].ContactURI.ToString(), "sip:[email protected]", "The contact URI for the first contact header was incorrect.");
                Assert.AreEqual(okResp.Header.Contact[0].Expires, 3298, "The expires value for the first contact header was incorrect.");
                Assert.AreEqual(okResp.Header.Contact[0].Q, "0.1", "The q value for the first contact header was incorrect.");
                Assert.AreEqual(okResp.Header.Contact[1].ContactName, "Joe Bloggs", "The contact name for the first contact header was incorrect.");
                Assert.AreEqual(okResp.Header.Contact[1].ContactURI.ToString(), "sip:[email protected]:64226;rinstance=5720c5fed8cbcd34", "The contact URI for the first contact header was incorrect.");
                Assert.AreEqual(okResp.Header.Contact[1].Expires, 3600, "The expires value for the second contact header was incorrect.");
                Assert.AreEqual(okResp.Header.Contact[1].Q, "0.1", "The q value for the second contact header was incorrect.");
                Console.WriteLine("-----------------------------------------");
            }
Exemplo n.º 4
0
        public static SIPResponse ParseSIPResponse(SIPMessage sipMessage)
        {
            try
            {
                SIPResponse sipResponse = new SIPResponse();
                sipResponse.LocalSIPEndPoint  = sipMessage.LocalSIPEndPoint;
                sipResponse.RemoteSIPEndPoint = sipMessage.RemoteSIPEndPoint;
                string statusLine = sipMessage.FirstLine;

                int firstSpacePosn = statusLine.IndexOf(" ");

                sipResponse.SIPVersion   = statusLine.Substring(0, firstSpacePosn).Trim();
                statusLine               = statusLine.Substring(firstSpacePosn).Trim();
                sipResponse.StatusCode   = Convert.ToInt32(statusLine.Substring(0, 3));
                sipResponse.Status       = SIPResponseStatusCodes.GetStatusTypeForCode(sipResponse.StatusCode);
                sipResponse.ReasonPhrase = statusLine.Substring(3).Trim();

                sipResponse.Header = SIPHeader.ParseSIPHeaders(sipMessage.SIPHeaders);
                sipResponse.Body   = sipMessage.Body;

                return(sipResponse);
            }
            catch (SIPValidationException)
            {
                throw;
            }
            catch (Exception excp)
            {
                logger.LogError("Exception ParseSIPResponse. " + excp.Message);
                logger.LogError(sipMessage.RawMessage);
                throw new SIPValidationException(SIPValidationFieldsEnum.Response, "Error parsing SIP Response");
            }
        }
        /// <summary>
        /// Attempts to extract SIP messages from the data that has been received on the SIP stream connection.
        /// </summary>
        /// <param name="recvChannel">The receiving SIP channel.</param>
        /// <param name="buffer">The buffer holding the current data from the stream. Note that the buffer can
        /// stretch over multiple receives.</param>
        /// <param name="bytesRead">The bytes that were read by the latest receive operation (the new bytes available).</param>
        public void ExtractSIPMessages(SIPChannel recvChannel, byte[] buffer, int bytesRead)
        {
            RecvEndPosn += bytesRead;

            int bytesSkipped = 0;

            byte[] sipMsgBuffer = SIPMessage.ParseSIPMessageFromStream(buffer, RecvStartPosn, RecvEndPosn, out bytesSkipped);

            while (sipMsgBuffer != null)
            {
                // A SIP message is available.
                if (SIPMessageReceived != null)
                {
                    LastTransmission = DateTime.Now;
                    SIPMessageReceived(recvChannel, new SIPEndPoint(ConnectionProtocol, RemoteEndPoint), sipMsgBuffer);
                }

                RecvStartPosn += (sipMsgBuffer.Length + bytesSkipped);

                if (RecvStartPosn == RecvEndPosn)
                {
                    // All data has been successfully extracted from the receive buffer.
                    RecvStartPosn = RecvEndPosn = 0;
                    break;
                }
                else
                {
                    // Try and extract another SIP message from the receive buffer.
                    sipMsgBuffer = SIPMessage.ParseSIPMessageFromStream(buffer, RecvStartPosn, RecvEndPosn, out bytesSkipped);
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Attempts to parse a SIP message from a string containing a single SIP request or response.
        /// </summary>
        /// <param name="message">The string to parse.</param>
        /// <param name="localSIPEndPoint">The end point the message was received on.</param>
        /// <param name="remoteSIPEndPoint">The end point the message was received from.</param>
        /// <returns>If successfull a SIP message or null if not.</returns>
        public static SIPMessage ParseSIPMessage(string message, SIPEndPoint localSIPEndPoint, SIPEndPoint remoteSIPEndPoint)
        {
            try
            {
                SIPMessage sipMessage = new SIPMessage();
                sipMessage.LocalSIPEndPoint  = localSIPEndPoint;
                sipMessage.RemoteSIPEndPoint = remoteSIPEndPoint;

                sipMessage.RawMessage = message;
                int endFistLinePosn = message.IndexOf(m_CRLF);

                if (endFistLinePosn != -1)
                {
                    sipMessage.FirstLine = message.Substring(0, endFistLinePosn);

                    if (sipMessage.FirstLine.Substring(0, 3) == SIP_RESPONSE_PREFIX)
                    {
                        sipMessage.SIPMessageType = SIPMessageTypesEnum.Response;
                    }
                    else
                    {
                        sipMessage.SIPMessageType = SIPMessageTypesEnum.Request;
                    }

                    int endHeaderPosn = message.IndexOf(m_CRLF + m_CRLF);
                    if (endHeaderPosn == -1)
                    {
                        // Assume flakey implementation if message does not contain the required CRLFCRLF sequence and treat the message as having no body.
                        string headerString = message.Substring(endFistLinePosn + 2, message.Length - endFistLinePosn - 2);
                        sipMessage.SIPHeaders = SIPHeader.SplitHeaders(headerString); //Regex.Split(headerString, m_CRLF);
                    }
                    else
                    {
                        string headerString = message.Substring(endFistLinePosn + 2, endHeaderPosn - endFistLinePosn - 2);
                        sipMessage.SIPHeaders = SIPHeader.SplitHeaders(headerString); //Regex.Split(headerString, m_CRLF);

                        if (message.Length > endHeaderPosn + 4)
                        {
                            sipMessage.Body = message.Substring(endHeaderPosn + 4);
                        }
                    }

                    return(sipMessage);
                }
                else
                {
                    logger.LogWarning("Error ParseSIPMessage, there were no end of line characters in the string being parsed.");
                    return(null);
                }
            }
            catch (Exception excp)
            {
                logger.LogError("Exception ParseSIPMessage. " + excp.Message + "\nSIP Message=" + message + ".");
                return(null);
            }
        }
Exemplo n.º 7
0
        public static SIPRequest ParseSIPRequest(SIPMessage sipMessage)
        {
            string uriStr = null;

            try
            {
                SIPRequest sipRequest = new SIPRequest();
                sipRequest.LocalSIPEndPoint  = sipMessage.LocalSIPEndPoint;
                sipRequest.RemoteSIPEndPoint = sipMessage.RemoteSIPEndPoint;

                string statusLine = sipMessage.FirstLine;

                int firstSpacePosn = statusLine.IndexOf(" ");

                string method = statusLine.Substring(0, firstSpacePosn).Trim();
                sipRequest.Method = SIPMethods.GetMethod(method);
                if (sipRequest.Method == SIPMethodsEnum.UNKNOWN)
                {
                    sipRequest.UnknownMethod = method;
                    logger.LogWarning("Unknown SIP method received " + sipRequest.UnknownMethod + ".");
                }

                statusLine = statusLine.Substring(firstSpacePosn).Trim();
                int secondSpacePosn = statusLine.IndexOf(" ");

                if (secondSpacePosn != -1)
                {
                    uriStr = statusLine.Substring(0, secondSpacePosn);

                    sipRequest.URI        = SIPURI.ParseSIPURI(uriStr);
                    sipRequest.SIPVersion = statusLine.Substring(secondSpacePosn, statusLine.Length - secondSpacePosn).Trim();
                    sipRequest.Header     = SIPHeader.ParseSIPHeaders(sipMessage.SIPHeaders);
                    sipRequest.Body       = sipMessage.Body;

                    return(sipRequest);
                }
                else
                {
                    throw new SIPValidationException(SIPValidationFieldsEnum.Request, "URI was missing on Request.");
                }
            }
            catch (SIPValidationException)
            {
                throw;
            }
            catch (Exception excp)
            {
                logger.LogError("Exception parsing SIP Request. " + excp.Message);
                logger.LogError(sipMessage.RawMessage);
                throw new SIPValidationException(SIPValidationFieldsEnum.Request, "Unknown error parsing SIP Request");
            }
        }
Exemplo n.º 8
0
            public void ParseMultiLineRecordRouteResponse()
            {
                Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

                string sipMsg =
                    "SIP/2.0 200 OK" + m_CRLF +
                    "Via: SIP/2.0/UDP 10.0.0.100:5060;rport=61540;branch=z9hG4bK40661a8b4a2d4973ae75fa52f1940383" + m_CRLF +
                    "From: <sip:[email protected]>;tag=1014391101" + m_CRLF +
                    "To: <sip:[email protected]>;tag=gj-2k5-490f768a-00005cf1-00002e1aR2f0f2383.b" + m_CRLF +
                    "Call-ID: 1960514b216a465fb900e2966d30e9bb" + m_CRLF +
                    "CSeq: 2 INVITE" + m_CRLF +
                    "Record-Route: <sip:77.75.25.44:5060;lr=on>" + m_CRLF +
                    "Record-Route: <sip:77.75.25.45:5060;lr=on;ftag=1014391101>" + m_CRLF +
                    "Accept: application/sdp, application/isup, application/dtmf, application/dtmf-relay,  multipart/mixed" + m_CRLF +
                    "Contact: <sip:[email protected]:5060>" + m_CRLF +
                    "Allow: INVITE,ACK,CANCEL,BYE,REGISTER,REFER,INFO,SUBSCRIBE,NOTIFY,PRACK,UPDATE,OPTIONS" + m_CRLF +
                    "Supported: timer" + m_CRLF +
                    "Session-Expires: 600;refresher=uas" + m_CRLF +
                    "Content-Length:  232" + m_CRLF +
                    "Content-Disposition: session; handling=required" + m_CRLF +
                    "Content-Type: application/sdp" + m_CRLF +
                    m_CRLF +
                    "v=0" + m_CRLF +
                    "o=Sonus_UAC 4125 3983 IN IP4 64.152.60.78" + m_CRLF +
                    "s=SIP Media Capabilities" + m_CRLF +
                    "c=IN IP4 64.152.60.164" + m_CRLF +
                    "t=0 0" + m_CRLF +
                    "m=audio 19144 RTP/AVP 0 101" + m_CRLF +
                    "a=rtpmap:0 PCMU/8000" + m_CRLF +
                    "a=rtpmap:101 telephone-event/8000" + m_CRLF +
                    "a=fmtp:101 0-15" + m_CRLF +
                    "a=sendrecv" + m_CRLF +
                    "a=ptime:20" + m_CRLF;

                SIPMessage  sipMessage = SIPMessage.ParseSIPMessage(Encoding.UTF8.GetBytes(sipMsg), null, null);
                SIPResponse okResp     = SIPResponse.ParseSIPResponse(sipMessage);

                Assert.IsTrue(okResp.Header.RecordRoutes.Length == 2, "The wrong number of Record-Route headers were present in the parsed response.");
                Assert.IsTrue(okResp.Header.RecordRoutes.PopRoute().ToString() == "<sip:77.75.25.44:5060;lr=on>", "The top Record-Route header was incorrect.");
                SIPRoute nextRoute = okResp.Header.RecordRoutes.PopRoute();

                Assert.IsTrue(nextRoute.ToString() == "<sip:77.75.25.45:5060;lr=on;ftag=1014391101>", "The second Record-Route header was incorrect, " + nextRoute.ToString() + ".");

                Console.WriteLine("-----------------------------------------");
            }
Exemplo n.º 9
0
 public static SIPRequest ParseSIPRequest(string sipMessageStr)
 {
     try
     {
         SIPMessage sipMessage = SIPMessage.ParseSIPMessage(sipMessageStr, null, null);
         return(SIPRequest.ParseSIPRequest(sipMessage));
     }
     catch (SIPValidationException)
     {
         throw;
     }
     catch (Exception excp)
     {
         logger.Error("Exception ParseSIPRequest. " + excp.Message);
         logger.Error(sipMessageStr);
         throw new SIPValidationException(SIPValidationFieldsEnum.Request, "Unknown error parsing SIP Request");
     }
 }
Exemplo n.º 10
0
 public static SIPResponse ParseSIPResponse(string sipMessageStr)
 {
     try
     {
         SIPMessage sipMessage = SIPMessage.ParseSIPMessage(sipMessageStr, null, null);
         return(SIPResponse.ParseSIPResponse(sipMessage));
     }
     catch (SIPValidationException)
     {
         throw;
     }
     catch (Exception excp)
     {
         logger.LogError("Exception ParseSIPResponse. " + excp.Message);
         logger.LogError(sipMessageStr);
         throw new SIPValidationException(SIPValidationFieldsEnum.Response, "Error parsing SIP Response");
     }
 }
Exemplo n.º 11
0
        public SIPEndPoint LocalSIPEndPoint;                // The local SIP socket the message was received on or sent from.

        /// <summary>
        /// Attempts to parse a SIP message from a single buffer that can only contain a single message.
        /// </summary>
        /// <param name="buffer">The buffer rhat will be parsed for a SIP message.</param>
        /// <param name="localSIPEndPoint">The end point the message was received on.</param>
        /// <param name="remoteSIPEndPoint">The end point the message was received from.</param>
        /// <returns>If successfull a SIP message or null if not.</returns>
        public static SIPMessage ParseSIPMessage(byte[] buffer, SIPEndPoint localSIPEndPoint, SIPEndPoint remoteSIPEndPoint)
        {
            string message = null;

            try
            {
                if (buffer == null || buffer.Length < m_minFirstLineLength)
                {
                    // Ignore.
                    return(null);
                }
                else if (buffer.Length > SIPConstants.SIP_MAXIMUM_RECEIVE_LENGTH)
                {
                    throw new ApplicationException("SIP message received that exceeded the maximum allowed message length, ignoring.");
                }
                else if (!ByteBufferInfo.HasString(buffer, 0, buffer.Length, SIP_MESSAGE_IDENTIFIER, m_CRLF))
                {
                    // Message does not contain "SIP" anywhere on the first line, ignore.
                    return(null);
                }
                else
                {
                    message = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                    SIPMessage sipMessage = ParseSIPMessage(message, localSIPEndPoint, remoteSIPEndPoint);

                    if (sipMessage != null)
                    {
                        sipMessage.RawBuffer = buffer;
                        return(sipMessage);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            catch (Exception excp)
            {
                message = message.Replace("\n", "LF");
                message = message.Replace("\r", "CR");
                logger.LogError("Exception ParseSIPMessage. " + excp.Message + "\nSIP Message=" + message + ".");
                return(null);
            }
        }
Exemplo n.º 12
0
            public void ParseAsteriskOKUnitTest()
            {
                Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

                string sipMsg =
                    "SIP/2.0 200 OK" + m_CRLF +
                    "Via: SIP/2.0/UDP 213.168.225.135:5060;branch=z9hG4bKT36BdhXPlT5cqPFQQr81yMmZ37U=" + m_CRLF +
                    "Via: SIP/2.0/UDP 192.168.1.2:5065;received=220.240.255.198:64216;branch=z9hG4bK7D8B6549580844AEA104BD4A837049DD" + m_CRLF +
                    "From: bluesipd <sip:bluesipd@bluesipd:5065>;tag=630217013" + m_CRLF +
                    "To: <sip:303@bluesipd>;tag=as46f418e9" + m_CRLF +
                    "Call-ID: [email protected]" + m_CRLF +
                    "CSeq: 27481 INVITE" + m_CRLF +
                    "User-Agent: asterisk" + m_CRLF +
                    "Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY" + m_CRLF +
                    "Contact: <sip:[email protected]>" + m_CRLF +
                    "Content-Type: application/sdp" + m_CRLF +
                    "Content-Length: 352" + m_CRLF +
                    m_CRLF +
                    "v=0" + m_CRLF +
                    "o=root 24710 24712 IN IP4 213.168.225.133" + m_CRLF +
                    "s=session" + m_CRLF +
                    "c=IN IP4 213.168.225.133" + m_CRLF +
                    "t=0 0" + m_CRLF +
                    "m=audio 18656 RTP/AVP 0 8 18 3 97 111 101" + m_CRLF +
                    "a=rtpmap:0 PCMU/8000" + m_CRLF +
                    "a=rtpmap:8 PCMA/8000" + m_CRLF +
                    "a=rtpmap:18 G729/8000" + m_CRLF +
                    "a=rtpmap:3 GSM/8000" + m_CRLF +
                    "a=rtpmap:97 iLBC/8000" + m_CRLF +
                    "a=rtpmap:111 G726-32/8000" + m_CRLF +
                    "a=rtpmap:101 telephone-event/8000" + m_CRLF +
                    "a=fmtp:101 0-16" + m_CRLF +
                    "a=silenceSupp:off - - - -" + m_CRLF;

                SIPMessage  sipMessage = SIPMessage.ParseSIPMessage(Encoding.UTF8.GetBytes(sipMsg), null, null);
                SIPResponse okResp     = SIPResponse.ParseSIPResponse(sipMessage);

                Assert.IsTrue(okResp.Status == SIPResponseStatusCodesEnum.Ok, "The SIP response status was not parsed correctly.");
                Assert.IsTrue(okResp.Body.Length == 352, "The SIP response body length was not correct.");

                Console.WriteLine("-----------------------------------------");
            }
Exemplo n.º 13
0
            public void ParseMultiLineViaResponse()
            {
                Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

                string sipMsg =
                    "SIP/2.0 200 OK" + m_CRLF +
                    "Via: SIP/2.0/UDP 194.213.29.100:5060;branch=z9hG4bK5feb18267ce40fb05969b4ba843681dbfc9ffcff, SIP/2.0/UDP 194.213.29.54:5061;branch=z9hG4bK52b6a8b7" + m_CRLF +
                    "Record-Route: <sip:194.213.29.100:5060;lr>" + m_CRLF +
                    "From: Unknown <sip:[email protected]:5061>;tag=as58cbdbd1" + m_CRLF +
                    "To: <sip:[email protected]:5060>;tag=1144090013" + m_CRLF +
                    "Call-ID: [email protected]" + m_CRLF +
                    "CSeq: 102 INVITE" + m_CRLF +
                    "Contact: <sip:[email protected]:5060>" + m_CRLF +
                    "Server: Patton SN4634 3BIS 00A0BA04469B R5.3 2009-01-15 H323 SIP BRI M5T SIP Stack/4.0.28.28" + m_CRLF +
                    "Supported: replaces" + m_CRLF +
                    "Content-Type: application/sdp" + m_CRLF +
                    "Content-Length: 298" + m_CRLF +
                    m_CRLF +
                    "v=0" + m_CRLF +
                    "o=MxSIP 0 56 IN IP4 10.10.10.155" + m_CRLF +
                    "s=SIP Call" + m_CRLF +
                    "c=IN IP4 10.10.10.155" + m_CRLF +
                    "t=0 0" + m_CRLF +
                    "m=audio 4974 RTP/AVP 0 18 8 101" + m_CRLF +
                    "a=rtpmap:0 PCMU/8000" + m_CRLF +
                    "a=rtpmap:18 G729/8000" + m_CRLF +
                    "a=rtpmap:8 PCMA/8000" + m_CRLF +
                    "a=rtpmap:101 telephone-event/8000" + m_CRLF +
                    "a=fmtp:18 annexb=no" + m_CRLF +
                    "a=fmtp:101 0-16" + m_CRLF +
                    "a=sendrecv" + m_CRLF +
                    "m=video 0 RTP/AVP 31 34 103 99";

                SIPMessage  sipMessage = SIPMessage.ParseSIPMessage(Encoding.UTF8.GetBytes(sipMsg), null, null);
                SIPResponse okResp     = SIPResponse.ParseSIPResponse(sipMessage);

                Assert.IsTrue(okResp.Header.Vias.Length == 2, "The wrong number of Record-Route headers were present in the parsed response.");
                Assert.IsTrue(okResp.Header.Vias.TopViaHeader.ContactAddress == "194.213.29.100:5060", "The top via contact address was not ocrrectly parsed.");

                Console.WriteLine("-----------------------------------------");
            }
Exemplo n.º 14
0
            public void ParseMSCOkResponse()
            {
                Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

                string sipMsg =
                    "SIP/2.0 200 OK" + m_CRLF +
                    "From: Blue Face<sip:[email protected]>;tag=as5fd53de7" + m_CRLF +
                    "To: sip:[email protected];tag=MTHf2-ol1Yn0" + m_CRLF +
                    "Call-ID: [email protected]:5061" + m_CRLF +
                    "CSeq: 102 INVITE" + m_CRLF +
                    "Via: SIP/2.0/UDP 213.168.225.133:5060;branch=z9hG4bKG+WGOVwLyT6vOW9s" + m_CRLF +
                    "Via: SIP/2.0/UDP 213.168.225.133:5061;branch=z9hG4bK09db9c73" + m_CRLF +
                    "Contact: +3535xxx<sip:[email protected]:5061>" + m_CRLF +
                    "User-Agent: MSC/VC510  Build-Date Nov  7 2005" + m_CRLF +
                    "Allow: INVITE,BYE,CANCEL,OPTIONS,PRACK,NOTIFY,UPDATE,REFER" + m_CRLF +
                    "Supported: timer,replaces" + m_CRLF +
                    "Record-Route: <sip:213.168.225.133:5060;lr>,<sip:213.168.225.133:5061;lr>" + m_CRLF +
                    "Content-Type: application/sdp" + m_CRLF +
                    "Content-Length: 182" + m_CRLF +
                    m_CRLF +
                    "v=0" + m_CRLF +
                    "o=xxxxxxxxx 75160 1 IN IP4 127.127.127.30" + m_CRLF +
                    "s=-" + m_CRLF +
                    "c=IN IP4 127.127.127.30" + m_CRLF +
                    "t=0 0" + m_CRLF +
                    "m=audio 8002 RTP/AVP 0 101" + m_CRLF +
                    "a=rtpmap:0 PCMU/8000" + m_CRLF +
                    "a=rtpmap:101 telephone-event/8000" + m_CRLF +
                    "a=ptime:20";

                SIPMessage  sipMessage = SIPMessage.ParseSIPMessage(Encoding.UTF8.GetBytes(sipMsg), null, null);
                SIPResponse okResp     = SIPResponse.ParseSIPResponse(sipMessage);

                Console.WriteLine("To: " + okResp.Header.To.ToString());

                Assert.AreEqual(SIPResponseStatusCodesEnum.Ok, okResp.Status, "Response should have been ok.");
                Assert.AreEqual("127.0.0.1", okResp.Header.To.ToURI.Host, "To URI host was not parsed correctly.");

                Console.WriteLine("-----------------------------------------");
            }
Exemplo n.º 15
0
            public void ParseForbiddenResponse()
            {
                Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

                string sipMsg = "SIP/2.0 403 Forbidden" + m_CRLF +
                                "Via: SIP/2.0/UDP 192.168.1.1;branch=z9hG4bKbcb78f72d221beec" + m_CRLF +
                                "From: <sip:sip.blueface.ie>;tag=9a4c86234adcc297" + m_CRLF +
                                "To: <sip:sip.blueface.ie>;tag=as6900b876" + m_CRLF +
                                "Call-ID: [email protected]" + m_CRLF +
                                "CSeq: 100 REGISTER" + m_CRLF +
                                "User-Agent: asterisk" + m_CRLF +
                                "Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY" + m_CRLF +
                                "Contact: <sip:[email protected]>" + m_CRLF +
                                "Content-Length: 0" + m_CRLF + m_CRLF;

                SIPMessage  sipMessage    = SIPMessage.ParseSIPMessage(Encoding.UTF8.GetBytes(sipMsg), null, null);
                SIPResponse forbiddenResp = SIPResponse.ParseSIPResponse(sipMessage);

                Assert.IsTrue(forbiddenResp.Status == SIPResponseStatusCodesEnum.Forbidden, "The SIP response status was not parsed correctly.");

                Console.WriteLine("-----------------------------------------");
            }
Exemplo n.º 16
0
            public void ParseResponseNoEndDoubleCRLFUnitTest()
            {
                Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

                string sipMsg =
                    "SIP/2.0 100 Trying" + m_CRLF +
                    "Via: SIP/2.0/UDP 213.168.225.135:5060;branch=z9hG4bKD+ta2mJ+C+VV/L50aPO1lFJnrag=" + m_CRLF +
                    "Via: SIP/2.0/UDP 192.168.1.2:5065;received=220.240.255.198:64193;branch=z9hG4bKB86FC8D2431F49E9862D1EE439C78AD8" + m_CRLF +
                    "From: bluesipd <sip:bluesipd@bluesipd:5065>;tag=3272744142" + m_CRLF +
                    "To: <sip:303@bluesipd>" + m_CRLF +
                    "Call-ID: [email protected]" + m_CRLF +
                    "CSeq: 45560 INVITE" + m_CRLF +
                    "User-Agent: asterisk" + m_CRLF +
                    "Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY" + m_CRLF +
                    "Contact: <sip:[email protected]>" + m_CRLF +
                    "Content-Length: 0" + m_CRLF;

                SIPMessage sipMessage = SIPMessage.ParseSIPMessage(Encoding.UTF8.GetBytes(sipMsg), null, null);

                Assert.IsTrue(sipMessage != null, "The SIP message not parsed correctly.");

                Console.WriteLine("-----------------------------------------");
            }
Exemplo n.º 17
0
            public void ParseOptionsResponse()
            {
                Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

                string sipMsg =
                    "SIP/2.0 200 OK" + m_CRLF +
                    "Via: SIP/2.0/UDP 194.213.29.11:5060;branch=z9hG4bK330f55c874" + m_CRLF +
                    "From: Anonymous <sip:194.213.29.11:5060>;tag=6859154930" + m_CRLF +
                    "To: <sip:[email protected]:10062>;tag=0013c339acec0fe007b80bbf-30071da3" + m_CRLF +
                    "Call-ID: [email protected]" + m_CRLF +
                    "Date: Mon, 01 May 2006 13:47:24 GMT" + m_CRLF +
                    "CSeq: 915 OPTIONS" + m_CRLF +
                    "Server: CSCO/7" + m_CRLF +
                    "Content-Type: application/sdp" + m_CRLF +
                    "Content-Length: 247" + m_CRLF +
                    "Allow: OPTIONS,INVITE,BYE,CANCEL,REGISTER,ACK,NOTIFY,REFER" + m_CRLF +
                    m_CRLF +
                    "v=0" + m_CRLF +
                    "o=Cisco-SIPUA (null) (null) IN IP4 192.168.1.100" + m_CRLF +
                    "s=SIP Call" + m_CRLF +
                    "c=IN IP4 192.168.1.100" + m_CRLF +
                    "t=0 0" + m_CRLF +
                    "m=audio 1 RTP/AVP 0 8 18 101" + m_CRLF +
                    "a=rtpmap:0 PCMU/8000" + m_CRLF +
                    "a=rtpmap:8 PCMA/8000" + m_CRLF +
                    "a=rtpmap:18 G729/8000" + m_CRLF +
                    "a=rtpmap:101 telephone-event/8000" + m_CRLF +
                    "a=fmtp:101 0-15" + m_CRLF + m_CRLF;

                SIPMessage  sipMessage  = SIPMessage.ParseSIPMessage(Encoding.UTF8.GetBytes(sipMsg), null, null);
                SIPResponse optionsResp = SIPResponse.ParseSIPResponse(sipMessage);

                Assert.IsTrue(optionsResp.Status == SIPResponseStatusCodesEnum.Ok, "The SIP response status was not parsed correctly.");

                Console.WriteLine("-----------------------------------------");
            }
Exemplo n.º 18
0
            public void ParseCiscoOptionsResponseUnitTest()
            {
                Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

                string sipMsg =
                    "SIP/2.0 200 OK" + m_CRLF +
                    "Via: SIP/2.0/UDP 213.168.225.133:5060;branch=z9hG4bK7ae332e73550dbdf2f159061651e7ed5bb88ac52, SIP/2.0/UDP 194.213.29.52:5064;branch=z9hG4bK1121681627" + m_CRLF +
                    "From: <sip:[email protected]:5064>;tag=8341482660" + m_CRLF +
                    "To: <sip:[email protected]:5060>;tag=000e38e46c60ef28651381fe-201e6ab1" + m_CRLF +
                    "Call-ID: [email protected]" + m_CRLF +
                    "Date: Wed, 29 Nov 2006 22:31:58 GMT" + m_CRLF +
                    "CSeq: 148 OPTIONS" + m_CRLF +
                    "Server: CSCO/7" + m_CRLF +
                    "Content-Type: application/sdp" + m_CRLF +
                    "Allow: OPTIONS,INVITE,BYE,CANCEL,REGISTER,ACK,NOTIFY,REFER" + m_CRLF +
                    "Content-Length: 193" + m_CRLF +
                    m_CRLF +
                    "v=0" + m_CRLF +
                    "o=Cisco-SIPUA (null) (null) IN IP4 87.198.196.121" + m_CRLF +
                    "s=SIP Call" + m_CRLF +
                    "c=IN IP4 87.198.196.121" + m_CRLF +
                    "t=0 0" + m_CRLF +
                    "m=audio 1 RTP/AVP 18 0 8" + m_CRLF +
                    "a=rtpmap:18 G729/8000" + m_CRLF +
                    "a=rtpmap:0 PCMU/8000" + m_CRLF +
                    "a=rtpmap:8 PCMA/8000" + m_CRLF +
                    m_CRLF;

                SIPMessage  sipMessage  = SIPMessage.ParseSIPMessage(Encoding.UTF8.GetBytes(sipMsg), null, null);
                SIPResponse sipResponse = SIPResponse.ParseSIPResponse(sipMessage);

                Assert.IsTrue(sipMessage != null, "The SIP message not parsed correctly.");
                Assert.IsTrue(sipResponse.Header.Vias.Length == 2, "The SIP reponse did not end up with the right number of Via headers.");

                Console.WriteLine("-----------------------------------------");
            }
Exemplo n.º 19
0
        public static SIPMessage ParseSIPMessage(string message, SIPEndPoint localSIPEndPoint, SIPEndPoint remoteSIPEndPoint)
        {
            try
            {
                SIPMessage sipMessage = new SIPMessage();
                sipMessage.LocalSIPEndPoint = localSIPEndPoint;
                sipMessage.RemoteSIPEndPoint = remoteSIPEndPoint;

                sipMessage.RawMessage = message;
                int endFistLinePosn = message.IndexOf(m_CRLF);

                if (endFistLinePosn != -1)
                {
                    sipMessage.FirstLine = message.Substring(0, endFistLinePosn);

                    if (sipMessage.FirstLine.Substring(0, 3) == SIP_RESPONSE_PREFIX)
                    {
                        sipMessage.SIPMessageType = SIPMessageTypesEnum.Response;
                    }
                    else
                    {
                        sipMessage.SIPMessageType = SIPMessageTypesEnum.Request;
                    }

                    int endHeaderPosn = message.IndexOf(m_CRLF + m_CRLF);
                    if (endHeaderPosn == -1)
                    {
                        // Assume flakey implementation if message does not contain the required CRLFCRLF sequence and treat the message as having no body.
                        string headerString = message.Substring(endFistLinePosn + 2, message.Length - endFistLinePosn - 2);
                        sipMessage.SIPHeaders = SIPHeader.SplitHeaders(headerString); //Regex.Split(headerString, m_CRLF);
                    }
                    else
                    {
                        string headerString = message.Substring(endFistLinePosn + 2, endHeaderPosn - endFistLinePosn - 2);
                        sipMessage.SIPHeaders = SIPHeader.SplitHeaders(headerString); //Regex.Split(headerString, m_CRLF);

                        if (message.Length > endHeaderPosn + 4)
                        {
                            sipMessage.Body = message.Substring(endHeaderPosn + 4);
                        }
                    }

                    return sipMessage;
                }
                else
                {
                    logger.Warn("Error ParseSIPMessage, there were no end of line characters in the string being parsed.");
                    return null;
                }
            }
            catch(Exception excp)
            {
                logger.Error("Exception ParseSIPMessage. " + excp.Message + "\nSIP Message=" + message + ".");
                return null;
            }
        }
Exemplo n.º 20
0
        public static SIPResponse ParseSIPResponse(SIPMessage sipMessage)
        {
            try
            {
                SIPResponse sipResponse = new SIPResponse();
                sipResponse.LocalSIPEndPoint = sipMessage.LocalSIPEndPoint;
                sipResponse.RemoteSIPEndPoint = sipMessage.RemoteSIPEndPoint;
                string statusLine = sipMessage.FirstLine;

                int firstSpacePosn = statusLine.IndexOf(" ");

                sipResponse.SIPVersion = statusLine.Substring(0, firstSpacePosn).Trim();
                statusLine = statusLine.Substring(firstSpacePosn).Trim();
                sipResponse.StatusCode = Convert.ToInt32(statusLine.Substring(0, 3));
                sipResponse.Status = SIPResponseStatusCodes.GetStatusTypeForCode(sipResponse.StatusCode);
                sipResponse.ReasonPhrase = statusLine.Substring(3).Trim();

                sipResponse.Header = SIPHeader.ParseSIPHeaders(sipMessage.SIPHeaders);
                sipResponse.Body = sipMessage.Body;

                return sipResponse;
            }
            catch (SIPValidationException)
            {
                throw;
            }
            catch (Exception excp)
            {
                logger.Error("Exception ParseSIPResponse. " + excp.Message);
                logger.Error(sipMessage.RawMessage);
                throw new SIPValidationException(SIPValidationFieldsEnum.Response, "Error parsing SIP Response");
            }
        }
Exemplo n.º 21
0
		public static SIPRequest ParseSIPRequest(SIPMessage sipMessage)
		{
            string uriStr = null;

            try
            {
                SIPRequest sipRequest = new SIPRequest();
                sipRequest.LocalSIPEndPoint = sipMessage.LocalSIPEndPoint;
                sipRequest.RemoteSIPEndPoint = sipMessage.RemoteSIPEndPoint;

                string statusLine = sipMessage.FirstLine;

                int firstSpacePosn = statusLine.IndexOf(" ");

                string method = statusLine.Substring(0, firstSpacePosn).Trim();
                sipRequest.Method = SIPMethods.GetMethod(method);
                if (sipRequest.Method == SIPMethodsEnum.UNKNOWN)
                {
                    sipRequest.UnknownMethod = method;
                    logger.Warn("Unknown SIP method received " + sipRequest.UnknownMethod + ".");
                }

                statusLine = statusLine.Substring(firstSpacePosn).Trim();
                int secondSpacePosn = statusLine.IndexOf(" ");

                if (secondSpacePosn != -1)
                {
                    uriStr = statusLine.Substring(0, secondSpacePosn);

                    sipRequest.URI = SIPURI.ParseSIPURI(uriStr);
                    sipRequest.SIPVersion = statusLine.Substring(secondSpacePosn, statusLine.Length - secondSpacePosn).Trim();
                    sipRequest.Header = SIPHeader.ParseSIPHeaders(sipMessage.SIPHeaders);
                    sipRequest.Body = sipMessage.Body;

                    return sipRequest;
                }
                else
                {
                    throw new SIPValidationException(SIPValidationFieldsEnum.Request, "URI was missing on Request.");
                }
            }
            catch (SIPValidationException)
            {
                throw;
            }
            catch (Exception excp)
            {
                logger.Error("Exception parsing SIP Request. " + excp.Message);
                logger.Error(sipMessage.RawMessage);
                throw new SIPValidationException(SIPValidationFieldsEnum.Request, "Unknown error parsing SIP Request");
            }
		}