Skip to content

Commit

Permalink
#18 Ping using specific interface mostly done
Browse files Browse the repository at this point in the history
  • Loading branch information
chucker committed May 22, 2021
1 parent c4d8293 commit 069df1c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
15 changes: 15 additions & 0 deletions AltNetworkUtility.macOS/Services/MacNetworkInterfacesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ public struct if_data64
};
}

private List<NetworkInterfaceViewModel>? _Interfaces;

public IEnumerable<NetworkInterfaceViewModel> GetAvailableInterfaces()
{
// CHECK: are BSD names always unique?
Expand Down Expand Up @@ -168,6 +170,8 @@ public IEnumerable<NetworkInterfaceViewModel> GetAvailableInterfaces()
}
}

_Interfaces = viewModels;

return viewModels;
}

Expand Down Expand Up @@ -229,5 +233,16 @@ public bool TryGetStatistics(NetworkInterfaceViewModel viewModel,

return false;
}

public bool TryFindInterfaceByName(string name,
[NotNullWhen(true)] out NetworkInterfaceViewModel? networkInterfaceViewModel)
{
if (_Interfaces == null)
throw new InvalidOperationException($"Call {nameof(GetAvailableInterfaces)} first");

networkInterfaceViewModel = _Interfaces.Find(netIf => netIf.Name == name);

return networkInterfaceViewModel != null;
}
}
}
3 changes: 3 additions & 0 deletions AltNetworkUtility/Services/INetworkInterfacesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ public interface INetworkInterfacesService

bool TryGetStatistics(NetworkInterfaceViewModel viewModel,
[NotNullWhen(true)] out NetworkInterfaceStatistics.RawValues? statistics);

bool TryFindInterfaceByName(string specificInterface,
[NotNullWhen(true)] out NetworkInterfaceViewModel? netIf);
}
}
9 changes: 8 additions & 1 deletion AltNetworkUtility/Services/PreferencesService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Xamarin.Essentials;
using System;

using Xamarin.Essentials;

namespace AltNetworkUtility.Services
{
Expand Down Expand Up @@ -36,6 +38,11 @@ public int Get(string key, int defaultValue)
public string Get(string key, string defaultValue)
=> Preferences.Get(MakeKey(key), defaultValue);

/// <param name="key">Preference key.</param>
/// <summary>Removes a key and its associated value if it exists.</summary>
/// <remarks />
public void Remove(string key) => Preferences.Remove(key);

/// <param name="key">Preference key.</param>
/// <param name="value">Preference value.</param>
/// <summary>Sets a value for a given key.</summary>
Expand Down
35 changes: 35 additions & 0 deletions AltNetworkUtility/Tabs/Ping/PingPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;

using AltNetworkUtility.Services;
Expand Down Expand Up @@ -107,6 +108,25 @@ public bool UseSpecificInterface
SetProperty(ref _UseSpecificInterface, value);

Preferences.Set(nameof(UseSpecificInterface), value);

UpdateCommandLine();
}
}

private NetworkInterfaceViewModel? _SpecificInterface;
public NetworkInterfaceViewModel? SpecificInterface
{
get => _SpecificInterface;
set
{
SetProperty(ref _SpecificInterface, value);

if (value?.Name != null)
Preferences.Set(nameof(SpecificInterface), value.Name);
else
Preferences.Remove(nameof(SpecificInterface));

UpdateCommandLine();
}
}

Expand All @@ -122,6 +142,17 @@ private void UpdateCommandLine()
arguments.Add(SpecificCount);
}

static bool isIPv4(System.Net.IPAddress ad) =>
ad.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork;

if (UseSpecificInterface &&
!string.IsNullOrEmpty(SpecificInterface?.Name) &&
SpecificInterface?.IPAddresses.Any(isIPv4) == true)
{
arguments.Add("-S");
arguments.Add(SpecificInterface.IPAddresses.First(isIPv4).ToString());
}

arguments.Add(Host);

DebufferedCommandViewModel.SetArguments(arguments);
Expand Down Expand Up @@ -154,6 +185,10 @@ public PingPageViewModel()
{
AvailableNetworkInterfaces.Add(item);
}

var specificInterface = Preferences.Get(nameof(SpecificInterface), "");
if (svc.TryFindInterfaceByName(specificInterface, out var netIf))
SpecificInterface = netIf;
}
}
}
Expand Down

0 comments on commit 069df1c

Please sign in to comment.