public void GetStringIndexSIPInviteUnitTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); string sipMsg = "INVITE sip:[email protected]:5060;TCID-0 SIP/2.0\r\n" + "From: UNAVAILABLE<sip:[email protected]:5060>;tag=c0a83dfe-13c4-26bf01-975a21d0-2d8a\r\n" + "To: <sip:[email protected]:5060>\r\n" + "Call-ID: [email protected]\r\n" + "CSeq: 1 INVITE\r\n" + "Via: SIP/2.0/UDP 86.9.84.23:5060;branch=z9hG4bK-26bf01-975a21d0-1ffb\r\n" + "Max-Forwards: 70\r\n" + "User-Agent: TA612V-V1.2_54\r\n" + "Supported: timer,replaces\r\n" + "Contact: <sip:[email protected]:5060>\r\n" + "Content-Type: application/SDP\r\n" + "Content-Length: 386\r\n" + "\r\n" + "v=0\r\n" + "o=b0000 613 888 IN IP4 88.8.88.88\r\n" + "s=SIP Call\r\n" + "c=IN IP4 88.8.88.88\r\n" + "t=0 0\r\n" + "m=audio 10000 RTP/AVP 0 101 18 100 101 2 103 8\r\n" + "a=fmtp:101 0-15\r\n" + "a=fmtp:18 annexb=no\r\n" + "a=sendrecv\r\n" + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:101 telephone-event/8000\r\n" + "a=rtpmap:18 G729/8000\r\n" + "a=rtpmap:100 G726-16/8000\r\n" + "a=rtpmap:101 G726-24/8000\r\n" + "a=rtpmap:2 G726-32/8000\r\n" + "a=rtpmap:103 G726-40/8000\r\n" + "a=rtpmap:8 PCMA/8000"; byte[] sample = Encoding.ASCII.GetBytes(sipMsg); int endOfMsgIndex = ByteBufferInfo.GetStringPosition(sample, 0, Int32.MaxValue, "\r\n\r\n", null); Assert.True(endOfMsgIndex == sipMsg.IndexOf("\r\n\r\n"), "The string position was not correctly found in the buffer. Index found was " + endOfMsgIndex + ", should have been " + sipMsg.IndexOf("\r\n\r\n") + "."); }
/// <summary> /// Processes a buffer from a TCP read operation to extract the first full SIP message. If no full SIP /// messages are available it returns null which indicates the next read should be appended to the current /// buffer and the process re-attempted. /// </summary> /// <param name="receiveBuffer">The buffer to check for the SIP message in.</param> /// <param name="start">The position in the buffer to start parsing for a SIP message.</param> /// <param name="length">The position in the buffer that indicates the end of the received bytes.</param> /// <returns>A byte array holding a full SIP message or if no full SIP messages are available null.</returns> public static byte[] ParseSIPMessageFromStream(byte[] receiveBuffer, int start, int length, out int bytesSkipped) { // NAT keep-alives can be interspersed between SIP messages. Treat any non-letter character // at the start of a receive as a non SIP transmission and skip over it. bytesSkipped = 0; bool letterCharFound = false; while (!letterCharFound && start < length) { if ((int)receiveBuffer[start] >= 65) { break; } else { start++; bytesSkipped++; } } if (start < length) { int endMessageIndex = ByteBufferInfo.GetStringPosition(receiveBuffer, start, length, m_sipMessageDelimiter, null); if (endMessageIndex != -1) { int contentLength = GetContentLength(receiveBuffer, start, endMessageIndex); int messageLength = endMessageIndex - start + m_sipMessageDelimiter.Length + contentLength; if (length - start >= messageLength) { byte[] sipMsgBuffer = new byte[messageLength]; Buffer.BlockCopy(receiveBuffer, start, sipMsgBuffer, 0, messageLength); return(sipMsgBuffer); } } } return(null); }
public void GetStringIndexUnitTest() { Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name); string sipMsg = "REGISTER sip:Blue Face SIP/2.0\r\n" + "Via: SIP/2.0/UDP 127.0.0.1:1720;branch=z9hG4bKlgnUQcaywCOaPcXR\r\n" + "Max-Forwards: 70\r\n" + "User-Agent: PA168S\r\n" + "From: \"user\" <sip:user@Blue Face>;tag=81swjAV7dHG1yjd5\r\n" + "To: \"user\" <sip:user@Blue Face>\r\n" + "Call-ID: [email protected]\r\n" + "CSeq: 15754 REGISTER\r\n" + "Contact: <sip:[email protected]:1720>\r\n" + "Expires: 30\r\n" + "Content-Length: 0\r\n\r\n"; byte[] sample = Encoding.ASCII.GetBytes(sipMsg); int endOfMsgIndex = ByteBufferInfo.GetStringPosition(sample, 0, Int32.MaxValue, "\r\n\r\n", null); Assert.True(endOfMsgIndex == sample.Length - 4, "The string position was not correctly found in the buffer."); }