From de7fc626608bf11590e52a5bbdc9f4ba30f56cd9 Mon Sep 17 00:00:00 2001 From: Gleb Salmanov Date: Wed, 30 Oct 2024 02:23:50 +0300 Subject: [PATCH] Fix TTL not following SsdpConstants (#88) --- src/Rssdp/SsdpCommunicationsServer.cs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Rssdp/SsdpCommunicationsServer.cs b/src/Rssdp/SsdpCommunicationsServer.cs index 9ba47f9..c567002 100644 --- a/src/Rssdp/SsdpCommunicationsServer.cs +++ b/src/Rssdp/SsdpCommunicationsServer.cs @@ -363,7 +363,7 @@ namespace Rssdp.Infrastructure { try { - var socket = CreateUdpMulticastSocket(multicastGroupAddress, intf, _MulticastTtl, SsdpConstants.MulticastPort); + var socket = CreateUdpMulticastSocket(multicastGroupAddress, intf, SsdpConstants.MulticastPort); _ = ListenToSocketInternal(socket, intf); sockets.Add(socket); } @@ -385,7 +385,7 @@ namespace Rssdp.Infrastructure { try { - var socket = CreateSsdpUdpSocket(intf, _LocalPort); + var socket = CreateSsdpUdpSocket(intf, _MulticastTtl, _LocalPort); _ = ListenToSocketInternal(socket, intf); sockets.Add(socket); } @@ -507,11 +507,16 @@ namespace Rssdp.Infrastructure }); } - private Socket CreateSsdpUdpSocket(IPData bindInterface, int localPort) + private Socket CreateSsdpUdpSocket(IPData bindInterface, int multicastTimeToLive, int localPort) { var interfaceAddress = bindInterface.Address; ArgumentNullException.ThrowIfNull(interfaceAddress); + if (multicastTimeToLive <= 0) + { + throw new ArgumentException("multicastTimeToLive cannot be zero or less.", nameof(multicastTimeToLive)); + } + if (localPort < 0) { throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort)); @@ -521,6 +526,7 @@ namespace Rssdp.Infrastructure try { socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); + socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive); socket.Bind(new IPEndPoint(interfaceAddress, localPort)); return socket; @@ -533,17 +539,12 @@ namespace Rssdp.Infrastructure } } - private Socket CreateUdpMulticastSocket(IPAddress multicastAddress, IPData bindInterface, int multicastTimeToLive, int localPort) + private Socket CreateUdpMulticastSocket(IPAddress multicastAddress, IPData bindInterface, int localPort) { var bindIPAddress = bindInterface.Address; ArgumentNullException.ThrowIfNull(multicastAddress); ArgumentNullException.ThrowIfNull(bindIPAddress); - if (multicastTimeToLive <= 0) - { - throw new ArgumentException("multicastTimeToLive cannot be zero or less.", nameof(multicastTimeToLive)); - } - if (localPort < 0) { throw new ArgumentException("localPort cannot be less than zero.", nameof(localPort)); @@ -556,7 +557,6 @@ namespace Rssdp.Infrastructure socket.MulticastLoopback = false; socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true); - socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, multicastTimeToLive); if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) {