static void Main() { Console.WriteLine("Example STUN Server"); ConfigureConsoleLog(); // STUN servers need two separater end points to listen on. IPEndPoint primaryEndPoint = new IPEndPoint(IPAddress.Any, 3478); IPEndPoint secondaryEndPoint = new IPEndPoint(IPAddress.Any, 3479); // Create the two STUN listeners and wire up the STUN server. STUNListener primarySTUNListener = new STUNListener(primaryEndPoint); STUNListener secondarySTUNListener = new STUNListener(secondaryEndPoint); STUNServer stunServer = new STUNServer(primaryEndPoint, primarySTUNListener.Send, secondaryEndPoint, secondarySTUNListener.Send); primarySTUNListener.MessageReceived += stunServer.STUNPrimaryReceived; secondarySTUNListener.MessageReceived += stunServer.STUNSecondaryReceived; // Optional. Provides verbose logs of STUN server activity. EnableVerboseLogs(stunServer); Console.WriteLine("STUN server successfully initialised."); Console.Write("press any key to exit..."); Console.Read(); primarySTUNListener.Close(); secondarySTUNListener.Close(); stunServer.Stop(); }
private void StartSTUNServer(IPEndPoint primaryEndPoint, IPEndPoint secondaryEndPoint, SIPTransport sipTransport) { STUNListener secondarySTUNListener = new STUNListener(secondaryEndPoint); // This end point is only for secondary STUN messages. STUNSendMessageDelegate primarySend = (dst, buffer) => { m_sipTransport.SendRaw(m_sipTransport.GetDefaultSIPEndPoint(SIPProtocolsEnum.udp), new SIPEndPoint(dst), buffer); }; m_stunServer = new STUNServer(primaryEndPoint, primarySend, secondaryEndPoint, secondarySTUNListener.Send); sipTransport.STUNRequestReceived += m_stunServer.STUNPrimaryReceived; sipTransport.STUNRequestReceived += LogPrimarySTUNRequestReceived; secondarySTUNListener.MessageReceived += m_stunServer.STUNSecondaryReceived; secondarySTUNListener.MessageReceived += LogSecondarySTUNRequestReceived; logger.Debug("STUN server successfully initialised."); }
/// <summary> /// Logs receives and sends by the STUN server. /// </summary> /// <param name="stunServer">The STUN server to enable verbose logs for.</param> private static void EnableVerboseLogs(STUNServer stunServer) { stunServer.STUNPrimaryRequestInTraceEvent += (localEndPoint, fromEndPoint, stunMessage) => { Log.LogDebug($"pri recv {localEndPoint}<-{fromEndPoint}: {stunMessage.ToString()}"); }; stunServer.STUNSecondaryRequestInTraceEvent += (localEndPoint, fromEndPoint, stunMessage) => { Log.LogDebug($"sec recv {localEndPoint}<-{fromEndPoint}: {stunMessage.ToString()}"); }; stunServer.STUNPrimaryResponseOutTraceEvent += (localEndPoint, fromEndPoint, stunMessage) => { Log.LogDebug($"pri send {localEndPoint}->{fromEndPoint}: {stunMessage.ToString()}"); }; stunServer.STUNSecondaryResponseOutTraceEvent += (localEndPoint, fromEndPoint, stunMessage) => { Log.LogDebug($"sec send {localEndPoint}->{fromEndPoint}: {stunMessage.ToString()}"); }; }