public SIPEndPoint ToSIPEndPoint() { if (IPSocket.IsIPSocket(Host) || IPSocket.IsIPAddress(Host)) { if (Host.IndexOf(":") != -1) { return(new SIPEndPoint(Protocol, IPSocket.GetIPEndPoint(Host))); } else { if (Protocol == SIPProtocolsEnum.tls) { return(new SIPEndPoint(Protocol, IPSocket.GetIPEndPoint(Host + ":" + m_defaultSIPTLSPort))); } else { return(new SIPEndPoint(Protocol, IPSocket.GetIPEndPoint(Host + ":" + m_defaultSIPPort))); } } } else { return(null); } }
/// <summary> /// Places an outgoing SIP call. /// </summary> /// <param name="destination">The SIP URI to place a call to. The destination can be a full SIP URI in which case the all will /// be placed anonymously directly to that URI. Alternatively it can be just the user portion of a URI in which case it will /// be sent to the configured SIP server.</param> public void Call(MediaManager mediaManager, string destination) { //_initialisationTask.Wait(_cancelCallTokenSource.Token); _mediaManager = mediaManager; _mediaManager.NewCall(); // Determine if this is a direct anonymous call or whether it should be placed using the pre-configured SIP server account. SIPURI callURI = null; string sipUsername = null; string sipPassword = null; string fromHeader = null; if (destination.Contains("@") || m_sipServer == null) { // Anonymous call direct to SIP server specified in the URI. callURI = SIPURI.ParseSIPURIRelaxed(destination); fromHeader = (new SIPFromHeader(m_sipFromName, SIPURI.ParseSIPURI(SIPFromHeader.DEFAULT_FROM_URI), null)).ToString(); } else { // This call will use the pre-configured SIP account. callURI = SIPURI.ParseSIPURIRelaxed(destination + "@" + m_sipServer); sipUsername = m_sipUsername; sipPassword = m_sipPassword; fromHeader = (new SIPFromHeader(m_sipFromName, new SIPURI(m_sipUsername, m_sipServer, null), null)).ToString(); } StatusMessage("Starting call to " + callURI.ToString() + "."); m_uac = new SIPClientUserAgent(m_sipTransport, null, null, null, null); m_uac.CallTrying += CallTrying; m_uac.CallRinging += CallRinging; m_uac.CallAnswered += CallAnswered; m_uac.CallFailed += CallFailed; // Get the SDP requesting that the public IP address be used if the host on the call destination is not a private IP address. SDP sdp = _mediaManager.GetSDP(!(IPSocket.IsIPAddress(callURI.Host) && IPSocket.IsPrivateAddress(callURI.Host))); System.Diagnostics.Debug.WriteLine(sdp.ToString()); SIPCallDescriptor callDescriptor = new SIPCallDescriptor(sipUsername, sipPassword, callURI.ToString(), fromHeader, null, null, null, null, SIPCallDirection.Out, _sdpMimeContentType, sdp.ToString(), null); m_uac.Call(callDescriptor); }
/// <summary> /// Places an outgoing SIP call. /// </summary> /// <param name="destination">The SIP URI to place a call to. The destination can be a full SIP URI in which case the all will /// be placed anonymously directly to that URI. Alternatively it can be just the user portion of a URI in which case it will /// be sent to the configured SIP server.</param> public void Call(string destination) { // Determine if this is a direct anonymous call or whether it should be placed using the pre-configured SIP server account. SIPURI callURI = null; string sipUsername = null; string sipPassword = null; string fromHeader = null; if (destination.Contains("@") || m_sipServer == null) { // Anonymous call direct to SIP server specified in the URI. callURI = SIPURI.ParseSIPURIRelaxed(destination); } else { // This call will use the pre-configured SIP account. callURI = SIPURI.ParseSIPURIRelaxed(destination + "@" + m_sipServer); sipUsername = m_sipUsername; sipPassword = m_sipPassword; fromHeader = (new SIPFromHeader(m_sipFromName, new SIPURI(m_sipUsername, m_sipServer, null), null)).ToString(); } StatusMessage("Starting call to " + callURI.ToString() + "."); m_uac = new SIPClientUserAgent(m_sipTransport, null, null, null, null); m_uac.CallTrying += CallTrying; m_uac.CallRinging += CallRinging; m_uac.CallAnswered += CallAnswered; m_uac.CallFailed += CallFailed; _audioChannel = new AudioChannel(); // Get the SDP requesting that the public IP address be used if the host on the call destination is not a private IP address. SDP sdp = _audioChannel.GetSDP(!(IPSocket.IsIPAddress(callURI.Host) && IPSocket.IsPrivateAddress(callURI.Host))); SIPCallDescriptor callDescriptor = new SIPCallDescriptor(sipUsername, sipPassword, callURI.ToString(), fromHeader, null, null, null, null, SIPCallDirection.Out, SDP.SDP_MIME_CONTENTTYPE, sdp.ToString(), null); m_uac.Call(callDescriptor); }
public void ParseTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); string host = null; int port = 0; string endpoint = "localhost:5060"; Assert.False(IPSocket.Parse(endpoint, out host, out port), $"'{endpoint}' Valid endpoint address"); Assert.True(host == "localhost", "The host was not parsed correctly."); Assert.True(port == 5060, "The port was not parsed correctly."); Assert.False(IPSocket.IsIPAddress(host), $"'{endpoint}' Valid ip address"); endpoint = "host.domain.tld:5060"; Assert.False(IPSocket.Parse(endpoint, out host, out port), $"'{endpoint}' Valid endpoint address"); Assert.True(host == "host.domain.tld", "The host was not parsed correctly."); Assert.True(port == 5060, "The port was not parsed correctly."); Assert.False(IPSocket.IsIPAddress(host), $"'{endpoint}' Valid ip address"); endpoint = "127.0.0.1:5060"; Assert.True(IPSocket.Parse(endpoint, out host, out port), $"'{endpoint}' Invalid endpoint address"); Assert.True(host == "127.0.0.1", "The host was not parsed correctly."); Assert.True(port == 5060, "The port was not parsed correctly."); Assert.True(IPSocket.IsIPAddress(host), $"'{endpoint}' Invalid ip address"); endpoint = "[::1]:5060"; Assert.True(IPSocket.Parse(endpoint, out host, out port), $"'{endpoint}' Invalid endpoint address"); Assert.True(host == "::1", "The host was not parsed correctly."); Assert.True(port == 5060, "The port was not parsed correctly."); Assert.True(IPSocket.IsIPAddress(host), $"'{endpoint}' Invalid ip address"); endpoint = "[::ffff:127.0.0.1]:5060"; Assert.True(IPSocket.Parse(endpoint, out host, out port), $"'{endpoint}' Invalid endpoint address"); Assert.True(host == "::ffff:127.0.0.1", "The host was not parsed correctly."); Assert.True(port == 5060, "The port was not parsed correctly."); Assert.True(IPSocket.IsIPAddress(host), $"'{endpoint}' Invalid ip address"); endpoint = "localhost"; Assert.False(IPSocket.Parse(endpoint, out host, out port), $"'{endpoint}' Valid endpoint address"); Assert.True(host == "localhost", "The host was not parsed correctly."); endpoint = "host.domain.tld"; Assert.False(IPSocket.Parse(endpoint, out host, out port), $"'{endpoint}' Valid endpoint address"); Assert.True(host == "host.domain.tld", "The host was not parsed correctly."); endpoint = "127.0.0.1"; Assert.True(IPSocket.Parse(endpoint, out host, out port), $"'{endpoint}' Invalid endpoint address"); Assert.True(host == "127.0.0.1", "The host was not parsed correctly."); Assert.True(IPSocket.IsIPAddress(host), $"'{endpoint}' Invalid ip address"); endpoint = "[::1]"; Assert.True(IPSocket.Parse(endpoint, out host, out port), $"'{endpoint}' Invalid endpoint address"); Assert.True(host == "::1", "The host was not parsed correctly."); Assert.True(IPSocket.IsIPAddress(host), $"'{endpoint}' Invalid ip address"); endpoint = "[::ffff:127.0.0.1]"; Assert.True(IPSocket.Parse(endpoint, out host, out port), $"'{endpoint}' Invalid endpoint address"); Assert.True(host == "::ffff:127.0.0.1", "The host was not parsed correctly."); Assert.True(IPSocket.IsIPAddress(host), $"'{endpoint}' Invalid ip address"); endpoint = "::1"; Assert.True(IPSocket.Parse(endpoint, out host, out port), $"'{endpoint}' Invalid endpoint address"); Assert.True(host == "::1", "The host was not parsed correctly."); Assert.True(IPSocket.IsIPAddress(host), $"'{endpoint}' Invalid ip address"); endpoint = "::ffff:127.0.0.1"; Assert.True(IPSocket.Parse(endpoint, out host, out port), $"'{endpoint}' Invalid endpoint address"); Assert.True(host == "::ffff:127.0.0.1", "The host was not parsed correctly."); Assert.True(IPSocket.IsIPAddress(host), $"'{endpoint}' Invalid ip address"); endpoint = "::ffff:127.0.0..1"; Assert.Throws <FormatException>(() => IPSocket.Parse(endpoint, out host, out port)); endpoint = "::ffff:"; Assert.Throws <FormatException>(() => IPSocket.Parse(endpoint, out host, out port)); endpoint = "127.0.0..1"; Assert.False(IPSocket.Parse(endpoint, out host, out port), $"'{endpoint}' Valid endpoint address"); Assert.False(IPSocket.IsIPAddress(host), $"'{endpoint}' Valid ip address"); endpoint = "127.0.0."; Assert.False(IPSocket.Parse(endpoint, out host, out port), $"'{endpoint}' Valid endpoint address"); Assert.False(IPSocket.IsIPAddress(host), $"'{endpoint}' Valid ip address"); endpoint = "328.0.0.1"; Assert.False(IPSocket.Parse(endpoint, out host, out port), $"'{endpoint}' Valid endpoint address"); Assert.False(IPSocket.IsIPAddress(host), $"'{endpoint}' Valid ip address"); endpoint = "\0"; Assert.False(IPSocket.Parse(endpoint, out host, out port), $"'{endpoint}' Valid endpoint address"); Assert.False(IPSocket.IsIPAddress(host), $"'{endpoint}' Valid ip address"); }