Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create option to include loopback interfaces, fix #102 #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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