From 469098019adbf015d1ce90b1682af810a888637a Mon Sep 17 00:00:00 2001 From: Georg Hinkel Date: Tue, 12 Jan 2021 15:55:32 +0100 Subject: [PATCH] Create option to include loopback interfaces, fix #102 --- src/MulticastService.cs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/MulticastService.cs b/src/MulticastService.cs index d39491b..e9197df 100644 --- a/src/MulticastService.cs +++ b/src/MulticastService.cs @@ -164,6 +164,17 @@ public MulticastService(Func, IEnumerable public bool IgnoreDuplicateMessages { get; set; } + /// + /// Determines whether loopback interfaces should be excluded when other network interfaces are available + /// + /// + /// true to include loopback interfaces also when other network interfaces are up. Defaults to false. + /// + /// + /// If not set, loopback network interfaces will be ignored regardless of the network interface filter. + /// + public static bool IncludeLoopbackInterfaces { get; set; } = false; + /// /// The interval for discovering network interfaces. /// @@ -197,13 +208,15 @@ public MulticastService(Func, IEnumerable public static IEnumerable 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);