public void CreateRtpAndControlSocketsDuplicateStartPortUnitTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); List <Socket> sockets = new List <Socket>(); for (int i = 0; i < 20; i++) { Socket rtpSocket = null; Socket controlSocket = null; NetServices.CreateRtpSocket(49152, 65534, 51277, true, null, out rtpSocket, out controlSocket); Assert.NotNull(rtpSocket); Assert.NotNull(controlSocket); sockets.Add(rtpSocket); sockets.Add(controlSocket); } foreach (var socket in sockets) { socket.Close(); } }
/// <summary> /// Creates a SIP channel to listen for and send SIP messages over UDP. /// </summary> /// <param name="endPoint">The IP end point to listen on and send from.</param> /// <param name="useDualMode">If true then IPv6 sockets will be created as dual mode IPv4/IPv6 on supporting systems.</param> public SIPUDPChannel(IPEndPoint endPoint, bool useDualMode = false) : base() { if (endPoint == null) { throw new ArgumentNullException("endPoint", "The end point must be specified when creating a SIPUDPChannel."); } ListeningIPAddress = endPoint.Address; Port = endPoint.Port; SIPProtocol = SIPProtocolsEnum.udp; IsReliable = false; m_cts = new CancellationTokenSource(); m_udpSocket = NetServices.CreateBoundUdpSocket(endPoint.Port, endPoint.Address, false, useDualMode); if (endPoint.Port == 0) { Port = (m_udpSocket.LocalEndPoint as IPEndPoint).Port; } m_recvBuffer = new byte[SIPConstants.SIP_MAXIMUM_RECEIVE_LENGTH]; logger.LogInformation($"SIP UDP Channel created for {ListeningEndPoint}."); Receive(); Task.Factory.StartNew(ExpireFailedSends, TaskCreationOptions.LongRunning); }
private async Task GetIceCandidatesAsync() { var localIPAddresses = _offerAddresses ?? NetServices.GetAllLocalIPAddresses(); IceNegotiationStartedAt = DateTime.Now; LocalIceCandidates = new List <IceCandidate>(); foreach (var address in localIPAddresses.Where(x => x.AddressFamily == _rtpChannel.RTPLocalEndPoint.AddressFamily)) { var iceCandidate = new IceCandidate(address, _rtpChannel.RTPPort); if (_turnServerEndPoint != null) { iceCandidate.TurnServer = new TurnServer() { ServerEndPoint = _turnServerEndPoint }; iceCandidate.InitialStunBindingCheck = SendTurnServerBindingRequest(iceCandidate); } LocalIceCandidates.Add(iceCandidate); } await Task.WhenAll(LocalIceCandidates.Where(x => x.InitialStunBindingCheck != null).Select(x => x.InitialStunBindingCheck)); }
public void GetLocalForInternetIPv6AdressUnitTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); if (Socket.OSSupportsIPv6) { if (NetServices.LocalIPAddresses.Any(x => x.AddressFamily == AddressFamily.InterNetworkV6 && !x.IsIPv6LinkLocal && !x.IsIPv6SiteLocal && !x.IsIPv6Teredo && !IPAddress.IsLoopback(x))) { var localAddress = NetServices.GetLocalAddressForRemote(IPAddress.Parse("2606:db00:0:62b::2")); Assert.NotNull(localAddress); logger.LogDebug($"Local address {localAddress}."); } else { logger.LogDebug("Test skipped as no public IPv6 address available."); } } else { logger.LogDebug("Test skipped as OS does not support IPv6."); } }
public void GetLocalIPv6AddressUnitTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); if (Socket.OSSupportsIPv6) { var ipv6LinkLocal = NetServices.LocalIPAddresses.Where(x => x.IsIPv6LinkLocal).FirstOrDefault(); if (ipv6LinkLocal == null) { logger.LogDebug("No IPv6 link local address available."); } else { logger.LogDebug($"IPv6 link local address for this host {ipv6LinkLocal}."); var localAddress = NetServices.GetLocalAddressForRemote(ipv6LinkLocal); Assert.NotNull(localAddress); Assert.Equal(ipv6LinkLocal, localAddress); logger.LogDebug($"Local address {localAddress}."); } } else { logger.LogDebug("Test skipped as OS does not support IPv6."); } }
public void CreateRtpAndControlMultipleSocketsUnitTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); List <Socket> sockets = new List <Socket>(); for (int i = 0; i < 20; i++) { Socket rtpSocket = null; Socket controlSocket = null; NetServices.CreateRtpSocket(true, null, out rtpSocket, out controlSocket); Assert.NotNull(rtpSocket); Assert.NotNull(controlSocket); Assert.True((rtpSocket.LocalEndPoint as IPEndPoint).Port % 2 == 0); Assert.False((controlSocket.LocalEndPoint as IPEndPoint).Port % 2 == 0); sockets.Add(rtpSocket); sockets.Add(controlSocket); } foreach (var socket in sockets) { socket.Close(); } }
public void CheckFailsOnDuplicateForIP6AnyThenIPv4AnyUnitTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); if (Socket.OSSupportsIPv6 && NetServices.SupportsDualModeIPv4PacketInfo) { Socket rtpSocket = null; Socket controlSocket = null; NetServices.CreateRtpSocket(true, IPAddress.IPv6Any, out rtpSocket, out controlSocket); Assert.NotNull(rtpSocket); Assert.NotNull(controlSocket); Assert.Throws <ApplicationException>(() => NetServices.CreateBoundUdpSocket((rtpSocket.LocalEndPoint as IPEndPoint).Port, IPAddress.Any)); rtpSocket.Close(); controlSocket.Close(); } else { logger.LogDebug("Test skipped as IPv6 dual mode sockets are not in use on this OS."); } }
/// <summary> /// Attempts to get a list of local ICE candidates. /// </summary> private async Task GetIceCandidatesAsync() { // The media is being multiplexed so the audio and video RTP channel is the same. var rtpChannel = GetRtpChannel(SDPMediaTypesEnum.audio); if (rtpChannel == null) { throw new ApplicationException("Cannot start gathering ICE candidates without an RTP channel."); } else { var localIPAddresses = _offerAddresses ?? NetServices.GetAllLocalIPAddresses(); IceNegotiationStartedAt = DateTime.Now; LocalIceCandidates = new List <IceCandidate>(); foreach (var address in localIPAddresses.Where(x => x.AddressFamily == rtpChannel.RTPLocalEndPoint.AddressFamily)) { var iceCandidate = new IceCandidate(address, rtpChannel.RTPPort); if (_turnServerEndPoint != null) { iceCandidate.TurnServer = new TurnServer() { ServerEndPoint = _turnServerEndPoint }; iceCandidate.InitialStunBindingCheck = SendTurnServerBindingRequest(iceCandidate); } LocalIceCandidates.Add(iceCandidate); } await Task.WhenAll(LocalIceCandidates.Where(x => x.InitialStunBindingCheck != null).Select(x => x.InitialStunBindingCheck)).ConfigureAwait(false); } }
private void GetIceCandidates(ManualResetEvent iceGatheringCompleteMRE) { IceNegotiationStartedAt = DateTime.Now; LocalIceCandidates = new List <IceCandidate>(); var addresses = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetUnicastAddresses() .Where(x => x.Address.AddressFamily == AddressFamily.InterNetwork && // Exclude IPv6 at this stage. IPAddress.IsLoopback(x.Address) == false && (x.Address != null && x.Address.ToString().StartsWith(AUTOMATIC_PRIVATE_ADRRESS_PREFIX) == false)); foreach (var address in addresses) { logger.Debug("Attempting to create RTP socket with IP address " + address.Address + "."); Socket rtpSocket = null; Socket controlSocket = null; NetServices.CreateRtpSocket(address.Address, WEBRTC_START_PORT, WEBRTC_END_PORT, false, out rtpSocket, out controlSocket); if (rtpSocket != null) { logger.Debug("RTP socket successfully created on " + rtpSocket.LocalEndPoint + "."); var iceCandidate = new IceCandidate() { LocalAddress = address.Address, Port = ((IPEndPoint)rtpSocket.LocalEndPoint).Port, LocalRtpSocket = rtpSocket, LocalControlSocket = controlSocket, TurnServer = (_turnServerEndPoint != null) ? new TurnServer() { ServerEndPoint = _turnServerEndPoint } : null }; LocalIceCandidates.Add(iceCandidate); var listenerTask = Task.Run(() => { StartWebRtcRtpListener(iceCandidate); }); iceCandidate.RtpListenerTask = listenerTask; if (_turnServerEndPoint != null) { var stunBindingTask = Task.Run(() => { SendInitialStunBindingRequest(iceCandidate, iceGatheringCompleteMRE); }); } else { iceCandidate.IsGatheringComplete = true; // Potentially save a few seconds if all the ICE candidates are now ready. if (LocalIceCandidates.All(x => x.IsGatheringComplete)) { iceGatheringCompleteMRE.Set(); } } } } }
private void cmdSerialize_Click(object sender, EventArgs e) { NetServices serialize = new NetServices(); GcpBEDocumentoVenda fatura = new GcpBEDocumentoVenda(); fatura = PriEngine.Engine.Comercial.Vendas.EditaID(txtDocId.Text); txtXml.Text = serialize.ErpReflectionService.SerializeBE(fatura, "Gcp", txtuser.Text, txtempresa.Text); }
static SIPChannel() { LocalIPAddresses = NetServices.GetAllLocalIPAddresses(); // When using IPAddress.Any a default end point is still needed for placing in SIP headers and payloads. // Using 0.0.0.0 in SIP headers causes issues for some SIP software stacks. InternetDefaultAddress = NetServices.GetLocalAddressForInternet(); }
/// <summary> /// Creates a new UDP transport capable of encapsulating SCTP packets. /// </summary> /// <param name="udpEncapPort">The port to bind to for the UDP encapsulation socket.</param> public SctpUdpTransport(int udpEncapPort = 0) { NetServices.CreateRtpSocket(false, IPAddress.IPv6Any, udpEncapPort, out _udpEncapSocket, out _); UdpReceiver udpReceiver = new UdpReceiver(_udpEncapSocket); udpReceiver.OnPacketReceived += OnEncapsulationSocketPacketReceived; udpReceiver.OnClosed += OnEncapsulationSocketClosed; udpReceiver.BeginReceiveFrom(); }
public static async Task SetNetServiceAsync(this ISCPConnection connection, NetServices service) { await connection.SendCommandAsync(new NetService() { Serivce = service }, 10000, (om, sm) => sm.Command == "NLS"); // after sending NSV, NLT + NLS (many) are expected connection.MessageProcessingWaitHandle.WaitOne(); }
//[TestCategory("IPv6")] public void GetLocalForIPv6LoopbackAddressUnitTest() { Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name); var localAddress = NetServices.GetLocalAddressForRemote(IPAddress.IPv6Loopback); Assert.Equal(IPAddress.IPv6Loopback, localAddress); Console.WriteLine($"Local address {localAddress}."); }
public RTPSink(IPAddress localAddress, ArrayList inUsePorts) { m_udpListener = NetServices.CreateRandomUDPListener(localAddress, RTP_PORTRANGE_START, RTP_PORTRANGE_END, inUsePorts, out m_localEndPoint); m_udpListener.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.TypeOfService, RTPPacketTypeOfService); logger.LogInformation("RTPSink established on " + m_localEndPoint.Address.ToString() + ":" + m_localEndPoint.Port + "."); //receivelogger.LogInformation("Send Time,Send Timestamp,Receive Time,Receive Timestamp,Receive Offset(ms),Timestamp Diff,SeqNum,Bytes"); //sendlogger.LogInformation("Send Time,Send Timestamp,Send Offset(ms),SeqNum,Bytes"); }
public void GetInternetAddressUnitTest() { Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name); var localInternetAddresses = NetServices.GetLocalAddressForInternet(); Assert.NotNull(localInternetAddresses); Console.WriteLine($"Local Internet address {localInternetAddresses}."); }
//[TestCategory("IPv6")] public void GetLocalIPv6AddressUnitTest() { Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name); var localAddress = NetServices.GetLocalAddressForRemote(IPAddress.Parse("fe80::54a9:d238:b2ee:abc")); Assert.NotNull(localAddress); Console.WriteLine($"Local address {localAddress}."); }
public void GetLocalForInternetAdressUnitTest() { Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name); var localAddress = NetServices.GetLocalAddressForRemote(IPAddress.Parse("67.222.131.147")); Assert.NotNull(localAddress); Console.WriteLine($"Local address {localAddress}."); }
/// <summary> /// Sends two separate RTP streams to an application like ffplay. /// /// ffplay -i ffplay_av.sdp -protocol_whitelist "file,rtp,udp" -loglevel debug /// /// The SDP that describes the streams is: /// /// v=0 /// o=- 1129870806 2 IN IP4 127.0.0.1 /// s=- /// c=IN IP4 192.168.11.50 /// t=0 0 /// m=audio 4040 RTP/AVP 0 /// a=rtpmap:0 PCMU/8000 /// m=video 4042 RTP/AVP 100 /// a=rtpmap:100 VP8/90000 /// </summary> private void SendSamplesAsRtp(IPEndPoint dstBaseEndPoint) { try { Socket videoSrcRtpSocket = null; Socket videoSrcControlSocket = null; Socket audioSrcRtpSocket = null; Socket audioSrcControlSocket = null; // WebRtc multiplexes all the RTP and RTCP sessions onto a single UDP connection. // The approach needed for ffplay is the original way where each media type has it's own UDP connection and the RTCP // also require a separate UDP connection on RTP port + 1. IPAddress localIPAddress = IPAddress.Any; IPEndPoint audioRtpEP = dstBaseEndPoint; IPEndPoint audioRtcpEP = new IPEndPoint(dstBaseEndPoint.Address, dstBaseEndPoint.Port + 1); IPEndPoint videoRtpEP = new IPEndPoint(dstBaseEndPoint.Address, dstBaseEndPoint.Port + 2); IPEndPoint videoRtcpEP = new IPEndPoint(dstBaseEndPoint.Address, dstBaseEndPoint.Port + 3); RTPSession audioRtpSession = new RTPSession((int)RTPPayloadTypesEnum.PCMU, null, null); RTPSession videoRtpSession = new RTPSession(VP8_PAYLOAD_TYPE_ID, null, null); DateTime lastRtcpSenderReportSentAt = DateTime.Now; NetServices.CreateRtpSocket(localIPAddress, RAW_RTP_START_PORT_RANGE, RAW_RTP_END_PORT_RANGE, true, out audioSrcRtpSocket, out audioSrcControlSocket); NetServices.CreateRtpSocket(localIPAddress, ((IPEndPoint)audioSrcRtpSocket.LocalEndPoint).Port, RAW_RTP_END_PORT_RANGE, true, out videoSrcRtpSocket, out videoSrcControlSocket); OnMediaSampleReady += (mediaType, timestamp, sample) => { if (mediaType == MediaSampleTypeEnum.VP8) { videoRtpSession.SendVp8Frame(videoSrcRtpSocket, videoRtpEP, timestamp, sample); } else { audioRtpSession.SendAudioFrame(audioSrcRtpSocket, audioRtpEP, timestamp, sample); } // Deliver periodic RTCP sender reports. This helps the receiver to sync the audio and video stream timestamps. // If there are gaps in the media, silence supression etc. then the sender repors shouldn't be triggered from the media samples. // In this case the samples are from an mp4 file which provides a constant uninterrupted stream. if (DateTime.Now.Subtract(lastRtcpSenderReportSentAt).TotalSeconds >= RTCP_SR_PERIOD_SECONDS) { videoRtpSession.SendRtcpSenderReport(videoSrcControlSocket, videoRtcpEP, _vp8Timestamp); audioRtpSession.SendRtcpSenderReport(audioSrcControlSocket, audioRtcpEP, _mulawTimestamp); lastRtcpSenderReportSentAt = DateTime.Now; } }; } catch (Exception excp) { logger.Error("Exception SendSamplesAsRtp. " + excp); } }
public void GetInternetAddressUnitTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); var localInternetAddresses = NetServices.GetLocalAddressForInternet(); Assert.NotNull(localInternetAddresses); logger.LogDebug($"Local Internet address {localInternetAddresses}."); }
public void GetLocalForInternetAdressUnitTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); var localAddress = NetServices.GetLocalAddressForRemote(IPAddress.Parse("67.222.131.147")); Assert.NotNull(localAddress); logger.LogDebug($"Local address {localAddress}."); }
public void GetLocalForLoopbackAddressUnitTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); var localAddress = NetServices.GetLocalAddressForRemote(IPAddress.Loopback); Assert.Equal(IPAddress.Loopback, localAddress); logger.LogDebug($"Local address {localAddress}."); }
public void GetLocalIPv6AddressUnitTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); var localAddress = NetServices.GetLocalAddressForRemote(IPAddress.Parse("fe80::54a9:d238:b2ee:abc")); Assert.NotNull(localAddress); logger.LogDebug($"Local address {localAddress}."); }
private string CreateOfferInternal(IPAddress destinationAddress = null) { var destinationAddressToUse = FindDestinationAddressToUse(destinationAddress); IPAddress localIPAddress = NetServices.GetLocalAddressForRemote(destinationAddressToUse); var localSDP = GetSDP(localIPAddress); AdjustSdpForMediaState(localSDP); return(localSDP.ToString()); }
/// <summary> /// Gets the local IP address this SIP channel will use for communicating with the destination /// IP address. /// </summary> /// <param name="dst">The destination IP address.</param> /// <returns>The local IP address this channel selects to use for connecting to the destination.</returns> protected IPAddress GetLocalIPAddressForDestination(IPAddress dst) { IPAddress localAddress = ListeningIPAddress; if (IPAddress.Any.Equals(ListeningIPAddress) || IPAddress.IPv6Any.Equals(ListeningIPAddress)) { // This channel is listening on IPAddress.Any. localAddress = NetServices.GetLocalAddressForRemote(dst); } return(localAddress); }
public STUNServer(IPEndPoint primaryEndPoint, STUNSendMessageDelegate primarySend, IPEndPoint secondaryEndPoint, STUNSendMessageDelegate secondarySend) { m_primaryEndPoint = primaryEndPoint; m_primarySend = primarySend; m_secondaryEndPoint = secondaryEndPoint; m_secondarySend = secondarySend; m_primaryDiffPortSocket = NetServices.CreateRandomUDPListener(m_primaryEndPoint.Address, out m_primaryDiffPortEndPoint); m_secondaryDiffPortSocket = NetServices.CreateRandomUDPListener(m_secondaryEndPoint.Address, out m_secondaryDiffPortEndPoint); logger.LogDebug("STUN Server additional sockets, primary=" + IPSocket.GetSocketString(m_primaryDiffPortEndPoint) + ", secondary=" + IPSocket.GetSocketString(m_secondaryDiffPortEndPoint) + "."); }
public void CreateRtpAndControlSocketsUnitTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); Socket rtpSocket = null; Socket controlSocket = null; NetServices.CreateRtpSocket(10000, 20000, 13677, true, null, out rtpSocket, out controlSocket); Assert.NotNull(rtpSocket); Assert.NotNull(controlSocket); }
public void GetAllLocalIPAddressesUnitTest() { Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name); var localAddresses = NetServices.GetAllLocalIPAddresses(); Assert.NotNull(localAddresses); foreach (var localAddress in localAddresses) { Console.WriteLine($"Local address {localAddress}."); } }
public void GetIPAddressesForInterfaceUnitTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); var localAddresses = NetServices.GetLocalAddressesOnInterface(null); Assert.NotEmpty(localAddresses); foreach (var localAddress in localAddresses) { logger.LogDebug($"Local address {localAddress}."); } }
public void GetAllLocalIPAddressesUnitTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); var localAddresses = NetServices.GetAllLocalIPAddresses(); Assert.NotNull(localAddresses); foreach (var localAddress in localAddresses) { logger.LogDebug($"Local address {localAddress}."); } }