使用SIP-Sorcery C#SUBSCRIBE响应

时间:2014-01-20 12:43:24

标签: c# sip

我正在尝试使用来自codeplex的sip巫术,在C#SIP客户端项目中,向我的服务器发送和接收请求和响应。当我使用SIPUDPChannel.Send('remoteEP','msg')时,我可以看到SUBSCRIBE数据包被发送到我的服务器,而我的服务器正在回复状态响应。

但是如何使用SIP Sorcery API ???

捕获响应/请求事件

1 个答案:

答案 0 :(得分:1)

在回答第二个问题时,您在评论中发布了第二个SUBSCRIBE请求为对话内(或更准确地在交易中)的原因;不是。

但是,因为您正在重新使用从第一个SUBSCRIBE请求的响应中获得的SIP标头,所以最终会设置一些标头,使其看起来好像是现有事务的一部分。 / p>

正确的方法是重新使用原始SIP请求中的标头并增加CSeq并重新生成CallID。下面的代码行(注意我没有编译它来检查语法错误)。

    static SIPTransport Transport;
    static SIPMessage msg;
    static string inputMsg;
    static SIPUDPChannel channel;
    static SIPRequest subscribeRequest;

    static void Main(string[] args)
    {
        InitializeSIP();
        Console.Read();
    }

    static void InitializeSIP()
    {
        //ResolveIPEndPoint
        SIPEndPoint sipep = new SIPEndPoint(new IPEndPoint(IPAddress.Parse("192.168.102.12"), 5060));
        IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("192.168.102.12"), 5060);

        //Set Transport
        Transport = new SIPTransport(SIPDNSManager.ResolveSIPService, new SIPTransactionEngine());

        //IPEndPoint
        IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("192.168.100.10"), 8080);

        //Create Channel object
        channel = new SIPUDPChannel(localEndPoint);
        Transport.AddSIPChannel(channel);

        //Wire transport with incoming requests
        Transport.SIPTransportRequestReceived += new SIPTransportRequestDelegate(Transport_SIPTransportRequestReceived);

        //Wire transport with incoming responses
        Transport.SIPTransportResponseReceived += new SIPTransportResponseDelegate(Transport_SIPTransportResponseReceived);

        inputMsg = "SUBSCRIBE sip:myUSer@192.168.102.12 SIP/2.0" + SIPConstants.CRLF +
        "Via: SIP/2.0/UDP 192.168.100.10:8080;rport;branch=z9hG4bKFBB7EAC06934405182D13950BD51F001" + SIPConstants.CRLF +
        "From: <sip:subscribeUser@192.168.102.12>;tag=196468136" + SIPConstants.CRLF +
        "To: <sip:myUser@192.168.102.12>" + SIPConstants.CRLF +
        "Contact: <sip:subscribeUser@>" + SIPConstants.CRLF +
        "Call-ID: 1337505490-453410046-705466123" + SIPConstants.CRLF +
        "CSeq: 1 SUBSCRIBE" + SIPConstants.CRLF +
        "Max-Forwards: 70" + SIPConstants.CRLF +
        "Event: Presence" + SIPConstants.CRLF +
        "Content-Length: 0";

        subscribeRequest = SIPRequest.ParseSIPRequest(inputMsg);

        channel.Send(remoteEP, subscribeRequest.ToString());
    }

    static void Transport_SIPTransportResponseReceived(SIPEndPoint localSIPEndPoint, SIPEndPoint remoteEndPoint, SIPResponse sipResponse)
    {
        Console.WriteLine("Response method: " + sipResponse.Header.CSeqMethod);
        if(sipResponse.StatusCode == 401 && sipResponse.Header.CSeqMethod == SIPMethodsEnum.SUBSCRIBE)
        {
                            //Resubscribe with Digist
            //SIP Header
            SIPHeader header = subscribeRequest.Header;
            header.CSeq++;
            header.CallID = "some_new_callID";
            header.AuthenticationHeader = sipResponse.Header.AuthenticationHeader;
            header.Expires = 120;

            //New Request
            SIPRequest request = new SIPRequest(SIPMethodsEnum.SUBSCRIBE, new SIPURI(SIPSchemesEnum.sip, remoteEndPoint));
            request.LocalSIPEndPoint = localSIPEndPoint;
            request.RemoteSIPEndPoint = remoteEndPoint;
            request.Header = header;

            //Send request
            channel.Send(remoteEndPoint.GetIPEndPoint(), request.ToString());
        }
        else
            Console.WriteLine(string.Format("Error {0} {1}.", sipResponse.StatusCode, sipResponse.Status));
    }

    static void Transport_SIPTransportRequestReceived(SIPEndPoint localSIPEndPoint, SIPEndPoint remoteEndPoint, SIPRequest sipRequest)
    {
        Console.WriteLine("Request body: " + sipRequest.Body);
    }

<强>更新

您可以向SIP请求添加凭据,如下面的代码所示。请注意,身份验证标头基于每个请求而不是每个传输或每个通道。在sipsorcery代码中,SIPTransport可以具有多个SIPChannel,SIPChannel可以与多个SIP端点通信,因此在传输或通道上设置凭据是没有意义的。

SIPAuthorisationDigest authDigest = sipResponse.Header.AuthenticationHeader.SIPDigest;
authDigest.SetCredentials(username, password, uri, SIPMethodsEnum.INVITE.ToString());
authRequest.Header.AuthenticationHeader = new SIPAuthenticationHeader(authDigest);
authRequest.Header.AuthenticationHeader.SIPDigest.Response = authDigest.Digest;

更新2:

向SIP请求发送简单响应的最简单方法如下。

static void Transport_SIPTransportRequestReceived(SIPEndPoint localSIPEndPoint, SIPEndPoint remoteEndPoint, SIPRequest sipRequest)
{
    SIPResponse response = SIPTransport.GetResponse(sipRequest, SIPResponseStatusCodesEnum.Ok, null);
    _sipTransport.SendResponse(response);
}