/// <summary> /// 恢复录像播放 /// </summary> /// <param name="localIp">本地ip</param> /// <param name="mediaPort">端口号</param> /// <param name="fromTag">from tag</param> /// <param name="cSeq">序号</param> /// <param name="callId">呼叫编号</param> /// <returns></returns> public SIPRequest BackVideoContinuePlayingControlReq(string localIp, int[] mediaPort, string fromTag, int cSeq, string callId) { SIPURI remoteUri = new SIPURI(_deviceId, _msgCore.RemoteEndPoint.ToHost(), ""); SIPURI localUri = new SIPURI(_msgCore.LocalSIPId, _msgCore.LocalEndPoint.ToHost(), ""); SIPFromHeader from = new SIPFromHeader(null, localUri, fromTag); SIPToHeader to = new SIPToHeader(null, remoteUri, null); SIPRequest backReq = _msgCore.Transport.GetRequest(SIPMethodsEnum.INFO, remoteUri); SIPContactHeader contactHeader = new SIPContactHeader(null, localUri); backReq.Header.Contact.Clear(); backReq.Header.Contact.Add(contactHeader); backReq.Header.Allow = null; backReq.Header.From = from; backReq.Header.To = to; backReq.Header.UserAgent = _msgCore.UserAgent; backReq.Header.CSeq = cSeq; backReq.Header.CallId = callId; backReq.Header.Subject = SetSubject(); backReq.Header.ContentType = "application/mansrtsp"; backReq.Body = SetMediaPauseReq(); _realReqSession = backReq; return(backReq); }
/// <summary> /// 结束录像点播视频请求 /// </summary> /// <returns></returns> public void BackVideoStopPlayingControlReq() { if (_realReqSession == null) { OnSIPServiceChange(_deviceName + "-" + _deviceId, SipServiceStatus.Wait); return; } SIPURI localUri = new SIPURI(_msgCore.LocalSIPId, _msgCore.LocalEndPoint.ToHost(), ""); SIPURI remoteUri = new SIPURI(_deviceId, _msgCore.RemoteEndPoint.ToHost(), ""); SIPFromHeader from = new SIPFromHeader(null, localUri, _realReqSession.Header.From.FromTag); SIPToHeader to = new SIPToHeader(null, remoteUri, _realReqSession.Header.To.ToTag); SIPRequest byeReq = _msgCore.Transport.GetRequest(SIPMethodsEnum.BYE, remoteUri); SIPHeader header = new SIPHeader(from, to, _realReqSession.Header.CSeq, _realReqSession.Header.CallId); header.CSeqMethod = byeReq.Header.CSeqMethod; header.Vias = byeReq.Header.Vias; header.MaxForwards = byeReq.Header.MaxForwards; header.UserAgent = _msgCore.UserAgent; byeReq.Header.From = from; byeReq.Header = header; this.Stop(); _msgCore.Transport.SendRequest(_msgCore.RemoteEndPoint, byeReq); _byeTask = new TaskTiming(byeReq, _msgCore.Transport); _msgCore.SendRequestTimeout += _byeTask.MessageSendRequestTimeout; _byeTask.OnCloseRTPChannel += Task_OnCloseRTPChannel; _byeTask.Start(); }
/// <summary> /// 查询设备目录请求 /// </summary> /// <returns></returns> private SIPRequest QueryItems(SIPEndPoint remoteEndPoint, string remoteSIPId) { string fromTag = CallProperties.CreateNewTag(); string toTag = CallProperties.CreateNewTag(); int cSeq = CallProperties.CreateNewCSeq(); string callId = CallProperties.CreateNewCallId(); SIPURI remoteUri = new SIPURI(RemoteSIPId, RemoteEndPoint.ToHost(), ""); SIPURI localUri = new SIPURI(LocalSIPId, LocalEndPoint.ToHost(), ""); SIPFromHeader from = new SIPFromHeader(null, localUri, fromTag); //SIPToHeader to = new SIPToHeader(null, remoteUri, toTag); SIPToHeader to = new SIPToHeader(null, remoteUri, null); SIPRequest catalogReq = Transport.GetRequest(SIPMethodsEnum.MESSAGE, remoteUri); catalogReq.Header.From = from; catalogReq.Header.Contact = null; catalogReq.Header.Allow = null; catalogReq.Header.To = to; catalogReq.Header.UserAgent = UserAgent; catalogReq.Header.CSeq = cSeq; catalogReq.Header.CallId = callId; catalogReq.Header.ContentType = "application/MANSCDP+xml"; return(catalogReq); }
/// <summary> /// 录像点播请求 /// </summary> /// <param name="localIp">本地ip</param> /// <param name="mediaPort">端口号</param> /// <param name="startTime">开始时间</param> /// <param name="stopTime">结束时间</param> /// <param name="fromTag">from tag</param> /// <param name="cSeq">序号</param> /// <param name="callId">呼叫编号</param> /// <returns></returns> private SIPRequest BackVideoReq(int[] mediaPort, uint startTime, uint stopTime) { string localIp = _msgCore.LocalEndPoint.Address.ToString(); string fromTag = CallProperties.CreateNewTag(); int cSeq = CallProperties.CreateNewCSeq(); string callId = CallProperties.CreateNewCallId(); SIPURI remoteUri = new SIPURI(_deviceId, _remoteEndPoint.ToHost(), ""); SIPURI localUri = new SIPURI(_msgCore.LocalSIPId, _msgCore.LocalEndPoint.ToHost(), ""); SIPFromHeader from = new SIPFromHeader(null, localUri, fromTag); SIPToHeader to = new SIPToHeader(null, remoteUri, null); SIPRequest backReq = _msgCore.Transport.GetRequest(SIPMethodsEnum.INVITE, remoteUri); SIPContactHeader contactHeader = new SIPContactHeader(null, localUri); backReq.Header.Contact.Clear(); backReq.Header.Contact.Add(contactHeader); backReq.Header.Allow = null; backReq.Header.From = from; backReq.Header.To = to; backReq.Header.UserAgent = _msgCore.UserAgent; backReq.Header.CSeq = cSeq; backReq.Header.CallId = callId; backReq.Header.Subject = SetSubject(); backReq.Header.ContentType = "application/sdp"; backReq.Body = SetMediaReq(localIp, mediaPort, startTime, stopTime); _realReqSession = backReq; return(backReq); }
private SIPResponse GetResponse(SIPEndPoint localSIPEndPoint, SIPEndPoint remoteEndPoint, SIPResponseStatusCodesEnum responseCode, string reasonPhrase, SIPRequest request) { try { SIPResponse response = new SIPResponse(responseCode, reasonPhrase, localSIPEndPoint); SIPSchemesEnum sipScheme = (localSIPEndPoint.Protocol == SIPProtocolsEnum.tls) ? SIPSchemesEnum.sips : SIPSchemesEnum.sip; SIPFromHeader from = request.Header.From; from.FromTag = request.Header.From.FromTag; SIPToHeader to = request.Header.To; response.Header = new SIPHeader(from, to, request.Header.CSeq, request.Header.CallId) { CSeqMethod = request.Header.CSeqMethod, Vias = request.Header.Vias, UserAgent = SIPConstants.SIP_USERAGENT_STRING, CSeq = request.Header.CSeq }; if (response.Header.To.ToTag == null || request.Header.To.ToTag.Trim().Length == 0) { response.Header.To.ToTag = CallProperties.CreateNewTag(); } return(response); } catch (Exception excp) { logger.Error("Exception SIPTransport GetResponse. " + excp.Message); throw; } }
/// <summary> /// 确认接收视频请求 /// </summary> /// <param name="response">响应消息</param> /// <returns></returns> public void AckRequest(SIPResponse response) { _rtpChannel = new RTPChannel(_remoteEndPoint.GetIPEndPoint(), _mediaPort[0], _mediaPort[1], FrameTypesEnum.H264); _rtpChannel.OnFrameReady += _rtpChannel_OnFrameReady; _rtpChannel.Start(); SIPURI localUri = new SIPURI(_msgCore.LocalSIPId, _msgCore.LocalEndPoint.ToHost(), ""); SIPURI remoteUri = new SIPURI(_deviceId, _remoteEndPoint.ToHost(), ""); SIPRequest ackReq = _msgCore.Transport.GetRequest(SIPMethodsEnum.ACK, remoteUri); SIPFromHeader from = new SIPFromHeader(null, response.Header.From.FromURI, response.Header.From.FromTag); SIPToHeader to = new SIPToHeader(null, remoteUri, response.Header.To.ToTag); SIPContactHeader contactHeader = new SIPContactHeader(null, localUri); SIPHeader header = new SIPHeader(from, to, response.Header.CSeq, response.Header.CallId); header.CSeqMethod = SIPMethodsEnum.ACK; header.Contact = response.Header.Contact; header.Contact.Clear(); header.Contact.Add(contactHeader); header.Vias = response.Header.Vias; header.MaxForwards = response.Header.MaxForwards; header.ContentLength = response.Header.ContentLength; header.UserAgent = _msgCore.UserAgent; header.Allow = null; ackReq.Header = header; _okTag = response.Header.To.ToTag; _contact = header.Contact.FirstOrDefault(); _via = header.Vias; _msgCore.Transport.SendRequest(_remoteEndPoint, ackReq); }
/// <summary> /// Used to create a SIP response for a request when it was not possible to parse the incoming SIP request. /// The response generated by this method may or may not make it back to the requester. Because the SIP /// request could not be parsed there are no Via headers available and without those the return network /// path is missing. Instead a new Via header is generated that may get through if the requester is only /// one SIP hop away. /// </summary> /// <param name="localSIPEndPoint">The local SIP end point the request was received on.</param> /// <param name="remoteSIPEndPoint">The remote SIP end point the request was received on.</param> /// <param name="responseCode">The response code to set on the response.</param> /// <param name="reasonPhrase">Optional reason phrase to set on the response (keep short).</param> public static SIPResponse GetResponse(SIPEndPoint localSIPEndPoint, SIPEndPoint remoteSIPEndPoint, SIPResponseStatusCodesEnum responseCode, string reasonPhrase) { try { SIPResponse response = new SIPResponse(responseCode, reasonPhrase); response.SetSendFromHints(localSIPEndPoint); SIPSchemesEnum sipScheme = (localSIPEndPoint.Protocol == SIPProtocolsEnum.tls) ? SIPSchemesEnum.sips : SIPSchemesEnum.sip; SIPFromHeader from = new SIPFromHeader(null, new SIPURI(sipScheme, localSIPEndPoint), null); SIPToHeader to = new SIPToHeader(null, new SIPURI(sipScheme, localSIPEndPoint), null); int cSeq = 1; string callId = CallProperties.CreateNewCallId(); response.Header = new SIPHeader(from, to, cSeq, callId); response.Header.CSeqMethod = SIPMethodsEnum.NONE; response.Header.Vias.PushViaHeader(new SIPViaHeader(new SIPEndPoint(localSIPEndPoint.Protocol, remoteSIPEndPoint.GetIPEndPoint()), CallProperties.CreateBranchId())); response.Header.MaxForwards = Int32.MinValue; response.Header.Allow = m_allowedSIPMethods; return(response); } catch (Exception excp) { logger.LogError("Exception SIPResponse.GetResponse. " + excp.Message); throw; } }
private SIPRequest GetRequest(SIPMethodsEnum method) { try { SIPURI uri = SIPURI.ParseSIPURIRelaxed(m_callDescriptor.Uri); SIPRequest request = new SIPRequest(method, uri); SIPFromHeader fromHeader = m_callDescriptor.GetFromHeader(); fromHeader.FromTag = CallProperties.CreateNewTag(); SIPToHeader toHeader = new SIPToHeader(null, uri, null); int cseq = Crypto.GetRandomInt(10000, 20000); SIPHeader header = new SIPHeader(fromHeader, toHeader, cseq, CallProperties.CreateNewCallId()); header.CSeqMethod = method; header.UserAgent = m_userAgent; request.Header = header; header.Vias.PushViaHeader(SIPViaHeader.GetDefaultSIPViaHeader()); try { if (m_callDescriptor.CustomHeaders != null && m_callDescriptor.CustomHeaders.Count > 0) { foreach (string customHeader in m_callDescriptor.CustomHeaders) { if (customHeader.IsNullOrBlank()) { continue; } else if (customHeader.Trim().StartsWith(SIPHeaders.SIP_HEADER_USERAGENT)) { request.Header.UserAgent = customHeader.Substring(customHeader.IndexOf(":") + 1).Trim(); } else { request.Header.UnknownHeaders.Add(customHeader); } } } } catch (Exception excp) { logger.LogError("Exception Parsing CustomHeader for SIPNonInviteClientUserAgent GetRequest. " + excp.Message + m_callDescriptor.CustomHeaders); } if (!m_callDescriptor.Content.IsNullOrBlank()) { request.Body = m_callDescriptor.Content; request.Header.ContentType = m_callDescriptor.ContentType; request.Header.ContentLength = request.Body.Length; } return(request); } catch (Exception excp) { logger.LogError("Exception SIPNonInviteClientUserAgent GetRequest. " + excp.Message); throw; } }
public SIPRequest GetSIPRequest(SIPMethodsEnum sipMethod, string requestURIStr, string fromURIStr, int cseq, string callId, string contentType, string body) { SIPURI requestURI = (requestURIStr.StartsWith("sip:")) ? SIPURI.ParseSIPURI(requestURIStr) : SIPURI.ParseSIPURI("sip:" + requestURIStr); SIPURI fromURI = (fromURIStr.StartsWith("sip:")) ? SIPURI.ParseSIPURI(fromURIStr) : SIPURI.ParseSIPURI("sip:" + fromURIStr); SIPFromHeader fromHeader = new SIPFromHeader(null, fromURI, CallProperties.CreateNewTag()); SIPToHeader toHeader = new SIPToHeader(null, requestURI, null); SIPRequest sipRequest = new SIPRequest(sipMethod, requestURI); IPEndPoint localSIPEndPoint = m_sipTransport.GetIPEndPointsList()[0]; SIPHeader sipHeader = new SIPHeader(fromHeader, toHeader, cseq, callId); sipHeader.Contact = SIPContactHeader.ParseContactHeader("sip:" + localSIPEndPoint.ToString()); sipHeader.CSeqMethod = sipMethod; sipRequest.Header = sipHeader; SIPViaHeader viaHeader = new SIPViaHeader(localSIPEndPoint.Address.ToString(), localSIPEndPoint.Port, CallProperties.CreateBranchId()); sipRequest.Header.Via.PushViaHeader(viaHeader); if (body != null && body.Trim().Length > 0) { sipRequest.Body = body; //sipRequest.Body = "Signal=5\r\nDuration=250"; //sipRequest.Body = "<rtcp>blah blah blah</rtcp>"; sipRequest.Header.ContentLength = sipRequest.Body.Length; sipRequest.Header.ContentType = contentType; } return(sipRequest); }
/// <summary> /// Builds the REFER request to transfer an established call. /// </summary> /// <param name="sipDialogue">A SIP dialogue object representing the established call.</param> /// <param name="referToUri">The SIP URI to transfer the call to.</param> /// <returns>A SIP REFER request.</returns> private static SIPRequest GetReferRequest(SIPClientUserAgent uac, SIPURI referToUri) { SIPDialogue sipDialogue = uac.SIPDialogue; SIPRequest referRequest = new SIPRequest(SIPMethodsEnum.REFER, sipDialogue.RemoteTarget); referRequest.SetSendFromHints(uac.ServerTransaction.TransactionRequest.LocalSIPEndPoint); SIPFromHeader referFromHeader = SIPFromHeader.ParseFromHeader(sipDialogue.LocalUserField.ToString()); SIPToHeader referToHeader = SIPToHeader.ParseToHeader(sipDialogue.RemoteUserField.ToString()); int cseq = sipDialogue.CSeq + 1; sipDialogue.CSeq++; SIPHeader referHeader = new SIPHeader(referFromHeader, referToHeader, cseq, sipDialogue.CallId); referHeader.CSeqMethod = SIPMethodsEnum.REFER; referRequest.Header = referHeader; referRequest.Header.Routes = sipDialogue.RouteSet; referRequest.Header.ProxySendFrom = sipDialogue.ProxySendFrom; referRequest.Header.Vias.PushViaHeader(SIPViaHeader.GetDefaultSIPViaHeader()); referRequest.Header.ReferTo = referToUri.ToString(); referRequest.Header.Contact = new List <SIPContactHeader>() { SIPContactHeader.GetDefaultSIPContactHeader() }; return(referRequest); }
/// <summary> /// Builds the REFER request to transfer an established call. /// </summary> /// <param name="sipDialogue">A SIP dialogue object representing the established call.</param> /// <param name="referToUri">The SIP URI to transfer the call to.</param> /// <returns>A SIP REFER request.</returns> private static SIPRequest GetReferRequest(SIPClientUserAgent uac, SIPURI referToUri) { SIPDialogue sipDialogue = uac.SIPDialogue; SIPRequest referRequest = new SIPRequest(SIPMethodsEnum.REFER, sipDialogue.RemoteTarget); SIPFromHeader referFromHeader = SIPFromHeader.ParseFromHeader(sipDialogue.LocalUserField.ToString()); SIPToHeader referToHeader = SIPToHeader.ParseToHeader(sipDialogue.RemoteUserField.ToString()); int cseq = sipDialogue.CSeq + 1; sipDialogue.CSeq++; SIPHeader referHeader = new SIPHeader(referFromHeader, referToHeader, cseq, sipDialogue.CallId); referHeader.CSeqMethod = SIPMethodsEnum.REFER; referRequest.Header = referHeader; referRequest.Header.Routes = sipDialogue.RouteSet; referRequest.Header.ProxySendFrom = sipDialogue.ProxySendFrom; SIPViaHeader viaHeader = new SIPViaHeader(uac.ServerTransaction.LocalSIPEndPoint, CallProperties.CreateBranchId()); referRequest.Header.Vias.PushViaHeader(viaHeader); referRequest.Header.ReferTo = referToUri.ToString(); referRequest.Header.Contact = new List <SIPContactHeader>() { new SIPContactHeader(null, uac.ServerTransaction.TransactionRequest.Header.Contact.First().ContactURI) }; referRequest.RemoteSIPEndPoint = uac.ServerTransaction.RemoteEndPoint; return(referRequest); }
public void ByeVideoReq() { if (_audioChannel == null) { return; } _audioChannel.Stop(); _audioChannel = null; SIPURI localUri = new SIPURI(_account.SIPPassword, LocalEP.ToHost(), ""); SIPURI remoteUri = new SIPURI(_ackRequest.Header.From.FromURI.User, _byeRemoteEP.ToHost(), ""); SIPFromHeader from = new SIPFromHeader(null, localUri, _ackRequest.Header.To.ToTag); SIPToHeader to = new SIPToHeader(null, remoteUri, _ackRequest.Header.From.FromTag); SIPRequest byeReq = new SIPRequest(SIPMethodsEnum.BYE, localUri); SIPHeader header = new SIPHeader(from, to, _ackRequest.Header.CSeq + 1, _ackRequest.Header.CallId); header.CSeqMethod = SIPMethodsEnum.BYE; SIPViaHeader viaHeader = new SIPViaHeader(LocalEP, CallProperties.CreateBranchId()); viaHeader.Branch = CallProperties.CreateBranchId(); viaHeader.Transport = SIPProtocolsEnum.udp; SIPViaSet viaSet = new SIPViaSet(); viaSet.Via = new List <SIPViaHeader>(); viaSet.Via.Add(viaHeader); header.Vias = viaSet; header.UserAgent = SIPConstants.SIP_USERAGENT_STRING; header.Contact = _ackRequest.Header.Contact; byeReq.Header = header; SendRequest(_byeRemoteEP, byeReq); }
/// <summary> /// 目录订阅请求 /// </summary> /// <returns></returns> private SIPRequest SubscribeCatalog(SIPEndPoint remoteEndPoint, string remoteSIPId) { string fromTag = CallProperties.CreateNewTag(); int cSeq = CallProperties.CreateNewCSeq(); string callId = CallProperties.CreateNewCallId(); SIPURI remoteUri = new SIPURI(remoteSIPId, remoteEndPoint.ToHost(), ""); SIPURI localUri = new SIPURI(LocalSIPId, LocalEP.ToHost(), ""); SIPFromHeader from = new SIPFromHeader(null, localUri, fromTag); SIPToHeader to = new SIPToHeader(null, remoteUri, null); SIPRequest catalogReq = _transport.GetRequest(SIPMethodsEnum.SUBSCRIBE, remoteUri); catalogReq.Header.From = from; catalogReq.Header.Contact = new List <SIPContactHeader> { new SIPContactHeader(null, localUri) }; catalogReq.Header.Allow = null; catalogReq.Header.To = to; catalogReq.Header.UserAgent = SIPConstants.SIP_USERAGENT_STRING; catalogReq.Header.Event = "Catalog";//"presence";//"Catalog;id=1894"; catalogReq.Header.Expires = 60000; catalogReq.Header.CSeq = cSeq; catalogReq.Header.CallId = callId; catalogReq.Header.ContentType = "application/MANSCDP+xml";//"Application/MANSCDP+xml" return(catalogReq); }
public void ParseToHeaderTest() { Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); string testToHeader = "User <sip:[email protected]>;tag=abcdef"; SIPToHeader sipToHeader = SIPToHeader.ParseToHeader(testToHeader); Assert.IsTrue(sipToHeader.ToName == "User", "The To header name was not parsed correctly."); Assert.IsTrue(sipToHeader.ToURI.ToString() == "sip:[email protected]", "The To header URI was not parsed correctly."); Assert.IsTrue(sipToHeader.ToTag == "abcdef", "The To header Tag was not parsed correctly."); }
/// <summary> /// Builds a very basic SIP request. In most cases additional headers will need to be added in order for it to be useful. /// When this method is called the channel used for sending the request has not been decided. The headers below depend on /// the sending channel. By setting them to "0.0.0.0:0" the send request methods will substitute in the appropriate value /// at send time: /// - Top Via header. /// - From header. /// - Contact header. /// </summary> /// <param name="method">The method for the SIP request.</param> /// <param name="uri">The destination URI for the request.</param> /// <param name="to">The To header for the request.</param> /// <param name="from">The From header for the request.</param> /// <returns>A SIP request object.</returns> public static SIPRequest GetRequest(SIPMethodsEnum method, SIPURI uri, SIPToHeader to, SIPFromHeader from) { SIPRequest request = new SIPRequest(method, uri); SIPHeader header = new SIPHeader(from, to, 1, CallProperties.CreateNewCallId()); request.Header = header; header.CSeqMethod = method; header.Allow = m_allowedSIPMethods; header.Vias.PushViaHeader(SIPViaHeader.GetDefaultSIPViaHeader()); return(request); }
private void SendNotifyRequestForSubscription(SIPEventSubscription subscription) { try { subscription.SubscriptionDialogue.CSeq++; //logger.Debug(DateTime.Now.ToString("HH:mm:ss:fff") + " Sending NOTIFY request for " + subscription.SubscriptionDialogue.Owner + " event " + subscription.SubscriptionEventPackage.ToString() // + " and " + subscription.ResourceURI.ToString() + " to " + subscription.SubscriptionDialogue.RemoteTarget.ToString() + ", cseq=" + (subscription.SubscriptionDialogue.CSeq) + "."); int secondsRemaining = Convert.ToInt32(subscription.LastSubscribe.AddSeconds(subscription.Expiry).Subtract(DateTime.Now).TotalSeconds % Int32.MaxValue); SIPRequest notifyRequest = m_sipTransport.GetRequest(SIPMethodsEnum.NOTIFY, subscription.SubscriptionDialogue.RemoteTarget); notifyRequest.Header.From = SIPFromHeader.ParseFromHeader(subscription.SubscriptionDialogue.LocalUserField.ToString()); notifyRequest.Header.To = SIPToHeader.ParseToHeader(subscription.SubscriptionDialogue.RemoteUserField.ToString()); notifyRequest.Header.Event = subscription.SubscriptionEventPackage.ToString(); notifyRequest.Header.CSeq = subscription.SubscriptionDialogue.CSeq; notifyRequest.Header.CallId = subscription.SubscriptionDialogue.CallId; notifyRequest.Body = subscription.GetNotifyBody(); notifyRequest.Header.ContentLength = notifyRequest.Body.Length; notifyRequest.Header.SubscriptionState = "active;expires=" + secondsRemaining.ToString(); notifyRequest.Header.ContentType = subscription.NotifyContentType; notifyRequest.Header.ProxySendFrom = subscription.SubscriptionDialogue.ProxySendFrom; // If the outbound proxy is a loopback address, as it will normally be for local deployments, then it cannot be overriden. SIPEndPoint dstEndPoint = m_outboundProxy; if (m_outboundProxy != null && IPAddress.IsLoopback(m_outboundProxy.Address)) { dstEndPoint = m_outboundProxy; } else if (subscription.SubscriptionDialogue.ProxySendFrom != null) { // The proxy will always be listening on UDP port 5060 for requests from internal servers. dstEndPoint = new SIPEndPoint(SIPProtocolsEnum.udp, new IPEndPoint(SIPEndPoint.ParseSIPEndPoint(subscription.SubscriptionDialogue.ProxySendFrom).Address, m_defaultSIPPort)); } SIPNonInviteTransaction notifyTransaction = m_sipTransport.CreateNonInviteTransaction(notifyRequest, dstEndPoint, m_sipTransport.GetDefaultSIPEndPoint(dstEndPoint), m_outboundProxy); notifyTransaction.NonInviteTransactionFinalResponseReceived += (local, remote, transaction, response) => { NotifyTransactionFinalResponseReceived(local, remote, transaction, response, subscription); }; m_sipTransport.SendSIPReliable(notifyTransaction); //logger.Debug(notifyRequest.ToString()); MonitorLogEvent_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.NotifySent, "Notification sent for " + subscription.SubscriptionEventPackage.ToString() + " and " + subscription.ResourceURI.ToString() + " to " + subscription.SubscriptionDialogue.RemoteTarget.ToString() + " (cseq=" + notifyRequest.Header.CSeq + ").", subscription.SubscriptionDialogue.Owner)); subscription.NotificationSent(); } catch (Exception excp) { logger.Error("Exception SendNotifyRequestForSubscription. " + excp.Message); throw; } }
public void ParseMSCToHeaderTest() { Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); string testToHeader = "sip:[email protected];tag=AZHf2-ZMfDX0"; SIPToHeader sipToHeader = SIPToHeader.ParseToHeader(testToHeader); Console.WriteLine("To header: " + sipToHeader.ToString()); Assert.IsTrue(sipToHeader.ToName == null, "The To header name was not parsed correctly."); Assert.IsTrue(sipToHeader.ToURI.ToString() == "sip:[email protected]", "The To header URI was not parsed correctly."); Assert.IsTrue(sipToHeader.ToTag == "AZHf2-ZMfDX0", "The To header Tag was not parsed correctly."); }
public void ParseByeToHeader() { Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); string testHeader = "\"Joe Bloggs\" <sip:[email protected]>;tag=0013c339acec34652d988c7e-4fddcdef"; SIPToHeader sipToHeader = SIPToHeader.ParseToHeader(testHeader); Console.WriteLine("To header: " + sipToHeader.ToString()); Assert.IsTrue(sipToHeader.ToName == "Joe Bloggs", "The To header name was not parsed correctly."); Assert.IsTrue(sipToHeader.ToURI.ToString() == "sip:[email protected]", "The To header URI was not parsed correctly."); Assert.IsTrue(sipToHeader.ToTag == "0013c339acec34652d988c7e-4fddcdef", "The To header Tag was not parsed correctly."); }
public void ToStringToHeaderTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); string testToHeader = "User <sip:[email protected]>;tag=abcdef"; SIPToHeader sipToHeader = SIPToHeader.ParseToHeader(testToHeader); Assert.True(sipToHeader.ToName == "User", "The To header name was not parsed correctly."); Assert.True(sipToHeader.ToURI.ToString() == "sip:[email protected]", "The To header URI was not parsed correctly."); Assert.True(sipToHeader.ToTag == "abcdef", "The To header Tag was not parsed correctly."); Assert.True(sipToHeader.ToString() == "\"User\" <sip:[email protected]>;tag=abcdef", "The To header was not put ToString correctly."); }
/// <summary> /// Helper method to create a SIP response for a SIP request. This method can be thought of as creating a /// vanilla (or no frills) response for a request. It's suitable for generating error responses. For requests that /// require an action such as creating a call or registering a contact the response will require additional /// information and this method will not be suitable. /// </summary> /// <param name="sipRequest">The SIP request to create the response for.</param> /// <param name="responseCode">The response code.</param> /// <param name="reasonPhrase">Optional reason phrase to set on the response (needs to be short).</param> /// <returns>A SIP response object.</returns> public static SIPResponse GetResponse(SIPRequest sipRequest, SIPResponseStatusCodesEnum responseCode, string reasonPhrase) { try { SIPResponse response = new SIPResponse(responseCode, reasonPhrase); response.SetSendFromHints(sipRequest.LocalSIPEndPoint); if (reasonPhrase != null) { response.ReasonPhrase = reasonPhrase; } SIPHeader requestHeader = sipRequest.Header; SIPFromHeader from = (requestHeader == null || requestHeader.From != null) ? requestHeader.From : new SIPFromHeader(null, new SIPURI(sipRequest.URI.Scheme, sipRequest.LocalSIPEndPoint), null); SIPToHeader to = (requestHeader == null || requestHeader.To != null) ? requestHeader.To : new SIPToHeader(null, new SIPURI(sipRequest.URI.Scheme, sipRequest.LocalSIPEndPoint), null); int cSeq = (requestHeader == null || requestHeader.CSeq != -1) ? requestHeader.CSeq : 1; string callId = (requestHeader == null || requestHeader.CallId != null) ? requestHeader.CallId : CallProperties.CreateNewCallId(); response.Header = new SIPHeader(from, to, cSeq, callId); response.Header.CSeqMethod = (requestHeader != null) ? requestHeader.CSeqMethod : SIPMethodsEnum.NONE; if (requestHeader == null || requestHeader.Vias == null || requestHeader.Vias.Length == 0) { response.Header.Vias.PushViaHeader(new SIPViaHeader(sipRequest.RemoteSIPEndPoint, CallProperties.CreateBranchId())); } else { response.Header.Vias = requestHeader.Vias; } response.Header.MaxForwards = Int32.MinValue; response.Header.Allow = m_allowedSIPMethods; return(response); } catch (Exception excp) { logger.LogError("Exception SIPResponse.GetResponse. " + excp.Message); throw excp; } }
public SIPRequest GetInviteRequest(string inviteURIStr, string fromURIStr, string body, int rtpPort) { SIPURI inviteURI = (inviteURIStr.StartsWith("sip:")) ? SIPURI.ParseSIPURI(inviteURIStr) : SIPURI.ParseSIPURI("sip:" + inviteURIStr); SIPFromHeader fromHeader = SIPFromHeader.ParseFromHeader(fromURIStr); // (fromURIStr.StartsWith("sip:")) ? SIPFromHeader.ParseFromHeader(fromURIStr) : SIPFromHeader.ParseFromHeader("sip:" + fromURIStr); SIPToHeader toHeader = new SIPToHeader(null, inviteURI, null); SIPRequest inviteRequest = new SIPRequest(SIPMethodsEnum.INVITE, inviteURI); IPEndPoint localSIPEndPoint = m_sipTransport.GetIPEndPointsList()[0]; SIPHeader inviteHeader = new SIPHeader(fromHeader, toHeader, 1, CallProperties.CreateNewCallId()); inviteHeader.From.FromTag = CallProperties.CreateNewTag(); inviteHeader.Contact = SIPContactHeader.ParseContactHeader("sip:" + localSIPEndPoint.ToString()); inviteHeader.CSeqMethod = SIPMethodsEnum.INVITE; //inviteHeader.UnknownHeaders.Add("BlueFace-Test: 12324"); inviteRequest.Header = inviteHeader; SIPViaHeader viaHeader = new SIPViaHeader(localSIPEndPoint.Address.ToString(), localSIPEndPoint.Port, CallProperties.CreateBranchId()); inviteRequest.Header.Via.PushViaHeader(viaHeader); rtpPort = (rtpPort != 0) ? rtpPort : Crypto.GetRandomInt(10000, 20000); string sessionId = Crypto.GetRandomInt(1000, 5000).ToString(); if (body != null && body.Trim().Length > 0) { inviteRequest.Body = body; } else { inviteRequest.Body = "v=0" + CRLF + "o=- " + sessionId + " " + sessionId + " IN IP4 " + localSIPEndPoint.Address.ToString() + CRLF + "s=session" + CRLF + "c=IN IP4 " + localSIPEndPoint.Address.ToString() + CRLF + "t=0 0" + CRLF + "m=audio " + rtpPort + " RTP/AVP 0 101" + CRLF + "a=rtpmap:0 PCMU/8000" + CRLF + "a=rtpmap:101 telephone-event/8000" + CRLF + "a=fmtp:101 0-16" + CRLF + "a=sendrecv"; } inviteRequest.Header.ContentLength = inviteRequest.Body.Length; inviteRequest.Header.ContentType = "application/sdp"; return(inviteRequest); }
/// <summary> /// 查询设备目录请求 /// </summary> /// <returns></returns> private SIPRequest QueryItems() { SIPURI remoteUri = new SIPURI(_sipAccount.RemoteSipId, _remoteEndPoint.ToHost(), ""); SIPURI localUri = new SIPURI(_sipAccount.LocalSipId, _localEndPoint.ToHost(), ""); SIPFromHeader from = new SIPFromHeader(null, localUri, CallProperties.CreateNewTag()); SIPToHeader to = new SIPToHeader(null, remoteUri, null); SIPRequest queryReq = _m_sipTransport.GetRequest(SIPMethodsEnum.DO, remoteUri); queryReq.Header.From = from; queryReq.Header.Contact = null; queryReq.Header.Allow = null; queryReq.Header.To = to; queryReq.Header.CSeq = CallProperties.CreateNewCSeq(); queryReq.Header.CallId = CallProperties.CreateNewCallId(); queryReq.Header.ContentType = "Application/DDCP"; return(queryReq); }
private SIPRequest GetByeRequest() { SIPRequest byeRequest = new SIPRequest(SIPMethodsEnum.BYE, SIPDialogue.RemoteTarget); SIPFromHeader byeFromHeader = SIPFromHeader.ParseFromHeader(SIPDialogue.LocalUserField.ToString()); SIPToHeader byeToHeader = SIPToHeader.ParseToHeader(SIPDialogue.RemoteUserField.ToString()); int cseq = SIPDialogue.CSeq + 1; SIPHeader byeHeader = new SIPHeader(byeFromHeader, byeToHeader, cseq, SIPDialogue.CallId); byeHeader.CSeqMethod = SIPMethodsEnum.BYE; byeRequest.Header = byeHeader; byeRequest.Header.Routes = SIPDialogue.RouteSet; byeRequest.Header.ProxySendFrom = SIPDialogue.ProxySendFrom; byeRequest.Header.Vias.PushViaHeader(SIPViaHeader.GetDefaultSIPViaHeader()); return(byeRequest); }
/// <summary> /// 查询设备目录请求 /// </summary> /// <returns></returns> private SIPRequest PTZRequest(string fromTag, string toTag, int cSeq, string callId) { SIPURI remoteUri = new SIPURI(_deviceId, _remoteEndPoint.ToHost(), ""); SIPURI localUri = new SIPURI(_msgCore.LocalSIPId, _msgCore.LocalEndPoint.ToHost(), ""); SIPFromHeader from = new SIPFromHeader(null, localUri, fromTag); SIPToHeader to = new SIPToHeader(null, remoteUri, toTag); SIPRequest catalogReq = _msgCore.Transport.GetRequest(SIPMethodsEnum.MESSAGE, remoteUri); catalogReq.Header.From = from; catalogReq.Header.Contact = null; catalogReq.Header.Allow = null; catalogReq.Header.To = to; catalogReq.Header.UserAgent = _msgCore.UserAgent; catalogReq.Header.CSeq = cSeq; catalogReq.Header.CallId = callId; catalogReq.Header.ContentType = "application/MANSCDP+xml"; return(catalogReq); }
/// <summary> /// Builds a basic SIP request with the header fields set to correctly identify it as an /// in dialog request. Calling this method also increments the dialog's local CSeq counter. /// This is safe to do even if the request does not end up being sent. /// </summary> /// <param name="method">The method of the SIP request to create.</param> /// <returns>An in dialog SIP request.</returns> public SIPRequest GetInDialogRequest(SIPMethodsEnum method) { CSeq++; SIPRequest inDialogRequest = new SIPRequest(method, RemoteTarget); SIPFromHeader fromHeader = SIPFromHeader.ParseFromHeader(LocalUserField.ToString()); SIPToHeader toHeader = SIPToHeader.ParseToHeader(RemoteUserField.ToString()); int cseq = CSeq; SIPHeader header = new SIPHeader(fromHeader, toHeader, cseq, CallId); header.CSeqMethod = method; inDialogRequest.Header = header; inDialogRequest.Header.Routes = RouteSet; inDialogRequest.Header.ProxySendFrom = ProxySendFrom; inDialogRequest.Header.Vias.PushViaHeader(SIPViaHeader.GetDefaultSIPViaHeader()); return(inDialogRequest); }
public void ChangeTagToHeaderTest() { Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); string testToHeader = "User <sip:[email protected]>;tag=abcdef"; SIPToHeader sipToHeader = SIPToHeader.ParseToHeader(testToHeader); string newTag = "123456"; sipToHeader.ToTag = newTag; Console.WriteLine("To header with new tag: " + sipToHeader.ToString()); Assert.IsTrue(sipToHeader.ToName == "User", "The To header name was not parsed correctly."); Assert.IsTrue(sipToHeader.ToURI.ToString() == "sip:[email protected]", "The To header URI was not parsed correctly."); Assert.IsTrue(sipToHeader.ToTag == newTag, "The To header Tag was not parsed correctly."); Assert.IsTrue(sipToHeader.ToString() == "\"User\" <sip:[email protected]>;tag=123456", "The To header was not put ToString correctly."); }
private SIPRequest GetByeRequest(SIPResponse inviteResponse, SIPURI byeURI) { SIPRequest byeRequest = new SIPRequest(SIPMethodsEnum.BYE, byeURI); byeRequest.SetSendFromHints(inviteResponse.LocalSIPEndPoint); SIPFromHeader byeFromHeader = inviteResponse.Header.From; SIPToHeader byeToHeader = inviteResponse.Header.To; int cseq = inviteResponse.Header.CSeq + 1; SIPHeader byeHeader = new SIPHeader(byeFromHeader, byeToHeader, cseq, inviteResponse.Header.CallId); byeHeader.CSeqMethod = SIPMethodsEnum.BYE; byeHeader.ProxySendFrom = m_serverTransaction.TransactionRequest.Header.ProxySendFrom; byeRequest.Header = byeHeader; byeRequest.Header.Routes = (inviteResponse.Header.RecordRoutes != null) ? inviteResponse.Header.RecordRoutes.Reversed() : null; byeRequest.Header.Vias.PushViaHeader(SIPViaHeader.GetDefaultSIPViaHeader(null)); return(byeRequest); }
private SIPRequest GetByeRequest(SIPEndPoint localSIPEndPoint) { SIPRequest byeRequest = new SIPRequest(SIPMethodsEnum.BYE, m_sipDialogue.RemoteTarget); SIPFromHeader byeFromHeader = SIPFromHeader.ParseFromHeader(m_sipDialogue.LocalUserField.ToString()); SIPToHeader byeToHeader = SIPToHeader.ParseToHeader(m_sipDialogue.RemoteUserField.ToString()); int cseq = m_sipDialogue.CSeq + 1; SIPHeader byeHeader = new SIPHeader(byeFromHeader, byeToHeader, cseq, m_sipDialogue.CallId); byeHeader.CSeqMethod = SIPMethodsEnum.BYE; byeRequest.Header = byeHeader; byeRequest.Header.Routes = m_sipDialogue.RouteSet; byeRequest.Header.ProxySendFrom = m_sipDialogue.ProxySendFrom; SIPViaHeader viaHeader = new SIPViaHeader(localSIPEndPoint, CallProperties.CreateBranchId()); byeRequest.Header.Vias.PushViaHeader(viaHeader); return(byeRequest); }
/// <summary> /// 向上级平台注册 /// </summary> private void RegisterToPlatform() { string fromTag = CallProperties.CreateNewTag(); string callId = CallProperties.CreateNewCallId(); int cSeq = CallProperties.CreateNewCSeq(); string branch = CallProperties.CreateBranchId(); SIPURI localUri = new SIPURI(LocalSIPId, LocalEndPoint.ToHost(), ""); SIPURI remoteUri = new SIPURI(RemoteSIPId, RemoteEndPoint.ToHost(), ""); SIPRequest registerReq = new SIPRequest(SIPMethodsEnum.REGISTER, remoteUri); SIPViaHeader via = new SIPViaHeader(LocalEndPoint, branch); SIPViaSet viaSet = new SIPViaSet(); viaSet.PushViaHeader(via); SIPFromHeader from = new SIPFromHeader(null, localUri, fromTag); SIPToHeader to = new SIPToHeader(null, localUri, null); SIPHeader header = new SIPHeader(from, to, cSeq, callId); registerReq.Header = header; SIPContactHeader contact = new SIPContactHeader(null, localUri); header.Contact = new List <SIPContactHeader>(); header.Contact.Add(contact); header.Vias = viaSet; header.AuthenticationHeader = _auth; header.Expires = 3600; header.CSeqMethod = SIPMethodsEnum.REGISTER; header.MaxForwards = 70; header.ContentLength = 0; header.UserAgent = SIPConstants.SIP_SERVER_STRING; Transport.SendRequest(RemoteEndPoint, registerReq); }
private SIPRequest GetOptionsRequest(SIPURI serverURI, int cseq, IPEndPoint contact) { SIPRequest optionsRequest = new SIPRequest(SIPMethodsEnum.OPTIONS, serverURI); SIPFromHeader fromHeader = new SIPFromHeader(null, SIPURI.ParseSIPURI("sip:" + contact.ToString()), null); SIPToHeader toHeader = new SIPToHeader(null, serverURI, null); string callId = CallProperties.CreateNewCallId(); string branchId = CallProperties.CreateBranchId(); SIPHeader header = new SIPHeader(fromHeader, toHeader, cseq, callId); header.CSeqMethod = SIPMethodsEnum.OPTIONS; header.MaxForwards = 0; SIPViaHeader viaHeader = new SIPViaHeader(contact.Address.ToString(), contact.Port, branchId); header.Vias.PushViaHeader(viaHeader); optionsRequest.Header = header; return(optionsRequest); }
public SIPRequest GetInviteRequest(string inviteURIStr, string fromURIStr, string body, int rtpPort) { SIPURI inviteURI = (inviteURIStr.StartsWith("sip:")) ? SIPURI.ParseSIPURI(inviteURIStr) : SIPURI.ParseSIPURI("sip:" + inviteURIStr); SIPFromHeader fromHeader = SIPFromHeader.ParseFromHeader(fromURIStr); // (fromURIStr.StartsWith("sip:")) ? SIPFromHeader.ParseFromHeader(fromURIStr) : SIPFromHeader.ParseFromHeader("sip:" + fromURIStr); SIPToHeader toHeader = new SIPToHeader(null, inviteURI, null); SIPRequest inviteRequest = new SIPRequest(SIPMethodsEnum.INVITE, inviteURI); IPEndPoint localSIPEndPoint = m_sipTransport.GetIPEndPointsList()[0]; SIPHeader inviteHeader = new SIPHeader(fromHeader, toHeader, 1, CallProperties.CreateNewCallId()); inviteHeader.From.FromTag = CallProperties.CreateNewTag(); inviteHeader.Contact = SIPContactHeader.ParseContactHeader("sip:" + localSIPEndPoint.ToString()); inviteHeader.CSeqMethod = SIPMethodsEnum.INVITE; //inviteHeader.UnknownHeaders.Add("BlueFace-Test: 12324"); inviteRequest.Header = inviteHeader; SIPViaHeader viaHeader = new SIPViaHeader(localSIPEndPoint.Address.ToString(), localSIPEndPoint.Port, CallProperties.CreateBranchId()); inviteRequest.Header.Via.PushViaHeader(viaHeader); rtpPort = (rtpPort != 0) ? rtpPort : Crypto.GetRandomInt(10000, 20000); string sessionId = Crypto.GetRandomInt(1000, 5000).ToString(); if (body != null && body.Trim().Length > 0) { inviteRequest.Body = body; } else { inviteRequest.Body = "v=0" + CRLF + "o=- " + sessionId + " " + sessionId + " IN IP4 " + localSIPEndPoint.Address.ToString() + CRLF + "s=session" + CRLF + "c=IN IP4 " + localSIPEndPoint.Address.ToString() + CRLF + "t=0 0" + CRLF + "m=audio " + rtpPort + " RTP/AVP 0 101" + CRLF + "a=rtpmap:0 PCMU/8000" + CRLF + "a=rtpmap:101 telephone-event/8000" + CRLF + "a=fmtp:101 0-16" + CRLF + "a=sendrecv"; } inviteRequest.Header.ContentLength = inviteRequest.Body.Length; inviteRequest.Header.ContentType = "application/sdp"; return inviteRequest; }
public SIPRequest GetSIPRequest(SIPMethodsEnum sipMethod, string requestURIStr, string fromURIStr, int cseq, string callId, string contentType, string body) { SIPURI requestURI = (requestURIStr.StartsWith("sip:")) ? SIPURI.ParseSIPURI(requestURIStr) : SIPURI.ParseSIPURI("sip:" + requestURIStr); SIPURI fromURI = (fromURIStr.StartsWith("sip:")) ? SIPURI.ParseSIPURI(fromURIStr) : SIPURI.ParseSIPURI("sip:" + fromURIStr); SIPFromHeader fromHeader = new SIPFromHeader(null, fromURI, CallProperties.CreateNewTag()); SIPToHeader toHeader = new SIPToHeader(null, requestURI, null); SIPRequest sipRequest = new SIPRequest(sipMethod, requestURI); IPEndPoint localSIPEndPoint = m_sipTransport.GetIPEndPointsList()[0]; SIPHeader sipHeader = new SIPHeader(fromHeader, toHeader, cseq, callId); sipHeader.Contact = SIPContactHeader.ParseContactHeader("sip:" + localSIPEndPoint.ToString()); sipHeader.CSeqMethod = sipMethod; sipRequest.Header = sipHeader; SIPViaHeader viaHeader = new SIPViaHeader(localSIPEndPoint.Address.ToString(), localSIPEndPoint.Port, CallProperties.CreateBranchId()); sipRequest.Header.Via.PushViaHeader(viaHeader); if (body != null && body.Trim().Length > 0) { sipRequest.Body = body; //sipRequest.Body = "Signal=5\r\nDuration=250"; //sipRequest.Body = "<rtcp>blah blah blah</rtcp>"; sipRequest.Header.ContentLength = sipRequest.Body.Length; sipRequest.Header.ContentType = contentType; } return sipRequest; }