Skip to content

Commit

Permalink
fix: Allow other network than Ethernet. (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemaitre-aneo authored Aug 11, 2023
2 parents fa68172 + 18e7dbc commit eeff19a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Common/src/Pollster/Pollster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public Pollster(IPullQueueStorage pullQueueStorage,
workerStreamHandler_ = workerStreamHandler;
agentHandler_ = agentHandler;
TaskProcessing = "";
ownerPodId_ = LocalIPv4.GetLocalIPv4Ethernet();
ownerPodId_ = LocalIpFinder.LocalIpv4Address();
ownerPodName_ = Dns.GetHostName();
Failed = false;
}
Expand Down
75 changes: 0 additions & 75 deletions Common/src/Utils/LocalIPv4.cs

This file was deleted.

62 changes: 62 additions & 0 deletions Common/src/Utils/LocalIpFinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// This file is part of the ArmoniK project
//
// Copyright (C) ANEO, 2021-2023. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System.Collections.Immutable;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.Sockets;

using JetBrains.Annotations;

namespace ArmoniK.Core.Common.Utils;

/// <summary>
/// Helper to get local IP address.
/// </summary>
[PublicAPI]
public class LocalIpFinder
{
/// <summary>
/// Get local IPv4 address from a network interface.
/// </summary>
/// <param name="type">Interface type from which to get the IP.</param>
/// <returns>
/// <see cref="string" /> representing the IP.
/// </returns>
public static string LocalIpv4Address(NetworkInterfaceType type = NetworkInterfaceType.Ethernet)
{
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())))
.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.First();
}

// 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");
}
}
14 changes: 7 additions & 7 deletions Common/tests/TestBase/TaskTableTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1004,7 +1004,7 @@ public async Task AcquireAcquiredTaskShouldReturnSame()
{
if (RunTests)
{
var hostname = LocalIPv4.GetLocalIPv4Ethernet();
var hostname = LocalIpFinder.LocalIpv4Address();

var taskSubmitted = taskSubmittedData_ with
{
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit eeff19a

Please sign in to comment.