Skip to content

Commit

Permalink
fix(MulticastService): use loopback NIC when all others are down
Browse files Browse the repository at this point in the history
Fix an edge case.  If no network interface is operational, then the loopback interface(s)
are included (127.0.0.1 and/or ::1).  This will include the loopback adressses in
the ServiceProfile.
  • Loading branch information
richardschneider committed Jan 18, 2019
1 parent 5345892 commit 6596ff4
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/MulticastService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,26 @@ public MulticastService(Func<IEnumerable<NetworkInterface>, IEnumerable<NetworkI
/// <remarks>
/// The following filters are applied
/// <list type="bullet">
/// <item><description>is enabled</description></item>
/// <item><description>not a loopback</description></item>
/// <item><description>interface is enabled</description></item>
/// <item><description>interface is not a loopback</description></item>
/// </list>
/// <para>
/// If no network interface is operational, then the loopback interface(s)
/// are included (127.0.0.1 and/or ::1).
/// </para>
/// </remarks>
public static IEnumerable<NetworkInterface> GetNetworkInterfaces()
{
return NetworkInterface.GetAllNetworkInterfaces()
var nics = NetworkInterface.GetAllNetworkInterfaces()
.Where(nic => nic.OperationalStatus == OperationalStatus.Up)
.Where(nic => nic.NetworkInterfaceType != NetworkInterfaceType.Loopback);
.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);
}

/// <summary>
Expand All @@ -172,6 +183,10 @@ public static IEnumerable<NetworkInterface> GetNetworkInterfaces()
/// <returns>
/// A sequence of IP addresses of the local machine.
/// </returns>
/// <remarks>
/// The loopback addresses (127.0.0.1 and ::1) are NOT included in the
/// returned sequences.
/// </remarks>
public static IEnumerable<IPAddress> GetIPAddresses()
{
return GetNetworkInterfaces()
Expand Down

0 comments on commit 6596ff4

Please # to comment.