From 6d4d647a490672fab7b266c4ce537771dbd3f23f Mon Sep 17 00:00:00 2001 From: wkirschenmann Date: Fri, 11 Aug 2023 13:51:31 +0200 Subject: [PATCH 1/2] Fix: Allow other network than Ethernet. Fix #457 --- Common/src/Pollster/Pollster.cs | 2 +- .../Utils/{LocalIPv4.cs => LocalIpFinder.cs} | 49 +++++++------------ Common/tests/TestBase/TaskTableTestBase.cs | 14 +++--- 3 files changed, 27 insertions(+), 38 deletions(-) rename Common/src/Utils/{LocalIPv4.cs => LocalIpFinder.cs} (50%) diff --git a/Common/src/Pollster/Pollster.cs b/Common/src/Pollster/Pollster.cs index 493987c6c..39d26940b 100644 --- a/Common/src/Pollster/Pollster.cs +++ b/Common/src/Pollster/Pollster.cs @@ -104,7 +104,7 @@ public Pollster(IPullQueueStorage pullQueueStorage, workerStreamHandler_ = workerStreamHandler; agentHandler_ = agentHandler; TaskProcessing = ""; - ownerPodId_ = LocalIPv4.GetLocalIPv4Ethernet(); + ownerPodId_ = LocalIpFinder.LocalIpv4Address(); ownerPodName_ = Dns.GetHostName(); Failed = false; } diff --git a/Common/src/Utils/LocalIPv4.cs b/Common/src/Utils/LocalIpFinder.cs similarity index 50% rename from Common/src/Utils/LocalIPv4.cs rename to Common/src/Utils/LocalIpFinder.cs index 5d1aa110c..30fdda3f6 100644 --- a/Common/src/Utils/LocalIPv4.cs +++ b/Common/src/Utils/LocalIpFinder.cs @@ -15,6 +15,10 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; using System.Net.NetworkInformation; using System.Net.Sockets; @@ -28,48 +32,33 @@ namespace ArmoniK.Core.Common.Utils; /// Helper to get local IP address. /// [PublicAPI] -public class LocalIPv4 +public class LocalIpFinder { /// - /// Get local IP from a network interface. + /// Get local IPv4 address from a network interface. /// /// Interface type from which to get the IP. /// /// representing the IP. /// /// - public static string GetLocalIPv4(NetworkInterfaceType type) + public static string LocalIpv4Address(NetworkInterfaceType type = NetworkInterfaceType.Ethernet) { - var output = string.Empty; - foreach (var item in NetworkInterface.GetAllNetworkInterfaces()) - { - if (item.NetworkInterfaceType == type && item.OperationalStatus == OperationalStatus.Up) - { - foreach (var ip in item.GetIPProperties() - .UnicastAddresses) - { - if (ip.Address.AddressFamily == AddressFamily.InterNetwork) - { - output = ip.Address.ToString(); - } - } - } - } + var result = NetworkInterface.GetAllNetworkInterfaces() + .Where(@interface => @interface.OperationalStatus == OperationalStatus.Up) + .SelectMany(@interface => @interface.GetIPProperties() + .UnicastAddresses.Select(information => information.Address) + .Where(address => address.AddressFamily == AddressFamily.InterNetwork) + .Select(address => (@interface.NetworkInterfaceType, address: address.ToString()))) + .ToImmutableDictionary(tuple => tuple.NetworkInterfaceType, + tuple => tuple.address); - if (string.IsNullOrEmpty(output)) + if (result.TryGetValue(type, + out var ethernet)) { - throw new ArmoniKException("No local IPv4 found"); + return ethernet; } - return output; + return result.Values.FirstOrDefault("127.0.0.1"); } - - /// - /// Get local IP from Ethernet network interface. - /// - /// - /// representing the IP. - /// - public static string GetLocalIPv4Ethernet() - => GetLocalIPv4(NetworkInterfaceType.Ethernet); } diff --git a/Common/tests/TestBase/TaskTableTestBase.cs b/Common/tests/TestBase/TaskTableTestBase.cs index 3ad906c2b..0ff859ccf 100644 --- a/Common/tests/TestBase/TaskTableTestBase.cs +++ b/Common/tests/TestBase/TaskTableTestBase.cs @@ -921,7 +921,7 @@ public async Task AcquireTaskShouldSucceed() { if (RunTests) { - var ownerpodid = LocalIPv4.GetLocalIPv4Ethernet(); + var ownerpodid = LocalIpFinder.LocalIpv4Address(); var ownerpodname = Dns.GetHostName(); var receptionDate = DateTime.UtcNow.Date; @@ -955,7 +955,7 @@ public async Task ReleaseTaskShouldSucceed() { if (RunTests) { - var ownerpodid = LocalIPv4.GetLocalIPv4Ethernet(); + var ownerpodid = LocalIpFinder.LocalIpv4Address(); var ownerpodname = Dns.GetHostName(); var receptionDate = DateTime.UtcNow.Date; var taskSubmitted = taskSubmittedData_ with @@ -1004,7 +1004,7 @@ public async Task AcquireAcquiredTaskShouldReturnSame() { if (RunTests) { - var hostname = LocalIPv4.GetLocalIPv4Ethernet(); + var hostname = LocalIpFinder.LocalIpv4Address(); var taskSubmitted = taskSubmittedData_ with { @@ -1050,7 +1050,7 @@ public async Task AcquireTaskShouldFail() { var task = taskFailedData_ with { - OwnerPodId = LocalIPv4.GetLocalIPv4Ethernet(), + OwnerPodId = LocalIpFinder.LocalIpv4Address(), OwnerPodName = Dns.GetHostName(), ReceptionDate = DateTime.UtcNow, AcquisitionDate = DateTime.UtcNow, @@ -1060,7 +1060,7 @@ public async Task AcquireTaskShouldFail() CancellationToken.None) .ConfigureAwait(false); - Assert.AreNotEqual(LocalIPv4.GetLocalIPv4Ethernet(), + Assert.AreNotEqual(LocalIpFinder.LocalIpv4Address(), result.OwnerPodId); Assert.AreEqual(TaskStatus.Error, @@ -1075,7 +1075,7 @@ public async Task AcquireCreatingTaskShouldFail() { var task = taskCreatingData_ with { - OwnerPodId = LocalIPv4.GetLocalIPv4Ethernet(), + OwnerPodId = LocalIpFinder.LocalIpv4Address(), OwnerPodName = Dns.GetHostName(), ReceptionDate = DateTime.UtcNow, AcquisitionDate = DateTime.UtcNow, @@ -1085,7 +1085,7 @@ public async Task AcquireCreatingTaskShouldFail() CancellationToken.None) .ConfigureAwait(false); - Assert.AreNotEqual(LocalIPv4.GetLocalIPv4Ethernet(), + Assert.AreNotEqual(LocalIpFinder.LocalIpv4Address(), result.OwnerPodId); Assert.AreEqual(TaskStatus.Creating, From 18e7dbcfff92f8b21072b8170b6d4273e695ed9a Mon Sep 17 00:00:00 2001 From: wkirschenmann Date: Fri, 11 Aug 2023 14:02:19 +0200 Subject: [PATCH 2/2] Update LocalIpFinder.cs --- Common/src/Utils/LocalIpFinder.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Common/src/Utils/LocalIpFinder.cs b/Common/src/Utils/LocalIpFinder.cs index 30fdda3f6..33aace1cc 100644 --- a/Common/src/Utils/LocalIpFinder.cs +++ b/Common/src/Utils/LocalIpFinder.cs @@ -15,15 +15,11 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -using System; -using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Net.NetworkInformation; using System.Net.Sockets; -using ArmoniK.Core.Common.Exceptions; - using JetBrains.Annotations; namespace ArmoniK.Core.Common.Utils; @@ -41,7 +37,6 @@ public class LocalIpFinder /// /// representing the IP. /// - /// public static string LocalIpv4Address(NetworkInterfaceType type = NetworkInterfaceType.Ethernet) { var result = NetworkInterface.GetAllNetworkInterfaces() @@ -50,15 +45,18 @@ public static string LocalIpv4Address(NetworkInterfaceType type = NetworkInterfa .UnicastAddresses.Select(information => information.Address) .Where(address => address.AddressFamily == AddressFamily.InterNetwork) .Select(address => (@interface.NetworkInterfaceType, address: address.ToString()))) - .ToImmutableDictionary(tuple => tuple.NetworkInterfaceType, - tuple => tuple.address); + .GroupBy(tuple => tuple.NetworkInterfaceType) // there might be several interfaces of the same type + .ToImmutableDictionary(tuple => tuple.Key, + tuple => tuple.Select(valueTuple => valueTuple.address)); if (result.TryGetValue(type, out var ethernet)) { - return ethernet; + return ethernet.First(); } - return result.Values.FirstOrDefault("127.0.0.1"); + // No interface of desired type ; choose any other available interface and in last resort, the default 127.0.0.1 + return result.Values.SelectMany(enumerable => enumerable) + .FirstOrDefault("127.0.0.1"); } }