/// <summary> /// Initialises the SIP transport layer. /// </summary> public async Task InitialiseSIP() { if (_isIntialised == false) { await Task.Run(() => { _isIntialised = true; if (String.IsNullOrEmpty(m_DnsServer) == false) { // Use a custom DNS server. m_DnsServer = m_DnsServer.Contains(":") ? m_DnsServer : m_DnsServer + ":53"; DNSManager.SetDNSServers(new List <IPEndPoint> { IPSocket.ParseSocketString(m_DnsServer) }); } // Configure the SIP transport layer. m_sipTransport = new SIPTransport(SIPDNSManager.ResolveSIPService, new SIPTransactionEngine()); bool sipChannelAdded = false; if (m_sipSocketsNode != null) { // Set up the SIP channels based on the app.config file. List <SIPChannel> sipChannels = SIPTransportConfig.ParseSIPChannelsNode(m_sipSocketsNode); if (sipChannels?.Count > 0) { m_sipTransport.AddSIPChannel(sipChannels); sipChannelAdded = true; } } if (sipChannelAdded == false) { // Use default options to set up a SIP channel. int port = FreePort.FindNextAvailableUDPPort(_defaultSIPUdpPort); var sipChannel = new SIPUDPChannel(new IPEndPoint(_defaultLocalAddress, port)); m_sipTransport.AddSIPChannel(sipChannel); } }); // Wire up the transport layer so incoming SIP requests have somewhere to go. m_sipTransport.SIPTransportRequestReceived += SIPTransportRequestReceived; // Log all SIP packets received to a log file. m_sipTransport.SIPRequestInTraceEvent += (localSIPEndPoint, endPoint, sipRequest) => { _sipTraceLogger.Debug("Request Received : " + localSIPEndPoint + "<-" + endPoint + "\r\n" + sipRequest.ToString()); }; m_sipTransport.SIPRequestOutTraceEvent += (localSIPEndPoint, endPoint, sipRequest) => { _sipTraceLogger.Debug("Request Sent: " + localSIPEndPoint + "->" + endPoint + "\r\n" + sipRequest.ToString()); }; m_sipTransport.SIPResponseInTraceEvent += (localSIPEndPoint, endPoint, sipResponse) => { _sipTraceLogger.Debug("Response Received: " + localSIPEndPoint + "<-" + endPoint + "\r\n" + sipResponse.ToString()); }; m_sipTransport.SIPResponseOutTraceEvent += (localSIPEndPoint, endPoint, sipResponse) => { _sipTraceLogger.Debug("Response Sent: " + localSIPEndPoint + "->" + endPoint + "\r\n" + sipResponse.ToString()); }; } }
static void Main(string[] args) { Console.WriteLine("SIPSorcery registration user agent example."); Console.WriteLine("Press ctrl-c to exit."); // Logging configuration. Can be ommitted if internal SIPSorcery debug and warning messages are not required. var loggerFactory = new Microsoft.Extensions.Logging.LoggerFactory(); var loggerConfig = new LoggerConfiguration() .Enrich.FromLogContext() .MinimumLevel.Is(Serilog.Events.LogEventLevel.Debug) .WriteTo.Console() .CreateLogger(); loggerFactory.AddSerilog(loggerConfig); SIPSorcery.Sys.Log.LoggerFactory = loggerFactory; // If your default DNS server supports SRV records there is no need to set a specific DNS server. DNSManager.SetDNSServers(new List <IPEndPoint> { IPEndPoint.Parse("8.8.8.8:53") }); // Set up a default SIP transport. var sipTransport = new SIPTransport(SIPDNSManager.ResolveSIPService, new SIPTransactionEngine()); int port = FreePort.FindNextAvailableUDPPort(SIPConstants.DEFAULT_SIP_PORT); var sipChannel = new SIPUDPChannel(new IPEndPoint(LocalIPConfig.GetDefaultIPv4Address(), port)); sipTransport.AddSIPChannel(sipChannel); // Create a client user agent to maintain a periodic registration with a SIP server. var regUserAgent = new SIPRegistrationUserAgent( sipTransport, "softphonesample", "password", "sipsorcery.com"); // Event handlers for the different stages of the registration. regUserAgent.RegistrationFailed += (uri, err) => SIPSorcery.Sys.Log.Logger.LogError($"{uri.ToString()}: {err}"); regUserAgent.RegistrationTemporaryFailure += (uri, msg) => SIPSorcery.Sys.Log.Logger.LogWarning($"{uri.ToString()}: {msg}"); regUserAgent.RegistrationRemoved += (uri) => SIPSorcery.Sys.Log.Logger.LogError($"{uri.ToString()} registration failed."); regUserAgent.RegistrationSuccessful += (uri) => SIPSorcery.Sys.Log.Logger.LogInformation($"{uri.ToString()} registration succeeded."); // Start the thread to perform the initial registration and then periodically resend it. regUserAgent.Start(); }
/// <summary> /// Initialises the SIP transport layer. /// </summary> public async Task InitialiseSIP() { if (_isInitialised == false) { await Task.Run(() => { _isInitialised = true; if (String.IsNullOrEmpty(m_DnsServer) == false) { // Use a custom DNS server. m_DnsServer = m_DnsServer.Contains(":") ? m_DnsServer : m_DnsServer + ":53"; DNSManager.SetDNSServers(new List <IPEndPoint> { IPSocket.ParseSocketString(m_DnsServer) }); } // Configure the SIP transport layer. SIPTransport = new SIPTransport(); bool sipChannelAdded = false; if (m_sipSocketsNode != null) { // Set up the SIP channels based on the app.config file. List <SIPChannel> sipChannels = SIPTransportConfig.ParseSIPChannelsNode(m_sipSocketsNode); if (sipChannels?.Count > 0) { SIPTransport.AddSIPChannel(sipChannels); sipChannelAdded = true; } } if (sipChannelAdded == false) { // Use default options to set up a SIP channel. SIPUDPChannel udpChannel = null; try { udpChannel = new SIPUDPChannel(new IPEndPoint(IPAddress.Any, SIP_DEFAULT_PORT)); } catch (SocketException bindExcp) { logger.Warn($"Socket exception attempting to bind UDP channel to port {SIP_DEFAULT_PORT}, will use random port. {bindExcp.Message}."); udpChannel = new SIPUDPChannel(new IPEndPoint(IPAddress.Any, 0)); } var tcpChannel = new SIPTCPChannel(new IPEndPoint(IPAddress.Any, udpChannel.Port)); SIPTransport.AddSIPChannel(new List <SIPChannel> { udpChannel, tcpChannel }); } }); // Wire up the transport layer so incoming SIP requests have somewhere to go. SIPTransport.SIPTransportRequestReceived += SIPTransportRequestReceived; // Log all SIP packets received to a log file. SIPTransport.SIPRequestInTraceEvent += SIPRequestInTraceEvent; SIPTransport.SIPRequestOutTraceEvent += SIPRequestOutTraceEvent; SIPTransport.SIPResponseInTraceEvent += SIPResponseInTraceEvent; SIPTransport.SIPResponseOutTraceEvent += SIPResponseOutTraceEvent; } }