Warm tip: This article is reproduced from serverfault.com, please click

Sip parser in c#

发布于 2013-10-13 13:51:59

I am looking for a library or class in c# that can parse sip packets. I need functions that will help me get the Call-ID field from the packet, types of requests, and basically breakdown the sip packet to its fields.

Does anybody know something that can help me? Thanks, ofek

Questioner
Ofek Agmon
Viewed
0
sipsorcery 2013-10-17 05:11:17

This class from my sipsorcery project can do it for you.

Update: If you have a string that contains a full SIP packet you can parse the full thing by using:

var req = SIPSorcery.SIP.SIPRequest.ParseSIPRequest(reqStr); 
var headers = req.Header;

var resp = SIPSorcery.SIP.SIPResponse.ParseSIPResponse(respStr);  
var headers = resp.Header;

If you don't know whether the SIP packet is a request or a response you can use the SIPMessage class:

var mess = SIPSorcery.SIP.SIPMessage.ParseSIPMessage(messStr, null, null);
var headers = SIPSorcery.SIP.SIPHeader.ParseSIPHeaders(mess.SIPHeaders);        

Update 2:

Given you're using pcap.net to capture the SIP packets you are probably ending up with a block of bytes rather than a string. You can use the SIPMessage class to parse the SIP packet from a UDP payload:

var mess = SIPSorcery.SIP.SIPMessage.ParseSIPMessage(packet.Ethernet.IPv4datagram.Udp.Payload, null, null);
var headers = SIPSorcery.SIP.SIPHeader.ParseSIPHeaders(mess.SIPHeaders);