[LCS] an in-depth understanding of sipsnoop-Events

Source: Internet
Author: User

Write by panqi @ ultrapower

Abstract: sipsnoop is a cool application provided by lcs2005 to listen for the SIP messages of response and request.ProgramAlthough it is not as powerful as ethereal, it is also sufficient for general listening. This article will analyze its core methods.

L knowledge point: [mspl] built-in variable siprequest: The siprequest variable contains the SIP request message from the Live Communications Server to the application. [Mspl] built-in variable sipresponse: The sipresponse variable contains the SIP response message from the Live Communications Server to the application. [Mspl] dispatch method: the dispatch method assigns an event to the internal event handler of the application. If the method is successfully forwarded to the specified method, the system returns true; otherwise, the system returns no. [Microsoft. RTC. Sip] requestreceivedeventargs: requestreceivedeventargs class defines information about arriving SIP requests on the application. When a request is successfully allocated by the mspl message filter, an event containing the requestreceivedeventargs object signature will be allocated to the specified method. L have a deep understanding of sipsnoop 1. Ask the question: 1.1 judge the Message Type and assign different events to the application for processing for different message types. 1.2 The application intercepts and processes all response and request messages through LCS. 2. analyze the problem: 2.1 determine the SIP Message Type through the built-in variables siprequest and sipresponse of mspl. Then, use the dispatch method of mspl to allocate different events to the managed service.Code . 2.2 The hosted code application instantiates the response class and resquest class in the event of processing the mspl allocation. Through these two objects, all requests and response messages through LCS can be obtained. 3 solution: there are six SIP request methods: Invite (invitation); ack (confirmation); options (optional); bye (goodbye); cancel (Cancellation ); register (registration); in the core events requesthandler and responsehandler of sipsnoop, the number of SIP requests passed through LCS is calculated based on the hash table. The following is a detailed analysis of these two events. 3.1 intercept and count the request message: 3.1.1 mspl-assigned events, trigger the requesthandler event handler, which has a requestreceivedeventargs object signature, the request attribute of the object signature intercepts the request message currently sent through LCS. The Code is as follows: Public   Void Requesthandler ( Object Sender, requestreceivedeventargs E)
{
Request request
=E. request;
...

3.1.2 determine the request method after obtaining the SIP request message through LCS. Different processing methods are performed for different request methods. The purpose is to count the request message, invite and ACK are processed separately. Other methods use the other method for counting. If it is an invite request method, it indicates that the session was first established. You need to create a session instance and assign the states (Status) attribute of the session to initializing. Next, lock the syncroot attribute of the sessionstatetable hash table to ensure thread security. Then, lock the cal1_header of the sessionstatetable hash table. value assignment. The Code is as follows: If (Request. standardmethod = Request. standardmethodtype. Invite)
{

/**/ ///Extract the call-ID and create session state
Header calender Header = Request. allheaders. findfirst ( " Call-ID " );

If (Calender Header ! =   Null )
{
Session newsession
=   New Session ();
Newsession. State
= Session. States. Initializing;

Lock (Sessionstatetable. syncroot)
{
Sessionstatetable [caltailheader. value]
=Newsession;
}

}

}

If the request method is ack, the request is confirmed. Obtain the call-ID in the header, and find the matched session in the hash table sessionstatetable Based on the obtained call-id, then, convert the object into a session object, and then judge the session object. If the object is not empty, the session state is assigned established, indicating that the session connection is determined and the update method is executed, increment the values of the specified variables totalsessions and activesessions in the form of an atomic operation and store the results. The Code is as follows:

Else   If (Request. standardmethod = Request. standardmethodtype. Ack)
{

/**/ ///Extract the call-ID and update session state, ignore errors
Header calender Header = Request. allheaders. findfirst ( " Call-ID " );

If (Calender Header ! =   Null )
{
Session session
= Sessionstatetable [caltailheader. value] As Session;
If (Session ! =   Null )
{
Session. State
= Session. States. established;
Statistics. Update (
True   /**/ /*New session*/ );
}

}

}

The code for incrementing the specified variable in the update method is as follows:

Public   Void Update ( Bool Sessionestablished)
{
If (Sessionestablished)
{
Interlocked. increment (
Ref Totalsessions );
Interlocked. increment (
Ref Activesessions );
}

Else
{
Interlocked. decrement (
RefActivesessions );
}

}

If there are request methods other than invite and ACK in the request message, count them using one method. The implementation code is as follows:

Statistics. Update (request. standardmethod );

3.1.3 after obtaining the request method, obtain the sender and receiver in the request message header and update them to the usertable hash table. The Code is as follows:Header fromheader=Request. allheaders. findfirst ("From");
Header toheader
=Request. allheaders. findfirst ("To");

Statistics. Update (uriparser. getuserathost (fromheader. Value ));
Statistics. Update (uriparser. getuserathost (toheader. Value ));

Update method: Public   Void Update ( String User)
{
If (User ! =   Null )
{< br> lock (usertable. syncroot)
{< br> usertable [user] = User;
}

}
 
}
 

At this point, the request message acquisition and statistics functions are implemented. 3.2 interception and statistics of response messages: In the sipsnoop Response Message statistics, the hashtable remove operation is performed on the Request Method bye. For other request methods, the hash table is not used and only the count is used; the code implementation process is similar to the request.Write by panqi @ ultrapower2005-6-9

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.