Skip to content

Commit

Permalink
Create option to include loopback interfaces, fix richardschneider#102
Browse files Browse the repository at this point in the history
  • Loading branch information
Georg Hinkel committed Jan 12, 2021
1 parent b9f2f81 commit 4690980
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/MulticastService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ public MulticastService(Func<IEnumerable<NetworkInterface>, IEnumerable<NetworkI
/// </remarks>
public bool IgnoreDuplicateMessages { get; set; }

/// <summary>
/// Determines whether loopback interfaces should be excluded when other network interfaces are available
/// </summary>
/// <value>
/// <b>true</b> to include loopback interfaces also when other network interfaces are up. Defaults to <b>false</b>.
/// </value>
/// <remarks>
/// If not set, loopback network interfaces will be ignored regardless of the network interface filter.
/// </remarks>
public static bool IncludeLoopbackInterfaces { get; set; } = false;

/// <summary>
/// The interval for discovering network interfaces.
/// </summary>
Expand Down Expand Up @@ -197,13 +208,15 @@ public MulticastService(Func<IEnumerable<NetworkInterface>, IEnumerable<NetworkI
/// </remarks>
public static IEnumerable<NetworkInterface> GetNetworkInterfaces()
{
var nics = NetworkInterface.GetAllNetworkInterfaces()
.Where(nic => nic.OperationalStatus == OperationalStatus.Up)
.Where(nic => nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.ToArray();
if (nics.Length > 0)
return nics;

if(!IncludeLoopbackInterfaces)
{
var nics = NetworkInterface.GetAllNetworkInterfaces()
.Where( nic => nic.OperationalStatus == OperationalStatus.Up )
.Where( nic => nic.NetworkInterfaceType != NetworkInterfaceType.Loopback )
.ToArray();
if(nics.Length > 0)
return nics;
}
// Special case: no operational NIC, then use loopbacks.
return NetworkInterface.GetAllNetworkInterfaces()
.Where(nic => nic.OperationalStatus == OperationalStatus.Up);
Expand Down

0 comments on commit 4690980

Please sign in to comment.